Loading tsbโฆ
Linear Gaussian state-space model โ Kalman filter (forward pass) and
RTS smoother (backward pass). Mirrors statsmodels.tsa.statespace
and pykalman.KalmanFilter.
A linear Gaussian SSM describes a latent state x_t and
observations y_t via two equations:
F โ state transition matrix (n_states ร n_states)H โ observation matrix (n_obs ร n_states)Q โ process noise covarianceR โ observation noise covariancem_0, P_0 โ initial state distribution
The Kalman filter computes filtered state
estimates x_{t|t} (posterior after seeing observation t).
The RTS smoother computes smoothed estimates
x_{t|T} using all T observations.
The simplest SSM: a hidden state that follows a random walk, observed with noise. Perfect for denoising a noisy scalar time series or estimating a slowly changing mean.
The filter only uses observations up to time t. The smoother uses all observations to produce better estimates, especially for time-steps near missing values. Smoothed uncertainty is always โค filtered.
A 2-state model: [level, slope]. The level increases by the
slope each step; both drift over time. Great for tracking slowly changing
trends with missing observations.
Build your own model by specifying the four matrices directly. Here: a state that follows an AR(1) process with coefficient 0.9.
The Kalman filter naturally handles multi-dimensional observations. Here: 2 sensors observing a single latent state.
KalmanFilter.localLevel(opts?) โ random-walk + noise (1-D)KalmanFilter.localLinearTrend(opts?) โ level + slope (2-D state)new KalmanFilter(opts) โ custom F, H, Q, R, m0, P0kf.filter(observations) โ KalmanFilterResultkf.smooth(observations) โ KalmanSmootherResultkalmanFilter1D(obs, opts?) โ scalar convenience wrapperkalmanSmooth1D(obs, opts?) โ scalar smoother wrapperextractScalarMeans(means) โ extract 1-D means arrayfilteredPredictionInterval(result, z?) โ {lower, upper}
Missing observations: pass null in any observation row. The
filter skips the update step for that time-step (covariance grows).
The smoother retroactively interpolates using future observations.