Secures work in progress so the rake branch and the shell repo can be merged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
58 lines
2.1 KiB
Rust
58 lines
2.1 KiB
Rust
//! Paired comparison of two measurements over the same deals: the difference per deal removes
|
|
//! what both runs share, so a small change shows long before the two win rates' intervals part.
|
|
|
|
/// 95 % two-sided normal quantile.
|
|
const Z95: f64 = 1.959_964;
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum Verdict {
|
|
/// Not a single result moved.
|
|
Identical,
|
|
/// The interval of the difference lies above 0.
|
|
Better,
|
|
/// The interval of the difference lies below 0.
|
|
Worse,
|
|
/// Something moved, but the interval still contains 0.
|
|
Unclear,
|
|
}
|
|
|
|
impl Verdict {
|
|
/// Judges a difference and the half width of its interval.
|
|
pub fn of(delta: f64, ci95: f64) -> Verdict {
|
|
if delta - ci95 > 0.0 {
|
|
Verdict::Better
|
|
} else if delta + ci95 < 0.0 {
|
|
Verdict::Worse
|
|
} else {
|
|
Verdict::Unclear
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
pub struct Comparison {
|
|
/// New minus base, bb/100.
|
|
pub delta_bb_per_100: f64,
|
|
/// Half width of the 95 % interval of the difference, bb/100.
|
|
pub ci95: f64,
|
|
pub changed_deals: u32,
|
|
pub verdict: Verdict,
|
|
}
|
|
|
|
/// Compares per-deal results in bb. The deals must be the same ones, in the same order.
|
|
pub fn compare(base: &[f64], new: &[f64]) -> Result<Comparison, String> {
|
|
if base.len() != new.len() || base.is_empty() {
|
|
return Err(format!("{} gegen {} Austeilungen", base.len(), new.len()));
|
|
}
|
|
let diffs: Vec<f64> = base.iter().zip(new).map(|(b, n)| n - b).collect();
|
|
let changed_deals = diffs.iter().filter(|d| **d != 0.0).count() as u32;
|
|
if changed_deals == 0 {
|
|
return Ok(Comparison { delta_bb_per_100: 0.0, ci95: 0.0, changed_deals, verdict: Verdict::Identical });
|
|
}
|
|
let n = diffs.len() as f64;
|
|
let mean = diffs.iter().sum::<f64>() / n;
|
|
let var = diffs.iter().map(|d| (d - mean).powi(2)).sum::<f64>() / (n - 1.0).max(1.0);
|
|
let (delta, ci95) = (100.0 * mean, 100.0 * Z95 * (var / n).sqrt());
|
|
Ok(Comparison { delta_bb_per_100: delta, ci95, changed_deals, verdict: Verdict::of(delta, ci95) })
|
|
}
|