backshift 0.3.0

Time-series forecasting checked against R and statsmodels: ARIMA, ETS, Theta, VAR, Croston/TSB, GARCH and changepoint detection, with streaming filters that run without a heap
Documentation
//! Checks the MSTL decomposition against statsmodels.

use crate::mstl_data::ALL;
use backshift::{AutoArima, FitError, box_cox, box_cox_lambda, mstl};

/// Absolute tolerance against statsmodels, as for STL.
const TOLERANCE: f64 = 1e-9;

/// Asserts two equally long slices agree within [`TOLERANCE`].
fn assert_close(what: &str, actual: &[f64], expected: &[f64]) {
    assert_eq!(actual.len(), expected.len(), "{what} length");
    for (t, (a, e)) in actual.iter().zip(expected).enumerate() {
        assert!((a - e).abs() <= TOLERANCE, "{what}[{t}]: {a} vs {e}");
    }
}

#[test]
fn matches_statsmodels() {
    for case in ALL {
        let values = if case.lambda.is_nan() {
            case.values.to_vec()
        } else {
            if case.auto_lambda {
                // scipy's Brent stops at a relative tolerance of about 1.5e-8.
                let lambda = box_cox_lambda(case.values).unwrap();
                assert!(
                    (lambda - case.lambda).abs() < 1e-6,
                    "{lambda} vs {}",
                    case.lambda
                );
            }

            box_cox(case.values, case.lambda).unwrap()
        };
        let parts = mstl(&values, case.periods, case.iterate).unwrap();
        assert_eq!(parts.seasonal.len(), case.kept);
        let n = case.values.len();
        for (i, component) in parts.seasonal.iter().enumerate() {
            let what = format!("periods {:?} seasonal {i}", case.periods);
            assert_close(&what, component, &case.seasonal[i * n..(i + 1) * n]);
        }

        assert_close("trend", &parts.trend, case.trend);
        assert_close("remainder", &parts.remainder, case.remainder);
    }
}

#[test]
fn rejects_what_statsmodels_cannot_decompose() {
    let y = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
    assert!(mstl(&y, &[4], 2).is_none(), "4 is half the length");
    assert!(mstl(&y, &[1], 2).is_none());
    assert!(mstl(&y, &[2], 0).is_none());
    let mut gap = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0];
    gap[4] = f64::INFINITY;
    assert!(mstl(&gap, &[2], 2).is_none());
}

/// A series with periods 7 and 30 on AR(1) noise: `n` values plus `horizon` held out.
fn two_seasons(n: usize) -> Vec<f64> {
    let weekly = [2.0, 1.0, 0.0, -0.5, -1.0, -2.0, 0.5];
    let mut state = 3_u32;
    let mut ar = 0.0;
    (0..n)
        .map(|t| {
            state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
            ar = 0.5 * ar + f64::from(state >> 8) / f64::from(1_u32 << 24) - 0.5;
            let monthly = 3.0 * (std::f64::consts::TAU * (t % 30) as f64 / 30.0).sin();
            10.0 + weekly[t % 7] + monthly + ar
        })
        .collect()
}

#[test]
fn mstl_forecast_tracks_both_seasons() {
    // 367 is a multiple of neither period, so the repeated cycles must keep their phase.
    let (n, horizon) = (367, 30);
    let y = two_seasons(n + horizon);
    let model = AutoArima::new()
        .max_orders(2, 2, 0, 0)
        .fit_mstl(&y[..n], &[30, 7])
        .unwrap();
    let forecast = model.forecast::<8>(0.95, horizon).unwrap();

    let error: f64 = forecast
        .mean
        .iter()
        .zip(&y[n..])
        .map(|(f, a)| (f - a).abs())
        .sum::<f64>()
        / horizon as f64;
    // The noise has a standard deviation of about 0.33; the seasons swing by +-5.
    assert!(error < 0.6, "mean absolute error {error}");

    let covered = (0..horizon)
        .filter(|&h| forecast.lower[h] <= y[n + h] && y[n + h] <= forecast.upper[h])
        .count();
    assert!(
        covered >= 25,
        "{covered} of {horizon} inside the 95% interval"
    );
}

#[test]
fn mstl_fit_rejects_missing_values_and_small_filters() {
    let mut y = two_seasons(120);
    let model = AutoArima::new().max_orders(1, 1, 0, 0);
    assert!(
        model
            .fit_mstl(&y, &[7])
            .unwrap()
            .forecast::<0>(0.9, 3)
            .is_err()
    );
    y[10] = f64::NAN;
    assert_eq!(model.fit_mstl(&y, &[7]), Err(FitError::Decomposition));
}

#[test]
fn stream_starts_where_the_batch_forecast_does_and_keeps_tracking() {
    let (n, horizon) = (367, 30);
    let y = two_seasons(n + 2 * horizon);
    let model = AutoArima::new()
        .max_orders(2, 2, 0, 0)
        .fit_mstl(&y[..n], &[30, 7])
        .unwrap();
    let batch = model.forecast::<8>(0.95, horizon).unwrap();
    let mut stream = model.stream::<8, 37>().unwrap();
    let (mut mean, mut lower, mut upper) = ([0.0; 30], [0.0; 30], [0.0; 30]);
    stream.forecast_interval_into(0.95, &mut mean, &mut lower, &mut upper);
    assert_eq!(mean.as_slice(), batch.mean);
    assert_eq!(lower.as_slice(), batch.lower);

    // Stream the next 30 values, then forecast the 30 after them.
    for &value in &y[n..n + horizon] {
        stream.update(value);
    }
    stream.forecast_interval_into(0.95, &mut mean, &mut lower, &mut upper);
    let error: f64 = mean
        .iter()
        .zip(&y[n + horizon..])
        .map(|(f, a)| (f - a).abs())
        .sum::<f64>()
        / horizon as f64;
    assert!(error < 0.6, "mean absolute error {error}");
}

#[test]
fn stream_rejects_small_capacities() {
    let y = two_seasons(200);
    let model = AutoArima::new()
        .max_orders(1, 1, 0, 0)
        .fit_mstl(&y, &[30, 7])
        .unwrap();
    assert!(matches!(
        model.stream::<8, 36>(),
        Err(backshift::MstlFilterError::Capacity {
            needed: 37,
            capacity: 36
        })
    ));
}