backshift
Time-series forecasting for Rust, checked against R and statsmodels, with streaming filters that run on targets without a
heap. Seasonal ARIMA by exact maximum likelihood and auto-ARIMA, exponential smoothing (ETS), Theta, VAR, Croston and
TSB for intermittent demand, GARCH volatility and changepoint detection, plus backtesting to choose between them.
A model is fitted wherever there is an allocator (a laptop, a server, statsmodels if you prefer), and its parameters are
all a microcontroller needs to filter new values and forecast them with prediction intervals. The name is the backshift
operator B that ARIMA models are written in: (1 - B)^d y_t is the series differenced d times.
Try it in the browser: fit, forecast, stream and refit, compiled to WebAssembly.
use ;
let history = ;
let fitted = new.fit.unwrap;
println!;
// The device side needs only the coefficients.
let mut filter = new.unwrap;
filter.warm_up;
let innovation = filter.update.unwrap;
let = ;
filter.forecast_interval_into;
Who is this for
The crate is for forecasting where statsmodels and R won't go: on a microcontroller watching a sensor, in WebAssembly,
or in a Rust service that would rather not link BLAS. The streaming filters are the part that sets it apart. For ARIMA
each new value costs one Kalman step in O(r²) for r = max(p, q + 1) states, the innovation it returns doubles as an
anomaly score (its variance comes with it), and the whole state lives inline in a Copy struct. The other families
(ETS, Theta, VAR, Croston and TSB, GARCH) work the same way, with O(1) or O(K²) work per value, and the changepoint
detectors (CUSUM, BOCPD) watch the innovations any of them produce.
For model selection it has the Hyndman–Khandakar stepwise search of R's auto.arima. Its choices match pmdarima's on
the test series once the differencing orders are fixed. The differencing tests themselves differ on purpose: D comes
from STL seasonal strength (R's default) rather than pmdarima's OCSB test, which misses the seasonal unit root of a
simulated airline series that the crate finds. In Rust, foresight also offers
a stepwise auto-ARIMA, but does not build for bare-metal targets.
What's in it
Arima::new(p, d, q).seasonal(P, D, Q, s).fit(y): exact Gaussian maximum likelihood withsigma2concentrated out, Hannan–Rissanen starting values, refined by a conditional-sum-of-squares warm start (R'sCSS-ML), and an in-crate BFGS; AR and MA parts stay stationary and invertible through statsmodels' PACF transforms.Fittedreports the log-likelihood, AIC, AICc and BIC.fit_fromstarts the optimiser from a previous fit or a related model instead (R'sarima(init=)), andfit_fixedkeeps whole parts at given values (R'sarima(fixed=), statsmodels'fix_params)..method(Method::Css)stops at the conditional-sum-of-squares estimates (R'smethod="CSS"): 8× faster on the airline model, 0.003 below its maximum log-likelihood, more on short strongly seasonal series.fit_pmm2refines a fit by PMM2 for skewed innovations; on simulated Gamma(2,1) innovations its AR estimates had 1.4–2× less mean squared error than maximum likelihood, and the same with Gaussian ones.Filter<R, M>: built from anArimaSpecalone, it expands the seasonal polynomials into its own arrays, differences raw values in place, returns one-step innovations, treatsNaNas missing, and forecasts original-scale means, variances and intervals.Ris the state capacity,ArimaSpec::states(), i.e.max(p + P s, q + Q s + 1) + d + D s; the airline model ARIMA(0, 1, 1)(0, 1, 1, 12) needsR = 27.M(default 0) holds the regression coefficients and does not grow the covariance.with_steady_state()opts into statsmodels' default steady-state shortcut: once the covariance converges, each update costsO(R)instead ofO(R²), and the results match statsmodels with its default tolerance.Conformal<H, N>: heap-free conformal prediction intervals next to the filter's Gaussian ones. A rolling buffer of the lastNabsoluteh-step errors per horizon gives the half-width, so the intervals hold their coverage under skewed or heavy-tailed noise without a change to the model (on one series this is empirical, not guaranteed).OnlineAr<M>: an adaptive AR(M) predictor that learns its coefficients one value at a time (ARMA-ONS, Anava et al. 2013), heap-free, for series whose dynamics drift or switch. It trades the filter's exact likelihood, variances and statsmodels parity for adaptation without a refit.- Regression with ARIMA errors:
fit_exog,update_exog,forecast_exog_intoand friends modely_t - x_t . betawith the regressors differenced along withy, as R'sarima(xreg=)does.examples/holidays_outages.rsfits daily users with bank-holiday and outage dummies, and forecasts through a known upcoming holiday. - Prophet-style models as a recipe:
trend_row(heap-free) andtrend_regressorsbuild the hinge columns of a piecewise-linear trend, whose coefficients are the slope changes at each changepoint. With Fourier terms and holiday dummies they make Prophet's model, with ARIMA errors instead of white noise.examples/prophet_recipe.rsrecovers two slope changes from a grid of seven changepoints, and forecasts with the trend at its last slope. - Fourier regressors:
fourier_row(heap-free, one row per new value on a device) andfourier(several periods at once) give sine/cosine pairs in the column order of R'sforecast::fourier(). Withfit_exogandupdate_exogthey model long or non-integer seasons (365.25, 8766) as dynamic harmonic regression, which a seasonal ARIMA cannot hold in memory.AutoArima::fit_fourierpicks the number of harmonics per period by the search criterion (AICc by default), as FPP3 recommends. loglikeandinnovations(and their_exogforms) for any model size, through the same recursion as the filter.AutoArima: the stepwise search (or an exhaustive one, parallel with therayonfeature) withndiffsandnsdiffschoosingdandD; AICc by default, AIC or BIC on request..approximation(true)ranks the candidates by CSS fits and refits only the winner exactly (R'sapproximation=TRUE): 15× faster on the airline series, with the same choice there and on 7 of the 8 pmdarima series (the eighth is a tie within 0.01 AICc).Ets,AutoEtsandEtsFilter<M>: exponential smoothing in R'sforecast::etsform, all 15 models of R's default set (additive or multiplicative error; no, additive or damped trend; no, additive or multiplicative season). With additive components the fit concentrates the initial states out by least squares and searches only the smoothing parameters; with multiplicative ones it optimises them jointly from R's starting states. It reaches the same or a higher likelihood than R on every model of its conformance suite. The heap-freeEtsFiltercontinues from a fit's final states atO(1)per value and forecasts with R's analytic intervals (classes 1 to 3, the last in caller scratch), corrected where R's are off (ETS(A,N,A), ETS(M,N,A), ETS(M,Ad,M)).ThetaandThetaFilter<M>: the Theta method as R'sthetaf(simple exponential smoothing with drift, Hyndman and Billah 2003), with its seasonality test and multiplicative deseasonalising; the heap-free filter keeps the level learning and forecasts with R's intervals.CrostonFilterandTsbFilter: intermittent demand, series that are zero most of the time. Croston's method as R'scroston(every fitted value to 1e-12), with the SBA and SBJ bias corrections, and the method of Teunter, Syntetos and Babai, whose forecast decays through long gaps. Both are heap-free withO(1)work per value, includingoptimised, which picks the smoothing parameters that minimise the in-sample error.GarchandGarchFilter: GARCH(1,1) conditional variance for returns, or for the innovations of any other model here. The fit maximisestseries::garch's likelihood and reaches its optimum where tseries' own optimiser stops short; the heap-free filter tracks the variance atO(1)per value, treats a missing value as its expectation, and forecasts the variance's decay to its long-run level.CusumandBocpd<R>: streaming changepoint detection on standardised innovations, heap-free.Cusumis Page's two-sided cumulative sum, the classic alarm for a mean shift, tuned by its average run length.Bocpdis Bayesian online changepoint detection (Adams and MacKay) with a Normal-Gamma model, so it catches shifts in the mean or the scale and reports how long the current segment has lasted; it keepsRrun-length hypotheses and is exact while the series is shorter than that.accuracy,cross_validation_errorsandselect_family: R'saccuracymeasures (ME, RMSE, MAE, MPE, MAPE, MASE) andtsCVrolling-origin errors for any forecasting closure, both to 1e-12.select_familycompares automatic ARIMA, automatic ETS, Theta, naive and seasonal naive by their mean MASE on the last few forecast origins, since information criteria do not compare across families, then refits the winner and forecasts.Var,select_var_orderandOnlineVar<K, D>: vector autoregression on several series at once, with exogenous regressors (VARX) and lag selection by AIC, BIC, HQIC or FPE, matching statsmodels'VARto 1e-8. The heap-freeOnlineVarstreams a row ofKvalues per step and scores it by the squared Mahalanobis distance of its errors, which flags values that are only unusual together. With learning on, recursive least squares keeps its coefficients equal to least squares on all values so far, or, with a forgetting factor, follows dynamics that drift.stl: STL decomposition with statsmodels' defaults, and the seasonal strength it implies.mstl: MSTL for several seasonal periods (daily and weekly on hourly data), andstl_withfor a chosen seasonal window; both match statsmodels to 1e-9.box_coxandbox_cox_lambda(scipy's maximum-likelihood lambda) transform the series first, as statsmodels'MSTL(lmbda=...)does.AutoArima::fit_mstlforecasts with MSTL plus ARIMA (R'sstlf): ARIMA on the seasonally adjusted series, each seasonal component's last cycle repeated; the intervals ignore the seasonal components' uncertainty.MstlArima::streamturns the fit into a heap-freeMstlFilter<R, L>for devices: a filter on the adjusted series plus the last cycle of each period.find_period: ranked seasonal period candidates by coreforecast's rule (the autocovariance of the first differences, confirmed by STL seasonal strength above 0.64), to feedAutoArima::seasonal,stlorfourier. A heuristic.- Diagnostics:
ljung_box,jarque_beraandkpss(heap-free, so a device can check its own residuals), andndiffsto choosed. - The building blocks:
acov,acorr,pacf,difference,integrate,yule_walker,burg,hannan_rissanenandnormal_quantile.
Running without a heap
The crate is no_std. With default-features = false pick one of the floating-point backends:
| Features | For |
|---|---|
std (default) |
Everything, using the standard library's math functions. Implies alloc. |
libm |
Bare-metal targets: every streaming filter (Filter, EtsFilter, ThetaFilter, OnlineVar, CrostonFilter and TsbFilter with their optimised fits, GarchFilter, MstlFilter, OnlineAr, Conformal), the changepoint detectors, ArimaSpec::refit, accuracy, the diagnostic tests, fourier_row, trend_row, acov/acorr and normal_quantile. No allocator needed. |
libm, alloc |
Bare-metal targets with an allocator: adds the fits and searches of every family, backtesting, pacf, differencing, STL and the estimators. |
rayon |
Fits the candidates of an exhaustive AutoArima search in parallel. Implies std. |
[]
= { = "0.3", = false, = ["libm"] }
CI links a no_std/no_main binary for thumbv7em-none-eabihf that runs an ARIMA(1,1,1) filter, forecasts with
intervals, refits the model, and streams through every other filter and detector, and a counting allocator asserts
that the same device paths allocate nothing. A firmware pays only for the families it calls: the linker drops the
rest, so the crate needs no feature per family. The .text each one adds on its own, with the model read at run time
(as from flash), so the compiler cannot fold its branches away:
| Linked | opt-level = 3 |
opt-level = "s" |
|---|---|---|
ARIMA Filter, with forecasts and intervals |
31 KB | 23 KB |
ARIMA Filter and ArimaSpec::refit |
43 KB | 28 KB |
EtsFilter, with forecasts and intervals |
24 KB | 16 KB |
ThetaFilter |
11 KB | 10 KB |
OnlineVar |
14 KB | 13 KB |
CrostonFilter and TsbFilter |
4 KB | 5 KB |
the same, with TsbFilter::optimised |
23 KB | 13 KB |
GarchFilter |
3 KB | 3 KB |
Cusum and Bocpd |
15 KB | 13 KB |
| all of the above | 111 KB | 61 KB |
The families share the libm functions, so together they take less than the sum of their rows. The ARIMA
filter's state lives inline:
Capacity (M = 0 unless given) |
size_of::<Filter<R, M>>() |
Fits, for example |
|---|---|---|
R = 4 |
384 bytes | ARIMA(1,1,1), ARIMA(2,1,2) |
R = 8 |
960 bytes | ARIMA(2,0,1)(1,0,0,4) |
R = 16 |
2,880 bytes | ARIMA(1,0,0)(1,0,0,12) |
R = 27 |
7,192 bytes | the airline model ARIMA(0,1,1)(0,1,1,12) |
R = 3, M = 50 |
680 bytes | an ARMA(2,2) error with 50 regressors, such as Fourier terms |
Filter::new needs about three times that on the stack while it solves for the starting covariance, and the
forecasts about twice.
Refitting on the device
ArimaSpec::refit re-estimates a model by exact maximum likelihood on a window of recent values, without a heap. It
starts from the model's current coefficients and runs the same objective and optimiser as Arima::fit, so a device
can notice that its innovations are no longer white (ljung_box on them) and adapt in place. The caller owns all
memory: refit_scratch_len and coefficient_len are const fns, so Refitter can hold both buffers in a static.
use ;
const MODEL: = ArimaSpec ;
const WINDOW: usize = 64;
const OPTIONS: RefitOptions = RefitOptions ;
static REFITTER: Mutexrefit_scratch_len }, >,
> = new;
let window: = from_fn;
let mut refitter = REFITTER.lock.unwrap;
let refit = refitter.refit.unwrap;
let mut filter = new.unwrap;
filter.warm_up; // continue from the end of the window
A failed refit, such as FitError::Degenerate from a stuck sensor, leaves the running model untouched. The scratch is
about 4 r² + (exog.len() + 2) n values for r ARMA states and n differenced values: 2.2 KB for ARIMA(1,1,1) with
drift over 100 values, 9.3 KB for the airline model over 144. examples/device_refit.rs
runs the whole loop: stream, check every window, refit on drift, swap:
In JavaScript
The @sunsided/backshift npm package brings ARIMA fitting,
forecasts, the streaming filter and refits to the browser and Node through WebAssembly, built from the same code with the libm
backend. The demo site runs on it; bindings/wasm has the
API.
Checked against statsmodels and R
Every result is compared with statsmodels 0.14 on reference series generated by
scripts/generate_reference.py:
| What | Against | Tolerance |
|---|---|---|
acorr, pacf, acov, yule_walker, burg |
acf, pacf(method="ldb"), acovf, yule_walker, burg |
1e-9 (Burg's sigma2: 1e-8) |
| Log-likelihood, innovations, streaming | SARIMAX(...).filter(params), results.extend(...) |
1e-9 |
| Forecast means, variances and intervals | ARIMA(...).get_forecast(h), conf_int(...) |
1e-9 |
hannan_rissanen |
hannan_rissanen(..., unbiased=False) |
1e-9 |
ljung_box, jarque_bera, kpss |
acorr_ljungbox, jarque_bera, kpss(nlags=...) |
1e-9, p-values included |
stl |
STL(values, period).fit() |
1e-9 |
AutoArima (fixed d, D) |
pmdarima auto_arima(information_criterion="aicc") |
identical orders and constants |
Arima::fit |
ARIMA(...).fit() |
log-likelihood never more than 1e-6 below statsmodels', parameters 5e-4 |
A second suite checks against R 4.4.2 and forecast 8.23 (scripts/generate_r_reference.R,
run in the pinned image of scripts/r/): arima(method = "ML") fits and fixed =, predict forecasts and
standard errors (1e-6, d up to 2 and the airline model), acf, pacf, Box.test (1e-9), fourier (exact),
ndiffs, nsdiffs and auto.arima's orders. The other families have suites of their own: ets (likelihood,
parameters, forecasts and intervals of all 15 default models), thetaf, croston, accuracy and tsCV against R,
tseries::garch for GARCH, and statsmodels' VAR (1e-8). docs/CONFORMANCE.md
lists every suite, how to regenerate it, and the known differences between the references.
docs/VERIFICATION.md covers the property tests,
the Kani proofs of the device path and mutation testing.
In practice the fit ends up 1e-9 to 6e-8 above statsmodels' log-likelihood; the parameter tolerance is loose because two optimisers stop at slightly different points of a flat likelihood.
Three conventions differ from statsmodels' defaults on purpose:
- For
d > 0the likelihood is that of the differenced series (statsmodels'simple_differencing=True, and what R'sarimadoes). statsmodels' default keeps the firstdvalues through a diffuse start, which moves the log-likelihood by about 1e-5; forecasts agree either way. - BIC and AICc count only non-missing values, as R does. statsmodels counts missing ones too.
- The filter always runs the full Kalman recursion. statsmodels stops updating the state covariance once it changes by
less than 1e-19, which moves later innovations by up to about 1e-9, so the references are generated with
ssm.tolerance = 0.
Benchmarks
ci/compare fits the same models with backshift, foresight 0.7.3
and anofox-forecast 0.21.0, with a mean or drift wherever d + D <= 1.
It scores every crate's estimates with the same exact Gaussian log-likelihood (higher is better), so the column shows
how close each optimiser gets to the maximum. Each repeat runs all crates back to back, and the table shows the fastest
of 15 repeats (5 for the searches) on an Intel Core i7-13700H with Rust 1.100, release build:
"approximate" is backshift with Method::Css for the fits and approximation(true) for the searches.
| Model | backshift | backshift, approximate | foresight | anofox-forecast |
|---|---|---|---|---|
| ARMA(2,1), n=300 | 0.38 ms, −423.0037 | 0.19 ms, −423.0061 | 18.99 ms, −423.0037 | 0.48 ms, −423.0195 |
| ARIMA(1,1,1) with drift, n=300 | 0.24 ms, −435.5482 | 0.13 ms, −435.5516 | 8.96 ms, −435.5482 | 0.68 ms, −435.5591 |
| airline (0,1,1)(0,1,1,12), n=144 | 1.06 ms, −194.3233 | 0.13 ms, −194.3261 | 12.78 ms, −194.3233 | 0.26 ms, −195.2358 |
Fully automatic auto-ARIMA, each crate choosing its own d and D:
| Series | backshift | backshift, approximate | foresight | anofox-forecast |
|---|---|---|---|---|
| ARMA(2,1), n=300 | 7.8 ms, (2,0,1) | 4.2 ms, (2,0,1) | 75.1 ms, (2,0,1) | 8.0 ms, (2,0,1) |
| ARIMA(1,1,1), n=300 | 5.9 ms, (0,1,2) | 3.1 ms, (0,1,2) | 55.8 ms, (0,1,2) | 5.2 ms, (0,1,2) |
| airline, n=144 | 93.5 ms, (0,1,1)(0,1,1,12) | 6.2 ms, (0,1,1)(0,1,1,12) | 368.9 ms, (0,2,2)(1,0,1,12) | 4,030 ms, (0,2,2)(1,0,1,12) |
On the non-seasonal series all crates pick the same model. On the seasonal series backshift recovers the generating airline model, because its STL seasonal-strength test finds the seasonal unit root; the other two crates miss it and difference twice at lag 1 instead.
The seasonal search times differ by more than fit speed. Choosing d = 2, D = 0 leaves the yearly pattern to seasonal
AR and MA terms at lag 12, so every candidate has a large state and fits slowly: even backshift's exact fit of
anofox-forecast's choice (0,2,2)(1,0,1,12) takes 8.4 ms, against 1.1 ms for the airline model. anofox-forecast also
visits 35 models to backshift's 14, and at this size (n <= 150, m <= 12) it follows R's rule and fits each candidate
by exact likelihood through its port of R's arima, at about 105 ms per model, while its standalone SARIMA fit of
the same order takes 0.5 ms. Its own with_approximation(true) takes 486 ms. backshift spends 6.3 ms per candidate,
or 0.4 ms approximately. Fitting one given order by CSS, the two crates are level (0.49 and 0.45 ms on (0,2,2)(1,0,1,12)).
backshift and foresight reach the same maximum in the fits above, and anofox-forecast stops short of it on all three:
it optimises the conditional sum of squares, which needs no Kalman filter. backshift's exact fits use that only as a
warm start (R's CSS-ML); its approximate fits stop there, like R's method = "CSS", and are the fastest on every
model while landing within 0.004 of the maximum, closer than anofox-forecast on all three. The approximate search
refits its choice exactly, so the model it returns is an exact fit. Neither of the other two crates streams an ARIMA
model (anofox-forecast updates its exponential-smoothing models incrementally): backshift's Filter::<27>::update takes
about 0.38 µs per value for the airline model.
ETS, scored on the last values of each series, which no crate sees (root mean squared error, lower is better):
| ETS fit | backshift | foresight | anofox-forecast |
|---|---|---|---|
| damped trend (A,Ad,N), n=120 | 2.6 ms, 1.950 | 2.6 ms, 1.950 | 0.09 ms, 1.660 |
| trend + monthly season (A,A,A), n=144 | 4.7 ms, 1.195 | 56.3 ms, 1.195 | 11.3 ms, 1.195 |
| Automatic ETS | backshift | foresight | anofox-forecast |
|---|---|---|---|
| damped trend, n=120 | 9.2 ms, (A,Ad,N) | 11.3 ms, (A,Ad,N) | 2.3 ms, (A,Ad,N) |
| trend + monthly season, n=144 | 229.0 ms, (A,A,A) | 559.6 ms, (A,A,A) | 224.5 ms, (A,A,A) |
All three choose the same models. backshift's automatic search covers R's whole default set, multiplicative errors and seasons included, which it fits jointly rather than by least squares; that is most of its search time, and level with anofox-forecast on the seasonal series. On the seasonal series backshift fits fastest at the same forecast error. On the damped series anofox-forecast is fastest and forecasts best: its estimates differ from backshift's and foresight's, which agree with each other (and backshift's reach R's likelihood or higher).
The other families, scored the same way (GARCH by the log-likelihood of each crate's estimate under
tseries::garch's definition, higher is better):
| Model | backshift | foresight | anofox-forecast |
|---|---|---|---|
| VAR(2), 3 series, n=500, fit and forecast | 0.04 ms, 1.218 | – | 0.09 ms, 1.218 |
| Theta, trend, n=120 | 0.07 ms, 3.431 | 0.10 ms, 3.431 | 0.05 ms, 3.431 |
| Theta, monthly season, n=144 | 0.09 ms, 1.801 | 0.12 ms, 1.801 | 0.07 ms, 1.801 |
| SBA, optimised alpha, n=150 | 0.02 ms, 2.429 | 0.02 ms, 2.429 | 0.02 ms, 2.429 |
| TSB, n=150 | 0.10 ms (optimised), 2.400 | < 0.01 ms, 2.366 | < 0.01 ms (fixed 0.1), 2.366 |
| GARCH(1,1), n=1000 | 3.1 ms, −1332.851 | – | 23.9 ms, −1332.851 |
All crates agree on VAR, Theta, SBA and GARCH. backshift estimates Theta's smoothing by R's likelihood and searches Croston's and TSB's parameters on a 100-point grid before refining them, scoring 20 grid points per pass over the history; anofox-forecast's Theta remains slightly faster. backshift's optimised TSB fits the history better but forecasts this hold-out slightly worse than the fixed parameters of the other two.
Reproduce with cargo run --release --manifest-path ci/compare/Cargo.toml (add --features rayon for the parallel
seasonal search); cargo bench runs the in-crate Criterion benchmarks.
Status
The crate covers seasonal ARIMA(p, d, q)(P, D, Q, s) with an optional mean or drift and exogenous regressors and automatic order selection; ETS in all 15 of R's default models; Theta; VAR and VARX; Croston, SBA, SBJ and TSB; GARCH(1,1); streaming changepoint detection; STL and MSTL; conformal intervals and an online AR mode; the usual diagnostics; and backtesting to choose between the families. The GARCH variants (EGARCH, GJR-GARCH, IGARCH, FIGARCH, DCC-GARCH, Student-t innovations) are filed as issues; the roadmap has the plan and what was left out on purpose. The minimum supported Rust version is 1.85.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.