The problem: two imperfect sources

In 1968, Apollo 8 flew to the Moon with a computer that had only a few kilobytes of working memory. It had to know where the spacecraft was at every moment, and it had two ways to find out:

  1. Physics (a model). If you know where you were and how fast you were moving, you can calculate where you are now. But every small error is carried into the next calculation, so the estimate slowly drifts.
  2. Measurement (a sensor). Star sightings are tied to the real world, so they don't drift. But every sighting is noisy: the readings jump around.

Trusting only the sensor gives a jittery estimate. Trusting only the model gives a drifting one. A fixed 50/50 average only works if both sources are equally reliable, which is rarely true.

What we need is a weight that reflects how much each source deserves to be trusted right now. The Kalman filter computes exactly that weight. Your phone uses the same idea to keep the blue dot on the map steady.

Describing uncertainty with a bell curve

The state is the set of numbers that describes the system, for example a car's position \(x\).

Instead of storing one number ("the car is at 100 m"), the Kalman filter stores a bell curve (Gaussian):

\[ p(x) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\!\left(-\frac{(x-\mu)^2}{2\sigma^2}\right) \]
  • The mean \(\mu\) is our best guess.
  • The variance \(\sigma^2\) is how unsure we are. A bigger variance means a wider curve.
  • The standard deviation \(\sigma = \sqrt{\sigma^2}\) is in metres. About 68% of the time the truth is within \(\pm\sigma\) of the mean.

So variance 4 means "usually within about 2 m", and variance 12 means "usually within about 3.5 m".

Two facts about variance are used throughout:

\[ \operatorname{Var}(aX) = a^2\,\operatorname{Var}(X), \qquad \operatorname{Var}(X+Y) = \operatorname{Var}(X) + \operatorname{Var}(Y) \ \text{ (if independent)} \]

A worked example

A car drives along a straight road.

  • The model predicts the car is at \(x_{\text{pred}} = 100\) m, with variance \(\sigma^2_{\text{pred}} = 4\).
  • The GPS says the car is at \(z = 106\) m, with variance \(\sigma^2_{\text{GPS}} = 12\).

The difference between them is the innovation (the "surprise"):

\[ y = z - x_{\text{pred}} = 106 - 100 = 6 \text{ m} \]

How much of this 6 m surprise should we believe? The answer is the Kalman gain:

\[ K = \frac{\sigma^2_{\text{pred}}}{\sigma^2_{\text{pred}} + \sigma^2_{\text{GPS}}} = \frac{4}{4 + 12} = \frac{1}{4} \]

We move one quarter of the way from the prediction towards the measurement:

\[ \hat{x} = x_{\text{pred}} + K\,(z - x_{\text{pred}}) = 100 + \tfrac{1}{4}\cdot 6 = 101.5 \text{ m} \]

And the new variance is:

\[ \sigma^2_{\text{new}} = (1 - K)\,\sigma^2_{\text{pred}} = \tfrac{3}{4}\cdot 4 = 3 \]

The surprising result: the new variance (3) is smaller than the prediction's (4) and much smaller than the GPS's (12). Combining a good source with a worse one gives an estimate that is better than the good source alone.

Where does the gain come from?

The new estimate is a mixture of the two sources:

\[ \hat{x} = (1 - K)\,x_{\text{pred}} + K\,z \]

Write each source as "truth + error": \(x_{\text{pred}} = x + e_p\) and \(z = x + e_g\), with independent errors of variance \(\sigma_p^2\) and \(\sigma_g^2\). Then the error of our estimate is \((1-K)e_p + K e_g\), and its variance is:

\[ \sigma^2_{\text{new}}(K) = (1 - K)^2\,\sigma_p^2 + K^2\,\sigma_g^2 \]

As \(K\) grows, the first term shrinks and the second grows, so there is a lowest point in between. Set the derivative to zero:

\[ \frac{d\sigma^2_{\text{new}}}{dK} = -2(1-K)\,\sigma_p^2 + 2K\,\sigma_g^2 = 0 \quad\Longrightarrow\quad K = \frac{\sigma_p^2}{\sigma_p^2 + \sigma_g^2} \]

The second derivative, \(2\sigma_p^2 + 2\sigma_g^2\), is positive, so this is a minimum. For the car: \(\frac{d}{dK}\left[4(1-K)^2 + 12K^2\right] = 32K - 8 = 0\), so \(K = \tfrac14\).

Precisions add

Plugging the best \(K\) back in gives \(\sigma^2_{\text{new}} = \dfrac{\sigma_p^2\,\sigma_g^2}{\sigma_p^2 + \sigma_g^2}\). Taking the reciprocal:

\[ \frac{1}{\sigma^2_{\text{new}}} = \frac{1}{\sigma_p^2} + \frac{1}{\sigma_g^2} \]

The quantity \(1/\sigma^2\) is called precision, and the rule is simply: precisions add. For the car, \(\tfrac14 + \tfrac1{12} = \tfrac13\), so the new variance is 3. Every source adds some precision, so combining can never make you less certain.

Sanity checks

CaseGainMeaning
Perfect sensor (\(\sigma_g^2 = 0\))\(K = 1\)Jump to the measurement
Useless sensor (\(\sigma_g^2 \to \infty\))\(K \to 0\)Ignore the measurement
Equally good sources\(K = \tfrac12\)Plain average, variance halved

The predict–update loop

The car keeps moving, so the filter repeats two steps forever.

Predict. Move the estimate forward with the model, and add process noise \(q\) because the model is never perfect:

\[ x^- = x + v\,\Delta t, \qquad P^- = P + q \]

Update. When a measurement \(z\) arrives (sensor variance \(r\)):

\[ K = \frac{P^-}{P^- + r}, \qquad x = x^- + K\,(z - x^-), \qquad P = (1 - K)\,P^- \]

Prediction makes the bell curve wider; measurement makes it narrower.

Watching the filter settle

Start with a terrible guess, \(P^- = 100\), and use \(q = 1\), \(r = 12\):

Step\(P^-\) (after predict)Gain \(K\)\(P\) (after update)
1100.0000.89310.714
211.7140.4945.928
36.9280.3664.392
54.7200.2823.388
104.0370.2523.021
∞40.253

At first the filter trusts the GPS almost completely (the model is still very uncertain). As it gains confidence, it relies more on its own prediction.

The steady state, exactly

In steady state, \(P\) stops changing:

\[ P = \frac{r\,(P + q)}{P + q + r} \quad\Longrightarrow\quad P^2 + P - 12 = 0 \quad\Longrightarrow\quad P = 3 \]

So \(P^- = 4\) and \(K = \tfrac{4}{16} = \tfrac14\). In general, \(P_\infty = \tfrac12\left(-q + \sqrt{q^2 + 4qr}\right)\).

This means the first example wasn't chosen by hand: it was the filter after it had settled. Nobody told the filter to trust the GPS by one quarter. That weight came from the noise levels alone.

More than one number: position and velocity

Now estimate both position \(p\) and velocity \(v\), while the GPS still measures only position. The uncertainty becomes a covariance matrix:

\[ \mathbf{x} = \begin{bmatrix} p \\ v \end{bmatrix}, \qquad P = \begin{bmatrix} \operatorname{Var}(p) & \operatorname{Cov}(p,v) \\ \operatorname{Cov}(v,p) & \operatorname{Var}(v) \end{bmatrix} \]

The diagonal holds the individual uncertainties. The off-diagonal entries say how the errors are linked.

The equations have exactly the same structure as before:

\[ \begin{aligned} &\textbf{Predict:} && \hat{\mathbf{x}}^- = F\hat{\mathbf{x}}, && P^- = FPF^\top + Q \\ &\textbf{Update:} && K = P^-H^\top\left(HP^-H^\top + R\right)^{-1}, && \hat{\mathbf{x}} = \hat{\mathbf{x}}^- + K\left(\mathbf{z} - H\hat{\mathbf{x}}^-\right), \quad P = (I - KH)\,P^- \end{aligned} \]

For our car with \(\Delta t = 1\) s:

\[ F = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}, \quad H = \begin{bmatrix} 1 & 0 \end{bmatrix}, \quad Q = \begin{bmatrix} 1 & 0 \\ 0 & 0 \end{bmatrix}, \quad R = 12 \]

Worked example

Start with \(\hat{\mathbf{x}} = \begin{bmatrix} 242.8 \\ 20 \end{bmatrix}\) and \(P = \begin{bmatrix} 3 & 0 \\ 0 & 4 \end{bmatrix}\).

Predict:

\[ \hat{\mathbf{x}}^- = \begin{bmatrix} 262.8 \\ 20 \end{bmatrix}, \qquad P^- = \begin{bmatrix} 3 & 4 \\ 0 & 4 \end{bmatrix}\begin{bmatrix} 1 & 0 \\ 1 & 1 \end{bmatrix} + Q = \begin{bmatrix} 7 & 4 \\ 4 & 4 \end{bmatrix} + \begin{bmatrix} 1 & 0 \\ 0 & 0 \end{bmatrix} = \begin{bmatrix} 8 & 4 \\ 4 & 4 \end{bmatrix} \]

The off-diagonal 4 appears because position moves according to velocity: if the velocity estimate is too high, the predicted position also ends up too far ahead.

Update with a GPS reading \(z = 252.8\) m (10 m behind the prediction):

\[ y = 252.8 - 262.8 = -10, \qquad S = 8 + 12 = 20, \qquad K = \frac{1}{20}\begin{bmatrix} 8 \\ 4 \end{bmatrix} = \begin{bmatrix} 0.4 \\ 0.2 \end{bmatrix} \]
\[ \hat{\mathbf{x}} = \begin{bmatrix} 262.8 - 4 \\ 20 - 2 \end{bmatrix} = \begin{bmatrix} 258.8 \\ 18 \end{bmatrix}, \qquad P = \begin{bmatrix} 0.6 & 0 \\ -0.2 & 1 \end{bmatrix}\begin{bmatrix} 8 & 4 \\ 4 & 4 \end{bmatrix} = \begin{bmatrix} 4.8 & 2.4 \\ 2.4 & 3.2 \end{bmatrix} \]

The GPS never measured velocity, yet velocity was corrected from 20 to 18 m/s. Because the filter knows position and velocity errors are linked, it reasons: "if I was too far ahead, I was probably also going too fast." The velocity variance also dropped from 4 to 3.2.

Limits and variations

The system must be linear. A bell curve passed through a straight-line function stays a bell curve; a curved function distorts it. For nonlinear systems (turning vehicles, radar measuring range and angle):

  • Extended Kalman Filter (EKF): approximates the system with a straight line around the current estimate.
  • Unscented Kalman Filter (UKF): sends a few carefully chosen points through the real nonlinear function.
  • Particle filter: uses a large cloud of samples when one bell curve can't describe the uncertainty.

The noise levels must be realistic. If we claim the model is perfect (\(q = 0\)), the variance keeps shrinking and the gain heads to zero. With \(P_0 = 100\) and \(r = 12\), after 100 steps \(K \approx 0.01\): the filter accepts only 1% of each surprise and drifts away from reality while becoming more confident that it's right.

  • \(Q\) too small: overconfident, ignores the sensor, lags behind real changes.
  • \(Q\) too large: trusts every noisy reading, estimate becomes jittery.

Cheat sheet

1-DMatrix form
Predict state\(x^- = x + v\Delta t\)\(\hat{\mathbf{x}}^- = F\hat{\mathbf{x}}\)
Predict uncertainty\(P^- = P + q\)\(P^- = FPF^\top + Q\)
Gain\(K = \dfrac{P^-}{P^- + r}\)\(K = P^-H^\top(HP^-H^\top + R)^{-1}\)
Update state\(x = x^- + K(z - x^-)\)\(\hat{\mathbf{x}} = \hat{\mathbf{x}}^- + K(\mathbf{z} - H\hat{\mathbf{x}}^-)\)
Update uncertainty\(P = (1 - K)P^-\)\(P = (I - KH)P^-\)

Underneath all the matrices, the Kalman filter does the same thing over and over: it keeps track of what it believes, how uncertain that belief is, and how reliable each new piece of information is. Then it uses that uncertainty to decide how much weight each source deserves.

📄 Go deeper: Download the full Kalman Filter notes (PDF) for all figures, complete derivations, the matrix Kalman gain proof, six practice problems with full solutions, and Python code.

Download PDF notes