Ch.25: Algorithms: How the Kalman Filter Tracks Through Noise

Outline

Transcript

0:00 A Kalman filter combines a prediction with a noisy measurement to track something we can't observe perfectly. We'll follow 1 position estimate through the calculation, including a missing measurement. Does the estimate have to freeze when the sensor goes quiet? No. Prediction can continue without a reading. Imagine an engineer tracking a cart along a straight rail. Wrong position readings make it appear to jump backward. My instinct is to average the readings. That should quiet the wobble. But the cart is moving; wouldn't an average also lag behind it?

0:31 It can. The filter predicts the movement before the next reading arrives, then uses that reading to correct the prediction. It carries uncertainty through both operations. We'll use MathWorks' documented equations, with source links in the description. Let's give the program numbers we can check. We need two things in memory: where we think the cart is, and how uncertain that estimate is. A guess alone doesn't tell us what to do with the next reading. Before combining those sources, take a concrete starting estimate: 0 meters. The program stores that guess; the cart's true position is unknown.

1:07 Alongside 0, store an uncertainty number of 2. This number is called variance. It measures the average squared estimation error we'd expect over comparable situations. For example, a 2-meter error contributes 4 square meters, whether we guessed ahead of the cart or behind it. Squaring makes both directions count positively and gives larger mistakes more weight. Averaging those squared errors gives a measure in square meters. So variance 2 cannot promise the cart is within 2 meters of our estimate.

1:37 It's a model of expected error, not a fence around the cart. Right. We'll assume errors aren't consistently pulling us in one direction. The technical word is unbiased: positive and negative errors average to 0. The movement input says the cart advances 10 meters each tick. Real movement can differ, so we assign that movement error a variance of 2 square meters. And the position sensor gets error variance 4. Larger variance means more expected squared error. We'll treat the movement error, measurement error and error in our starting estimate as independent.

2:10 Knowing one error doesn't tell us which way another went. For now the stored pair is position 0, variance 2. Let's see what happens to both when the cart moves. Now take the first movement tick. Add the known 10-meter movement to our stored 0: predicted position 10 meters. We haven't received another sensor reading, so why would our stored uncertainty number change? Because movement isn't exact. We carried variance 2 into this tick. Movement adds another independent 2, making 4. When we square the combined error, there's also a part involving the product of the two errors.

2:45 Why doesn't that change the average? Suppose our starting error were 2 meters. Independence means the next movement's error still averages 0 in those cases. Twice an average of 0 is 0. And a negative starting error times that 0 average still gives 0. So the product adds no average cost. Only the two variances remain. If we advance the position but leave uncertainty untouched, the program acts as though the movement were exact. We haven't earned that confidence. This addition uses our simple position model.

3:16 Other motion models can transform the old uncertainty before adding new movement error. Okay: prediction at 10 meters, variance 4. Now the sensor reports 12. We can compare its uncertainty with the prediction's. Next consider the first correction: compare 12 with our predicted 10. Their disagreement is 2 meters, but neither source comes with perfect information. Prediction and measurement both have variance 4. With equally uncertain independent estimates, give each half the weight. Half of the 2-meter disagreement is 1 meter.

3:47 Add that to the predicted 10, and our corrected position is 11 meters. The correction weight has a name: the Kalman gain. Here it's 1/2. It tells us how much of the measurement's disagreement to add. Ah, so a gain of 1 would replace the prediction with the reading. A gain of 0 would leave the prediction alone. Yes. For this 1-position example, the gain stays between those extremes because both error variances are positive. But we've still only changed the position. If the stored variance stays at 4, aren't we throwing away the benefit of combining two independent sources?

4:22 We are. The correction must update uncertainty too. Under this model, combining those equally uncertain sources reduces the expected squared error. Let's calculate that reduction before we carry 11 into another tick. Because that correction mixes two errors, the new error contains half from the prediction and half from the measurement. Those weights act before we square them. Halving an error quarters its square. Each variance of 4 therefore contributes a quarter of 4: 1. Add the two contributions: corrected variance 2. We leave the first cycle with position 11 meters and variance 2.

4:57 Both values changed. The independent errors' product averages to 0 again. That's why only the two squared contributions remain. Wait, does that mean 11 must be closer to the true position than the raw reading of 12? No. On one reading, the sensor might be exactly right. Our combination reduces expected squared error under the model; it doesn't win every individual comparison. Right. We choose the weight without seeing the true position. What happens if the sensor gets much noisier? Then we should follow it less.

5:27 We can change its variance while keeping the same prediction and the same reading. Now vary that equal-weight calculation. Call the predicted variance P and the measurement variance R. The Kalman gain is P divided by the sum of P and R. P is our uncertainty, yet it's on top. Why does being less sure of ourselves give the sensor more weight? Keep sensor uncertainty fixed. A larger P makes our prediction less trustworthy relative to that sensor, so the gain gives its reading a larger share. Right: when both variances are 4, their sum is 8.

6:02 Dividing 4 by 8 gives gain 1/2. Let's say the first prediction stays at 10, with variance 4, but sensor variance rises to 36. Its reading is still 12. 4 plus 36 is 40. 4 divided by 40 is 1/10. 12 remains 2 meters above the prediction, so take 1/10 of that gap. That adds 0.2 meters, giving 10.2. The same disagreement now moves the estimate much less. We'll calculate its new uncertainty when we check why this weight wins. With that noisier-sensor case finished, return to the original sensor variance 4.

6:38 The first corrected pair was 11 meters, variance 2. Now take the second tick. Add 10 to 11: predicted position 21. Add movement-error variance 2 to the stored 2: predicted variance 4 again. The sensor reads 19, 2 meters below 21. Equal variances still give gain 1/2, so the correction is minus 1 meter. Our updated position is 20, with variance 2. The estimate lands above the reading of 19 because the movement prediction also contributes. We're not averaging all the raw readings. The previous corrected pair already carries the information from the past that this model needs. Oh, so each tick needs just that pair and a fixed amount of arithmetic.

7:21 We don't need a growing history table. Exactly. Tracking more quantities needs more numbers; collecting more readings doesn't enlarge this fixed model. Two half-weight updates, and we're already tempted to hardcode it. What happens if the next sensor reading never arrives? Then we still predict, but skip correction. We won't bring uncertainty back down, and that changes the next calculation. Next take a gap in sensor data. The previous pair was position 20, variance 2. Predict 30 with variance 4; no measurement arrives, so keep that pair.

7:55 We skip correction. A missing value is not a 0 reading, nor evidence that the cart stayed at its last measured position. The model carried us through the gap, but new evidence hasn't reduced the uncertainty. On the following tick, predict another 10-meter move. That gives 40 meters. Variance starts at 4 this time; add movement variance 2, and predicted variance becomes 6. Now the sensor returns with a reading of 44. Its error variance is still 4. Our prediction is less certain than the measurement.

8:25 6 plus 4 is 10, so the gain is 6 divided by 10: 0.6. The reading is 4 meters above our prediction. Multiply that 4-meter gap by 0.6: a correction of 2.4 meters. Add it to 40, giving 42.4. Uncertainty changes with it. For the optimal gain, the same squared-weight calculation simplifies to predicted variance times 1 minus the gain. 6 times 0.4 gives 2.4. So the missing reading changed the next weight without a special rule for a returning sensor. Right. The larger predicted uncertainty did it. But if the cart hit something while the sensor was absent, our model wouldn't magically know.

9:06 With the updates complete, let's return to why we chose the gain. What does our particular weight buy us over another one? Return to the first prediction, with variance 4, and the hypothetical noisier sensor, with variance 36. Try half weight. Squaring half gives a quarter: 4 contributes 1, 36 contributes 9. Total expected squared-error cost: 10. 10? Ignoring the sensor would leave cost 4. Keeping half weight has made our expected cost bigger. Try 1/10 for the sensor and 9/10 for the prediction.

9:39 Square those shares: 9/10 squared times 4, plus 1/10 squared times 36. The cost is 3.6. That gives our earlier noisier-sensor estimate variance 3.6. We've beaten 0 weight and half weight. Could another weight get it lower still? Picture a bowl, with weight along its width and squared-error cost as height. The minimum is its lowest point. I can picture that. But how do we know 1/10 is the bottom, rather than just the best weight we've tried? The variances sum to 40. We can rewrite the cost as 3.6 plus 40 times the square of our weight's distance from 1/10.

10:19 Ah, a square can't be negative. At 1/10, the extra part is 0. Every other weight adds a positive penalty. That's what puts the bowl's bottom at 1/10. For positive P and R, the same calculation puts the minimum at P divided by the sum of P and R. I like that the fraction now has a reason. Giving more weight to the sensor reduces the prediction's share, so we're balancing two error costs. Yes. We choose the smallest modeled squared-error cost, not the answer that must be closest to the cart on this particular reading. But that minimum depends on the model.

10:55 Suppose the sensor consistently reports a position too far ahead. Its errors no longer average to 0. The filter can settle on a wrong answer while reporting a small variance. Giving that biased sensor a smaller variance won't remove its persistent offset. It's like a tight dart group that still sits off the bullseye. Tightness doesn't tell us whether the center is right. Yes. The program might report variance falling from 4 to 2 while its estimates stay consistently too far forward. The reported uncertainty doesn't establish actual accuracy.

11:30 The uncertainty isn't a second sensor independently checking the first one. Right. The calculation needs honest movement and sensor variances, as well as the independent, unbiased errors we assumed. Our motion and sensor rules combine values by scaling and adding. That's what linear means here. Adding a known movement and directly measuring 1 position fit that model. The strongest guarantee also assumes Gaussian errors: small errors cluster around 0, and larger errors become less likely in a bell-shaped pattern.

12:00 Then the estimate minimizes expected squared error given the readings. Without that bell shape, does the gain we derived still minimize the cost of this mixture? Yes. The proof still picks the best mixture of these two estimates. I think the easy trap is calling that the best possible rule without checking the extra assumption. When those assumptions fit, our current pair can summarize the past for the next calculation. That compactness mattered in real navigation work. Finally, that compact summary connects to a real navigation problem.

12:31 NASA engineers planned sightings of Earth and Moon in short sequences separated by long gaps. Their account says an earlier estimation approach looked too expensive even for their simulation computer. Kalman's sequential formulation offered a way to reduce that computational burden. Their navigation rules weren't just scaling and adding, so the linear theory needed an adaptation. They still had to test its accuracy. Our cart isn't a complete spacecraft navigator. Our estimate went from 11 to 20, through a prediction-only 30, and finally to 42.4.

13:04 Uncertainty accumulated in the gap, changing the next weight. I like that the returning sensor didn't need its own special rule. The ordinary weight changed because our prediction had become less certain. We keep the current guess and how uncertain it is, then let the next reading change both. Thanks for listening to Learning Podcasts.