Phase 2: Updating Your Prediction
From State to Sensors
While we were busy predicting, the GPS on our RC car was giving us positional data updates. TheseĀ sensor measurementsĀ zā give us valuable information about our world.
However, zāĀ and our state vectorĀ xāāā may not actually correspond; our measurements might be in one space, and our state in another! For instance, what if weāre measuring our state in meters, but all of our measurements are in feet? We need some way to remedy this.
Letās handle this by converting our state vector into our measurement space using anĀ observation matrixĀ H:
These equations represent the mean μāāā and covariance Ī£āāā of ourĀ predicted measurements.
For our RC car, weāre going from meters to feet, in both position and velocity. 1 meter is around 3.28 ft, so we would shape H to reflect this:
ā¦leaving us with predicted measurements that we can now compare to our sensor measurements zā. Note thatĀ HĀ is entirely dependent on whatās in your state and whatās being measured, so it can change from problem to problem.
Fig. 1: Moving our state from state space (meters, bottom left PDF) to measurement space (feet, top right PDF).
Our RC car example is a little simplistic, but this ability to translate ourĀ predicted state into predicted measurementsĀ is a big part of what makes Kalman filters so powerful. We can effectively compare our state with any and all sensor measurements, from any sensor. Thatās powerful stuff!
An aside: The behavior ofĀ HĀ is important. Vanilla Kalman filters use a linear Fā andĀ H; that is, there is only one set of equations relating the estimated state to the predicted state (Fā), and predicted state to predicted measurement (H).If the system isĀ non-linear, then this assumption doesnāt hold. Fā andĀ HĀ might change every time our state does! This is where innovations like theĀ Extended Kalman FilterĀ (EKF) and theĀ Unscented Kalman FilterĀ come into play. EKFs are the de facto standard in sensor fusion for this reason.
Letās add one more term for good measure: Rā, ourĀ sensor measurement covariance.Ā This represents the noise from our measurements. Everything is uncertain, right? It never ends.
The Beauty of PDFs
Since we converted our state space into the measurement space, we now have 2 comparable Gaussian PDFs:
- μāāā and Ī£āāā, which make up the Gaussian PDF for our predicted measurements
- zāĀ andĀ Rā, which make up the PDF for our sensor measurements
The strongest probability for our future state is the overlap between these two PDFs. How do we get this overlap?
Fig. 2: Our predicted state in measurement space is in red. Our measurements z are in blue. Notice how our measurements z have a much smaller covariance; this is going to come in handy.
We multiply them together! The product of two Gaussian functions is just another Gaussian function. Even better, this common Gaussian has aĀ smaller covarianceĀ than either the predicted PDF or the sensor PDF, meaning that our state is now much more certain.
ISNāT THAT NEAT.
Update Step, Solved.
Weāre not out of the woods yet; we still need to derive the math! Suffice to say⦠itās a lot. The basic gist is that multiplying two Gaussian functions results in its own Gaussian function. Letās do this with two PDFs now, Gaussian functions with means μā, μā and variances Ļā², Ļā². Multiplying these two PDFs leaves us with our final Gaussian PDF, and thus our final mean and covariance terms:
When we substitute in our derived state and covariance matrices, these equations represent theĀ update stepĀ of a Kalman filter.
See reference (Bromiley, P.A.) for a good explanation on how to multiply two PDFs and derive the above. It takes a good page to write out; youāve been warned.
This is admittedly pretty painful to read as-is. We can simplify this by defining theĀ Kalman GainĀ Kā:
With Kā in the mix, we find that our equations are much kinder on the eyes:
Weāve done it! Combined, the newĀ xĀ andĀ PĀ create our final Gaussian distribution, the one that crunches all of that data to give us our updated state. See how our updated state spikes in probability (the blue spike in Fig. 3). Thatās the power of Kalman Filters!
Fig. 3: Original state in green. Predicted state in red. Updated final state in blue. Look at how certain we are now! Such. Wow.
What Now?
Well⦠do it again! The Kalman filter is recursive, meaning that it uses its output as the next input to the cycle. In other words,Ā the finalĀ xĀ is your newĀ xā!Ā The cycle continues in the next prediction state.
Kalman filters are great for all sorts of reasons:
- They extend to anything that can be modeled and measured. Automotives, touchscreens, econometrics, etc etc.
- Your sensor data can come from anywhere. As long as thereās a connection between state and measurement, new data can be folded in.
- Kalman filters also allow for the selective use of data. Did your sensor fail? Donāt count that sensor for this iteration! Easy.
- They are a good way to model prediction. After completing one pass of the prediction step, just do it again (and again, and again) for an easy temporal model.
Of course, if this is too much and youād rather do⦠anything else,Ā Tangram VisionĀ is developing solutions to these very same problems! Weāre creating tools that help any vision-enabled system operate to its best. If you like this article, youāll love seeing what weāre up to.
Code Examples and Graphics
The code used to render these graphs and figures is hosted on Tangram Visionās public repository for the blog. Head down, check it out, and play around with the math yourself! If you can improve on our implementations, even better; we might put it in here. And be sure toĀ tweet at usĀ with your improvements.
Further Reading
If this article didnāt help, here are some other sources that dive into Kalman Filters in similar ways:
- Faragher, R. (2012, September). Understanding the Basis of the Kalman Filter.Ā https://drive.google.com/file/d/1nVtDUrfcBN9zwKlGuAclK-F8Gnf2M_to/view
- Bzarg. How a Kalman filter works, in pictures.Ā https://www.bzarg.com/p/how-a-kalman-filter-works-in-pictures/
- Teammco, R. Kalman Filter Simulation.Ā https://www.cs.utexas.edu/~teammco/misc/kalman_filter/
- Bromiley, P.A. Products and Convolutions of Gaussian Probability Density Functions.Ā https://bit.ly/3nGDeweĀ [PDF]