use crate::assumptions::{
check_positivity, check_sparity, check_validity, log, AssumptionError, EstimatorError, Subject,
};
pub fn center(x: &[f64]) -> Result<f64, EstimatorError> {
check_validity(x, Subject::X)?;
crate::fast_center::fast_center(x)
.map_err(|_| EstimatorError::from(AssumptionError::validity(Subject::X)))
}
pub fn spread(x: &[f64]) -> Result<f64, EstimatorError> {
check_validity(x, Subject::X)?;
check_sparity(x, Subject::X)?;
crate::fast_spread::fast_spread(x)
.map_err(|_| EstimatorError::from(AssumptionError::validity(Subject::X)))
}
#[deprecated(since = "10.0.0", note = "use spread(x) / center(x).abs() instead")]
pub fn rel_spread(x: &[f64]) -> Result<f64, EstimatorError> {
check_validity(x, Subject::X)?;
check_positivity(x, Subject::X)?;
let center_val = crate::fast_center::fast_center(x)
.map_err(|_| EstimatorError::from(AssumptionError::validity(Subject::X)))?;
let spread_val = crate::fast_spread::fast_spread(x)
.map_err(|_| EstimatorError::from(AssumptionError::validity(Subject::X)))?;
Ok(spread_val / center_val.abs())
}
pub fn shift(x: &[f64], y: &[f64]) -> Result<f64, EstimatorError> {
check_validity(x, Subject::X)?;
check_validity(y, Subject::Y)?;
crate::fast_shift::fast_shift(x, y)
.map_err(|_| EstimatorError::from(AssumptionError::validity(Subject::X)))
}
pub fn ratio(x: &[f64], y: &[f64]) -> Result<f64, EstimatorError> {
check_validity(x, Subject::X)?;
check_validity(y, Subject::Y)?;
check_positivity(x, Subject::X)?;
check_positivity(y, Subject::Y)?;
crate::fast_shift::fast_ratio(x, y)
.map_err(|_| EstimatorError::from(AssumptionError::validity(Subject::X)))
}
#[cfg(test)]
pub(crate) fn avg_spread(x: &[f64], y: &[f64]) -> Result<f64, EstimatorError> {
check_validity(x, Subject::X)?;
check_validity(y, Subject::Y)?;
check_sparity(x, Subject::X)?;
check_sparity(y, Subject::Y)?;
let n = x.len();
let m = y.len();
let spread_x = crate::fast_spread::fast_spread(x)
.map_err(|_| EstimatorError::from(AssumptionError::validity(Subject::X)))?;
let spread_y = crate::fast_spread::fast_spread(y)
.map_err(|_| EstimatorError::from(AssumptionError::validity(Subject::Y)))?;
Ok((n as f64 * spread_x + m as f64 * spread_y) / (n + m) as f64)
}
pub fn disparity(x: &[f64], y: &[f64]) -> Result<f64, EstimatorError> {
check_validity(x, Subject::X)?;
check_validity(y, Subject::Y)?;
check_sparity(x, Subject::X)?;
check_sparity(y, Subject::Y)?;
let shift_val = crate::fast_shift::fast_shift(x, y)
.map_err(|_| EstimatorError::from(AssumptionError::validity(Subject::X)))?;
let n = x.len();
let m = y.len();
let spread_x = crate::fast_spread::fast_spread(x)
.map_err(|_| EstimatorError::from(AssumptionError::validity(Subject::X)))?;
let spread_y = crate::fast_spread::fast_spread(y)
.map_err(|_| EstimatorError::from(AssumptionError::validity(Subject::Y)))?;
let avg_spread_val = (n as f64 * spread_x + m as f64 * spread_y) / (n + m) as f64;
Ok(shift_val / avg_spread_val)
}
pub const DEFAULT_MISRATE: f64 = 1e-3;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Bounds {
pub lower: f64,
pub upper: f64,
}
pub fn shift_bounds(x: &[f64], y: &[f64], misrate: f64) -> Result<Bounds, EstimatorError> {
check_validity(x, Subject::X)?;
check_validity(y, Subject::Y)?;
if misrate.is_nan() || !(0.0..=1.0).contains(&misrate) {
return Err(EstimatorError::from(AssumptionError::domain(
Subject::Misrate,
)));
}
let n = x.len();
let m = y.len();
let min_misrate = crate::min_misrate::min_achievable_misrate_two_sample(n, m)
.map_err(EstimatorError::from)?;
if misrate < min_misrate {
return Err(EstimatorError::from(AssumptionError::domain(
Subject::Misrate,
)));
}
let mut xs = x.to_vec();
let mut ys = y.to_vec();
xs.sort_by(|a, b| a.total_cmp(b));
ys.sort_by(|a, b| a.total_cmp(b));
let total = n * m;
if total == 1 {
let value = xs[0] - ys[0];
return Ok(Bounds {
lower: value,
upper: value,
});
}
let margin =
crate::pairwise_margin::pairwise_margin(n, m, misrate).map_err(EstimatorError::from)?;
let max_half_margin = (total - 1) / 2;
let mut half_margin = margin / 2;
if half_margin > max_half_margin {
half_margin = max_half_margin;
}
let k_left = half_margin;
let k_right = total - 1 - half_margin;
let denominator = (total - 1) as f64;
let p = vec![k_left as f64 / denominator, k_right as f64 / denominator];
let bounds = crate::fast_shift::fast_shift_quantiles(&xs, &ys, &p, true)
.map_err(EstimatorError::from)?;
let lower = bounds[0].min(bounds[1]);
let upper = bounds[0].max(bounds[1]);
Ok(Bounds { lower, upper })
}
pub fn ratio_bounds(x: &[f64], y: &[f64], misrate: f64) -> Result<Bounds, EstimatorError> {
check_validity(x, Subject::X)?;
check_validity(y, Subject::Y)?;
if misrate.is_nan() || !(0.0..=1.0).contains(&misrate) {
return Err(EstimatorError::from(AssumptionError::domain(
Subject::Misrate,
)));
}
let min_misrate = crate::min_misrate::min_achievable_misrate_two_sample(x.len(), y.len())
.map_err(EstimatorError::from)?;
if misrate < min_misrate {
return Err(EstimatorError::from(AssumptionError::domain(
Subject::Misrate,
)));
}
let log_x = log(x, Subject::X)?;
let log_y = log(y, Subject::Y)?;
let log_bounds = shift_bounds(&log_x, &log_y, misrate)?;
Ok(Bounds {
lower: log_bounds.lower.exp(),
upper: log_bounds.upper.exp(),
})
}
pub fn center_bounds(x: &[f64], misrate: f64) -> Result<Bounds, EstimatorError> {
check_validity(x, Subject::X)?;
if misrate.is_nan() || !(0.0..=1.0).contains(&misrate) {
return Err(EstimatorError::from(AssumptionError::domain(
Subject::Misrate,
)));
}
let n = x.len();
if n < 2 {
return Err(EstimatorError::from(AssumptionError::domain(Subject::X)));
}
let min_misrate = crate::min_misrate::min_achievable_misrate_one_sample(n)?;
if misrate < min_misrate {
return Err(EstimatorError::from(AssumptionError::domain(
Subject::Misrate,
)));
}
let margin = crate::signed_rank_margin::signed_rank_margin(n, misrate)?;
let total_pairs = (n * (n + 1) / 2) as i64;
let mut half_margin = (margin / 2) as i64;
let max_half_margin = (total_pairs - 1) / 2;
if half_margin > max_half_margin {
half_margin = max_half_margin;
}
let k_left = half_margin + 1;
let k_right = total_pairs - half_margin;
let mut sorted = x.to_vec();
sorted.sort_by(|a, b| a.total_cmp(b));
let (lo, hi) =
crate::fast_center_quantiles::fast_center_quantile_bounds(&sorted, k_left, k_right);
Ok(Bounds {
lower: lo,
upper: hi,
})
}
pub fn spread_bounds(x: &[f64], misrate: f64) -> Result<Bounds, EstimatorError> {
let mut rng = crate::rng::Rng::new();
spread_bounds_with_rng(x, misrate, &mut rng)
}
pub fn spread_bounds_with_seed(
x: &[f64],
misrate: f64,
seed: &str,
) -> Result<Bounds, EstimatorError> {
let mut rng = crate::rng::Rng::from_string(seed);
spread_bounds_with_rng(x, misrate, &mut rng)
}
#[cfg(test)]
pub(crate) fn avg_spread_bounds(
x: &[f64],
y: &[f64],
misrate: f64,
) -> Result<Bounds, EstimatorError> {
let mut rng_x = crate::rng::Rng::new();
let mut rng_y = crate::rng::Rng::new();
avg_spread_bounds_with_rngs(x, y, misrate, &mut rng_x, &mut rng_y)
}
#[cfg(test)]
pub(crate) fn avg_spread_bounds_with_seed(
x: &[f64],
y: &[f64],
misrate: f64,
seed: &str,
) -> Result<Bounds, EstimatorError> {
let mut rng_x = crate::rng::Rng::from_string(seed);
let mut rng_y = crate::rng::Rng::from_string(seed);
avg_spread_bounds_with_rngs(x, y, misrate, &mut rng_x, &mut rng_y)
}
fn avg_spread_bounds_with_rngs(
x: &[f64],
y: &[f64],
misrate: f64,
rng_x: &mut crate::rng::Rng,
rng_y: &mut crate::rng::Rng,
) -> Result<Bounds, EstimatorError> {
check_validity(x, Subject::X)?;
check_validity(y, Subject::Y)?;
if misrate.is_nan() || !(0.0..=1.0).contains(&misrate) {
return Err(EstimatorError::from(AssumptionError::domain(
Subject::Misrate,
)));
}
let n = x.len();
let m = y.len();
if n < 2 {
return Err(EstimatorError::from(AssumptionError::domain(Subject::X)));
}
if m < 2 {
return Err(EstimatorError::from(AssumptionError::domain(Subject::Y)));
}
let mx = n / 2;
let my = m / 2;
let min_x = crate::min_misrate::min_achievable_misrate_one_sample(mx)?;
let min_y = crate::min_misrate::min_achievable_misrate_one_sample(my)?;
let alpha = misrate / 2.0;
if alpha < min_x || alpha < min_y {
return Err(EstimatorError::from(AssumptionError::domain(
Subject::Misrate,
)));
}
check_sparity(x, Subject::X)?;
check_sparity(y, Subject::Y)?;
let bounds_x = spread_bounds_with_rng(x, alpha, rng_x)?;
let bounds_y = spread_bounds_with_rng(y, alpha, rng_y)?;
let weight_x = n as f64 / (n + m) as f64;
let weight_y = m as f64 / (n + m) as f64;
Ok(Bounds {
lower: weight_x * bounds_x.lower + weight_y * bounds_y.lower,
upper: weight_x * bounds_x.upper + weight_y * bounds_y.upper,
})
}
pub fn disparity_bounds(x: &[f64], y: &[f64], misrate: f64) -> Result<Bounds, EstimatorError> {
let mut rng_x = crate::rng::Rng::new();
let mut rng_y = crate::rng::Rng::new();
disparity_bounds_with_rngs(x, y, misrate, &mut rng_x, &mut rng_y)
}
pub fn disparity_bounds_with_seed(
x: &[f64],
y: &[f64],
misrate: f64,
seed: &str,
) -> Result<Bounds, EstimatorError> {
let mut rng_x = crate::rng::Rng::from_string(seed);
let mut rng_y = crate::rng::Rng::from_string(seed);
disparity_bounds_with_rngs(x, y, misrate, &mut rng_x, &mut rng_y)
}
fn disparity_bounds_with_rngs(
x: &[f64],
y: &[f64],
misrate: f64,
rng_x: &mut crate::rng::Rng,
rng_y: &mut crate::rng::Rng,
) -> Result<Bounds, EstimatorError> {
check_validity(x, Subject::X)?;
check_validity(y, Subject::Y)?;
if misrate.is_nan() || !(0.0..=1.0).contains(&misrate) {
return Err(EstimatorError::from(AssumptionError::domain(
Subject::Misrate,
)));
}
let n = x.len();
let m = y.len();
if n < 2 {
return Err(EstimatorError::from(AssumptionError::domain(Subject::X)));
}
if m < 2 {
return Err(EstimatorError::from(AssumptionError::domain(Subject::Y)));
}
let min_shift = crate::min_misrate::min_achievable_misrate_two_sample(n, m)
.map_err(EstimatorError::from)?;
let min_x = crate::min_misrate::min_achievable_misrate_one_sample(n / 2)?;
let min_y = crate::min_misrate::min_achievable_misrate_one_sample(m / 2)?;
let min_avg = 2.0 * min_x.max(min_y);
if misrate < min_shift + min_avg {
return Err(EstimatorError::from(AssumptionError::domain(
Subject::Misrate,
)));
}
let extra = misrate - (min_shift + min_avg);
let alpha_shift = min_shift + extra / 2.0;
let alpha_avg = min_avg + extra / 2.0;
check_sparity(x, Subject::X)?;
check_sparity(y, Subject::Y)?;
let shift_bounds = shift_bounds(x, y, alpha_shift)?;
let avg_bounds = avg_spread_bounds_with_rngs(x, y, alpha_avg, rng_x, rng_y)?;
let la = avg_bounds.lower;
let ua = avg_bounds.upper;
let ls = shift_bounds.lower;
let us = shift_bounds.upper;
if la > 0.0 {
let r1 = ls / la;
let r2 = ls / ua;
let r3 = us / la;
let r4 = us / ua;
let lower = r1.min(r2).min(r3).min(r4);
let upper = r1.max(r2).max(r3).max(r4);
return Ok(Bounds { lower, upper });
}
if ua <= 0.0 {
if ls == 0.0 && us == 0.0 {
return Ok(Bounds {
lower: 0.0,
upper: 0.0,
});
}
if ls >= 0.0 {
return Ok(Bounds {
lower: 0.0,
upper: f64::INFINITY,
});
}
if us <= 0.0 {
return Ok(Bounds {
lower: f64::NEG_INFINITY,
upper: 0.0,
});
}
return Ok(Bounds {
lower: f64::NEG_INFINITY,
upper: f64::INFINITY,
});
}
if ls > 0.0 {
return Ok(Bounds {
lower: ls / ua,
upper: f64::INFINITY,
});
}
if us < 0.0 {
return Ok(Bounds {
lower: f64::NEG_INFINITY,
upper: us / ua,
});
}
if ls == 0.0 && us == 0.0 {
return Ok(Bounds {
lower: 0.0,
upper: 0.0,
});
}
if ls == 0.0 && us > 0.0 {
return Ok(Bounds {
lower: 0.0,
upper: f64::INFINITY,
});
}
if ls < 0.0 && us == 0.0 {
return Ok(Bounds {
lower: f64::NEG_INFINITY,
upper: 0.0,
});
}
Ok(Bounds {
lower: f64::NEG_INFINITY,
upper: f64::INFINITY,
})
}
fn spread_bounds_with_rng(
x: &[f64],
misrate: f64,
rng: &mut crate::rng::Rng,
) -> Result<Bounds, EstimatorError> {
check_validity(x, Subject::X)?;
if misrate.is_nan() || !(0.0..=1.0).contains(&misrate) {
return Err(EstimatorError::from(AssumptionError::domain(
Subject::Misrate,
)));
}
let n = x.len();
let m = n / 2;
let min_misrate = crate::min_misrate::min_achievable_misrate_one_sample(m)?;
if misrate < min_misrate {
return Err(EstimatorError::from(AssumptionError::domain(
Subject::Misrate,
)));
}
check_sparity(x, Subject::X)?;
let margin = crate::sign_margin::sign_margin_randomized(m, misrate, rng)
.map_err(EstimatorError::from)?;
let mut half_margin = margin / 2;
let max_half_margin = (m - 1) / 2;
if half_margin > max_half_margin {
half_margin = max_half_margin;
}
let k_left = half_margin + 1;
let k_right = m - half_margin;
let indices: Vec<usize> = (0..n).collect();
let shuffled = rng.shuffle(&indices);
let mut diffs = Vec::with_capacity(m);
for i in 0..m {
let a = shuffled[2 * i];
let b = shuffled[2 * i + 1];
diffs.push((x[a] - x[b]).abs());
}
diffs.sort_by(|a, b| a.total_cmp(b));
let lower = diffs[k_left - 1];
let upper = diffs[k_right - 1];
Ok(Bounds { lower, upper })
}