Separating truth from noise in a noisy world.
Your kitchen has a temperature sensor, but it's broken. Every time you read it, the number bounces around wildly—sometimes it says 68°F, sometimes 72°F, sometimes 70°F—even though the room's real temperature barely changes. You want to know the actual temperature so you can control the heater, but the noise makes it hard to trust any single reading.
You could average the last few readings, but that's slow and lags behind real changes. You could trust the newest reading, but then you're at the mercy of random noise. What you really need is a way to combine what you knew before with what you're seeing now—a method that weighs old knowledge and new evidence fairly, and improves its own guess as it runs.
Enter the Kalman filter: a clever algorithm that solves exactly this problem. It's been used to track rockets, guide aircraft, estimate stock prices, and smooth GPS locations. By the end of this lesson, you'll understand what it does and why it works so well.
At its core, a Kalman filter doesn't track just a number—it tracks a belief: your best guess about the true state, plus a measure of how confident you are in that guess.
Think of it this way: right now, you believe the kitchen is 70°F, and you're pretty sure (say, ±2°F of confidence). That's your belief. Then a new sensor reading arrives: it says 72°F, but you know the sensor is noisy (roughly ±3°F).
The Kalman filter asks: should I move my belief toward 72°F or stick with 70°F? The answer depends on whether you trust the old belief or the new reading more. Since your belief is more confident (±2°F) than the sensor (±3°F), you should barely budge—maybe shift to 70.4°F. Next reading might be different, so you'll adjust again.
A Kalman filter has two alternating steps, like gears meshing: predict and update. Every time step, you run both.
Predict: Before the new measurement arrives, you ask, "What do I expect the state to be?" If your heater is off, the temperature might drop slightly. If it's been steady, it probably stays steady. You use a simple model (or physics) to adjust your belief forward in time—and your confidence usually gets worse (you're less sure the further you predict).
Update: Now the new measurement comes in. You compare what you predicted to what you observed. If they match, great—your confidence goes up. If they don't match, you adjust your belief partway toward the measurement, and your confidence might go up or down depending on how much the measurement surprised you.
The secret power of the Kalman filter lies in how it uses confidence (called uncertainty or variance) to decide what to trust. It's not a hand-waving heuristic—it's math.
Imagine your belief is 70°F ± 1°F (very confident) and a sensor reads 80°F ± 5°F (very noisy). The filter asks: "How much should I move toward 80?" Because the sensor is so uncertain, the answer is: almost not at all. You might shift to 70.2°F.
Now flip it: your belief is 70°F ± 5°F (uncertain) and the sensor reads 75°F ± 1°F (precise). The filter asks: "This sensor is very confident. How much should I move?" Answer: a lot. You might shift to 74.5°F.
Here's the beautiful part: the Kalman filter computes the right balance automatically. At each update step, it calculates a Kalman gain—a single number that tells you what fraction of the measurement error should adjust your belief.
The gain depends on two things: your uncertainty (how sure are you right now?) and the sensor noise (how sure is the sensor?). The formula is intuitive:
Gain = Your Uncertainty / (Your Uncertainty + Sensor Noise)
If you're very uncertain and the sensor is precise, gain ≈ 1.0 (move all the way toward the measurement). If you're very certain and the sensor is noisy, gain ≈ 0.0 (barely move). The filter balances them automatically, without you having to tweak anything by hand.
One of the deepest insights of the Kalman filter: your uncertainty gets better over time, as long as the sensor is better than guessing. Every time you get a new measurement, you learn a little more, and your confidence improves.
At first, you might not trust your belief at all—say, ±10°F. You get a measurement, you update, and now your uncertainty shrinks to ±9.5°F. Another measurement: ±9.1°F. You keep learning. After many measurements, your uncertainty might drop to ±0.5°F—much better than the sensor alone.
But here's the key: uncertainty shrinks only in the update step, when new data arrives. In the predict step, uncertainty usually grows (because you're extending your knowledge into the future). The filter is honest: predicting ahead always introduces doubt.
You might ask: couldn't I just average the last few measurements? Simple averaging is quick, but it has blind spots. It treats old and new data equally, even though the true state might be changing. It also lags—you have to wait for several measurements before you can react.
The Kalman filter is smarter. It uses your model to predict what should happen next. If the temperature was steady at 70°F and suddenly a measurement says 65°F, the filter asks: "Did the real temperature drop, or is this sensor noise?" It uses the model's prediction to decide. If the model says it should still be ~70°F, the filter barely moves. If the next three readings also say 65°F, the filter learns the temperature really did drop and adjusts.
The Kalman filter also adapts its own confidence in real time. Averaging doesn't. Once you lock in an averaging window, you're locked in. The filter keeps learning and getting better, even after months of running.
The Kalman filter was invented in 1960 by Rudolf Kalman to solve navigation problems. NASA used it for the Apollo moon landings—filtering the spacecraft's position from dozens of noisy radar and accelerometer readings, fusing them into a clean trajectory estimate.
Today it's everywhere. GPS receivers use Kalman filters to smooth jittery position fixes. Self-driving cars use them to track pedestrians and other vehicles even when vision is momentarily blocked. Stock traders use variants to forecast prices. Insulin pumps use them to estimate blood glucose from noisy sensors. Drones estimate altitude, attitude, and velocity—all with Kalman filters.
The reason is simple: whenever you have noisy measurements and a model of how things change, and you want a real-time estimate of the current state, the Kalman filter is near-optimal. The math proves it (under certain conditions: linear systems, Gaussian noise). And even when those assumptions break, variants of the filter—extended Kalman filters, unscented filters, particle filters—work beautifully in the real world.