use std::collections::{HashMap, HashSet};
use crate::Outcome;
use crate::error::VeridictError;
use crate::report::ScaleDiagnostics;
use crate::stats::bootstrap;
pub(crate) const ROBUST_SPAN_MIN_POSITIVE_BASELINES: usize = 20;
pub(crate) fn compute_scale_diagnostics(baselines: &[f64]) -> ScaleDiagnostics {
let mut positive: Vec<f64> = baselines.iter().copied().filter(|&b| b > 0.0).collect();
let non_positive_baseline_count = (baselines.len() - positive.len()) as u64;
if positive.is_empty() {
return ScaleDiagnostics {
positive_baseline_count: 0,
non_positive_baseline_count,
min_positive_baseline: 0.0,
max_positive_baseline: 0.0,
raw_orders_of_magnitude: 0.0,
robust_orders_of_magnitude: None,
};
}
positive.sort_by(f64::total_cmp);
let min_positive_baseline = positive[0];
let max_positive_baseline = positive[positive.len() - 1];
let raw_orders_of_magnitude = max_positive_baseline.log10() - min_positive_baseline.log10();
let robust_orders_of_magnitude = if positive.len() >= ROBUST_SPAN_MIN_POSITIVE_BASELINES {
let p05 = bootstrap::quantile(&positive, 0.05);
let p95 = bootstrap::quantile(&positive, 0.95);
Some(p95.log10() - p05.log10())
} else {
None
};
ScaleDiagnostics {
positive_baseline_count: positive.len() as u64,
non_positive_baseline_count,
min_positive_baseline,
max_positive_baseline,
raw_orders_of_magnitude,
robust_orders_of_magnitude,
}
}
pub(crate) struct OutcomeCollector {
paired_by_id: bool,
cluster_by_id: bool,
require_complete_pairs: bool,
baseline_wins: u64,
candidate_wins: u64,
draws: u64,
groups: HashMap<String, Vec<(usize, Outcome)>>,
singletons: Vec<Outcome>,
}
impl OutcomeCollector {
pub(crate) fn new(
paired_by_id: bool,
cluster_by_id: bool,
require_complete_pairs: bool,
) -> Self {
Self {
paired_by_id,
cluster_by_id,
require_complete_pairs,
baseline_wins: 0,
candidate_wins: 0,
draws: 0,
groups: HashMap::new(),
singletons: Vec::new(),
}
}
pub(crate) fn record(&mut self, line: usize, id: Option<&str>, outcome: Outcome) {
match (self.paired_by_id, self.cluster_by_id, id) {
(true, _, Some(id)) | (_, true, Some(id)) => {
self.groups
.entry(id.to_string())
.or_default()
.push((line, outcome));
}
(_, true, None) => self.singletons.push(outcome),
_ => self.tally(outcome),
}
}
fn tally(&mut self, outcome: Outcome) {
match outcome {
Outcome::BaselineWin => self.baseline_wins += 1,
Outcome::CandidateWin => self.candidate_wins += 1,
Outcome::Draw => self.draws += 1,
}
}
pub(crate) fn finish_clusters(mut self) -> Vec<Vec<Outcome>> {
let mut groups: Vec<_> = self.groups.drain().collect();
groups.sort_by_key(|(_, g)| g.iter().map(|(line, _)| *line).min().unwrap());
let mut clusters: Vec<Vec<Outcome>> = groups
.into_iter()
.map(|(_, g)| g.into_iter().map(|(_, o)| o).collect())
.collect();
clusters.extend(self.singletons.drain(..).map(|o| vec![o]));
clusters
}
pub(crate) fn finish(mut self) -> Result<(u64, u64, u64), VeridictError> {
let require_complete_pairs = self.require_complete_pairs;
for (id, group) in std::mem::take(&mut self.groups) {
match group.as_slice() {
[(line, outcome)] => {
if require_complete_pairs {
return Err(VeridictError::SchemaMismatch {
line: *line,
context: "paired-by-id",
detail: format!(
"id '{id}' appears once; --require-complete-pairs requires \
exactly 2 records per id"
),
});
}
self.tally(*outcome)
}
[(_, a), (_, b)] => {
let points = |o: &Outcome| match o {
Outcome::CandidateWin => 1.0,
Outcome::Draw => 0.5,
Outcome::BaselineWin => 0.0,
};
let total = points(a) + points(b);
#[allow(clippy::float_cmp)]
let net = if total > 1.0 {
Outcome::CandidateWin
} else if total < 1.0 {
Outcome::BaselineWin
} else {
Outcome::Draw
};
self.tally(net);
}
more => {
return Err(VeridictError::SchemaMismatch {
line: more[0].0,
context: "paired-by-id",
detail: format!(
"id '{id}' appears {} times; paired mode expects at most 2 records per id",
more.len()
),
});
}
}
}
Ok((self.baseline_wins, self.candidate_wins, self.draws))
}
}
pub(crate) struct DiffCollector {
paired_by_id: bool,
seen_ids: HashSet<String>,
diffs: Vec<f64>,
groups: HashMap<String, Vec<(usize, f64)>>,
}
impl DiffCollector {
pub(crate) fn new(paired_by_id: bool) -> Self {
Self {
paired_by_id,
seen_ids: HashSet::new(),
diffs: Vec::new(),
groups: HashMap::new(),
}
}
pub(crate) fn record(
&mut self,
line: usize,
id: Option<&str>,
diff: f64,
) -> Result<(), VeridictError> {
if self.paired_by_id {
match id {
Some(id) => {
self.groups
.entry(id.to_string())
.or_default()
.push((line, diff));
}
None => self.diffs.push(diff),
}
} else {
if let Some(id) = id
&& !self.seen_ids.insert(id.to_string())
{
return Err(VeridictError::DuplicateId {
id: id.to_string(),
line,
});
}
self.diffs.push(diff);
}
Ok(())
}
pub(crate) fn finish(mut self) -> Result<Vec<f64>, VeridictError> {
let mut groups: Vec<_> = self.groups.drain().collect();
groups.sort_by_key(|(_, g)| g.iter().map(|(line, _)| *line).min().unwrap());
for (id, group) in groups {
match group.as_slice() {
[(_, d)] => self.diffs.push(*d),
[(_, a), (_, b)] => {
let sum = a + b;
let paired_mean = if sum.is_finite() {
sum / 2.0
} else {
bootstrap::mean(&[*a, *b])
};
self.diffs.push(paired_mean);
}
more => {
return Err(VeridictError::SchemaMismatch {
line: more[0].0,
context: "paired-by-id",
detail: format!(
"id '{id}' appears {} times; paired mode expects at most 2 records per id",
more.len()
),
});
}
}
}
Ok(self.diffs)
}
}
pub(crate) struct SignCounts {
paired_by_id: bool,
seen_ids: HashSet<String>,
positive: u64,
negative: u64,
groups: HashMap<String, Vec<(usize, f64)>>,
}
impl SignCounts {
pub(crate) fn new(paired_by_id: bool) -> Self {
Self {
paired_by_id,
seen_ids: HashSet::new(),
positive: 0,
negative: 0,
groups: HashMap::new(),
}
}
pub(crate) fn record(
&mut self,
line: usize,
id: Option<&str>,
diff: f64,
) -> Result<(), VeridictError> {
if self.paired_by_id {
match id {
Some(id) => {
self.groups
.entry(id.to_string())
.or_default()
.push((line, diff));
}
None => self.tally(diff),
}
} else {
if let Some(id) = id
&& !self.seen_ids.insert(id.to_string())
{
return Err(VeridictError::DuplicateId {
id: id.to_string(),
line,
});
}
self.tally(diff);
}
Ok(())
}
fn tally(&mut self, diff: f64) {
if diff > 0.0 {
self.positive += 1;
} else if diff < 0.0 {
self.negative += 1;
}
}
pub(crate) fn finish(mut self) -> Result<(u64, u64), VeridictError> {
for (id, group) in std::mem::take(&mut self.groups) {
match group.as_slice() {
[(_, d)] => self.tally(*d),
[(_, a), (_, b)] => self.tally(bootstrap::mean(&[*a, *b])),
more => {
return Err(VeridictError::SchemaMismatch {
line: more[0].0,
context: "paired-by-id",
detail: format!(
"id '{id}' appears {} times; paired mode expects at most 2 records per id",
more.len()
),
});
}
}
}
Ok((self.positive, self.negative))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_baselines_has_zero_positive_count_and_no_robust_span() {
let diag = compute_scale_diagnostics(&[]);
assert_eq!(diag.positive_baseline_count, 0);
assert_eq!(diag.non_positive_baseline_count, 0);
assert_eq!(diag.raw_orders_of_magnitude, 0.0);
assert!(diag.robust_orders_of_magnitude.is_none());
}
#[test]
fn all_non_positive_baselines_has_zero_positive_count_and_no_span() {
let diag = compute_scale_diagnostics(&[0.0, -5.0, -10.0]);
assert_eq!(diag.positive_baseline_count, 0);
assert_eq!(diag.non_positive_baseline_count, 3);
assert_eq!(diag.raw_orders_of_magnitude, 0.0);
}
#[test]
fn raw_orders_of_magnitude_matches_log10_of_max_over_min() {
let diag = compute_scale_diagnostics(&[10.0, 100.0, 1_000.0]);
assert!((diag.raw_orders_of_magnitude - 2.0).abs() < 1e-9); assert_eq!(diag.positive_baseline_count, 3);
assert_eq!(diag.non_positive_baseline_count, 0);
assert_eq!(diag.min_positive_baseline, 10.0);
assert_eq!(diag.max_positive_baseline, 1_000.0);
}
#[test]
fn mixed_sign_baselines_count_positives_and_non_positives_separately() {
let diag = compute_scale_diagnostics(&[10.0, -5.0, 0.0, 1_000.0]);
assert_eq!(diag.positive_baseline_count, 2);
assert_eq!(diag.non_positive_baseline_count, 2);
}
#[test]
fn robust_span_is_none_below_the_20_positive_baseline_floor() {
let baselines: Vec<f64> = (1..=19).map(|i| i as f64).collect();
let diag = compute_scale_diagnostics(&baselines);
assert_eq!(diag.positive_baseline_count, 19);
assert!(diag.robust_orders_of_magnitude.is_none());
}
#[test]
fn robust_span_appears_at_the_20_positive_baseline_floor() {
let baselines: Vec<f64> = (1..=20).map(|i| i as f64).collect();
let diag = compute_scale_diagnostics(&baselines);
assert_eq!(diag.positive_baseline_count, 20);
assert!(diag.robust_orders_of_magnitude.is_some());
}
#[test]
fn a_single_extreme_outlier_does_not_move_the_robust_span_at_n40() {
let mut baselines: Vec<f64> = (0..39).map(|i| 100.0 + i as f64).collect();
baselines.push(10_000_000.0);
let diag = compute_scale_diagnostics(&baselines);
assert_eq!(diag.positive_baseline_count, 40);
let robust = diag.robust_orders_of_magnitude.unwrap();
assert!(
robust < 1.0,
"robust span {robust} was pulled up by the single outlier"
);
assert!(diag.raw_orders_of_magnitude > 4.0);
}
#[test]
fn result_does_not_depend_on_input_order() {
let a = compute_scale_diagnostics(&[5.0, 500.0, 50.0]);
let b = compute_scale_diagnostics(&[50.0, 5.0, 500.0]);
assert_eq!(a.raw_orders_of_magnitude, b.raw_orders_of_magnitude);
assert_eq!(a.min_positive_baseline, b.min_positive_baseline);
assert_eq!(a.max_positive_baseline, b.max_positive_baseline);
}
}