Time Series & State Space
NextStat provides a linear-Gaussian Kalman filter with EM parameter estimation, multi-step forecasting, and standard model builders — all computed in Rust.
Kalman Filter
import nextstat.timeseries as ts
# Filter + smooth
filtered = ts.kalman_filter(y, F, H, Q, R, x0, P0)
smoothed = ts.kalman_smooth(y, F, H, Q, R, x0, P0)
# EM parameter estimation
params = ts.kalman_em(y, F, H, n_iter=100)
# Combined fit (filter + EM + smooth)
result = ts.kalman_fit(y, model="local_level")Forecasting
# Multi-step-ahead with prediction intervals
forecast = ts.kalman_forecast(result, steps=12)
print(forecast.mean) # point forecasts
print(forecast.lower_95) # lower 95% PI
print(forecast.upper_95) # upper 95% PIModel Builders
- local_level — random walk + noise (structural time series baseline)
- local_trend — random walk with drift
- AR(1) — autoregressive with softplus + bounds transform
Simulation
# Simulate from a fitted model
sim = ts.kalman_simulate(result, n_steps=200, seed=42)Visualization
from nextstat.timeseries import plot_kalman_states, plot_forecast_bands
plot_kalman_states(smoothed)
plot_forecast_bands(forecast)Features
- Missing observation handling (skip update, correct likelihood)
- Rauch-Tung-Striebel (RTS) smoother
- EM estimation for Q, R (optionally F, H)
- Controllable seed for reproducible simulation
