use crate::assumptions::{AssumptionError, EstimatorError, Subject};
use crate::bounds::Bounds;
use crate::estimators;
use crate::measurement::Measurement;
use crate::measurement_unit::{
MeasurementUnit, UnitMismatchError, conversion_factor, finer, is_compatible,
};
use crate::sample::{Sample, check_compatible_units, check_non_weighted};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Metric {
Center,
Spread,
Shift,
Ratio,
Disparity,
}
impl Metric {
pub fn as_str(&self) -> &'static str {
match self {
Metric::Center => "center",
Metric::Spread => "spread",
Metric::Shift => "shift",
Metric::Ratio => "ratio",
Metric::Disparity => "disparity",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ComparisonVerdict {
Less,
Greater,
Inconclusive,
}
impl ComparisonVerdict {
pub fn as_str(&self) -> &'static str {
match self {
ComparisonVerdict::Less => "less",
ComparisonVerdict::Greater => "greater",
ComparisonVerdict::Inconclusive => "inconclusive",
}
}
}
#[derive(Debug, Clone)]
pub struct Threshold {
metric: Metric,
value: Measurement,
misrate: f64,
}
impl Threshold {
pub fn new(metric: Metric, value: Measurement, misrate: f64) -> Result<Self, EstimatorError> {
if !misrate.is_finite() || misrate <= 0.0 || misrate > 1.0 {
return Err(EstimatorError::from(AssumptionError::domain(
Subject::Misrate,
)));
}
if !value.value.is_finite() {
return Err(EstimatorError::Other(
"threshold value must be finite".to_string(),
));
}
Ok(Self {
metric,
value,
misrate,
})
}
pub fn metric(&self) -> Metric {
self.metric
}
pub fn value(&self) -> &Measurement {
&self.value
}
pub fn misrate(&self) -> f64 {
self.misrate
}
}
#[derive(Debug, Clone)]
pub struct Projection {
threshold: Threshold,
estimate: Measurement,
bounds: Bounds,
verdict: ComparisonVerdict,
}
impl Projection {
pub fn new(
threshold: Threshold,
estimate: Measurement,
bounds: Bounds,
verdict: ComparisonVerdict,
) -> Self {
Self {
threshold,
estimate,
bounds,
verdict,
}
}
pub fn threshold(&self) -> &Threshold {
&self.threshold
}
pub fn estimate(&self) -> &Measurement {
&self.estimate
}
pub fn bounds(&self) -> &Bounds {
&self.bounds
}
pub fn verdict(&self) -> ComparisonVerdict {
self.verdict
}
}
fn compute_verdict(bounds: &Bounds, threshold_value: f64) -> ComparisonVerdict {
if bounds.lower > threshold_value {
ComparisonVerdict::Greater
} else if bounds.upper < threshold_value {
ComparisonVerdict::Less
} else {
ComparisonVerdict::Inconclusive
}
}
type SeededBoundsFn = fn(&Sample, Option<&Sample>, f64, &str) -> Result<Bounds, EstimatorError>;
struct MetricSpec {
metric: Metric,
validate_and_normalize:
fn(&Threshold, &Sample, Option<&Sample>) -> Result<Measurement, EstimatorError>,
estimate: fn(&Sample, Option<&Sample>) -> Result<Measurement, EstimatorError>,
bounds: fn(&Sample, Option<&Sample>, f64) -> Result<Bounds, EstimatorError>,
seeded_bounds: Option<SeededBoundsFn>,
}
const COMPARE1_SPECS: &[MetricSpec] = &[
MetricSpec {
metric: Metric::Center,
validate_and_normalize: validate_center,
estimate: |x, _| estimators::center(x),
bounds: |x, _, misrate| estimators::center_bounds(x, misrate),
seeded_bounds: None,
},
MetricSpec {
metric: Metric::Spread,
validate_and_normalize: validate_spread,
estimate: |x, _| estimators::spread(x),
bounds: |x, _, misrate| estimators::spread_bounds(x, misrate),
seeded_bounds: Some(|x, _, misrate, seed| {
estimators::spread_bounds_with_seed(x, misrate, seed)
}),
},
];
const COMPARE2_SPECS: &[MetricSpec] = &[
MetricSpec {
metric: Metric::Shift,
validate_and_normalize: validate_shift,
estimate: |x, y| {
let y = y.expect("Shift requires y sample");
estimators::shift(x, y)
},
bounds: |x, y, misrate| {
let y = y.expect("Shift requires y sample");
estimators::shift_bounds(x, y, misrate)
},
seeded_bounds: None,
},
MetricSpec {
metric: Metric::Ratio,
validate_and_normalize: validate_ratio,
estimate: |x, y| {
let y = y.expect("Ratio requires y sample");
estimators::ratio(x, y)
},
bounds: |x, y, misrate| {
let y = y.expect("Ratio requires y sample");
estimators::ratio_bounds(x, y, misrate)
},
seeded_bounds: None,
},
MetricSpec {
metric: Metric::Disparity,
validate_and_normalize: validate_disparity,
estimate: |x, y| {
let y = y.expect("Disparity requires y sample");
estimators::disparity(x, y)
},
bounds: |x, y, misrate| {
let y = y.expect("Disparity requires y sample");
estimators::disparity_bounds(x, y, misrate)
},
seeded_bounds: Some(|x, y, misrate, seed| {
let y = y.expect("Disparity requires y sample");
estimators::disparity_bounds_with_seed(x, y, misrate, seed)
}),
},
];
fn validate_center(
threshold: &Threshold,
x: &Sample,
_: Option<&Sample>,
) -> Result<Measurement, EstimatorError> {
if !is_compatible(&threshold.value.unit, x.unit()) {
return Err(UnitMismatchError::new(&threshold.value.unit, x.unit()).into());
}
let factor = conversion_factor(&threshold.value.unit, x.unit());
Ok(Measurement::new(
threshold.value.value * factor,
x.unit().clone(),
))
}
fn validate_spread(
threshold: &Threshold,
x: &Sample,
_: Option<&Sample>,
) -> Result<Measurement, EstimatorError> {
validate_center(threshold, x, None) }
fn validate_shift(
threshold: &Threshold,
x: &Sample,
y: Option<&Sample>,
) -> Result<Measurement, EstimatorError> {
let y = y.expect("Shift requires y sample");
if !is_compatible(&threshold.value.unit, x.unit()) {
return Err(UnitMismatchError::new(&threshold.value.unit, x.unit()).into());
}
let target = finer(x.unit(), y.unit());
let factor = conversion_factor(&threshold.value.unit, target);
Ok(Measurement::new(
threshold.value.value * factor,
target.clone(),
))
}
fn validate_ratio(
threshold: &Threshold,
_: &Sample,
_: Option<&Sample>,
) -> Result<Measurement, EstimatorError> {
let unit = &threshold.value.unit;
if unit.id() != "ratio" && unit.id() != "number" {
return Err(UnitMismatchError::new(unit, &MeasurementUnit::ratio()).into());
}
let value = threshold.value.value;
if value <= 0.0 {
return Err(EstimatorError::Other(
"Ratio threshold value must be positive".to_string(),
));
}
Ok(Measurement::new(value, MeasurementUnit::ratio()))
}
fn validate_disparity(
threshold: &Threshold,
_: &Sample,
_: Option<&Sample>,
) -> Result<Measurement, EstimatorError> {
let unit = &threshold.value.unit;
if unit.id() != "disparity" && unit.id() != "number" {
return Err(UnitMismatchError::new(unit, &MeasurementUnit::disparity()).into());
}
let value = threshold.value.value;
if !value.is_finite() {
return Err(EstimatorError::Other(
"Disparity threshold value must be finite".to_string(),
));
}
Ok(Measurement::new(value, MeasurementUnit::disparity()))
}
fn get_spec(specs: &[MetricSpec], metric: Metric) -> Option<&MetricSpec> {
specs.iter().find(|s| s.metric == metric)
}
pub fn compare1(x: &Sample, thresholds: &[Threshold]) -> Result<Vec<Projection>, EstimatorError> {
compare1_impl(x, thresholds, None)
}
pub fn compare1_with_seed(
x: &Sample,
thresholds: &[Threshold],
seed: &str,
) -> Result<Vec<Projection>, EstimatorError> {
compare1_impl(x, thresholds, Some(seed))
}
pub fn compare2(
x: &Sample,
y: &Sample,
thresholds: &[Threshold],
) -> Result<Vec<Projection>, EstimatorError> {
compare2_impl(x, y, thresholds, None)
}
pub fn compare2_with_seed(
x: &Sample,
y: &Sample,
thresholds: &[Threshold],
seed: &str,
) -> Result<Vec<Projection>, EstimatorError> {
compare2_impl(x, y, thresholds, Some(seed))
}
fn compare1_impl(
x: &Sample,
thresholds: &[Threshold],
seed: Option<&str>,
) -> Result<Vec<Projection>, EstimatorError> {
check_non_weighted("x", x)?;
if thresholds.is_empty() {
return Err(EstimatorError::Other(
"thresholds list cannot be empty".to_string(),
));
}
for threshold in thresholds {
if !matches!(threshold.metric, Metric::Center | Metric::Spread) {
return Err(EstimatorError::Other(format!(
"Metric {} is not supported by Compare1. Use Compare2 instead.",
threshold.metric.as_str()
)));
}
}
let mut normalized_values = Vec::with_capacity(thresholds.len());
for threshold in thresholds {
let spec = get_spec(COMPARE1_SPECS, threshold.metric).expect("spec exists");
let normalized = (spec.validate_and_normalize)(threshold, x, None)?;
normalized_values.push(normalized);
}
let mut results: Vec<Option<Projection>> = vec![None; thresholds.len()];
for spec in COMPARE1_SPECS {
let entries: Vec<(usize, &Threshold, &Measurement)> = thresholds
.iter()
.zip(normalized_values.iter())
.enumerate()
.filter(|(_, (t, _))| t.metric == spec.metric)
.map(|(i, (t, n))| (i, t, n))
.collect();
if entries.is_empty() {
continue;
}
let estimate = (spec.estimate)(x, None)?;
for (input_idx, threshold, normalized_value) in entries {
let bounds = match (seed, spec.seeded_bounds) {
(Some(s), Some(seeded_fn)) => seeded_fn(x, None, threshold.misrate, s)?,
_ => (spec.bounds)(x, None, threshold.misrate)?,
};
let verdict = compute_verdict(&bounds, normalized_value.value);
results[input_idx] = Some(Projection::new(
threshold.clone(),
estimate.clone(),
bounds,
verdict,
));
}
}
Ok(results.into_iter().flatten().collect())
}
fn compare2_impl(
x: &Sample,
y: &Sample,
thresholds: &[Threshold],
seed: Option<&str>,
) -> Result<Vec<Projection>, EstimatorError> {
check_non_weighted("x", x)?;
check_non_weighted("y", y)?;
check_compatible_units(x, y)?;
if thresholds.is_empty() {
return Err(EstimatorError::Other(
"thresholds list cannot be empty".to_string(),
));
}
for threshold in thresholds {
if !matches!(
threshold.metric,
Metric::Shift | Metric::Ratio | Metric::Disparity
) {
return Err(EstimatorError::Other(format!(
"Metric {} is not supported by Compare2. Use Compare1 instead.",
threshold.metric.as_str()
)));
}
}
let target = finer(x.unit(), y.unit());
let x_conv = x
.convert_to(target)
.map_err(|e| EstimatorError::Other(e.to_string()))?;
let y_conv = y
.convert_to(target)
.map_err(|e| EstimatorError::Other(e.to_string()))?;
let mut normalized_values = Vec::with_capacity(thresholds.len());
for threshold in thresholds {
let spec = get_spec(COMPARE2_SPECS, threshold.metric).expect("spec exists");
let normalized = (spec.validate_and_normalize)(threshold, &x_conv, Some(&y_conv))?;
normalized_values.push(normalized);
}
let mut results: Vec<Option<Projection>> = vec![None; thresholds.len()];
for spec in COMPARE2_SPECS {
let entries: Vec<(usize, &Threshold, &Measurement)> = thresholds
.iter()
.zip(normalized_values.iter())
.enumerate()
.filter(|(_, (t, _))| t.metric == spec.metric)
.map(|(i, (t, n))| (i, t, n))
.collect();
if entries.is_empty() {
continue;
}
let estimate = (spec.estimate)(&x_conv, Some(&y_conv))?;
for (input_idx, threshold, normalized_value) in entries {
let bounds = match (seed, spec.seeded_bounds) {
(Some(s), Some(seeded_fn)) => {
seeded_fn(&x_conv, Some(&y_conv), threshold.misrate, s)?
}
_ => (spec.bounds)(&x_conv, Some(&y_conv), threshold.misrate)?,
};
let verdict = compute_verdict(&bounds, normalized_value.value);
results[input_idx] = Some(Projection::new(
threshold.clone(),
estimate.clone(),
bounds,
verdict,
));
}
}
Ok(results.into_iter().flatten().collect())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::sample::Sample;
#[test]
fn test_compare1_center() {
let x = Sample::new(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]).unwrap();
let threshold = Threshold::new(Metric::Center, Measurement::number(5.5), 0.05).unwrap();
let projections = compare1(&x, &[threshold]).unwrap();
assert_eq!(projections.len(), 1);
assert!(matches!(
projections[0].verdict,
ComparisonVerdict::Inconclusive
));
}
#[test]
fn test_compare1_spread() {
let x = Sample::new(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]).unwrap();
let threshold = Threshold::new(Metric::Spread, Measurement::number(0.1), 0.2).unwrap();
let projections = compare1_with_seed(&x, &[threshold], "test-seed").unwrap();
assert_eq!(projections.len(), 1);
assert!(matches!(projections[0].verdict, ComparisonVerdict::Greater));
}
#[test]
fn test_compare1_wrong_metric() {
let x = Sample::new(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]).unwrap();
let threshold = Threshold::new(Metric::Shift, Measurement::number(0.0), 0.1).unwrap();
assert!(compare1(&x, &[threshold]).is_err());
}
#[test]
fn test_compare1_empty_thresholds() {
let x = Sample::new(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]).unwrap();
assert!(compare1(&x, &[]).is_err());
}
#[test]
fn test_compare2_shift() {
let x = Sample::new((1..=30).map(|v| v as f64).collect()).unwrap();
let y = Sample::new((21..=50).map(|v| v as f64).collect()).unwrap();
let threshold = Threshold::new(Metric::Shift, Measurement::number(0.0), 0.02).unwrap();
let projections = compare2(&x, &y, &[threshold]).unwrap();
assert_eq!(projections.len(), 1);
assert!(matches!(projections[0].verdict, ComparisonVerdict::Less));
}
#[test]
fn test_compare2_wrong_metric() {
let x = Sample::new(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]).unwrap();
let y = Sample::new(vec![6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0]).unwrap();
let threshold = Threshold::new(Metric::Center, Measurement::number(5.0), 0.1).unwrap();
assert!(compare2(&x, &y, &[threshold]).is_err());
}
#[test]
fn test_compare2_empty_thresholds() {
let x = Sample::new(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]).unwrap();
let y = Sample::new(vec![6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0]).unwrap();
assert!(compare2(&x, &y, &[]).is_err());
}
#[test]
fn test_threshold_non_finite_value() {
assert!(
Threshold::new(
Metric::Center,
Measurement::new(f64::INFINITY, MeasurementUnit::number()),
0.1,
)
.is_err()
);
}
#[test]
fn test_threshold_invalid_misrate() {
assert!(Threshold::new(Metric::Center, Measurement::number(5.0), -0.1).is_err());
assert!(Threshold::new(Metric::Center, Measurement::number(5.0), 0.0).is_err());
assert!(Threshold::new(Metric::Center, Measurement::number(5.0), 1.5).is_err());
assert!(Threshold::new(Metric::Center, Measurement::number(5.0), 1.0).is_ok());
}
}