use crate::math;
use alloc::vec::Vec;
#[must_use]
pub fn box_cox(values: &[f64], lambda: f64) -> Option<Vec<f64>> {
if !lambda.is_finite() || !all_positive(values) {
return None;
}
Some(
values
.iter()
.map(|&y| {
let log = math::ln(y);
if lambda == 0.0 {
log
} else {
math::exp_m1(lambda * log) / lambda
}
})
.collect(),
)
}
#[must_use]
pub fn box_cox_lambda(values: &[f64]) -> Option<f64> {
let first = *values.first()?;
if values.len() < 2 || !all_positive(values) || values.iter().all(|&y| y == first) {
return None;
}
let logs: Vec<f64> = values.iter().map(|&y| math::ln(y)).collect();
let log_sum: f64 = logs.iter().sum();
let likelihood = |lambda: f64| log_likelihood(&logs, log_sum, lambda);
Some(golden_section_maximum(likelihood, -10.0, 10.0))
}
fn log_likelihood(logs: &[f64], log_sum: f64, lambda: f64) -> f64 {
let n = logs.len() as f64;
let transformed = |log: f64| {
if lambda == 0.0 {
log
} else {
math::exp(lambda * log) / lambda
}
};
let mean = logs.iter().map(|&l| transformed(l)).sum::<f64>() / n;
let variance = logs
.iter()
.map(|&l| {
let deviation = transformed(l) - mean;
deviation * deviation
})
.sum::<f64>()
/ n;
(lambda - 1.0) * log_sum - n / 2.0 * math::ln(variance)
}
fn golden_section_maximum(f: impl Fn(f64) -> f64, mut low: f64, mut high: f64) -> f64 {
const INVERSE_PHI: f64 = 0.618_033_988_749_894_9;
let mut left = high - INVERSE_PHI * (high - low);
let mut right = low + INVERSE_PHI * (high - low);
let (mut f_left, mut f_right) = (f(left), f(right));
for _ in 0..100 {
if f_left >= f_right {
high = right;
(right, f_right) = (left, f_left);
left = high - INVERSE_PHI * (high - low);
f_left = f(left);
} else {
low = left;
(left, f_left) = (right, f_right);
right = low + INVERSE_PHI * (high - low);
f_right = f(right);
}
if high - low < 1e-12 {
break;
}
}
f64::midpoint(low, high)
}
fn all_positive(values: &[f64]) -> bool {
values.iter().all(|&y| y.is_finite() && y > 0.0)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rejects_values_it_cannot_transform() {
assert!(box_cox(&[1.0, 0.0], 0.5).is_none());
assert!(box_cox(&[1.0, -2.0], 0.0).is_none());
assert!(box_cox(&[1.0, 2.0], f64::NAN).is_none());
assert!(box_cox_lambda(&[3.0, 3.0, 3.0]).is_none());
assert!(box_cox_lambda(&[3.0]).is_none());
assert!(box_cox_lambda(&[]).is_none());
}
#[test]
fn lambda_zero_is_the_logarithm() {
let y = box_cox(&[1.0, core::f64::consts::E], 0.0).unwrap();
assert_eq!(y[0], 0.0);
assert!((y[1] - 1.0).abs() < 1e-15);
}
}