TactivergeLite

Lite / Independent Engineering Research / Week 17

The Kalman gain as the variance-minimising weight

Why P/(P + R)? Not a guess, not a rule of thumb: it is the only weight that makes the error as small as possible.

About 45 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

The gain is the filter's only decision: how much to believe the newest measurement. Deriving it yourself shows that the Kalman filter is just inverse-variance weighting applied again and again, with the prediction as one of the two sensors.

Predict

The prediction has variance P⁻ = 3 and the measurement R = 1. The best gain K is…

  1. 0.25
  2. 0.75
  3. 0.5
  4. 1
Guess first, then reveal

K = 3/(3 + 1) = 0.75: the measurement is three times more precise than the prediction, so it gets three-quarters of the say.

Discover

Set R small (0.1) and step: K stays near 1, and the estimate jumps to every fix. Set R large (5): K is small and the estimate glides. Then fix R = 1 and step 15 times: watch K settle to a constant. That constant is the steady-state gain.

What the interactive shows (text version)

The derivation is fully written in text. The stepper's readout reports the gain at every update, so the convergence to a steady gain can be followed as numbers.

You can also open the interactive on the full site when you have a faster connection.

Watch (optional)

Watch for: Match each equation on screen to predict or update, and spot the gain formula in its matrix form.

Walks through the predict and update equations of the Kalman filter one line at a time, including what the gain and covariances mean.

“Optimal State Estimator Algorithm | Understanding Kalman Filters, Part 4” by MATLAB (opens YouTube; streaming video uses a lot of data)

The video is optional. Everything you need is in this lesson's text.

Understand

Reading the gain

K → 1 when R ≪ P⁻: trust the sensor, x ≈ z. K → 0 when R ≫ P⁻: trust the prediction, ignore the reading. K is not a tuning knob you set; it is computed from the variances you believe. What you tune are Q and R.

Steady state

With constant Q and R the variance stops changing. For the random-walk model x[k+1] = x[k] + u + w, the prior variance converges to P⁻∞ = (Q + √(Q² + 4QR))/2. With Q = 0.3, R = 2: P⁻∞ ≈ 0.939, K∞ ≈ 0.319. Many embedded filters precompute this constant gain.

In more dimensions

For vector states, K = P⁻Hᵀ(HP⁻Hᵀ + R)⁻¹ and P = (I − KH)P⁻, where H maps the state to what the sensor sees. The scalar case is H = 1. The same minimum-variance argument, done with matrices, gives the same structure.

Explain it another way

Intuitive

The gain is a trust dial between 'my model says' and 'my sensor says', set automatically from how unsure each one is right now.

Technical

For linear-Gaussian systems, the Kalman filter is the exact Bayesian posterior and the minimum-mean-square-error estimator. For non-Gaussian noise it is still the best LINEAR unbiased estimator.

Mathematical

Q = R = 1: P⁻∞ = (1 + √5)/2 ≈ 1.618 (the golden ratio) and K∞ = P⁻∞/(P⁻∞ + 1) ≈ 0.618.

Applied

Apollo's onboard navigation used a Kalman filter to fold in optical sightings of stars and landmarks; each sighting's gain depended on how uncertain the state had become since the last one.

Derivation: The scalar Kalman gain
  1. Update rule: x = x⁻ + K(z − x⁻) = (1 − K)x⁻ + Kz. Prediction error e⁻ has variance P⁻; measurement error v has variance R; they are independent.

  2. Posterior error e = (1 − K)e⁻ + K·v (the true x cancels because both terms are unbiased).

  3. Its variance: P(K) = (1 − K)²P⁻ + K²R.

  4. Minimise: dP/dK = −2(1 − K)P⁻ + 2KR = 0 ⇒ K = P⁻/(P⁻ + R).

  5. Substitute back: P = (1 − K)P⁻ = P⁻R/(P⁻ + R), i.e. 1/P = 1/P⁻ + 1/R: inverse-variance weighting, again.

Worked example 1

Q = 0.3, R = 2. Find the steady-state prior variance, gain and posterior variance.

P⁻∞ = (0.3 + √(0.09 + 2.4))/2 ≈ 0.939. K∞ = 0.939/2.939 ≈ 0.319. P∞ = (1 − 0.319) × 0.939 ≈ 0.639.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

Practice 3. A filter is told R = 0 (a perfect sensor). What does it do at each update?

m

m²

Apply it

In the stepper, measure the gain after 15 steps for Q = 0.2 and three values of R (0.5, 1, 2). Compare with the steady-state formula. Then explain, in one sentence each, what physically changing Q and R would mean for a rover.

Reflect

If you could improve either the camera (R) or the wheels (Q) by a factor of two, which would help the estimate more in steady state? Try both in the formula.

Write your answer on paper or in your own notes. Lite never stores what you write.

Go further

Research mode: limits and open questions

Comparing a filter's gain sequence with its innovation sequence is a standard diagnostic: in a well-tuned filter, innovations are white (uncorrelated in time) with variance HP⁻Hᵀ + R.

← Escape speed and Hohmann transfersTuning a two-state filter: position and bias →