The predict-then-update loop inside the Kalman filter

Today let's open up the machinery that actually produces a state space model's numbers: the Kalman filter's predict step and its update step, worked by hand until you can run them on any new year yourself.

The example is Nile again, the river's annual flow at Aswan for a hundred years, 1871 to 1970, already fit as a local level model. That fit gives two numbers: one for how much the hidden level itself drifts from year to year, and one for how noisy any single year's reading is. Here is the whole series again, exactly as it happened.

That line is the raw material. Everything from here on opens up the loop that reads it one year at a time and turns each reading into a filtered estimate of where the level really is.

The two moves: predict, then update

Every year, the filter takes two moves in order, and both of them use only what came before: last year's estimate, and this year's fresh reading.

The predict step comes first. It carries last year's filtered estimate forward unchanged, since a local level model has no trend to add, and it inflates that estimate's variance by the state variance, usually written Q.

\[ a_{t|t-1} = a_{t-1|t-1} \] \[ P_{t|t-1} = P_{t-1|t-1} + Q \]

Read \(a_{t|t-1}\) as "the estimate for year t, using data only up to year t minus 1": this year's prediction, built from last year's already-filtered estimate, \(a_{t-1|t-1}\). \(P_{t|t-1}\) is that prediction's own variance, how uncertain the predicted level is before this year's reading has even arrived. It grows by Q every year, because a level that is free to drift becomes a little less certain with every year that passes.

Then comes the update. It compares the new reading to what was predicted, and that gap gets its own name.

\[ v_t = y_t - a_{t|t-1} \]

\(v_t\) is called the innovation: literally, how much genuinely new information this year's reading carries over what the model already expected. A reading that lands exactly on the prediction has an innovation of 0, since it told the filter nothing it did not already know.

How much of that innovation gets folded into the estimate is set by a single number, the Kalman gain.

\[ K_t = \frac{P_{t|t-1}}{P_{t|t-1} + H} \] \[ a_{t|t} = a_{t|t-1} + K_t v_t \] \[ P_{t|t} = (1 - K_t) P_{t|t-1} \]

\(K_t\) is the predicted variance divided by itself plus H, the observation variance, so it always comes out between 0 and 1. The updated estimate, \(a_{t|t}\), is the prediction plus the gain times the innovation: a gain near 1 pulls the estimate almost all the way to the new reading, and a gain near 0 leaves it almost where it was predicted. The updated variance, \(P_{t|t}\), shrinks by that same factor, since a real reading was just used to sharpen the guess.

One diagram puts all three moves in the order they actually happen.

Every year of Nile's hundred, the filter does nothing but these three moves, over and over.