pub fn chi2_contingency_2x2(a: f64, b: f64, c: f64, d: f64) -> f64 {
let n = a + b + c + d;
if n == 0.0 {
return 0.0;
}
let numerator = n * (a * d - b * c).powi(2);
let denominator = (a + b) * (c + d) * (a + c) * (b + d);
if denominator == 0.0 {
0.0
} else {
numerator / denominator
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_chi2_contingency() {
let stat = chi2_contingency_2x2(10.0, 5.0, 10.0, 20.0);
assert!((stat - 4.5).abs() < 1e-7);
}
}