use crate::math::{self, all_finite, is_constant, mean};
use crate::special::chi_squared_survival;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TestResult {
pub statistic: f64,
pub p_value: f64,
}
#[must_use]
pub fn ljung_box(residuals: &[f64], lags: usize, model_df: usize) -> Option<TestResult> {
let n = residuals.len();
if lags == 0
|| lags >= n
|| model_df >= lags
|| !all_finite(residuals)
|| is_constant(residuals)
{
return None;
}
let mean = mean(residuals);
let c0: f64 = residuals
.iter()
.map(|x| {
let deviation = x - mean;
deviation * deviation
})
.sum();
if c0 == 0.0 {
return None;
}
let sum: f64 = (1..=lags)
.map(|k| {
let ck: f64 = (0..n - k)
.map(|t| (residuals[t] - mean) * (residuals[t + k] - mean))
.sum();
let r = ck / c0;
r * r / (n - k) as f64
})
.sum();
let nf = n as f64;
let statistic = nf * (nf + 2.0) * sum;
Some(TestResult {
statistic,
p_value: chi_squared_survival(statistic, lags - model_df),
})
}
#[must_use]
pub fn jarque_bera(residuals: &[f64]) -> Option<TestResult> {
let n = residuals.len();
if n < 2 || !all_finite(residuals) || is_constant(residuals) {
return None;
}
let mean = mean(residuals);
let (mut m2, mut m3, mut m4) = (0.0, 0.0, 0.0);
for x in residuals {
let deviation = x - mean;
let squared = deviation * deviation;
m2 += squared;
m3 += squared * deviation;
m4 += squared * squared;
}
let nf = n as f64;
let (m2, m3, m4) = (m2 / nf, m3 / nf, m4 / nf);
if m2 == 0.0 {
return None;
}
let skewness = m3 / (m2 * math::sqrt(m2));
let excess = m4 / (m2 * m2) - 3.0;
let statistic = n as f64 / 6.0 * (skewness * skewness + excess * excess / 4.0);
Some(TestResult {
statistic,
p_value: chi_squared_survival(statistic, 2),
})
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KpssNull {
Level,
Trend,
}
#[must_use]
pub fn kpss(x: &[f64], null: KpssNull, lags: usize) -> Option<TestResult> {
let n = x.len();
if n < 3 || lags >= n || !all_finite(x) || is_constant(x) {
return None;
}
let residual = kpss_residuals(x, null);
let (mut partial, mut eta, mut variance) = (0.0, 0.0, 0.0);
for t in 0..n {
let e = residual(t);
partial += e;
eta += partial * partial;
variance += e * e;
}
for i in 1..=lags {
let weight = 1.0 - i as f64 / (lags as f64 + 1.0);
let product: f64 = (i..n).map(|t| residual(t) * residual(t - i)).sum();
variance += 2.0 * weight * product;
}
let nf = n as f64;
let (eta, variance) = (eta / (nf * nf), variance / nf);
if variance <= 0.0 {
return None;
}
let statistic = eta / variance;
let critical = match null {
KpssNull::Level => LEVEL_CRITICAL,
KpssNull::Trend => TREND_CRITICAL,
};
Some(TestResult {
statistic,
p_value: interpolate_p_value(statistic, critical),
})
}
#[must_use]
pub fn kpss_lags(n: usize) -> usize {
let lags = 12.0 * math::sqrt(math::sqrt(n as f64 / 100.0));
let truncated = lags as usize;
let rounded = truncated + usize::from(lags > truncated as f64);
rounded.min(n.saturating_sub(1))
}
#[must_use]
pub fn kpss_short_lags(n: usize) -> usize {
let lags = (4.0 * math::sqrt(math::sqrt(n as f64 / 100.0))) as usize;
lags.min(n.saturating_sub(1))
}
#[cfg(feature = "alloc")]
#[must_use]
pub fn ndiffs(x: &[f64], alpha: f64, max_d: usize) -> usize {
let alpha = alpha.clamp(0.01, 0.10);
let mut series: alloc::vec::Vec<f64> = x.iter().copied().filter(|v| v.is_finite()).collect();
let critical = critical_value(alpha, LEVEL_CRITICAL);
for d in 0..max_d {
match kpss(&series, KpssNull::Level, ndiffs_lags(series.len())) {
Some(test) if test.statistic > critical => {
series = crate::difference(&series, 1);
}
_ => return d,
}
}
max_d
}
#[cfg(feature = "alloc")]
fn ndiffs_lags(n: usize) -> usize {
(3.0 * crate::math::sqrt(n as f64) / 13.0) as usize
}
fn kpss_residuals(x: &[f64], null: KpssNull) -> impl Fn(usize) -> f64 + '_ {
let n = x.len() as f64;
let level = mean(x);
let slope = match null {
KpssNull::Level => 0.0,
KpssNull::Trend => {
let t_mean = (n + 1.0) / 2.0;
let (sxy, sxx) = x.iter().enumerate().fold((0.0, 0.0), |(sxy, sxx), (t, v)| {
let dt = (t + 1) as f64 - t_mean;
(sxy + dt * (v - level), sxx + dt * dt)
});
sxy / sxx
}
};
let t_mean = (n + 1.0) / 2.0;
move |t| x[t] - level - slope * ((t + 1) as f64 - t_mean)
}
const P: [f64; 4] = [0.10, 0.05, 0.025, 0.01];
const LEVEL_CRITICAL: [f64; 4] = [0.347, 0.463, 0.574, 0.739];
const TREND_CRITICAL: [f64; 4] = [0.119, 0.146, 0.176, 0.216];
#[cfg(feature = "alloc")]
fn critical_value(alpha: f64, critical: [f64; 4]) -> f64 {
for i in 1..4 {
if alpha >= P[i] {
let fraction = (P[i - 1] - alpha) / (P[i - 1] - P[i]);
return critical[i - 1] + fraction * (critical[i] - critical[i - 1]);
}
}
critical[3]
}
fn interpolate_p_value(statistic: f64, critical: [f64; 4]) -> f64 {
if statistic <= critical[0] {
return P[0];
}
for i in 1..4 {
if statistic <= critical[i] {
let fraction = (statistic - critical[i - 1]) / (critical[i] - critical[i - 1]);
return P[i - 1] + fraction * (P[i] - P[i - 1]);
}
}
P[3]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rejects_invalid_input() {
let x = [1.0, 2.0, 0.5, 1.5];
assert!(ljung_box(&x, 0, 0).is_none());
assert!(ljung_box(&x, 4, 0).is_none());
assert!(ljung_box(&x, 2, 2).is_none());
assert!(ljung_box(&[1.0; 5], 2, 0).is_none());
assert!(jarque_bera(&[1.0]).is_none());
assert!(jarque_bera(&[f64::NAN, 1.0]).is_none());
assert!(kpss(&x, KpssNull::Level, 4).is_none());
}
#[test]
fn p_value_table_is_clipped_and_linear() {
let critical = [0.347, 0.463, 0.574, 0.739];
assert_eq!(interpolate_p_value(0.1, critical), 0.10);
assert_eq!(interpolate_p_value(2.0, critical), 0.01);
assert!((interpolate_p_value(0.463, critical) - 0.05).abs() < 1e-15);
assert!((interpolate_p_value(0.405, critical) - 0.075).abs() < 1e-12);
}
#[test]
#[cfg(feature = "alloc")]
fn critical_values_invert_the_table() {
assert!((critical_value(0.10, LEVEL_CRITICAL) - 0.347).abs() < 1e-15);
assert!((critical_value(0.05, LEVEL_CRITICAL) - 0.463).abs() < 1e-15);
assert!((critical_value(0.01, LEVEL_CRITICAL) - 0.739).abs() < 1e-15);
let midway = critical_value(0.075, LEVEL_CRITICAL);
assert!((interpolate_p_value(midway, LEVEL_CRITICAL) - 0.075).abs() < 1e-12);
}
#[test]
fn kpss_lags_follow_statsmodels() {
assert_eq!(kpss_lags(100), 12);
assert_eq!(kpss_lags(60), 11);
assert_eq!(kpss_lags(5), 4);
assert_eq!(kpss_lags(1000), 22);
}
#[test]
fn kpss_short_lags_follow_pmdarima() {
assert_eq!(kpss_short_lags(100), 4);
assert_eq!(kpss_short_lags(150), 4);
assert_eq!(kpss_short_lags(60), 3);
assert_eq!(kpss_short_lags(1000), 7);
assert_eq!(kpss_short_lags(2), 1);
}
#[cfg(feature = "alloc")]
fn walk(n: usize) -> alloc::vec::Vec<f64> {
let mut y = alloc::vec![0.0];
let mut state = 2_u32;
for _ in 1..n {
state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
y.push(y[y.len() - 1] + f64::from(state >> 8) / f64::from(1_u32 << 24) - 0.5);
}
y
}
#[cfg(feature = "alloc")]
fn cumulative(x: &[f64]) -> alloc::vec::Vec<f64> {
x.iter()
.scan(0.0, |sum, v| {
*sum += v;
Some(*sum)
})
.collect()
}
#[test]
#[cfg(feature = "alloc")]
fn ndiffs_counts_integration_orders() {
let y = walk(200);
assert_eq!(ndiffs(&crate::difference(&y, 1), 0.05, 2), 0);
assert_eq!(ndiffs(&y, 0.05, 2), 1);
assert_eq!(ndiffs(&cumulative(&y), 0.05, 2), 2);
let twice = cumulative(&cumulative(&y));
assert_eq!(ndiffs(&twice, 0.05, 2), 2);
assert_eq!(ndiffs(&twice, 0.05, 3), 3);
}
#[test]
fn ljung_box_handles_more_values_than_a_32_bit_product() {
let n = 70_000;
let x: std::vec::Vec<f64> = (0..n)
.map(|t| if t % 2 == 0 { 1.0 } else { -1.0 })
.collect();
let test = ljung_box(&x, 1, 0).unwrap();
let nf = f64::from(n);
let expected = (nf + 2.0) * (nf - 1.0) / nf;
assert!(
(test.statistic - expected).abs() < 1e-6 * expected,
"{test:?}"
);
}
#[test]
#[cfg(feature = "alloc")]
fn ndiffs_edges() {
let y = walk(200);
assert_eq!(ndiffs(&y, 0.01, 2), 1);
assert_eq!(ndiffs(&crate::difference(&y, 1), 0.5, 2), 0);
let mut gappy = y.clone();
gappy[50] = f64::NAN;
assert_eq!(ndiffs(&gappy, 0.05, 2), 1);
assert_eq!(ndiffs(&[1.0, 2.0], 0.05, 2), 0);
}
#[test]
fn constant_series_with_an_inexact_mean() {
let x = [0.1; 100];
assert_eq!(ljung_box(&x, 5, 0), None);
assert_eq!(jarque_bera(&x), None);
assert_eq!(kpss(&x, KpssNull::Level, 4), None);
#[cfg(feature = "alloc")]
assert_eq!(ndiffs(&x, 0.05, 2), 0);
}
#[test]
fn jarque_bera_needs_two_values() {
assert_eq!(jarque_bera(&[1.0]), None);
assert!(jarque_bera(&[1.0, 2.0]).is_some());
}
#[test]
fn kpss_needs_three_values() {
assert_eq!(kpss(&[1.0, 2.0], KpssNull::Level, 0), None);
assert!(kpss(&[1.0, 3.0, 2.0], KpssNull::Level, 0).is_some());
}
}