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.
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.
- Derive the scalar Kalman gain by minimising the posterior error variance.
- Interpret K at its limits and as the steady-state gain.
- Compute the steady-state variance for a random-walk model.
Predict
The prediction has variance P⁻ = 3 and the measurement R = 1. The best gain K is…
- 0.25
- 0.75
- 0.5
- 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.
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
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.
Posterior error e = (1 − K)e⁻ + K·v (the true x cancels because both terms are unbiased).
Its variance: P(K) = (1 − K)²P⁻ + K²R.
Minimise: dP/dK = −2(1 − K)P⁻ + 2KR = 0 ⇒ K = P⁻/(P⁻ + R).
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
K is a gain you tune by hand.
✓ K is computed from P⁻ and R. You tune the noise models Q and R; the gain follows.The Kalman filter is a different idea from averaging.
✓ Its update is inverse-variance weighting of the prediction and the measurement.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
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.