use backshift::{ArimaSpec, Filter, RefitOptions, Refitter, Seasonal};
use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::atomic::{AtomicUsize, Ordering};
struct Counting;
static ALLOCATIONS: AtomicUsize = AtomicUsize::new(0);
unsafe impl GlobalAlloc for Counting {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
ALLOCATIONS.fetch_add(1, Ordering::SeqCst);
unsafe { System.alloc(layout) }
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
unsafe { System.dealloc(ptr, layout) }
}
}
#[global_allocator]
static GLOBAL: Counting = Counting;
const MODEL: ArimaSpec<'static> = ArimaSpec {
ar: &[0.5],
d: 1,
ma: &[0.3],
seasonal: Seasonal::NONE,
exog: &[],
mean: 0.0,
sigma2: 1.0,
};
const WINDOW: usize = 48;
const OPTIONS: RefitOptions = RefitOptions {
mean: true,
max_iterations: 100,
};
static REFITTER: std::sync::Mutex<
Refitter<{ MODEL.refit_scratch_len(WINDOW, OPTIONS) }, { MODEL.coefficient_len() }>,
> = std::sync::Mutex::new(Refitter::new());
fn main() {
let history = [0.3, -0.4, 1.1, 0.2, -0.9, 0.5, 0.0, 0.7];
let mut mean = [0.0; 6];
let mut variance = [0.0; 6];
let before = ALLOCATIONS.load(Ordering::SeqCst);
let spec = ArimaSpec {
ar: &[0.5, -0.2],
d: 1,
ma: &[0.4],
mean: 0.1,
sigma2: 1.3,
..ArimaSpec::default()
};
let mut filter = Filter::<4>::new(&spec).expect("stationary model");
filter.warm_up(&history);
for y in [0.1, -0.2, f64::NAN, 0.4] {
std::hint::black_box(filter.update(y));
}
filter.forecast_into(&mut mean, &mut variance);
let (mut lower, mut upper) = ([0.0; 6], [0.0; 6]);
filter.forecast_interval_into(0.95, &mut mean, &mut lower, &mut upper);
let exog_spec = ArimaSpec {
ar: &[0.6],
exog: &[1.5, -0.5],
..ArimaSpec::default()
};
let mut exog_filter = Filter::<4>::new(&exog_spec).expect("stationary model");
exog_filter.warm_up_exog(&history[..2], &[0.1, 0.2, 0.3, 0.4]);
std::hint::black_box(exog_filter.update_exog(0.5, &[1.0, -1.0]));
let (mut exog_mean, mut exog_variance) = ([0.0; 2], [0.0; 2]);
exog_filter.forecast_exog_into(&[0.0, 1.0, 1.0, 0.0], &mut exog_mean, &mut exog_variance);
std::hint::black_box((&exog_mean, &exog_variance));
std::hint::black_box((
backshift::ljung_box(&history, 3, 1),
backshift::jarque_bera(&history),
backshift::kpss(&history, backshift::KpssNull::Level, 2),
));
let window: [f64; WINDOW] = core::array::from_fn(|t| {
let wobble = f64::from(u32::try_from((t * 7) % 5).expect("small"));
0.2 * f64::from(u32::try_from(t).expect("small")) + wobble / 4.0
});
let mut refitter = REFITTER.lock().expect("not poisoned");
let refit = refitter
.refit(&MODEL, &window, OPTIONS)
.expect("the window fits");
let mut refitted = Filter::<4>::new(&refit.spec).expect("stationary model");
refitted.warm_up(&window);
std::hint::black_box(refitted.update(10.0));
let after = ALLOCATIONS.load(Ordering::SeqCst);
std::hint::black_box((&mean, &variance, &lower, &upper));
assert_eq!(
after - before,
0,
"the device path allocated {} times",
after - before
);
assert!(variance.iter().all(|v| v.is_finite() && *v > 0.0));
println!("no_alloc: ok, 0 allocations");
}