SafeZone AI Learn
Learn/ Forecasting/ Time Series & Forecasting · lesson 4 of 7

State space and the Kalman filter

Hidden state, noisy observations, and the optimal predict–update recursion derived step by step from the Bayesian lesson's machinery — then watched live, tracking a level it can never observe directly.

Twice now this track has hinted that its models are “really state-space models” — ETS in lesson 2, ARIMA’s likelihood in lesson 3. This lesson opens the box. The state-space view is the track’s unifying formalism, and its crown jewel — the Kalman filter — is arguably the most deployed algorithm of the twentieth century: it navigated Apollo, sits in every GPS receiver and drone, and computes the exact likelihood of every structural time-series model. Better still, you have already derived most of it: it is the Bayesian update lesson running on a moving target.

The model: what you want is not what you see

Two equations, two admissions of ignorance:

xt=Axt1+wtstate evolves, noisilyyt=Cxt+vtyou observe a corrupted view\underbrace{x_t = A\, x_{t-1} + w_t}_{\text{state evolves, noisily}} \qquad \underbrace{y_t = C\, x_t + v_t}_{\text{you observe a corrupted view}}

with wtN(0,Q)w_t \sim \mathcal N(0, Q), vtN(0,R)v_t \sim \mathcal N(0, R). The state xtx_t is what you care about (true demand level, true position, true temperature); yty_t is what you get (bookings, GPS pings, a cheap sensor). The simplest instance — the local level model, xt=xt1+wtx_t = x_{t-1} + w_t, yt=xt+vty_t = x_t + v_t — is already profound: it says the world drifts (QQ) and the measurement lies (RR), and asks for the best running belief about the truth. Note it is exactly a random walk observed in noise — ARIMA(0,1,1) in disguise, and SES’s optimal home, as your exercises have twice foreshadowed.

The filter: Bayes, recursively

Maintain a Gaussian belief xt1y1:t1N(mt1,Pt1)x_{t-1} \mid y_{1:t-1} \sim \mathcal N(m_{t-1}, P_{t-1}) and alternate two steps.

Predict — push the belief through the dynamics; uncertainty grows:

mt=Amt1,Pt=APt1A+Q.m_t^- = A\, m_{t-1}, \qquad P_t^- = A P_{t-1} A^\top + Q .

Update — treat the prediction as the prior, the new observation as evidence, and apply the Gaussian-conjugacy algebra of the Bayesian lesson. For the scalar local level it collapses to three unforgettable lines:

Kt=PtPt+R,mt=mt+Kt(ytmt)surprise,Pt=(1Kt)Pt.K_t = \frac{P_t^-}{P_t^- + R}, \qquad m_t = m_t^- + K_t\,\underbrace{(y_t - m_t^-)}_{\text{surprise}}, \qquad P_t = (1 - K_t)\, P_t^- .

The Kalman gain Kt(0,1)K_t \in (0,1) is a trust dial set by the noise ratio: prediction uncertain relative to the sensor (PRP^- \gg R) ⟹ K1K \to 1, believe the data; sensor noisy (RPR \gg P^-) ⟹ K0K \to 0, believe the model. And “move your estimate a fraction KK toward the surprise” is — the third time this shape appears in the track — exponential smoothing, now with its α derived from first principles rather than tuned: in steady state, SES’s optimal α is exactly the converged Kalman gain of the local level model.

At the correct settings (√Q ≈ 0.22, √R = 1), the readout shows the filter’s whole value proposition: RMSE against the hidden truth roughly HALVES relative to trusting the raw observations — information extracted by nothing but two variances and Bayes. Now lie to it. Crank √Q up: the filter thinks the world is jumpy, K rises toward 1, the estimate glues itself to the noisy dots — smoothing gone. Crush √Q down: the filter thinks truth barely moves, K falls toward 0, and the estimate becomes a serene, confidently WRONG lag — watch it sail straight through the truth’s turns while its ±2σ band stays slim. That band tracks the filter’s SELF-assessed uncertainty, and a mis-specified filter is miscalibrated exactly when it is most confident. Q and R are the model; the filter is only as honest as they are.

Why this is the track’s master formalism

  • Every ETS and ARIMA model has a state-space form, and the filter’s one-step prediction errors give the exact Gaussian likelihood — this is literally how statsmodels/forecast compute the MLEs of the last two lessons. One algorithm underwrites the whole classical stack.
  • Missing data is trivial: no observation, no update — just predict. Irregular sampling, sensor dropouts, holidays: handled by the same three lines. (Try doing that to a bare ARIMA recursion.)
  • Smoothing (the RTS backward pass) refines every past state using the full series — for retrospective analysis; the filter alone is the honest choice for forecasting, since it uses only what was known at the time.
  • Structural models: add trend, seasonal and regression states and you get interpretable components with uncertainty — the Bayesian structural time series behind causal-impact analyses.
  • Beyond linear-Gaussian: real dynamics bend the assumptions, and the extensions map the terrain — EKF (linearize, the Jacobian doing the work the Matrix Calculus lesson trained), UKF (sample sigma points), and the particle filter (Monte Carlo over states — the 1/√n lesson’s machinery chasing a distribution through time). Each trades exactness for realism; all keep the predict–update heartbeat.
import numpy as np
rng = np.random.default_rng(3)

n, Q, R = 200, 0.05, 1.0
x = np.cumsum(np.sqrt(Q) * rng.standard_normal(n))     # hidden truth
y = x + np.sqrt(R) * rng.standard_normal(n)            # what you see

m, P, est = y[0], 10.0, []
for t in range(n):
    Pp = P + Q                       # predict
    K = Pp / (Pp + R)                # gain
    m = m + K * (y[t] - m)           # update on surprise
    P = (1 - K) * Pp
    est.append(m)

rmse = lambda a: np.sqrt(np.mean((np.array(a) - x) ** 2))
print(rmse(y), rmse(est), K)         # filter beats raw; K ≈ steady state

Exercises

Work these before the next lesson

  1. Derive the scalar update step from Gaussian conjugacy: prior N(m,P)\mathcal N(m^-, P^-), likelihood yN(x,R)y \sim \mathcal N(x, R) — show the posterior mean is m+K(ym)m^- + K(y - m^-) with K=P/(P+R)K = P^-/(P^- + R). (You did this algebra in the Bayesian lesson; here it just runs every tick.)
    Solution

    Worked solutions are part of Premiumunlock all of them for £5/month →

  2. 5 more exercises — each with a worked solution — are part of Premium. Unlock everything for £5/month →

References

  • R. E. Kalman, “A New Approach to Linear Filtering and Prediction Problems”, 1960 — the paper.
  • J. Durbin & S. J. Koopman, Time Series Analysis by State Space Methods, 2nd ed. — the reference for everything here.
  • S. Scott & H. Varian, “Predicting the Present with Bayesian Structural Time Series”, 2014 — structural models at work.