//! Recalibration of predicted probabilities, fitted on pooled out-of-sample forecasts: //! Platt (a logistic map on the logit) or isotonic regression (pool-adjacent-violators, a //! monotone step function, interpolated linearly between block centres). use super::glm::{self, Design}; #[derive(Clone, Debug)] pub enum Calib { Identity, Platt(f64, f64), /// Knots (predicted, observed), increasing in both. Iso(Vec<(f64, f64)>), } fn logit(p: f64) -> f64 { let p = p.clamp(1e-6, 1.0 - 1e-6); (p / (1.0 - p)).ln() } impl Calib { pub fn apply(&self, p: f64) -> f64 { let out = match self { Calib::Identity => p, Calib::Platt(a, b) => glm::sigmoid(a + b * logit(p)), Calib::Iso(k) => { if k.is_empty() { p } else if p <= k[0].0 { k[0].1 } else if p >= k[k.len() - 1].0 { k[k.len() - 1].1 } else { let i = k.partition_point(|x| x.0 <= p); let ((x0, y0), (x1, y1)) = (k[i - 1], k[i]); if x1 > x0 { y0 + (y1 - y0) * (p - x0) / (x1 - x0) } else { y1 } } } }; out.clamp(1e-5, 1.0 - 1e-5) } /// Platt on pooled (prediction, outcome) pairs; identity if there are too few positives. pub fn platt(pairs: &[(f64, bool)]) -> Calib { if pairs.iter().filter(|x| x.1).count() < 10 { return Calib::Identity; } let mut d = Design::new(2); for (p, y) in pairs { d.push(&[1.0, logit(*p)], *y as i32 as f64, 1.0); } let b = glm::logit(&d, 0.0); if b[1] > 0.0 { Calib::Platt(b[0], b[1]) } else { Calib::Identity } } /// Isotonic regression (pool-adjacent-violators) on pooled pairs. A small prior (one pseudo /// observation at the block's mean prediction) keeps thin blocks off 0 and 1. pub fn isotonic(pairs: &[(f64, bool)]) -> Calib { if pairs.iter().filter(|x| x.1).count() < 10 { return Calib::Identity; } let mut v: Vec<(f64, f64)> = pairs.iter().map(|(p, y)| (*p, *y as i32 as f64)).collect(); v.sort_by(|a, b| a.0.total_cmp(&b.0)); // blocks: (sum p, sum y, n) let mut blocks: Vec<(f64, f64, f64)> = Vec::new(); for (p, y) in v { blocks.push((p, y, 1.0)); while blocks.len() >= 2 { let n = blocks.len(); let (a, b) = (blocks[n - 2], blocks[n - 1]); if a.1 / a.2 >= b.1 / b.2 { blocks.pop(); blocks.pop(); blocks.push((a.0 + b.0, a.1 + b.1, a.2 + b.2)); } else { break; } } } let knots: Vec<(f64, f64)> = blocks.iter().map(|(sp, sy, n)| { let mp = sp / n; (mp, (sy + mp) / (n + 1.0)) }).collect(); Calib::Iso(knots) } } #[cfg(test)] mod tests { use super::*; #[test] fn isotonic_is_monotone_and_fixes_overconfidence() { // predictions twice too high let pairs: Vec<(f64, bool)> = (0..20000).map(|i| { let p = (i % 100) as f64 / 100.0; (p, ((i * 7919) % 1000) as f64 / 1000.0 < p / 2.0) }).collect(); let c = Calib::isotonic(&pairs); let (a, b) = (c.apply(0.2), c.apply(0.8)); assert!(a < b && (a - 0.1).abs() < 0.05 && (b - 0.4).abs() < 0.06, "{a} {b}"); let pl = Calib::platt(&pairs); assert!(pl.apply(0.8) < 0.6); } }