use backshift::{Arima, Filter};
const DAYS: usize = 730;
const HORIZON: usize = 28;
const UPCOMING_HOLIDAY: usize = 10;
const HOLIDAY_EFFECT: f64 = -250.0;
const OUTAGE_EFFECT: f64 = -400.0;
const HOLIDAYS: [usize; 20] = [
0, 88, 91, 120, 129, 139, 275, 358, 359, 364, 365, 453, 456, 485, 494, 504, 640, 723, 724, 729, ];
const OUTAGES: [usize; 4] = [212, 401, 602, 603];
const COLUMNS: usize = 2;
fn main() {
let regressors = history_regressors();
let users = simulate(®ressors);
let model = Arima::new(1, 0, 0).seasonal(0, 1, 1, 7);
let with = model
.fit_exog(&users, ®ressors, COLUMNS)
.expect("the series is long enough");
let without = model.fit(&users).expect("the series is long enough");
println!("SARIMA(1,0,0)(0,1,1,7) on {DAYS} days of active users\n");
println!(" with regressors without");
println!(
"AICc {:>15.1} {:>7.1}",
with.aicc(),
without.aicc()
);
println!(
"sigma {:>15.1} {:>7.1}",
with.sigma2.sqrt(),
without.sigma2.sqrt()
);
println!(
"holiday effect {:>15.1} (true {HOLIDAY_EFFECT})",
with.exog[0]
);
println!(
"outage effect {:>15.1} (true {OUTAGE_EFFECT})\n",
with.exog[1]
);
let spec = with.spec();
assert_eq!((spec.states(), spec.exog.len()), (15, 2));
let mut filter = Filter::<15, 2>::new(&spec).expect("the fitted model is stationary");
filter.warm_up_exog(&users, ®ressors);
let mut plain = Filter::<15>::new(&without.spec()).expect("the fitted model is stationary");
plain.warm_up(&users);
let future = future_regressors();
let (mut mean, mut lower, mut upper) = ([0.0; HORIZON], [0.0; HORIZON], [0.0; HORIZON]);
filter.forecast_interval_exog_into(0.95, &future, &mut mean, &mut lower, &mut upper);
let (mut plain_mean, mut plain_lower, mut plain_upper) =
([0.0; HORIZON], [0.0; HORIZON], [0.0; HORIZON]);
plain.forecast_interval_into(0.95, &mut plain_mean, &mut plain_lower, &mut plain_upper);
println!("day with regressors (95%) without (95%)");
for h in UPCOMING_HOLIDAY - 3..=UPCOMING_HOLIDAY + 3 {
let marker = if h == UPCOMING_HOLIDAY {
" <- bank holiday"
} else {
""
};
println!(
"{:>3} {:>6.0} [{:>6.0}, {:>6.0}] {:>6.0} [{:>6.0}, {:>6.0}]{marker}",
h + 1,
mean[h],
lower[h],
upper[h],
plain_mean[h],
plain_lower[h],
plain_upper[h],
);
}
assert!(
(with.exog[0] - HOLIDAY_EFFECT).abs() < 50.0,
"holiday effect recovered"
);
assert!(
(with.exog[1] - OUTAGE_EFFECT).abs() < 50.0,
"outage effect recovered"
);
assert!(
with.aicc() < without.aicc(),
"the regressors pay for themselves"
);
}
fn history_regressors() -> Vec<f64> {
let mut rows = vec![0.0; DAYS * COLUMNS];
for day in HOLIDAYS {
rows[day * COLUMNS] = 1.0;
}
for day in OUTAGES {
rows[day * COLUMNS + 1] = 1.0;
}
rows
}
fn future_regressors() -> [f64; HORIZON * COLUMNS] {
let mut rows = [0.0; HORIZON * COLUMNS];
rows[UPCOMING_HOLIDAY * COLUMNS] = 1.0;
rows
}
fn simulate(regressors: &[f64]) -> Vec<f64> {
let mut state = 7_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 week = [1000.0, 1040.0, 1050.0, 1030.0, 980.0, 700.0, 650.0];
let (phi, theta, sigma) = (0.5, -0.6, 15.0);
let mut u = Vec::with_capacity(DAYS);
let mut e = Vec::with_capacity(DAYS);
let mut z_previous = 0.0;
for t in 0..DAYS {
let shock = sigma * normal();
e.push(shock);
if t < 7 {
u.push(week[t] + shock);
continue;
}
let z = phi * z_previous + shock + theta * e[t - 7];
z_previous = z;
u.push(u[t - 7] + z);
}
u.iter()
.enumerate()
.map(|(t, u)| {
let row = ®ressors[t * COLUMNS..(t + 1) * COLUMNS];
u + row[0] * HOLIDAY_EFFECT + row[1] * OUTAGE_EFFECT
})
.collect()
}