---
title: The Kalman filter
heading: Estimating a state from noisy measurements by blending a model's prediction with each new measurement, the discrete predict and update steps, the steady-state gain from the noise ratio, the continuous-time filter as the dual of the LQR, and LQG control
eyebrow: S9 · Lesson 39 · The Kalman filter
xp: 50
---

::: hook
An observer's gain was chosen by hand: fast enough to track, slow enough not to amplify noise. Rudolf Kálmán showed in 1960 that if you know how noisy the measurements are and how much random disturbance the plant receives, there's one best gain, and it can be computed recursively, one measurement at a time. The Kalman filter guided Apollo to the Moon and now runs in every GPS receiver, phone and drone. This lesson builds it from the simplest case, a single noisy reading of a drifting quantity.
:::

::: play
- A quantity that drifts as a random walk (variance Q added each step) is measured with noise of variance R. The plot shows the steady-state Kalman gain K and the estimate's standard deviation (relative to the sensor's) against the ratio Q/R.
- When the quantity barely drifts (Q/R small), the filter trusts its prediction: K is small and the estimate averages many readings, so it's far less noisy than the sensor.
- When it drifts fast, K approaches 1 and the filter simply follows the latest measurement.
:::

```sim plot
view: graph
x: rho = 0.0001..100 log
y: v = 0..1
plot: (rho + sqrt(rho^2 + 4*rho))/2/((rho + sqrt(rho^2 + 4*rho))/2 + 1) | samples: 400 | label: Kalman gain K
plot: sqrt((rho + sqrt(rho^2 + 4*rho))/2/((rho + sqrt(rho^2 + 4*rho))/2 + 1)) | samples: 400 | color: ch3 | label: estimate σ / sensor σ
```

::: predict
## A sensor reading a constant has noise with standard deviation 0.5 °C. After 25 readings, how good can the best estimate be?

- [ ] 0.5 °C: noise doesn't average out
- [ ] 0.02 °C
- [x] 0.1 °C
- [ ] 0.25 °C

::: reveal
For a constant, the Kalman filter reduces to the running average, and averaging n independent readings divides the standard deviation by √n: 0.5/5 = 0.1 °C. Its gain after the nth reading is 1/n, which is exactly how a running average updates.
:::
:::

::: nameit
## The model

$$x_{k+1} = Ax_k + Bu_k + w_k, \qquad y_k = Cx_k + v_k$$

with independent zero-mean noises of covariance $R_w$ (process) and $R_v$ (measurement).

## The two steps

**Predict** with the model, and grow the uncertainty:

$$\hat{x}_k^- = A\hat{x}_{k-1} + Bu_{k-1}, \qquad P_k^- = AP_{k-1}A^T + R_w$$

**Update** with the measurement, weighting it by the **Kalman gain**:

$$K_k = P_k^-C^T(CP_k^-C^T + R_v)^{-1}, \quad \hat{x}_k = \hat{x}_k^- + K_k(y_k - C\hat{x}_k^-), \quad P_k = (I - K_kC)P_k^-$$

The filter is an observer whose gain changes with time, settling to a constant for a time-invariant system.

## One state

For a random walk, A = C = 1 with variances Q and R, the steady-state prior variance solves $P^{-2} - QP^- - QR = 0$:

$$P^- = \frac{Q + \sqrt{Q^2 + 4QR}}{2}, \qquad K = \frac{P^-}{P^- + R}, \qquad P = KR$$

The update $\hat{x} \leftarrow \hat{x} + K(y - \hat{x})$ is an exponential average with weight K.

## Continuous time and duality

For $\dot{x} = Ax + w$, $y = Cx + v$ with noise intensities $R_w$, $R_v$, the steady-state filter has gain $L = PC^TR_v^{-1}$, where $AP + PA^T - PC^TR_v^{-1}CP + R_w = 0$: the LQR's Riccati equation with $A \to A^T$, $B \to C^T$, $Q \to R_w$, $R \to R_v$. For one state, $L = a + \sqrt{a^2 + R_w/R_v}$ and the error pole is at $-\sqrt{a^2 + R_w/R_v}$.

## LQG

The LQR fed by a Kalman filter's estimate is optimal for linear plants with Gaussian noise and quadratic cost (the separation principle again). But its margins aren't guaranteed (Doyle, 1978), so LQG designs must be checked for robustness like any other.
:::

::: example
## A temperature drifts by a random walk with σ = 0.05 °C per sample, and its sensor has σ = 0.5 °C. Find the steady-state filter.

::: solution
1. **Noise variances.**
   $$\begin{aligned} Q &= 0.0025 \\ R &= 0.25 \end{aligned}$$
2. **Prior variance.**
   $$\begin{aligned} P^- &= \frac{Q + \sqrt{Q^2 + 4QR}}{2} \\ &= 0.0263 \end{aligned}$$
3. **Gain and posterior variance.**
   $$\begin{aligned} K &= \frac{P^-}{P^- + R} \\ &= 0.0951 \\ P &= KR \\ &= 0.0238 \end{aligned}$$
4. **The estimate's spread.** It has σ = 0.154 °C, about a third of the sensor's.
5. **What the filter is.** It's an exponential average with weight 0.095, remembering about 1/K ≈ 10.5 samples.
6. **Check.** Iterating the predict and update equations from any starting variance converges to the same numbers.

**Answer:** the steady-state gain is K = 0.0951, and the estimate has σ = 0.154 °C, about a third of the sensor's.
:::
:::

::: example
## The filter's prior is 20.0 °C with variance 0.04, and the sensor (variance 0.25) reads 20.6 °C. What's the update?

::: solution
1. **Gain.**
   $$\begin{aligned} K &= \frac{0.04}{0.04 + 0.25} \\ &= 0.138 \end{aligned}$$
2. **Update the estimate.** It moves 13.8% of the way toward the reading:
   $$\begin{aligned} \hat x &= 20.0 + 0.138 \times 0.6 \\ &= 20.08\ \text{°C} \end{aligned}$$
3. **Update the variance.**
   $$\begin{aligned} P &= (1 - 0.138) \times 0.04 \\ &= 0.0345 \end{aligned}$$
4. **Why the step is small.** A confident prior and a noisy sensor mean a small step.

**Answer:** the estimate moves to 20.08 °C, with variance 0.0345.
:::
:::

::: example
## Two sensors measure the same temperature, one with σ = 0.5 °C and one with σ = 0.2 °C. How should they be combined, and how good is the result?

::: solution
1. **Inverse-variance weights.**
   $$\begin{aligned} 1/0.25 &= 4 \\ 1/0.04 &= 25 \end{aligned}$$
2. **Share of the better sensor.**
   $$25/29 = 0.862$$
3. **Fused variance.**
   $$\begin{aligned} \sigma^2 &= \frac{1}{4 + 25} \\ &= 0.0345 \\ \sigma &= 0.186\ \text{°C} \end{aligned}$$
   better than either sensor alone.
4. **The Kalman view.** This is the Kalman update with the first sensor as the prior; adding a poor sensor still helps, a little.

**Answer:** weight each by its inverse variance, so the better sensor gets 0.862 of the weight; the result has σ = 0.186 °C.
:::
:::

::: check
given: Q = 0.001 | 0.01 | 0.1; R = 0.1 | 1
answer: (Q + sqrt(Q^2 + 4*Q*R))/2/((Q + sqrt(Q^2 + 4*Q*R))/2 + R)
tolerance: 0.5%

## A random walk with step variance {Q} is measured with noise variance {R}. What's the steady-state Kalman gain?

::: right
Right. $P^- = (Q + \sqrt{Q^2 + 4QR})/2$ and $K = P^-/(P^- + R)$ = {answer}.
:::

::: misses
- [Q/(Q + R)] The prior variance in steady state is larger than Q: it carries the accumulated uncertainty too.
- [any] Not yet. Solve $P^{-2} - QP^- - QR = 0$.
:::
:::

::: check
given: Q = 0.001 | 0.01 | 0.1; R = 0.1 | 1
answer: sqrt((Q + sqrt(Q^2 + 4*Q*R))/2/((Q + sqrt(Q^2 + 4*Q*R))/2 + R)*R)
tolerance: 0.5%

## For the same filter, what's the standard deviation of the estimate after each update?

::: right
Right. $\sqrt{P} = \sqrt{KR}$ = {answer}.
:::

::: misses
- [sqrt(R)] That's the sensor's own noise; the filter does better.
- [any] Not yet. P = KR after the update.
:::
:::

::: check
given: s = 0.2 | 1 | 5; n = 4 | 16 | 100
answer: s/sqrt(n)
tolerance: 0.5%

## A constant is measured {n} times with noise σ = {s}. What's the best estimate's standard deviation?

::: right
Right. $\sigma/\sqrt{n}$ = {answer}.
:::

::: misses
- [s/n] Variances divide by n; standard deviations by √n.
- [any] Not yet. Average the readings.
:::
:::

::: check
given: x = 10 | 50; p = 0.02 | 0.5; r = 0.1 | 1; d = -2 | 1
answer: x + p/(p + r)*d
tolerance: 0.1%

## A prior estimate {x} with variance {p} meets a measurement of {x} + ({d}) with noise variance {r}. What's the updated estimate?

::: right
Right. $K = p/(p + r)$, and $\hat{x} = \hat{x}^- + K(y - \hat{x}^-)$ = {answer}.
:::

::: misses
- [x + r/(p + r)*d] The gain is the prior's share of the total variance, $p/(p + r)$.
- [x + d] That trusts the measurement completely.
- [any] Not yet. $K = P^-/(P^- + R)$.
:::
:::

::: check
given: a = -1 | 0.5 | 2; W = 0.1 | 10; V = 0.01 | 1
answer: a + sqrt(a^2 + W/V)
tolerance: 0.5%

## A continuous-time state $\dot{x} = \given{a}x + w$ is measured as $y = x + v$, with noise intensities $R_w$ = {W}, $R_v$ = {V}. What's the steady-state Kalman gain?

::: right
Right. $L = a + \sqrt{a^2 + R_w/R_v}$ = {answer}, the dual of the scalar LQR gain.
:::

::: misses
- [sqrt(W/V)] Include the plant's own dynamics a.
- [any] Not yet. Solve $2aP - P^2/R_v + R_w = 0$, then $L = P/R_v$.
:::
:::

::: check
given: p = 0.04 | 0.3; r = 0.1 | 0.5
answer: (1 - p/(p + r))*p
tolerance: 0.5%

## A prior variance {p} meets a measurement with noise variance {r}. What's the variance after the update?

::: right
Right. $(1 - K)P^- = P^-R/(P^- + R)$ = {answer}: smaller than either.
:::

::: misses
- [p + r] Information adds, so the variance shrinks: $1/P = 1/P^- + 1/R$.
- [any] Not yet. $P = (1 - K)P^-$.
:::
:::

::: check
kind: parts
given: s = 0.2 | 0.5 | 1; t = 0.1 | 0.3 | 2

## Two independent sensors measure the same quantity, with σ = {s} and σ = {t}.

::: part
answer: 1/sqrt(1/s^2 + 1/t^2)
tolerance: 0.5%

### What is the standard deviation of the best combination?

::: misses
- [(s + t)/2] Inverse variances add.
- [any] $1/\sqrt{1/\sigma_1^2 + 1/\sigma_2^2}$.
:::
:::

::: part
answer: (1/s^2)/(1/s^2 + 1/t^2)
tolerance: 0.5%

### What weight does the first sensor get?

::: misses
- [any] Its inverse variance over the total.
:::
:::
:::

::: check
kind: derivation
vars: Q = 0.0001..10, R = 0.001..100

## A random walk with step variance Q is measured with noise variance R; the Kalman filter has reached steady state.

::: step
answer: (Q + sqrt(Q^2 + 4*Q*R))/2

### What is the prior variance $P^-$?
:::

::: step
answer: ((Q + sqrt(Q^2 + 4*Q*R))/2)/((Q + sqrt(Q^2 + 4*Q*R))/2 + R)

### What is the gain K?
:::

::: step
answer: ((Q + sqrt(Q^2 + 4*Q*R))/2)*R/((Q + sqrt(Q^2 + 4*Q*R))/2 + R)

### What is the posterior variance?
:::

::: right
Right. In steady state the prior is the posterior plus Q, which gives a quadratic for $P^-$.
:::
:::

::: explain
## Why does the Kalman gain fall when the measurements are noisier?

::: model
The gain sets how far each update moves the estimate toward the new measurement, and the filter chooses it to minimize the estimate's variance. A noisy measurement carries little information compared with the prediction, so the best compromise moves only a short way toward it; a precise measurement is worth trusting and gets a large gain. The formula $K = P^-/(P^- + R)$ says exactly this: K is the prediction's uncertainty as a share of the total, so larger measurement noise R shrinks it. In steady state this makes the filter average over more readings when the sensor is poor, at the cost of following real changes more slowly.
:::
:::

::: practice

::: check
given: Q = 0.0004 | 0.05; R = 0.04 | 2
answer: (Q + sqrt(Q^2 + 4*Q*R))/2/((Q + sqrt(Q^2 + 4*Q*R))/2 + R)
tolerance: 0.5%

## Steady-state K for Q = {Q}, R = {R}?

::: misses
- [any] $P^-/(P^- + R)$ with $P^- = (Q + \sqrt{Q^2 + 4QR})/2$.
:::
:::

::: check
given: x = 5 | 100; p = 0.1 | 2; r = 0.5 | 4; d = -3 | 2
answer: x + p/(p + r)*d
tolerance: 0.1%

## Update {x} (variance {p}) with a reading {d} higher (variance {r})?

::: misses
- [any] $\hat{x}^- + K(y - \hat{x}^-)$.
:::
:::

::: check
given: s = 0.3 | 3; n = 9 | 64
answer: s/sqrt(n)
tolerance: 0.5%

## σ of the average of {n} readings with σ = {s}?

::: misses
- [any] $\sigma/\sqrt{n}$.
:::
:::
:::
