What a Kalman Filter Is and Why It Works

Your guess says the kitchen is 70°F, give or take 2. A noisy sensor says 72°F, give or take 3. A Kalman filter answers 70.6°F, give or take 1.7: closer to the surer source, and sharper than either.

Pick a way to read the thermostat and drag the sensor noise. At minute 25 the heater turns on. Watch which tracker jitters, which one lags, and which one follows the room. Then we build the filter from zero: the guess, its doubt, the two steps and the one trust number.

You need averages and a little algebra. We build the rest from zero.

This is the gentle what and why. For the full derivation and the matrix form, go on to The Kalman Filter in Sensor Fusion and Kalman, EKF and UKF.

Read a noisy thermostat

±3.0°F

Illustrative toy: one reading a minute for 60 minutes. The room sits at 70°F, then the heater warms it to 74°F between minutes 25 and 33. The filter knows the heater's schedule: while it is on, predict adds 0.5°F a minute. Its Q is 0.05. Errors are root mean square.

Chapter 0

The Noisy Thermostat

See why the newest reading jitters, why a long average lags, and what a filter does instead

Your kitchen thermostat has a cheap sensor. The room sits at 70°F, but the readings bounce: 73, 68, 71, 66. Each reading is off by about 3°F. The heater controller sees every bounce and clicks on and off.

The first fix is to trust only the newest reading. It is up to date, but it jumps with every bit of noise. The second fix is to average the last N readings. The noise cancels, but the average describes the room as it was some minutes ago.

Pick an averaging window

Warm dots are readings. The yellow line averages the last N of them. Drag N and watch the heater step at minute 25.

10

Illustrative: the same toy room as the hero, sensor noise ±3°F.

Window NNoise left, 3 divided by the square root of NHow far back its centre sits
1 (newest only)±3.00°F0 minutes
4±1.50°F1.5 minutes
10±0.95°F4.5 minutes
20±0.67°F9.5 minutes

With these numbers, a 10-reading average is off by only ±0.95°F in a calm room. But its centre sits 4.5 readings in the past. When the heater turns on, it needs 10 readings to catch up.

No window is right for both jobs. A Kalman filter is a method that keeps a running guess plus a number for how sure it is. Each new reading moves the guess by an amount that depends on that number. It also uses a model: it knows the heater warms the room. So it can be smooth when the room is calm and quick when it changes.

The failure: a fixed window. Symptom: the heater overshoots, because the controller learns of the warm-up late. Measure: step the heater and count readings until the estimate is within 1°F of the room. Fix: weigh each reading by trust.

The road

  1. A guess and how sure you are: the belief, a bell with a centre and a width.
  2. Predict, then update: the two steps the filter runs every minute.
  3. Trust follows confidence: the surer source wins the tug of war.
  4. The gain: one trust number from 0 to 1.
  5. The doubt shrinks, then levels off: why the filter beats its own sensor.
  6. Why it beats averaging: the hero's Kalman line against its average line.
  7. Showcase: tune a live filter, heater and all.
  8. Where it came from, where it breaks: 1960, Apollo, and four ways to fool it.
  9. The field guide: numbers, code and where to go next.
You average the last 10 readings of a sensor that is off by about ±3°F. In a calm room, about how far off is the average?

Chapter 1

A Guess and How Sure You Are

Store a best guess together with its give-or-take, and draw the pair as a bell

Ask a friend how warm the kitchen is. "About 70, give or take 2" tells you more than "70". The second half says how far to trust the first.

A Kalman filter keeps that pair. Call it the belief: a best guess plus a spread. Call the best guess x, in °F. The filter stores the spread squared, called the variance, as P. Variances add and scale cleanly, so the arithmetic stays short.

The give-or-take you say aloud is the standard deviation, σ (sigma). It comes back from the variance with a square root.

Say it aloud

Sigma equals the square root of capital P.

σ
sigma the standard deviation: the give-or-take of the guess, in °F.
√
the square root of the number that, times itself, gives what follows.
P
capital P the variance of the guess, in °F squared.
In plain words

Take the variance P. Find the number that, times itself, gives P. That number is sigma, the give-or-take.

With numbers

With made-up numbers: P = 4°F², so σ = √4 = 2°F. The belief "70, give or take 2" stores x = 70 and P = 4.

Draw the belief as a bell curve. The peak sits at the guess x. The width is σ. If the belief is honest, the true temperature lies within one σ of the guess about 68 times in 100.

Shape your belief

The teal bell is a belief. The shaded band is plus or minus one sigma. Drag the guess and the spread.

70.0°F
±2.0°F

The 68-in-100 rule holds for a bell-shaped (Gaussian) belief.

A narrow bell says "I am sure". A wide bell says "I could be far off". Everything the filter does moves this bell or changes its width.

The failure: a belief with no spread. Symptom: the code cannot tell a wild guess from a measured fact, so it cannot weigh a new reading. Measure: ask what would make it change its mind; nothing can. Fix: store P next to x from the first line.

A belief stores P = 9°F². What is its give-or-take?

Chapter 2

Two Steps: Predict, Then Update

Widen the belief for the minute you did not watch, then pull it toward the new reading

Every minute the filter runs two steps, always in this order. Predict moves the belief forward one minute with a model of the room. Update blends in the new reading.

Our model of the room is plain. With the heater off, the temperature stays where it was. With it on, the room warms about 0.5°F a minute, so predict adds that to the guess x. But rooms drift. A door opens, the sun moves. So predict also widens the bell.

Call the variance the room can drift in one minute the process noise, Q. Call the variance after predict P̄, "P-bar". The bar means "before the reading".

Say it aloud

Capital P-bar equals capital P plus Q.

P̄
capital P-bar the variance after predict, before the reading, in °F².
P
capital P the variance after the last update.
Q
Q the process noise: how far the room can drift in one minute, as a variance in °F².
In plain words

Take last minute's variance P. Add Q for the minute you did not watch. The sum, P-bar, is never smaller than P.

With numbers

With made-up numbers: P = 3°F² and Q = 1°F². P̄ = 3 + 1 = 4°F², so the give-or-take grows from ±1.73°F to ±2°F.

Then the reading arrives. Update pulls the bell toward it and narrows it. Chapters 3 to 5 show by how much. Try both steps here.

Turn the two steps

Blue is the belief after predict. Teal is the belief after update. The warm mark is the reading. Press the steps in any order.

1.0

Illustrative: the belief starts at 70°F with P = 3. The sensor is ±3°F (R = 9). The first reading is 72°F; we made up later readings around 70°F.

Predict alone only widens. Update alone only narrows. Run them in turn and the width settles. Chapter 5 finds where.

The failure: skip predict. Symptom: P only shrinks, so after an hour the filter trusts itself and ignores the heater. Measure: P falls toward zero while the estimate stops following the room. Fix: add Q every minute, even when the model says "no change".

Start with P = 3 and Q = 1. No reading arrives for three minutes, so predict runs three times. What is P-bar now?

Chapter 3

Trust Follows Confidence

Let the surer of two sources pull harder, and see that the blend is sharper than both

Your belief says 70°F. The sensor says 80°F. Where should the new guess go? It depends on who is surer.

Case one: you are sure (±1°F) and the sensor is noisy (±5°F). The 80 is probably noise, so you barely move. Case two: you are unsure (±5°F), the sensor is sharp (±1°F), and it reads 75°F. Now you move almost all the way.

Weigh two opinions

Blue is your belief at 70°F. Warm is the reading. Teal is the blend. Pick a case, then drag the spreads.

±1.0°F
80.0°F
±5.0°F

The blend uses the gain of Chapter 4. We made up all the numbers.

BeliefReadingNew guessNew spread
70 ±1°F80 ±5°F70.38°F±0.98°F
70 ±5°F75 ±1°F74.81°F±0.98°F
70 ±2°F72 ±3°F70.62°F±1.66°F

Look at the new spreads. In both first cases the blend is ±0.98°F, a little sharper than the sharper source. Two honest opinions together beat the better one alone.

The rule behind the table is a weighted average: each source votes, and its vote counts in proportion to a weight. A sure source has a small variance, so it gets a big weight. Chapter 4 turns the two weights into one number.

The failure: a sensor spec that is too kind. Symptom: you tell the filter the sensor is ±0.5°F when it is ±3°F, and the estimate jumps with every reading. Measure: log the sensor in a steady room; the readings scatter far wider than claimed. Fix: use the measured spread.

Your belief is 70 ±5°F. A sharp sensor reads 75 ±1°F. About where does the new guess land?

Chapter 4

The Gain: One Trust Number

Turn two doubts into one slider from 0 to 1, and move the guess that share of the way

The Kalman gain, K, is a trust slider from 0 to 1. At 0 the filter ignores the reading. At 1 it jumps to the reading. Between, it moves part of the way.

Name the parts first. The predicted guess is x̄, "x-bar", with variance P̄. The reading is z. The variance of the sensor's error is R, the measurement noise.

Say it aloud

K equals capital P-bar divided by the sum of capital P-bar and R.

K
K the Kalman gain: the share of the gap the guess moves, from 0 to 1.
P̄
capital P-bar the doubt in the prediction, as a variance in °F².
( … + … )
the sum of the total doubt on both sides, prediction plus reading.
R
R the doubt in the reading: the sensor's variance in °F².
In plain words

Add the prediction's doubt P-bar to the reading's doubt R. K is the prediction's share of that total. The more you doubt the prediction, the more you trust the reading.

With numbers

With made-up numbers: P̄ = 4 (±2°F) and R = 9 (±3°F). K = 4/(4 + 9) = 4/13 = 0.31.

Then move the guess by that share of the gap between the reading and the prediction.

Say it aloud

x equals x-bar plus K times the quantity z minus x-bar.

x
x the new guess after the update, in °F.
x̄
x-bar the predicted guess, before the reading.
K
K the gain from the line above.
z − x̄
z minus x-bar the surprise: how far the reading z lands from the prediction.
In plain words

Find the gap between the reading z and the prediction x-bar. Multiply the gap by K. Add the result to x-bar. That is the new guess x.

With numbers

x̄ = 70°F, z = 72°F, K = 0.31. The gap is 2°F. x = 70 + 0.31 × 2 = 70.62°F: the hero's 70.6.

Slide the trust

The ruler runs from the prediction (70°F) to the reading (72°F). The purple marker is the new guess. Drag the two doubts.

4.0
9.0

Both doubts are variances in °F². Made-up numbers.

Check the ends. If R is huge, a broken sensor, K goes to 0 and the filter ignores the reading. If P̄ is huge, you know nothing, so K goes to 1 and you take the reading as it is. Nobody sets K by hand: the filter recomputes it from P̄ and R every minute.

The failure: R as a spread, not a variance. Symptom: a ±3°F sensor entered as R = 3 gets too much trust. Measure: print K; it sits higher than P̄/(P̄ + 9). Fix: square the spread, so 3°F gives R = 9.

The prediction and the reading are equally doubtful: P-bar = 9 and R = 9. What is K?

Chapter 5

The Doubt Shrinks, Then Levels Off

Watch each update cut the doubt, and find the floor where predict puts back what update removes

After an update the filter is surer than before. How much surer? The update keeps only the share of the doubt that the reading did not remove.

Say it aloud

Capital P equals one minus K, times capital P-bar.

P
capital P the variance after the update, in °F².
(1 − K)
one minus K the share of the prediction's doubt that stays.
P̄
capital P-bar the variance before the update.
In plain words

Take one minus the gain K. Multiply the prediction's variance P-bar by it. The result is the new variance P.

With numbers

From Chapter 4: K = 4/13 = 0.31 and P̄ = 4. P = (9/13) × 4 = 2.77°F². The give-or-take drops from ±2°F to ±1.66°F.

Look at what is missing: the reading z. How far the doubt shrinks depends only on P̄ and R. A surprising reading moves the guess more, but it does not change the width.

Now start knowing nothing, ±10°F, with a ±3°F sensor and Q = 0. One reading drops the doubt to ±2.87°F. Then ±2.08, ±1.71 and ±1.48. After 8 readings it is ±1.05°F, about three times sharper than the sensor.

Watch the doubt shrink

Each teal bar is the give-or-take after one update. The thin blue tick above it is the give-or-take after predict. Drag Q and the sensor spread.

0.25
±3.0°F

Illustrative: the belief starts at ±10°F (P = 100). The floor solves "predict adds what update removes".

With drift, Q puts doubt back every minute, so the shrinking stops at a floor. With Q = 0.25 and R = 9 the floor is ±1.17°F after each update, and K settles at 0.15. Predict adds back what update removes.

The failure: a floor set too low by a tiny Q. Symptom: the estimate trails the heater for many minutes. Measure: after a change, the truth stays outside the ±σ band for many readings in a row. Fix: raise Q until the band holds the truth about 2 times in 3.

Two filters have the same P-bar and R. One gets a reading right on its guess; the other gets a reading 6°F away. After the update, how do their variances P compare?

Chapter 6

Why It Beats Averaging

Find the plain average hiding inside the filter, then let Q make it forget at the right rate

Here is a surprise. Set Q = 0 and start knowing nothing. Then the Kalman filter is the plain average of every reading so far.

With no prior and no drift, the first reading gets full trust, the second half, the third a third. Call the count of readings so far n, and the gain at reading n Kn.

Say it aloud

K-n equals one over n.

Kn
K-n the gain at reading number n.
1/
one over one share out of n equal shares.
n
n how many readings have arrived: 1, 2, 3 and so on.
In plain words

At reading number n, move the guess one n-th of the way toward the new reading. That gives every reading so far the same weight.

With numbers

With made-up readings 71, 68 and 73°F. K1 = 1: guess 71. K2 = 1/2: 71 + (68 − 71)/2 = 69.5. K3 = 1/3: 69.5 + (73 − 69.5)/3 = 70.67, the average of all three.

So the plain average is the right filter for a room that never changes. Its gain shrinks toward 0, so it stops listening. When the heater turns on, it barely moves.

Q fixes this. It keeps P̄ above a floor, so K stays above a floor too. The filter forgets old readings at a steady rate, and Q and R set that rate.

Forget at the right rate

Yellow averages every reading so far. Teal is the filter. Set Q to 0 and the two lines become one. Then raise Q.

0.25

Illustrative: the hero's room, sensor ±3°F, the filter starts knowing nothing.

The hero's filter has one more edge: it knows when the heater is on, so predict raises the guess before the readings do. No average can use that knowledge.

With Q = 0.25 and R = 9, K settles at 0.15. Each step keeps 0.85 of the old guess, so a reading 10 minutes old keeps 0.8510, about a fifth of its first weight.

The failure: a filter that stops listening. Symptom: after a long calm spell, it ignores a real change. Measure: K drifts toward 0 in the log. Fix: give Q a value above 0.

Q = 0 and the filter starts knowing nothing. What is the gain at the fourth reading?

Chapter 7

Showcase: Tune a Live Thermostat Filter

Run the filter live, switch the heater, and break it with wrong numbers

Now run it live. Readings stream in about eight a second. Switch the heater on and off. Change how noisy the sensor is, and what the filter believes about it.

Tune the filter

Dashed is the true room. Warm dots are readings. Teal is the estimate with its ±σ band. Press Run, then switch the heater.

±2.0°F
±2.0°F
0.056

Illustrative toy. The room drifts about 0.2°F per reading. The heater warms the room 0.2°F per reading toward 74°F; off, it cools back to 70°F. "Inside the band" counts the last 60 readings.

Try four experiments.

  1. Set the filter's belief far below the real noise. The estimate jitters, and its thin band misses the truth.
  2. Set Q to its lowest, then switch the heater on. The estimate lags for many readings.
  3. Set Q to its highest. The estimate follows fast but shakes with the noise.
  4. Set Q low again and press Filter knows the heater. Predict now moves the guess with the heater, and the lag goes away.

The honest band test: if the filter's numbers are honest, the truth sits inside the ±σ band about 68 times in 100. Far fewer means the filter is overconfident: it claims more sureness than it has. Far more means it is underconfident.

With these numbers: real noise ±2°F, belief ±2°F and Q = 0.056. The band settles near ±0.7°F and holds the truth in a calm room. Drop the belief to ±0.5°F and the band thins while the error grows.

The failure: an overconfident filter. Symptom: a thin band that the truth keeps leaving. Measure: the inside-the-band count drops well below 68 in 100. Fix: raise R to the sensor's real spread, or raise Q.

Over the last 60 readings, the truth sat inside the filter's ±σ band only 25 times. What does that say?

Chapter 8

Where It Came From, Where It Breaks

Trace the filter from a 1960 paper to Apollo, and fool it in four ways

Rudolf Kalman published the filter in 1960, in "A New Approach to Linear Filtering and Prediction Problems". In the early 1960s, Stanley Schmidt's group at NASA Ames tried it in studies of Apollo navigation. Spacecraft paths curve, so they extended it to curved models: the extended Kalman filter. NASA credits the method with helping Apollo reach the Moon.

The filter's promise has a fine print. With a straight-line model and bell-shaped noise, no method has a smaller average squared error. With other noise, it is still the best method that only weighs and adds. The device breaks the fine print one line at a time.

Break one assumption

The same filter meets four rooms. Pick one and read the symptom.

Illustrative: sensor ±2°F (R = 4), filter Q = 0.05. The glitch adds 15°F to one reading. "Reads high" adds 2°F to every reading.

What breaksSymptomFix
A wild readingthe guess jumps by K times the glitchskip a reading whose surprise is far beyond its expected size (a gate)
A sensor that reads higha tight band around the wrong valuecalibrate, or put the offset in the belief
A change faster than Qthe guess lagsraise Q, or tell predict the heater is on
A curved model or sensorthe straight-line rules give wrong bellsthe extended or unscented filter
A belief with two humpsone bell cannot hold ita particle filter

With these numbers, the glitch is 15°F and K is about 0.11, so the guess jumps about 1.6°F before it drifts back. Phones use it to fuse GPS with motion sensors. The US aviation agency tracks aircraft with a version of it. Robots use it to know where they are.

The failure: trusting the band. Symptom: the "reads high" room shows a calm, tight band 2°F off. Measure: compare with a second thermometer. Fix: calibrate the sensor.

Every reading is 2°F too high, and the filter does not know. What does the filter show after many readings?

Chapter 9

The Field Guide

Keep the steps, the numbers, the checks and the code of the filter on one page

A Kalman filter keeps a guess x and its variance P. Predict adds Q for the minute it did not watch. The gain K is the prediction's share of the total doubt. Update moves the guess K of the way to the reading and keeps 1 − K of the doubt. Honest Q and R make it smooth and quick at once.

The numbers

StepRuleWorked numbers
Give-or-takeσ = √P√4 = 2°F
Predict the doubtP̄ = P + Q3 + 1 = 4°F²
GainK = P̄/(P̄ + R)4/13 = 0.31
Update the guessx = x̄ + K(z − x̄)70 + 0.31 × 2 = 70.62°F
Update the doubtP = (1 − K)P̄(9/13) × 4 = 2.77°F²
No drift, no priorKn = 1/nthe running average
Floor, Q 0.25, R 9predict adds what update removes±1.17°F, K = 0.15

The terms

TermMeaning
BeliefA best guess x plus its variance P.
Variance PThe give-or-take squared.
PredictMove the belief forward one step and add Q.
UpdateBlend in a reading with the gain K.
Process noise QHow far the world can stray from the model in one step.
Measurement noise RThe variance of the sensor's error.
Kalman gain KThe trust slider: the share of the surprise the guess moves.
OverconfidentThe band claims more sureness than the errors show.

The checklist

  1. Store a variance next to every guess.
  2. Measure R from the sensor in a steady room, as a variance.
  3. Start with a wide P; the first readings fix it fast.
  4. Give Q a value above 0.
  5. Check that the truth sits inside ±σ about 2 times in 3.
  6. Skip readings with impossible surprises.

The core as code

pythondef kalman_step(x, P, z, Q, R):
    P = P + Q                  # predict: the room may drift
    K = P / (P + R)            # gain: the trust slider
    x = x + K * (z - x)        # update the guess
    P = (1 - K) * P            # update the doubt
    return x, P, K

kalman_step(70, 3, 72, Q=1, R=9)   # (70.62, 2.77, 0.31)

x, P = 70.0, 100.0                # start knowing little
for z in readings:                  # one float per minute
    x, P, K = kalman_step(x, P, z, Q=0.25, R=9)

Keep going

The takeaway. A Kalman filter is a weighted average that never stops: each minute it weighs its own prediction against a new reading by how sure each one is. You set Q and R; the filter sets K.
Which sentence describes a Kalman filter?

Now press Present or Teach. Run one minute from memory: 70°F with P = 3, Q = 1, a reading of 72°F with R = 9. Then go back to the thermostat, pick Average of 10 and then Kalman filter, and say why one lags and the other does not.

Sources

  1. R. E. Kalman, "A New Approach to Linear Filtering and Prediction Problems," Journal of Basic Engineering 82(1), 35–45, 1960. doi
  2. L. A. McGee and S. F. Schmidt, "Discovery of the Kalman Filter as a Practical Tool for Aerospace and Industry," NASA TM-86847, Ames Research Center, 1985. NTRS
  3. NASA Ames, "Math Invented for Moon Landing Helps Your Flight Arrive on Time." nasa.gov
  4. G. Welch and G. Bishop, "An Introduction to the Kalman Filter," TR 95-041, University of North Carolina at Chapel Hill. UNC
What a Kalman Filter Is and Why It Works
Next: The Kalman Filter, in depth · Back to Gleams