Loading tsbโ€ฆ

โ† Back to index

๐Ÿ”ญ Kalman Filter & State-Space Models

Linear Gaussian state-space model โ€” Kalman filter (forward pass) and RTS smoother (backward pass). Mirrors statsmodels.tsa.statespace and pykalman.KalmanFilter.

๐Ÿ“ The State-Space Model

A linear Gaussian SSM describes a latent state x_t and observations y_t via two equations:

x_t = F ยท x_{t-1} + w_t, w_t ~ N(0, Q) (state transition)
y_t = H ยท x_t + v_t, v_t ~ N(0, R) (observation)
x_0 ~ N(m_0, P_0)

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.

๐Ÿ“ˆ Local-Level Model (Random Walk + Noise)

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.

example-1.ts
Click โ–ถ Run to execute

๐Ÿ”„ RTS Smoother โ€” Filling Gaps Retrospectively

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.

example-2.ts
Click โ–ถ Run to execute

๐Ÿ“Š Local Linear Trend (Level + Slope)

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.

example-3.ts
Click โ–ถ Run to execute

โš™๏ธ Custom State-Space Model (AR(1) State)

Build your own model by specifying the four matrices directly. Here: a state that follows an AR(1) process with coefficient 0.9.

example-4.ts
Click โ–ถ Run to execute

๐Ÿ”ข Multi-Dimensional Observations

The Kalman filter naturally handles multi-dimensional observations. Here: 2 sensors observing a single latent state.

example-5.ts
Click โ–ถ Run to execute

๐Ÿ“– API Reference

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.