use backshift::{ArimaSpec, Filter, RefitOptions, Refitter, Seasonal, ljung_box};
const DEPLOYED: ArimaSpec<'static> = ArimaSpec {
ar: &[0.2],
d: 0,
ma: &[],
seasonal: Seasonal::NONE,
exog: &[],
mean: 0.0,
sigma2: 1.0,
};
const WINDOW: usize = 100;
const OPTIONS: RefitOptions = RefitOptions {
mean: false,
max_iterations: 100,
};
const LAGS: usize = 10;
const ALPHA: f64 = 0.001;
fn main() {
let series = simulate(1000);
let mut refitter = Box::new(Refitter::<
{ DEPLOYED.refit_scratch_len(WINDOW, OPTIONS) },
{ DEPLOYED.coefficient_len() },
>::new());
let mut ar = [DEPLOYED.ar[0]];
let mut sigma2 = DEPLOYED.sigma2;
let mut filter = Filter::<2>::new(&DEPLOYED).expect("the deployed model is stationary");
let (mut window, mut standardised) = ([0.0; WINDOW], [0.0; WINDOW]);
for (t, &y) in series.iter().enumerate() {
let innovation = filter.update(y).expect("no differencing to prime");
window[t % WINDOW] = y;
standardised[t % WINDOW] = innovation.value / innovation.variance.sqrt();
if t % WINDOW != WINDOW - 1 {
continue;
}
let Some(check) = ljung_box(&standardised, LAGS, filter_parameters(&DEPLOYED)) else {
continue;
};
if check.p_value >= ALPHA {
println!("t = {t:4}: innovations white (p = {:.3})", check.p_value);
continue;
}
let current = ArimaSpec {
ar: &ar,
sigma2,
..DEPLOYED
};
match refitter.refit(¤t, &window, OPTIONS) {
Ok(refit) => {
ar.copy_from_slice(refit.spec.ar);
sigma2 = refit.spec.sigma2;
let mut refitted = Filter::<2>::new(&refit.spec).expect("refits are stationary");
refitted.warm_up(&window);
filter = refitted;
println!(
"t = {t:4}: drift (p = {:.1e}), refitted ar = {:.3} in {} iterations",
check.p_value, ar[0], refit.iterations
);
}
Err(error) => println!("t = {t:4}: drift, refit failed: {error}"),
}
}
}
fn filter_parameters(spec: &ArimaSpec<'_>) -> usize {
spec.ar.len() + spec.ma.len() + spec.seasonal.ar.len() + spec.seasonal.ma.len()
}
fn simulate(n: usize) -> Vec<f64> {
let mut state = 11_u64;
let mut uniform = move || {
state = state
.wrapping_mul(6_364_136_223_846_793_005)
.wrapping_add(1);
(state >> 11) as f64 / (1_u64 << 53) as f64
};
let mut normal = move || {
let (u, v) = (uniform().max(f64::MIN_POSITIVE), uniform());
(-2.0 * u.ln()).sqrt() * (core::f64::consts::TAU * v).cos()
};
let mut y = Vec::with_capacity(n);
let mut previous = 0.0;
for t in 0..n {
let phi = if t < n / 2 { 0.2 } else { 0.85 };
previous = phi * previous + normal();
y.push(previous);
}
y
}