Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Seeing the hidden level
Imagine weighing yourself every morning. The scale is noisy, and your true weight changes slowly. A state-space model says there is a hidden true value that drifts a little each day, and each measurement is that value plus some error. The Kalman filter combines yesterday’s best guess with today’s measurement, trusting each according to how noisy it is. If you skip a day, it simply keeps the old guess and becomes a bit less sure.
State-space models run official statistics such as seasonal adjustment at statistics offices, and the Kalman filter estimates GDP ‘nowcasts’ from incomplete data.
- 1. In a state-space model the true value is…
- 2. The Kalman filter combines…
- 3. When a measurement is missing the filter…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- State equation: α_t = Tα_(t−1) + η_t, how the hidden state evolves. Observation equation: y_t = Zα_t + ε_t.
- Local level model: μ_t = μ_(t−1) + η_t with Var η = q, and y_t = μ_t + ε_t with Var ε = r. The signal-to-noise ratio q/r controls how much the level moves.
- Structural models: add a slope (local linear trend), seasonal states and regression effects; each component has its own variance.
- Kalman filter: predict (mean and variance forward), then update with gain k = P/(P + r). Missing data mean predict only.
- Likelihood: the prediction-error decomposition log L = −½ Σ (ln 2πF_t + e_t²/F_t) gives maximum-likelihood variances.
- Smoother: a backward pass (Rauch–Tung–Striebel) uses future observations too and gives lower variance than the filter.
- 1. The local level model's level follows…
- 2. The likelihood comes from…
- 3. The smoother uses…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Filter, smoother and steady gain
Exercise 1.
- 500 points of a local level model with q = 0.25 and r = 1, starting from a diffuse prior.
- The Kalman filter (means, variances, log-likelihood) and the RTS smoother.
- RMSE of the one-step prediction, the filter and the smoother against the true level.
- The steady-state gain compared with the formula.
Practice.
- Start simple (local level or local linear trend) and add components only if residual checks call for them.
- Estimate variances by maximum likelihood, with diffuse initialisation for non-stationary states.
- Feed missing values as NaN: the filter skips the update; do not interpolate first.
- Use the smoother for historical estimates and the filter for real-time ones.
- Tools: statsmodels UnobservedComponents and SARIMAX (state space), R KFAS, PyMC and NumPyro for Bayesian structural time series.
def kalman_level(y, q, r, m=0.0, v=1e7):
out = []
for t, obs in enumerate(y):
v = v + (q if t else 0.0)
if obs == obs: # skip NaN (missing)
k = v / (v + r)
m, v = m + k * (obs - m), (1 - k) * v
out.append((m, v))
return out- 1. Missing values should be given to the filter as…
- 2. Historical estimates should use…
- 3. statsmodels class for structural time-series models:
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Prediction, filtering, smoothing. In Exercise 1 the level RMSE was:
- 0.750 for the one-step prediction, which uses data up to t−1;
- 0.581 for the filter, up to t;
- 0.489 for the smoother, which uses all the data.
The filter’s 95% intervals covered 0.97 of true levels, and the log-likelihood was −806.3.
Steady gain and exponential smoothing. For constant q and r the predicted variance converges to p = (q + √(q² + 4qr))/2 and the gain to k = p/(p + r). The filter update m_t = m_(t−1) + k(y_t − m_(t−1)) is then exactly simple exponential smoothing with α = k. For q/r = 0.25, k = 0.390. Exponential smoothing is therefore the steady-state Kalman filter of the local level model. The state-space form adds a likelihood, honest intervals and missing-data handling.
Missing data. Without an observation the filter only predicts. The mean stays put and the variance grows by q per step. The smoother then pulls estimates in a gap towards the observations on both sides. Interpolating first and then smoothing treats invented values as real data and gives no uncertainty.
- 1. The steady Kalman gain of the local level model equals…
- 2. Across a gap, the filter variance…
- 3. Interpolating before filtering…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.With maximum-likelihood variances, how accurately do the Kalman filter and smoother recover a hidden level when 0%, 20% or 50% of the observations are missing, compared with interpolation followed by exponential smoothing, and are the intervals honest?
History.
- R. Kalman (1960) introduced the filter, and H. Rauch, F. Tung and C. Striebel (1965) the smoother.
- J. Muth (1960) showed that exponential smoothing is optimal for a random walk plus noise.
- R. Harrison and C. Stevens (1976) developed Bayesian forecasting with dynamic linear models, and A. Harvey (1989) structural time-series models.
- J. Durbin and S. Koopman (2001) gave the standard treatment. S. Scott and H. Varian (2014) popularised Bayesian structural time series, and Google’s CausalImpact used it.
Exercise 2.
- 40 series of 300 points from local level models with r = 1 and q drawn from 0.05, 0.25 and 1.
- 0%, 20% or 50% of observations deleted at random.
- (q, r) estimated by maximum likelihood on a grid.
- The true level is estimated at every time with interpolation + SES (α by SSE), the Kalman filter and the RTS smoother.
Seed 0:
| missing | interpolate + SES | Kalman filter | Kalman smoother | smoother 95% coverage | median q̂/r̂ ÷ true q/r |
|---|---|---|---|---|---|
| 0% | 0.64 | 0.64 | 0.52 | 0.95 | 0.95 |
| 20% | 0.71 | 0.72 | 0.57 | 0.94 | 0.95 |
| 50% | 0.92 | 0.98 | 0.72 | 0.93 | 0.9 |
- The smoother was best at every missing rate, about 20% better than the one-sided methods.
- Interpolation + SES matched or slightly beat the filter. That is because interpolation peeks at the next observed value, so it is not truly one-sided. The fair two-sided comparison is the smoother, and it won clearly.
- Only the state-space model gave intervals, and they were honest: 93–95% coverage. Maximum likelihood recovered q/r to within about 10%.
- Seeds 1 and 2: smoother RMSE 0.52 and 0.56 with no missing data, 0.71 and 0.7 with half missing; coverage 0.93–0.94.
State-space models in practice
- Official statistics: seasonal adjustment (X-13ARIMA-SEATS) and nowcasting with dynamic factor models are state-space methods.
- Causal inference: Bayesian structural time series (CausalImpact) estimates what would have happened without an intervention.
- Deep state-space models: DeepState and deep Kalman filters learn the state-space parameters with neural networks across many series.
- Engineering: the same filter tracks vehicles, fuses sensors and estimates battery state of charge.
References
- J. Durbin and S. J. Koopman (2012). Time Series Analysis by State Space Methods, 2nd ed. Oxford University Press.
- A. C. Harvey (1989). Forecasting, Structural Time Series Models and the Kalman Filter. Cambridge University Press.
- R. E. Kalman (1960). “A new approach to linear filtering and prediction problems.” Journal of Basic Engineering.
- H. E. Rauch, F. Tung and C. T. Striebel (1965). “Maximum likelihood estimates of linear dynamic systems.” AIAA Journal.
- K. H. Brodersen, F. Gallusser, J. Koehler, N. Remy and S. L. Scott (2015). “Inferring causal impact using Bayesian structural time-series models.” Annals of Applied Statistics.