use std::collections::BTreeMap;
use crate::{CalibrationMap, CertError};
pub const MAP_KINDS: [&str; 2] = ["temperature", "dr"];
fn temperature(params: &BTreeMap<String, f64>) -> Option<f64> {
let t = *params.get("temperature")?;
(params.len() == 1 && t > 0.0).then_some(t)
}
fn dr(params: &BTreeMap<String, f64>, k: usize) -> Option<(f64, Vec<f64>)> {
let a = *params.get("a")?;
let b = (0..k)
.map(|i| params.get(&format!("b{i}")).copied())
.collect::<Option<Vec<f64>>>()?;
(params.len() == k + 1 && a > 0.0).then_some((a, b))
}
pub(crate) fn check(m: &CalibrationMap, k: usize) -> Result<(), CertError> {
let bad = |s: &str| Err(CertError::Invalid(s.to_string()));
match m.kind.as_str() {
"temperature" if temperature(&m.params).is_none() => {
bad("a `temperature` calibration_map needs exactly the param `temperature`, above 0")
}
"dr" if dr(&m.params, k).is_none() || !m.label_budget.is_some_and(|n| n >= 1) => bad(
"a `dr` calibration_map needs exactly the params `a`, above 0, and `b0` to `b{k-1}` for its k options, and a label_budget of at least 1",
),
_ => Ok(()),
}
}
pub fn probabilities(map: &CalibrationMap, scores: &[f64]) -> Option<Vec<f64>> {
if scores.len() < 2 {
return None;
}
let logits: Vec<f64> = match map.kind.as_str() {
"temperature" => {
let t = temperature(&map.params)?;
scores.iter().map(|s| s / t).collect()
}
"dr" => {
let (a, b) = dr(&map.params, scores.len())?;
scores.iter().zip(&b).map(|(s, b)| a * s + b).collect()
}
_ => return None,
};
if !logits.iter().all(|z| z.is_finite()) {
return None;
}
let top = logits.iter().copied().fold(f64::NEG_INFINITY, f64::max);
let e: Vec<f64> = logits.iter().map(|z| (z - top).exp()).collect();
let mut total = 0.0;
for x in &e {
total += x;
}
Some(e.iter().map(|x| x / total).collect())
}
pub fn confidence(map: &CalibrationMap, scores: &[f64]) -> Option<f64> {
probabilities(map, scores).map(|p| p.into_iter().fold(f64::NEG_INFINITY, f64::max))
}
#[cfg(test)]
mod tests {
use super::*;
fn map(kind: &str, params: &[(&str, f64)], label_budget: Option<u64>) -> CalibrationMap {
CalibrationMap {
kind: kind.into(),
params: params.iter().map(|(k, v)| (k.to_string(), *v)).collect(),
label_budget,
}
}
#[test]
fn temperature_divides_the_scores() {
let m = map("temperature", &[("temperature", 2.0)], None);
let p = probabilities(&m, &[2.0, 0.0]).unwrap();
let want = 1.0 / (1.0 + (-1.0f64).exp());
assert!((p[0] - want).abs() < 1e-15 && (p[0] + p[1] - 1.0).abs() < 1e-15);
assert!(probabilities(
&map("temperature", &[("temperature", 0.0)], None),
&[1.0, 0.0]
)
.is_none());
assert!(probabilities(&m, &[1.0]).is_none());
}
#[test]
fn dr_scales_and_shifts_the_scores() {
let m = map(
"dr",
&[("a", 0.5), ("b0", 1.0), ("b1", 0.0), ("b2", -1.0)],
Some(50),
);
let p = probabilities(&m, &[0.0, 0.0, 4.0]).unwrap();
let z = [1.0f64, 0.0, 1.0];
let s: f64 = z.iter().map(|x| x.exp()).sum();
for (got, z) in p.iter().zip(z) {
assert!((got - z.exp() / s).abs() < 1e-15);
}
assert_eq!(confidence(&m, &[0.0, 0.0, 4.0]), Some(p[0].max(p[2])));
assert!(probabilities(&m, &[0.0, 1.0]).is_none());
assert!(probabilities(&m, &[0.0, 1.0, 2.0, 3.0]).is_none());
assert!(probabilities(&map("isotonic", &[], None), &[0.0, 1.0]).is_none());
assert!(probabilities(&m, &[0.0, f64::MAX, 1e308]).is_some());
assert!(probabilities(
&map("dr", &[("a", 4.0), ("b0", 0.0), ("b1", 0.0)], Some(1)),
&[f64::MAX, 0.0]
)
.is_none());
}
#[test]
fn check_holds_each_kind_to_its_shape() {
let ok = |m: &CalibrationMap| check(m, 2).is_ok();
assert!(ok(&map("temperature", &[("temperature", 1.3)], None)));
assert!(ok(&map("temperature", &[("temperature", 1.3)], Some(200))));
assert!(!ok(&map("temperature", &[("temperature", -1.0)], None)));
assert!(!ok(&map(
"temperature",
&[("temperature", 1.3), ("x", 1.0)],
None
)));
assert!(!ok(&map("temperature", &[], None)));
let dr2 = [("a", 0.9), ("b0", 0.1), ("b1", -0.1)];
assert!(ok(&map("dr", &dr2, Some(1))));
assert!(!ok(&map("dr", &dr2, None)));
assert!(!ok(&map("dr", &dr2, Some(0))));
assert!(!ok(&map(
"dr",
&[("a", 0.0), ("b0", 0.1), ("b1", -0.1)],
Some(9)
)));
assert!(!ok(&map("dr", &[("a", 0.9), ("b0", 0.1)], Some(9))));
assert!(!ok(&map(
"dr",
&[("a", 0.9), ("b0", 0.1), ("b2", 0.1)],
Some(9)
)));
assert!(!ok(&map(
"dr",
&[("a", 0.9), ("b0", 0.1), ("b1", 0.1), ("b2", 0.1)],
Some(9)
)));
assert!(ok(&map("isotonic", &[("knots", 3.0)], None)));
}
}