use crate::util::median;
use robust_rs_core::error::RobustError;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct HodgesLehmannFit {
pub estimate: f64,
}
impl HodgesLehmannFit {
pub fn estimate(&self) -> f64 {
self.estimate
}
pub fn gaussian_efficiency(&self) -> f64 {
3.0 / std::f64::consts::PI
}
pub fn breakdown_point(&self) -> f64 {
1.0 - std::f64::consts::FRAC_1_SQRT_2
}
}
pub fn hodges_lehmann(data: &[f64]) -> Result<HodgesLehmannFit, RobustError> {
let n = data.len();
if n == 0 {
return Err(RobustError::InsufficientData { needed: 1, got: 0 });
}
let mut walsh = Vec::with_capacity(n * (n + 1) / 2);
for i in 0..n {
for &xj in &data[i..] {
walsh.push(0.5 * (data[i] + xj));
}
}
Ok(HodgesLehmannFit {
estimate: median(&mut walsh),
})
}