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.
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
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.
Warm dots are readings. The yellow line averages the last N of them. Drag N and watch the heater step at minute 25.
Illustrative: the same toy room as the hero, sensor noise ±3°F.
| Window N | Noise left, 3 divided by the square root of N | How far back its centre sits |
|---|---|---|
| 1 (newest only) | ±3.00°F | 0 minutes |
| 4 | ±1.50°F | 1.5 minutes |
| 10 | ±0.95°F | 4.5 minutes |
| 20 | ±0.67°F | 9.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.
Chapter 1
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.
Sigma equals the square root of capital P.
Take the variance P. Find the number that, times itself, gives P. That number is sigma, the give-or-take.
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.
The teal bell is a belief. The shaded band is plus or minus one sigma. Drag the guess and the spread.
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.
Chapter 2
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".
Capital P-bar equals capital P plus Q.
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 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.
Blue is the belief after predict. Teal is the belief after update. The warm mark is the reading. Press the steps in any order.
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".
Chapter 3
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.
Blue is your belief at 70°F. Warm is the reading. Teal is the blend. Pick a case, then drag the spreads.
The blend uses the gain of Chapter 4. We made up all the numbers.
| Belief | Reading | New guess | New spread |
|---|---|---|---|
| 70 ±1°F | 80 ±5°F | 70.38°F | ±0.98°F |
| 70 ±5°F | 75 ±1°F | 74.81°F | ±0.98°F |
| 70 ±2°F | 72 ±3°F | 70.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.
Chapter 4
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.
K equals capital P-bar divided by the sum of capital P-bar and R.
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 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.
x equals x-bar plus K times the quantity z minus x-bar.
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.
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.
The ruler runs from the prediction (70°F) to the reading (72°F). The purple marker is the new guess. Drag the two doubts.
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.
Chapter 5
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.
Capital P equals one minus K, times capital P-bar.
Take one minus the gain K. Multiply the prediction's variance P-bar by it. The result is the new variance P.
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.
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.
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.
Chapter 6
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.
K-n equals one over n.
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 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.
Yellow averages every reading so far. Teal is the filter. Set Q to 0 and the two lines become one. Then raise Q.
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.
Chapter 7
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.
Dashed is the true room. Warm dots are readings. Teal is the estimate with its ±σ band. Press Run, then switch the heater.
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.
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.
Chapter 8
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.
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 breaks | Symptom | Fix |
|---|---|---|
| A wild reading | the guess jumps by K times the glitch | skip a reading whose surprise is far beyond its expected size (a gate) |
| A sensor that reads high | a tight band around the wrong value | calibrate, or put the offset in the belief |
| A change faster than Q | the guess lags | raise Q, or tell predict the heater is on |
| A curved model or sensor | the straight-line rules give wrong bells | the extended or unscented filter |
| A belief with two humps | one bell cannot hold it | a 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.
Chapter 9
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.
| Step | Rule | Worked numbers |
|---|---|---|
| Give-or-take | σ = √P | √4 = 2°F |
| Predict the doubt | P̄ = P + Q | 3 + 1 = 4°F² |
| Gain | K = P̄/(P̄ + R) | 4/13 = 0.31 |
| Update the guess | x = x̄ + K(z − x̄) | 70 + 0.31 × 2 = 70.62°F |
| Update the doubt | P = (1 − K)P̄ | (9/13) × 4 = 2.77°F² |
| No drift, no prior | Kn = 1/n | the running average |
| Floor, Q 0.25, R 9 | predict adds what update removes | ±1.17°F, K = 0.15 |
| Term | Meaning |
|---|---|
| Belief | A best guess x plus its variance P. |
| Variance P | The give-or-take squared. |
| Predict | Move the belief forward one step and add Q. |
| Update | Blend in a reading with the gain K. |
| Process noise Q | How far the world can stray from the model in one step. |
| Measurement noise R | The variance of the sensor's error. |
| Kalman gain K | The trust slider: the share of the surprise the guess moves. |
| Overconfident | The band claims more sureness than the errors show. |
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)
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.