sim-lib-numbers-stats 0.6.0

Statistical and probability helpers for the SIM number stack.
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
//! Claim value support for statistics and fairness metrics.

use std::{any::Any, sync::Arc};

use sim_kernel::{
    Claim, Cx, Datum, Error, Expr, Factory, NumberLiteral, Object, ObjectCompat, Ref, Result,
    Symbol, Value,
};
use sim_lib_numbers_core::domains;

use crate::{BinaryOutcomeCounts, StatsResult, disparate_impact};

const FOUR_FIFTHS_THRESHOLD: f64 = 0.8;

/// Evidence carried by the disparate-impact fairness Claim.
#[derive(Clone, Debug, PartialEq)]
pub struct FairnessEvidence {
    /// Number of selected cases in the reference group.
    pub reference_selected: u64,
    /// Total cases in the reference group.
    pub reference_total: u64,
    /// Number of selected cases in the comparison group.
    pub comparison_selected: u64,
    /// Total cases in the comparison group.
    pub comparison_total: u64,
    /// Selection rate for the reference group.
    pub reference_rate: f64,
    /// Selection rate for the comparison group.
    pub comparison_rate: f64,
    /// Comparison rate divided by reference rate.
    pub ratio: f64,
    /// Four-fifths rule threshold.
    pub threshold: f64,
    /// Whether `ratio >= threshold`.
    pub passes_four_fifths: bool,
}

impl FairnessEvidence {
    /// Computes evidence for a disparate-impact Claim from validated counts.
    pub fn new(
        reference_selected: u64,
        reference_total: u64,
        comparison_selected: u64,
        comparison_total: u64,
    ) -> StatsResult<Self> {
        let reference = BinaryOutcomeCounts::new(reference_selected, reference_total)?;
        let comparison = BinaryOutcomeCounts::new(comparison_selected, comparison_total)?;
        let impact = disparate_impact(reference, comparison)?;
        Ok(Self {
            reference_selected,
            reference_total,
            comparison_selected,
            comparison_total,
            reference_rate: impact.reference_rate,
            comparison_rate: impact.comparison_rate,
            ratio: impact.ratio,
            threshold: FOUR_FIFTHS_THRESHOLD,
            passes_four_fifths: impact.passes_four_fifths,
        })
    }

    /// Returns the evidence as a browsable runtime table.
    pub fn table_value(&self, factory: &dyn Factory) -> Result<Value> {
        factory.table(vec![
            (
                Symbol::new("reference-selected"),
                u64_value(factory, self.reference_selected)?,
            ),
            (
                Symbol::new("reference-total"),
                u64_value(factory, self.reference_total)?,
            ),
            (
                Symbol::new("comparison-selected"),
                u64_value(factory, self.comparison_selected)?,
            ),
            (
                Symbol::new("comparison-total"),
                u64_value(factory, self.comparison_total)?,
            ),
            (
                Symbol::new("reference-rate"),
                f64_value(factory, self.reference_rate)?,
            ),
            (
                Symbol::new("comparison-rate"),
                f64_value(factory, self.comparison_rate)?,
            ),
            (Symbol::new("ratio"), f64_value(factory, self.ratio)?),
            (Symbol::new("value"), f64_value(factory, self.ratio)?),
            (
                Symbol::new("threshold"),
                f64_value(factory, self.threshold)?,
            ),
            (
                Symbol::new("passes-four-fifths"),
                factory.bool(self.passes_four_fifths)?,
            ),
        ])
    }

    fn datum(&self) -> Datum {
        Datum::Node {
            tag: Symbol::qualified("stats", "disparate-impact-evidence"),
            fields: vec![
                (
                    Symbol::new("reference-selected"),
                    u64_datum(self.reference_selected),
                ),
                (
                    Symbol::new("reference-total"),
                    u64_datum(self.reference_total),
                ),
                (
                    Symbol::new("comparison-selected"),
                    u64_datum(self.comparison_selected),
                ),
                (
                    Symbol::new("comparison-total"),
                    u64_datum(self.comparison_total),
                ),
                (
                    Symbol::new("reference-rate"),
                    f64_datum(self.reference_rate),
                ),
                (
                    Symbol::new("comparison-rate"),
                    f64_datum(self.comparison_rate),
                ),
                (Symbol::new("ratio"), f64_datum(self.ratio)),
                (Symbol::new("value"), f64_datum(self.ratio)),
                (Symbol::new("threshold"), f64_datum(self.threshold)),
                (
                    Symbol::new("passes-four-fifths"),
                    Datum::Bool(self.passes_four_fifths),
                ),
            ],
        }
    }
}

/// Evidence carried by a descriptive statistics Claim.
#[derive(Clone, Debug, PartialEq)]
pub struct StatsClaimEvidence {
    metric: Symbol,
    inputs: Vec<f64>,
    value: f64,
}

impl StatsClaimEvidence {
    /// Creates descriptive metric evidence from validated inputs and a result.
    pub fn new(metric: Symbol, inputs: Vec<f64>, value: f64) -> Self {
        Self {
            metric,
            inputs,
            value,
        }
    }

    /// Returns the metric named by this evidence.
    pub fn metric(&self) -> &Symbol {
        &self.metric
    }

    /// Returns the input samples or probabilities used to compute the metric.
    pub fn inputs(&self) -> &[f64] {
        &self.inputs
    }

    /// Returns the computed metric value.
    pub fn value(&self) -> f64 {
        self.value
    }

    /// Returns the evidence as a browsable runtime table.
    pub fn table_value(&self, factory: &dyn Factory) -> Result<Value> {
        factory.table(vec![
            (Symbol::new("metric"), factory.symbol(self.metric.clone())?),
            (
                Symbol::new("inputs"),
                f64_list_value(factory, &self.inputs)?,
            ),
            (
                Symbol::new("count"),
                u64_value(factory, self.inputs.len() as u64)?,
            ),
            (Symbol::new("value"), f64_value(factory, self.value)?),
        ])
    }

    fn datum(&self) -> Datum {
        Datum::Node {
            tag: Symbol::qualified("stats", "result-evidence"),
            fields: vec![
                (Symbol::new("metric"), Datum::Symbol(self.metric.clone())),
                (
                    Symbol::new("inputs"),
                    Datum::Vector(self.inputs.iter().copied().map(f64_datum).collect()),
                ),
                (Symbol::new("count"), u64_datum(self.inputs.len() as u64)),
                (Symbol::new("value"), f64_datum(self.value)),
            ],
        }
    }
}

/// Builds the public descriptive statistics Claim and interns its evidence object.
pub fn stats_result_claim(cx: &mut Cx, evidence: &StatsClaimEvidence) -> Result<Claim> {
    Claim::content_object(
        cx.datum_store_mut(),
        Ref::Symbol(evidence.metric().clone()),
        Symbol::new("stats-result"),
        evidence.datum(),
    )
}

/// A first-class runtime object wrapping a descriptive statistics Claim.
#[derive(Clone, Debug, PartialEq)]
pub struct StatsClaimValue {
    claim: Claim,
    evidence: StatsClaimEvidence,
}

impl StatsClaimValue {
    /// Creates a new runtime Claim wrapper.
    pub fn new(claim: Claim, evidence: StatsClaimEvidence) -> Self {
        Self { claim, evidence }
    }

    /// Returns the underlying kernel Claim record.
    pub fn claim(&self) -> &Claim {
        &self.claim
    }

    /// Returns the evidence used to compute the Claim object.
    pub fn evidence(&self) -> &StatsClaimEvidence {
        &self.evidence
    }

    /// Builds a runtime table exposing the Claim and evidence fields.
    pub fn table_value(&self, cx: &mut Cx) -> Result<Value> {
        let object = match &self.claim.object {
            Ref::Content(id) => cx.factory().string(format!("{id:?}"))?,
            other => cx.factory().string(format!("{other:?}"))?,
        };
        cx.factory().table(vec![
            (
                Symbol::new("kind"),
                cx.factory().string("claim".to_owned())?,
            ),
            (
                Symbol::new("subject"),
                cx.factory().symbol(self.evidence.metric().clone())?,
            ),
            (
                Symbol::new("predicate"),
                cx.factory().symbol(self.claim.predicate.clone())?,
            ),
            (Symbol::new("object"), object),
            (
                Symbol::new("evidence"),
                self.evidence.table_value(cx.factory())?,
            ),
        ])
    }
}

impl Object for StatsClaimValue {
    fn snapshot(&self, _cx: &mut Cx) -> Result<Option<Datum>> {
        Ok(Some(self.claim.canonical_datum()))
    }

    fn display(&self, _cx: &mut Cx) -> Result<String> {
        Ok(format!(
            "#<claim {} stats-result>",
            self.evidence.metric().as_qualified_str()
        ))
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl ObjectCompat for StatsClaimValue {
    fn as_expr(&self, _cx: &mut Cx) -> Result<Expr> {
        Ok(Expr::from(self.claim.canonical_datum()))
    }

    fn as_table(&self, cx: &mut Cx) -> Result<Value> {
        self.table_value(cx)
    }
}

/// Wraps a descriptive statistics Claim as a runtime value.
pub fn stats_result_claim_value(cx: &mut Cx, evidence: StatsClaimEvidence) -> Result<Value> {
    let claim = stats_result_claim(cx, &evidence)?;
    cx.factory()
        .opaque(Arc::new(StatsClaimValue::new(claim, evidence)))
}

/// Builds the public disparate-impact fairness Claim and interns its evidence object.
pub fn fairness_claim(cx: &mut Cx, evidence: &FairnessEvidence) -> Result<Claim> {
    Claim::content_object(
        cx.datum_store_mut(),
        Ref::Symbol(Symbol::qualified("stats", "disparate-impact")),
        Symbol::new("fairness-result"),
        evidence.datum(),
    )
}

/// A first-class runtime object wrapping the fairness Claim and its evidence.
#[derive(Clone, Debug, PartialEq)]
pub struct FairnessClaimValue {
    claim: Claim,
    evidence: FairnessEvidence,
}

impl FairnessClaimValue {
    /// Creates a new runtime Claim wrapper.
    pub fn new(claim: Claim, evidence: FairnessEvidence) -> Self {
        Self { claim, evidence }
    }

    /// Returns the underlying kernel Claim record.
    pub fn claim(&self) -> &Claim {
        &self.claim
    }

    /// Returns the evidence used to compute the Claim object.
    pub fn evidence(&self) -> &FairnessEvidence {
        &self.evidence
    }

    /// Builds a runtime table exposing the Claim and evidence fields.
    pub fn table_value(&self, cx: &mut Cx) -> Result<Value> {
        let object = match &self.claim.object {
            Ref::Content(id) => cx.factory().string(format!("{id:?}"))?,
            other => cx.factory().string(format!("{other:?}"))?,
        };
        cx.factory().table(vec![
            (
                Symbol::new("kind"),
                cx.factory().string("claim".to_owned())?,
            ),
            (
                Symbol::new("subject"),
                cx.factory()
                    .symbol(Symbol::qualified("stats", "disparate-impact"))?,
            ),
            (
                Symbol::new("predicate"),
                cx.factory().symbol(self.claim.predicate.clone())?,
            ),
            (Symbol::new("object"), object),
            (
                Symbol::new("evidence"),
                self.evidence.table_value(cx.factory())?,
            ),
        ])
    }
}

impl Object for FairnessClaimValue {
    fn snapshot(&self, _cx: &mut Cx) -> Result<Option<Datum>> {
        Ok(Some(self.claim.canonical_datum()))
    }

    fn display(&self, _cx: &mut Cx) -> Result<String> {
        Ok("#<claim stats/disparate-impact fairness-result>".to_owned())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl ObjectCompat for FairnessClaimValue {
    fn as_expr(&self, _cx: &mut Cx) -> Result<Expr> {
        Ok(Expr::from(self.claim.canonical_datum()))
    }

    fn as_table(&self, cx: &mut Cx) -> Result<Value> {
        self.table_value(cx)
    }
}

/// Wraps a fairness Claim as a runtime value.
pub fn fairness_claim_value(cx: &mut Cx, evidence: FairnessEvidence) -> Result<Value> {
    let claim = fairness_claim(cx, &evidence)?;
    cx.factory()
        .opaque(Arc::new(FairnessClaimValue::new(claim, evidence)))
}

fn u64_value(factory: &dyn Factory, value: u64) -> Result<Value> {
    factory.number_literal(domains::u64(), value.to_string())
}

fn f64_value(factory: &dyn Factory, value: f64) -> Result<Value> {
    factory.number_literal(domains::f64(), value.to_string())
}

fn f64_list_value(factory: &dyn Factory, values: &[f64]) -> Result<Value> {
    let values = values
        .iter()
        .copied()
        .map(|value| f64_value(factory, value))
        .collect::<Result<Vec<_>>>()?;
    factory.list(values)
}

fn u64_datum(value: u64) -> Datum {
    Datum::Number(NumberLiteral {
        domain: domains::u64(),
        canonical: value.to_string(),
    })
}

fn f64_datum(value: f64) -> Datum {
    Datum::Number(NumberLiteral {
        domain: domains::f64(),
        canonical: value.to_string(),
    })
}

pub(crate) fn stats_error_to_kernel(error: crate::StatsError) -> Error {
    Error::DomainError {
        domain: Symbol::qualified("numbers", "stats"),
        category: Symbol::new("invalid-input"),
        message: error.to_string(),
    }
}