rustev-core 0.2.0

Rustev's pure core: validation, value kinds, exact operators, compiler, calibration binding and selection policy.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! Value kinds (spec 002, 3.5). Each is its own type with private fields and
//! fallible construction; there is no `From` or `Into` between any two, so a
//! score never becomes a probability and a label never acquires mass.
//!
//! ```compile_fail
//! # use rustev_core::kinds::*;
//! fn launder(label: SelectedLabel) -> Distribution { label.into() }
//! ```
//!
//! ```compile_fail
//! # use rustev_core::kinds::*;
//! fn launder(d: Distribution) -> CalibratedProbability { d.into() }
//! ```
//!
//! ```compile_fail
//! # use rustev_core::kinds::*;
//! fn launder(s: ModelScore) -> Distribution { Distribution::from(s) }
//! ```
//!
//! ```compile_fail
//! # use rustev_core::kinds::*;
//! # use rustev_contract::ids::CalibrationId;
//! // A calibrated probability cannot be built without applying an artifact.
//! fn forge(id: CalibrationId) -> CalibratedProbability {
//!     CalibratedProbability { calibration: id, options: vec![], masses: vec![] }
//! }
//! ```

use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::fmt;

use rustev_contract::decimal::Decimal;
use rustev_contract::ids::{CalibrationId, PlanId};

/// Why a supplied value is not a valid value of its kind. Becomes
/// `invalid_backend_output` at the backend boundary.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KindError(pub String);

impl fmt::Display for KindError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl std::error::Error for KindError {}

fn err<T>(msg: impl Into<String>) -> Result<T, KindError> {
    Err(KindError(msg.into()))
}

/// Masses keyed exactly by `options`, in declared order.
fn masses_in_order(options: &[String], map: &BTreeMap<String, f64>) -> Result<Vec<f64>, KindError> {
    if let Some(extra) = map.keys().find(|k| !options.contains(k)) {
        return err(format!("undeclared option {extra:?}"));
    }
    options
        .iter()
        .map(|o| match map.get(o) {
            Some(v) => Ok(*v),
            None => err(format!("missing option {o:?}")),
        })
        .collect()
}

fn check_distribution(masses: &[f64], tolerance: f64) -> Result<(), KindError> {
    if masses.is_empty() {
        return err("empty distribution");
    }
    for (i, m) in masses.iter().enumerate() {
        if !m.is_finite() {
            return err(format!("non-finite mass at option {i}"));
        }
        if *m < 0.0 {
            return err(format!("negative mass at option {i}"));
        }
    }
    // Summed in declared option order (spec 002, 3.5.2).
    let sum: f64 = masses.iter().fold(0.0, |acc, m| acc + m);
    if (sum - 1.0).abs() > tolerance {
        return err(format!(
            "masses sum to {sum}, outside tolerance {tolerance}"
        ));
    }
    Ok(())
}

/// Index of the largest value; ties go to the earliest (declared order).
fn argmax(values: &[f64]) -> usize {
    let mut best = 0;
    for (i, v) in values.iter().enumerate().skip(1) {
        if *v > values[best] {
            best = i;
        }
    }
    best
}

/// Softmax with max subtraction, in declared order. Uses `exp`, so it is
/// outside exact computation (spec 002, 3.4.6), but it computes the same
/// bits on every target (spec 047).
pub(crate) fn softmax(z: &[f64]) -> Vec<f64> {
    let max = z.iter().copied().fold(f64::NEG_INFINITY, f64::max);
    let e: Vec<f64> = z
        .iter()
        .map(|v| {
            if *v == f64::NEG_INFINITY {
                0.0
            } else {
                crate::math::exp(v - max)
            }
        })
        .collect();
    let sum: f64 = e.iter().fold(0.0, |acc, v| acc + v);
    e.into_iter().map(|v| v / sum).collect()
}

/// Non-negative mass over declared options, summing to 1 within tolerance.
/// Uncalibrated: it is not a calibrated probability.
#[derive(Debug, Clone, PartialEq)]
pub struct Distribution {
    options: Vec<String>,
    masses: Vec<f64>,
}

impl Distribution {
    /// Validate a supplied distribution. Stored as supplied, never
    /// renormalized.
    pub fn new(
        options: &[String],
        map: &BTreeMap<String, f64>,
        tolerance: Decimal,
    ) -> Result<Self, KindError> {
        let masses = masses_in_order(options, map)?;
        check_distribution(&masses, tolerance.to_f64_nearest())?;
        Ok(Self {
            options: options.to_vec(),
            masses,
        })
    }

    /// Normalize logits by softmax (design 8.2: the core normalizes logits,
    /// it never invents mass for a label backend).
    pub fn from_logits(
        options: &[String],
        map: &BTreeMap<String, f64>,
        tolerance: Decimal,
    ) -> Result<Self, KindError> {
        let z = masses_in_order(options, map)?;
        if let Some(i) = z.iter().position(|v| !v.is_finite()) {
            return err(format!("non-finite logit at option {i}"));
        }
        let masses = softmax(&z);
        check_distribution(&masses, tolerance.to_f64_nearest())?;
        Ok(Self {
            options: options.to_vec(),
            masses,
        })
    }

    pub fn options(&self) -> &[String] {
        &self.options
    }

    pub fn masses(&self) -> &[f64] {
        &self.masses
    }

    pub fn mass(&self, option: &str) -> Option<f64> {
        self.options
            .iter()
            .position(|o| o == option)
            .map(|i| self.masses[i])
    }

    /// The top option; ties go to the earlier declared option.
    pub fn top(&self) -> (&str, f64) {
        let i = argmax(&self.masses);
        (&self.options[i], self.masses[i])
    }
}

/// A distribution passed through a calibration artifact bound to this step.
/// It records which transformation was applied; it does not establish that
/// the result is calibrated on current data (spec 002, 3.6.2).
#[derive(Debug, Clone, PartialEq)]
pub struct CalibratedProbability {
    calibration: CalibrationId,
    options: Vec<String>,
    masses: Vec<f64>,
}

impl CalibratedProbability {
    /// Only [`crate::calibrate::apply`] calls this, after checking the binding.
    pub(crate) fn from_applied(
        calibration: CalibrationId,
        options: Vec<String>,
        masses: Vec<f64>,
        tolerance: f64,
    ) -> Result<Self, KindError> {
        check_distribution(&masses, tolerance)?;
        Ok(Self {
            calibration,
            options,
            masses,
        })
    }

    pub fn calibration(&self) -> &CalibrationId {
        &self.calibration
    }

    pub fn options(&self) -> &[String] {
        &self.options
    }

    pub fn masses(&self) -> &[f64] {
        &self.masses
    }

    pub fn mass(&self, option: &str) -> Option<f64> {
        self.options
            .iter()
            .position(|o| o == option)
            .map(|i| self.masses[i])
    }

    pub fn top(&self) -> (&str, f64) {
        let i = argmax(&self.masses);
        (&self.options[i], self.masses[i])
    }
}

/// One option, with no mass and no uncertainty measure.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SelectedLabel {
    options: Vec<String>,
    index: usize,
}

impl SelectedLabel {
    pub fn new(options: &[String], label: &str) -> Result<Self, KindError> {
        match options.iter().position(|o| o == label) {
            Some(index) => Ok(Self {
                options: options.to_vec(),
                index,
            }),
            None => err(format!("undeclared label {label:?}")),
        }
    }

    pub fn label(&self) -> &str {
        &self.options[self.index]
    }

    pub fn index(&self) -> usize {
        self.index
    }
}

/// A level on a declared rubric, low to high, as a distribution over levels
/// or a single level. Its expectation is an index summary only.
#[derive(Debug, Clone, PartialEq)]
pub struct OrdinalLevel {
    levels: Vec<String>,
    repr: OrdinalRepr,
}

#[derive(Debug, Clone, PartialEq)]
enum OrdinalRepr {
    Distribution(Vec<f64>),
    Single(usize),
}

impl OrdinalLevel {
    pub fn from_distribution(d: Distribution) -> Self {
        Self {
            levels: d.options,
            repr: OrdinalRepr::Distribution(d.masses),
        }
    }

    pub fn from_label(l: SelectedLabel) -> Self {
        Self {
            levels: l.options,
            repr: OrdinalRepr::Single(l.index),
        }
    }

    pub fn levels(&self) -> &[String] {
        &self.levels
    }

    pub fn has_distribution(&self) -> bool {
        matches!(self.repr, OrdinalRepr::Distribution(_))
    }

    /// `sum(index * mass)` in level order, or the single level's index. An
    /// index summary, not an interval quantity.
    pub fn expectation(&self) -> f64 {
        match &self.repr {
            OrdinalRepr::Distribution(m) => m
                .iter()
                .enumerate()
                .fold(0.0, |acc, (i, p)| acc + (i as f64) * p),
            OrdinalRepr::Single(i) => *i as f64,
        }
    }

    /// The most likely level (ties to the lower level), or the single level.
    pub fn top(&self) -> &str {
        match &self.repr {
            OrdinalRepr::Distribution(m) => &self.levels[argmax(m)],
            OrdinalRepr::Single(i) => &self.levels[*i],
        }
    }
}

/// Where a model score may be compared: one plan, step and request.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ScoreScope {
    pub plan: PlanId,
    pub step: String,
    pub instance: Vec<String>,
}

/// An unnormalized score, comparable only within its scope.
#[derive(Debug, Clone, PartialEq)]
pub struct ModelScore {
    scope: ScoreScope,
    candidate: String,
    score: f64,
}

/// Two scores from different scopes were compared.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScopeMismatch;

impl ModelScore {
    pub fn new(scope: ScoreScope, candidate: String, score: f64) -> Result<Self, KindError> {
        if !score.is_finite() {
            return err(format!("non-finite score for {candidate:?}"));
        }
        Ok(Self {
            scope,
            candidate,
            score,
        })
    }

    pub fn candidate(&self) -> &str {
        &self.candidate
    }

    pub fn scope(&self) -> &ScoreScope {
        &self.scope
    }

    /// Order two scores of the same scope; refused across scopes.
    pub fn cmp_within(&self, other: &ModelScore) -> Result<Ordering, ScopeMismatch> {
        if self.scope != other.scope {
            return Err(ScopeMismatch);
        }
        Ok(self.score.total_cmp(&other.score))
    }
}

/// An order over candidates with the scores and components that produced it;
/// not a probability of being best.
#[derive(Debug, Clone, PartialEq)]
pub struct RankPosition {
    pub(crate) entries: Vec<(String, f64, BTreeMap<String, f64>)>,
}

impl RankPosition {
    /// Candidates in rank order.
    pub fn order(&self) -> impl Iterator<Item = &str> {
        self.entries.iter().map(|(c, _, _)| c.as_str())
    }

    pub fn len(&self) -> usize {
        self.entries.len()
    }

    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn opts(v: &[&str]) -> Vec<String> {
        v.iter().map(|s| s.to_string()).collect()
    }

    fn map(v: &[(&str, f64)]) -> BTreeMap<String, f64> {
        v.iter().map(|(k, m)| (k.to_string(), *m)).collect()
    }

    fn tol() -> Decimal {
        Decimal::parse("0.000001").unwrap()
    }

    #[test]
    fn a_valid_distribution_is_accepted_as_supplied() {
        let d = Distribution::new(&opts(&["a", "b"]), &map(&[("a", 0.25), ("b", 0.75)]), tol())
            .unwrap();
        assert_eq!(d.masses(), &[0.25, 0.75]);
        assert_eq!(d.top(), ("b", 0.75));
    }

    #[test]
    fn invalid_distributions_are_refused() {
        let o = opts(&["a", "b"]);
        for (bad, why) in [
            (map(&[("a", f64::NAN), ("b", 1.0)]), "NaN"),
            (map(&[("a", f64::INFINITY), ("b", 0.0)]), "infinite"),
            (map(&[("a", -0.1), ("b", 1.1)]), "negative"),
            (map(&[("a", 1.0)]), "missing key"),
            (map(&[("a", 0.5), ("b", 0.5), ("c", 0.0)]), "undeclared key"),
            (map(&[("a", 0.5), ("b", 0.6)]), "sum above tolerance"),
        ] {
            assert!(Distribution::new(&o, &bad, tol()).is_err(), "{why}");
        }
        // Negative control at the tolerance edge: 1 + 5e-7 passes, 1 + 2e-6 fails.
        assert!(Distribution::new(&o, &map(&[("a", 0.5), ("b", 0.5000005)]), tol()).is_ok());
        assert!(Distribution::new(&o, &map(&[("a", 0.5), ("b", 0.500002)]), tol()).is_err());
    }

    #[test]
    fn logits_are_normalized_and_non_finite_logits_refused() {
        let o = opts(&["a", "b"]);
        let d = Distribution::from_logits(&o, &map(&[("a", 0.0), ("b", 0.0)]), tol()).unwrap();
        assert_eq!(d.masses(), &[0.5, 0.5]);
        // Large logits do not overflow thanks to max subtraction.
        let d = Distribution::from_logits(&o, &map(&[("a", 1000.0), ("b", 999.0)]), tol()).unwrap();
        assert!(d.masses()[0] > d.masses()[1]);
        assert!(
            Distribution::from_logits(&o, &map(&[("a", f64::NAN), ("b", 0.0)]), tol()).is_err()
        );
    }

    #[test]
    fn ties_go_to_the_earlier_declared_option() {
        let d =
            Distribution::new(&opts(&["z", "a"]), &map(&[("z", 0.5), ("a", 0.5)]), tol()).unwrap();
        assert_eq!(d.top().0, "z");
    }

    #[test]
    fn a_label_must_be_declared() {
        assert!(SelectedLabel::new(&opts(&["a"]), "a").is_ok());
        assert!(SelectedLabel::new(&opts(&["a"]), "b").is_err());
    }

    #[test]
    fn ordinal_expectation_is_an_index_summary() {
        let d = Distribution::new(
            &opts(&["calm", "frustrated", "very_angry"]),
            &map(&[("calm", 0.2), ("frustrated", 0.3), ("very_angry", 0.5)]),
            tol(),
        )
        .unwrap();
        let o = OrdinalLevel::from_distribution(d);
        assert!((o.expectation() - 1.3).abs() < 1e-12);
        let l =
            OrdinalLevel::from_label(SelectedLabel::new(&opts(&["low", "high"]), "high").unwrap());
        assert_eq!(l.expectation(), 1.0);
        assert!(!l.has_distribution());
    }

    #[test]
    fn scores_compare_only_within_their_scope() {
        let plan = PlanId::parse(&format!("sha256:{}", "0".repeat(64))).unwrap();
        let s1 = ScoreScope {
            plan: plan.clone(),
            step: "r".into(),
            instance: vec![],
        };
        let s2 = ScoreScope {
            plan,
            step: "other".into(),
            instance: vec![],
        };
        let a = ModelScore::new(s1.clone(), "x".into(), 1.0).unwrap();
        let b = ModelScore::new(s1, "y".into(), 2.0).unwrap();
        let c = ModelScore::new(s2, "x".into(), 3.0).unwrap();
        assert_eq!(a.cmp_within(&b), Ok(Ordering::Less));
        assert_eq!(a.cmp_within(&c), Err(ScopeMismatch));
        assert!(ModelScore::new(a.scope().clone(), "z".into(), f64::NAN).is_err());
    }
}