(Image by Author)
When I first started learning to use Kalman filter, it seemed very complicated to me, mainly because there were so many inputs to it and you never really knew if you’ve set them correctly. By going through this example, I hope my readers will understand that the motivations and logic behind it is really very pragmatic in nature and not so complicated after all.
Drop a Tennis Ball
So let’s get started with our example.
I am dropping a tennis ball from a helicopter (or maybe my airship), I would like to predict its position in the air until it hits the ground.
Tennis Ball Dropping (Image by Author)
You would think this is a very simple problem, right? After all, we’ve got our equations of motion:
Equations of Motion
Where g is acceleration due to gravity (9.81 meters per second squared).
https://www.homeinstead.ie/rct/c-v-k4.html
https://www.homeinstead.ie/rct/c-v-k3.html
https://www.homeinstead.ie/rct/c-v-k2.html
https://www.homeinstead.ie/rct/c-v-k1.html
http://www.activentmarketing.com/cvs/video-france-v-angleterre-match-liv-fr1.html
http://www.activentmarketing.com/cvs/video-france-v-angleterre-match-liv-fr2.html
http://www.activentmarketing.com/cvs/video-france-v-angleterre-match-liv-fr3.html
http://www.activentmarketing.com/cvs/video-france-v-angleterre-match-liv-fr4.html
http://www.activentmarketing.com/cvs/video-france-v-angleterre-match-liv-fr5.html
http://www.activentmarketing.com/cvs/video-mayo-v-tipperary-gaa-liv-ie1.html
http://www.activentmarketing.com/cvs/video-mayo-v-tipperary-gaa-liv-ie2.html
http://www.activentmarketing.com/cvs/video-mayo-v-tipperary-gaa-liv-ie3.html
http://www.activentmarketing.com/cvs/video-mayo-v-tipperary-gaa-liv-ie4.html
http://www.activentmarketing.com/cvs/video-mayo-v-tipperary-gaa-liv-ie5.html
http://www.activentmarketing.com/cvs/video-mayo-v-tipperary-gaa-liv-ie6.html
http://www.activentmarketing.com/cvs/video-north-v-london-derby-liv-uk1.html
http://www.activentmarketing.com/cvs/video-north-v-london-derby-liv-uk2.html
http://www.activentmarketing.com/cvs/video-north-v-london-derby-liv-uk3.html
http://www.activentmarketing.com/cvs/video-north-v-london-derby-liv-uk4.html
http://www.activentmarketing.com/cvs/video-north-v-london-derby-liv-uk5.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-match-liv-fr1.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-match-liv-fr2.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-match-liv-fr3.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-match-liv-fr4.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-match-liv-uk1.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-match-liv-uk2.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-match-liv-uk3.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-match-liv-uk4.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-match-liv-uk5.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-match-liv-uk6.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-match-liv-uk7.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-viv-now01.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-viv-now02.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-viv-now03.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-viv-now04.html
https://www.homeinstead.ie/rct/video-mayo-v-tipperary-gaa-liv-ie1.html
https://www.homeinstead.ie/rct/video-mayo-v-tipperary-gaa-liv-ie2.html
https://www.homeinstead.ie/rct/video-mayo-v-tipperary-gaa-liv-ie3.html
https://www.homeinstead.ie/rct/video-mayo-v-tipperary-gaa-liv-ie4.html
https://www.homeinstead.ie/rct/video-mayo-v-tipperary-gaa-liv-ie5.html
https://www.homeinstead.ie/rct/video-mayo-v-tipperary-gaa-liv-ie6.html
http://www.activentmarketing.com/cvs/n-v-g1.html
http://www.activentmarketing.com/cvs/n-v-g2.html
http://www.activentmarketing.com/cvs/n-v-g3.html
http://www.activentmarketing.com/cvs/n-v-g4.html
http://www.activentmarketing.com/cvs/n-v-g5.html
http://www.activentmarketing.com/cvs/n-v-g6.html
http://www.activentmarketing.com/cvs/n-v-h1.html
http://www.activentmarketing.com/cvs/n-v-h2.html
http://www.activentmarketing.com/cvs/n-v-h3.html
http://www.activentmarketing.com/cvs/n-v-h4.html
http://www.activentmarketing.com/cvs/n-v-h5.html
http://www.activentmarketing.com/cvs/n-v-h6.html
http://www.activentmarketing.com/cvs/n-v-i1.html
http://www.activentmarketing.com/cvs/n-v-i2.html
http://www.activentmarketing.com/cvs/n-v-i3.html
http://www.activentmarketing.com/cvs/n-v-i4.html
http://www.activentmarketing.com/cvs/w-v-s1.html
http://www.activentmarketing.com/cvs/w-v-s2.html
http://www.activentmarketing.com/cvs/w-v-s3.html
http://www.activentmarketing.com/cvs/w-v-s4.html
http://www.activentmarketing.com/cvs/w-v-s5.html
http://www.activentmarketing.com/cvs/w-v-s6.html
http://www.activentmarketing.com/cvs/w-v-s7.html
http://www.activentmarketing.com/cvs/w-v-s8.html
http://www.activentmarketing.com/cvs/w-v-s9.html
http://ricfacility.byu.edu/rte/engla-v-fran1.html
http://ricfacility.byu.edu/rte/engla-v-fran2.html
http://ricfacility.byu.edu/rte/engla-v-fran3.html
http://ricfacility.byu.edu/rte/engla-v-fran4.html
http://ricfacility.byu.edu/rte/engla-v-fran5.html
http://ricfacility.byu.edu/rte/englan-v-france1.html
http://ricfacility.byu.edu/rte/englan-v-france2.html
http://ricfacility.byu.edu/rte/englan-v-france3.html
http://ricfacility.byu.edu/rte/englan-v-france4.html
http://ricfacility.byu.edu/rte/englan-v-france5.html
http://ricfacility.byu.edu/rte/gaa-v-may1.html
http://ricfacility.byu.edu/rte/gaa-v-may2.html
http://ricfacility.byu.edu/rte/gaa-v-may3.html
http://ricfacility.byu.edu/rte/gaa-v-may4.html
http://ricfacility.byu.edu/rte/gaa-v-may5.html
http://www.activentmarketing.com/cvs/engla-v-fran1.html
http://www.activentmarketing.com/cvs/engla-v-fran2.html
http://www.activentmarketing.com/cvs/engla-v-fran3.html
http://www.activentmarketing.com/cvs/engla-v-fran4.html
http://www.activentmarketing.com/cvs/engla-v-fran5.html
http://www.activentmarketing.com/cvs/englan-v-france1.html
http://www.activentmarketing.com/cvs/englan-v-france2.html
http://www.activentmarketing.com/cvs/englan-v-france3.html
http://www.activentmarketing.com/cvs/englan-v-france4.html
http://www.activentmarketing.com/cvs/englan-v-france5.html
http://www.activentmarketing.com/cvs/gaa-v-may1.html
http://www.activentmarketing.com/cvs/gaa-v-may2.html
http://www.activentmarketing.com/cvs/gaa-v-may3.html
http://www.activentmarketing.com/cvs/gaa-v-may4.html
http://www.activentmarketing.com/cvs/gaa-v-may5.html
http://www.activentmarketing.com/cvs/tot-v-ars1.html
http://www.activentmarketing.com/cvs/tot-v-ars2.html
http://www.activentmarketing.com/cvs/tot-v-ars3.html
http://www.activentmarketing.com/cvs/tot-v-ars4.html
http://www.activentmarketing.com/cvs/tot-v-ars5.html
http://www.activentmarketing.com/cvs/tot-v-ars6.html
http://www.activentmarketing.com/cvs/tot-v-ars7.html
https://www.homeinstead.ie/rct/gaa-v-may1.html
https://www.homeinstead.ie/rct/gaa-v-may2.html
https://www.homeinstead.ie/rct/gaa-v-may3.html
https://www.homeinstead.ie/rct/gaa-v-may4.html
https://www.homeinstead.ie/rct/gaa-v-may5.html
http://ricfacility.byu.edu/rte/Video-Arsenal-v-Tottenham-espn-Live-uk.html
http://ricfacility.byu.edu/rte/Video-Arsenal-v-Tottenham-Live-EPL-BBC.html
http://ricfacility.byu.edu/rte/Video-Arsenal-v-Tottenham-London-Derby-Live.html
http://ricfacility.byu.edu/rte/Video-Tottenham-v-Arsenal-BT-Live-Tv.html
http://ricfacility.byu.edu/rte/Video-Tottenham-v-Arsenal-Derby-Day-Live.html
http://ricfacility.byu.edu/rte/Video-Tottenham-v-Arsenal-epl-Live.html
http://ricfacility.byu.edu/rte/video-celtic-v-st-johnstone-live-uk1.html
http://ricfacility.byu.edu/rte/video-celtic-v-st-johnstone-live-uk2.html
http://ricfacility.byu.edu/rte/video-celtic-v-st-johnstone-live-uk3.html
http://ricfacility.byu.edu/rte/video-celtic-v-st-johnstone-live-uk4.html
http://ricfacility.byu.edu/rte/video-tottenham-v-arsenal-epl-live-gk1.html
http://ricfacility.byu.edu/rte/video-tottenham-v-arsenal-epl-live-gk2.html
http://ricfacility.byu.edu/rte/video-tottenham-v-arsenal-epl-live-gk3.html
http://ricfacility.byu.edu/rte/video-tottenham-v-arsenal-epl-live-gk4.html
http://ricfacility.byu.edu/rte/video-tottenham-v-arsenal-epl-live-gk5.html
http://ricfacility.byu.edu/rte/video-tottenham-v-arsenal-epl-live-gk6.html
http://www.activentmarketing.com/cvs/Video-Arsenal-v-Tottenham-espn-Live-uk.html
http://www.activentmarketing.com/cvs/Video-Arsenal-v-Tottenham-Live-EPL-BBC.html
http://www.activentmarketing.com/cvs/Video-Arsenal-v-Tottenham-London-Derby-Live.html
http://www.activentmarketing.com/cvs/Video-Tottenham-v-Arsenal-BT-Live-Tv.html
http://www.activentmarketing.com/cvs/Video-Tottenham-v-Arsenal-Derby-Day-Live.html
http://www.activentmarketing.com/cvs/Video-Tottenham-v-Arsenal-epl-Live.html
http://www.activentmarketing.com/cvs/video-celtic-v-st-johnstone-live-uk1.html
http://www.activentmarketing.com/cvs/video-celtic-v-st-johnstone-live-uk2.html
http://www.activentmarketing.com/cvs/video-celtic-v-st-johnstone-live-uk3.html
http://www.activentmarketing.com/cvs/video-celtic-v-st-johnstone-live-uk4.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-epl-live-gk1.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-epl-live-gk2.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-epl-live-gk3.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-epl-live-gk4.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-epl-live-gk5.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-epl-live-gk6.html
https://www.homeinstead.ie/rct/n-v-h1.html
https://www.homeinstead.ie/rct/n-v-h2.html
https://www.homeinstead.ie/rct/n-v-h3.html
https://www.homeinstead.ie/rct/n-v-h4.html
https://www.homeinstead.ie/rct/n-v-h5.html
https://www.homeinstead.ie/rct/n-v-h6.html
http://ricfacility.byu.edu/rte/video-tottenham-v-arsenal-match-liv-uk1.html
http://ricfacility.byu.edu/rte/video-tottenham-v-arsenal-match-liv-uk2.html
http://ricfacility.byu.edu/rte/video-tottenham-v-arsenal-match-liv-uk3.html
http://ricfacility.byu.edu/rte/video-tottenham-v-arsenal-match-liv-uk4.html
http://ricfacility.byu.edu/rte/video-tottenham-v-arsenal-match-liv-uk5.html
http://ricfacility.byu.edu/rte/video-tottenham-v-arsenal-match-liv-uk6.html
http://ricfacility.byu.edu/rte/video-tottenham-v-arsenal-match-liv-uk7.html
http://www.activentmarketing.com/cvs/May-v-Tipper-ie1.html
http://www.activentmarketing.com/cvs/May-v-Tipper-ie2.html
http://www.activentmarketing.com/cvs/May-v-Tipper-ie3.html
http://www.activentmarketing.com/cvs/Sta-v-Mec-be1.html
http://www.activentmarketing.com/cvs/Sta-v-Mec-be2.html
http://www.activentmarketing.com/cvs/Sta-v-Mec-be3.html
http://www.activentmarketing.com/cvs/Sta-v-Mec-be4.html
http://www.activentmarketing.com/cvs/Sta-v-Mec-be5.html
https://www.homeinstead.ie/rct/May-v-Tipper-ie1.html
https://www.homeinstead.ie/rct/May-v-Tipper-ie2.html
https://www.homeinstead.ie/rct/May-v-Tipper-ie3.html
http://www.activentmarketing.com/cvs/a-v-f1.html
http://www.activentmarketing.com/cvs/a-v-f2.html
http://www.activentmarketing.com/cvs/a-v-f3.html
http://www.activentmarketing.com/cvs/a-v-f4.html
http://www.activentmarketing.com/cvs/a-v-f5.html
http://www.activentmarketing.com/cvs/a-v-f6.html
http://www.activentmarketing.com/cvs/a-v-f7.html
http://www.activentmarketing.com/cvs/a-v-f8.html
http://www.activentmarketing.com/cvs/arsenal-v-tottenham-tvc1.html
http://www.activentmarketing.com/cvs/eng-v-fra-live-tv1.html
http://www.activentmarketing.com/cvs/fra-v-eng-live-tv1.html
http://www.activentmarketing.com/cvs/may-v-tip-live-tc1.html
http://www.activentmarketing.com/cvs/napoli-v-crotone-diretta-on1.html
http://www.activentmarketing.com/cvs/Video-England-v-France-Rugby-Live1.html
http://www.activentmarketing.com/cvs/Video-France-v-Angleterre-Direct-fr1.html
http://www.activentmarketing.com/cvs/video-mayo-v-tipperary-liv-gaa1.html
http://www.activentmarketing.com/cvs/video-tottenham-v-arsenal-liv-pl1.html
https://www.homeinstead.ie/rct/a-v-f3.html
https://www.homeinstead.ie/rct/a-v-f4.html
https://www.homeinstead.ie/rct/may-v-tip-live-tc1.html
https://www.homeinstead.ie/rct/video-mayo-v-tipperary-liv-gaa1.html
http://www.activentmarketing.com/cvs/t-v-o1.html
http://www.activentmarketing.com/cvs/t-v-o2.html
http://www.activentmarketing.com/cvs/t-v-o3.html
http://www.activentmarketing.com/cvs/t-v-p1.html
http://www.activentmarketing.com/cvs/t-v-p2.html
http://www.activentmarketing.com/cvs/t-v-p3.html
http://www.activentmarketing.com/cvs/t-v-p4.html
http://www.activentmarketing.com/cvs/t-v-q1.html
http://www.activentmarketing.com/cvs/t-v-q2.html
http://www.activentmarketing.com/cvs/t-v-q3.html
http://www.activentmarketing.com/cvs/t-v-q4.html
http://www.activentmarketing.com/cvs/t-v-q5.html
http://www.activentmarketing.com/cvs/t-v-q6.html
http://www.activentmarketing.com/cvs/t-v-r1.html
http://www.activentmarketing.com/cvs/t-v-r2.html
http://www.activentmarketing.com/cvs/t-v-r3.html
http://www.activentmarketing.com/cvs/t-v-r4.html
http://www.activentmarketing.com/cvs/t-v-r5.html
http://www.activentmarketing.com/cvs/t-v-r6.html
http://www.activentmarketing.com/cvs/t-v-w1.html
http://www.activentmarketing.com/cvs/t-v-w2.html
http://www.activentmarketing.com/cvs/t-v-w3.html
http://www.activentmarketing.com/cvs/t-v-w4.html
http://www.activentmarketing.com/cvs/t-v-w5.html
http://www.activentmarketing.com/cvs/t-v-w6.html
http://www.activentmarketing.com/cvs/t-v-w7.html
http://www.activentmarketing.com/cvs/t-v-w8.html
http://www.activentmarketing.com/cvs/t-v-w9.html
http://www.activentmarketing.com/cvs/t-v-w10.html
http://www.activentmarketing.com/cvs/t-v-x1.html
http://www.activentmarketing.com/cvs/t-v-x2.html
http://www.activentmarketing.com/cvs/t-v-x3.html
http://www.activentmarketing.com/cvs/t-v-x4.html
http://www.activentmarketing.com/cvs/t-v-x5.html
http://www.activentmarketing.com/cvs/t-v-x6.html
http://www.activentmarketing.com/cvs/t-v-x7.html
http://www.activentmarketing.com/cvs/t-v-x8.html
http://www.activentmarketing.com/cvs/t-v-x9.html
http://www.activentmarketing.com/cvs/t-v-x10.html
http://www.activentmarketing.com/cvs/t-v-y1.html
http://www.activentmarketing.com/cvs/t-v-y2.html
http://www.activentmarketing.com/cvs/t-v-y3.html
http://www.activentmarketing.com/cvs/t-v-y4.html
http://www.activentmarketing.com/cvs/t-v-y5.html
http://www.activentmarketing.com/cvs/t-v-y6.html
http://www.activentmarketing.com/cvs/t-v-y7.html
http://www.activentmarketing.com/cvs/t-v-y8.html
http://www.activentmarketing.com/cvs/t-v-y9.html
http://www.activentmarketing.com/cvs/t-v-y10.html
http://www.activentmarketing.com/cvs/t-v-z1.html
http://www.activentmarketing.com/cvs/t-v-z2.html
http://www.activentmarketing.com/cvs/t-v-z3.html
http://www.activentmarketing.com/cvs/t-v-z4.html
http://www.activentmarketing.com/cvs/t-v-z5.html
http://www.activentmarketing.com/cvs/t-v-z6.html
http://www.activentmarketing.com/cvs/t-v-z7.html
http://www.activentmarketing.com/cvs/t-v-z8.html
http://www.activentmarketing.com/cvs/t-v-z9.html
http://www.activentmarketing.com/cvs/t-v-z10.html
http://www.activentmarketing.com/cvs/c-v-k4.html
http://www.activentmarketing.com/cvs/c-v-k3.html
http://www.activentmarketing.com/cvs/c-v-k2.html
http://www.activentmarketing.com/cvs/c-v-k1.html
http://www.activentmarketing.com/cvs/Video-Arsenal-v-Tottenham-Live-CBC1.html
http://www.activentmarketing.com/cvs/a-v-f9.html
Well, unfortunately the real world is not that simple. If you are dropping a lead ball I suppose the equations of motion are all you need, but for something light like a tennis ball, it’s trajectory might be affected by all sorts of things.
For example, there might be a really strong wind blowing.
Wind Blowing (Image by Author)
Or a really hungry alligator might be near by…
Hungry Alligator (Image by Author)
The real world is complicated.
So, if your prediction solely comes from the equations, it might be really off by the time the ball reaches the ground.
Let’s take a closer look.
Simulation
Let’s simulate what happens.
Suppose we drop a ball 20,000 meters above ground, it will mostly follow the the equations of motion, with random forces such as wind and alligators affecting it’s trajectory:
Where F is the equations of motion we just discussed. G and N are the random forces affecting the ball in real life (wind and alligators). N is basically a random variable of normal distribution, with mean 0 and standard deviation sigma(a).
Our initial conditions, therefore, are as follows:
# initial values
x0 = np.matrix([[20000],
[0],
[-9.81]]) # gravity
And suppose that our random forces effectively have a standard deviation of say 1 meters per second squared.
sa = 1 # sigma a
The trajectory of the tennis ball will look something like below:
A REAL Tennis Ball Dropping (Image by Author)
Simulated with codes below:
def xk(F, xk1, G, sa):
_xk = F.dot(xk1) + \
G * np.random.normal(loc=0, scale=sa) return _xk
Prediction
How accurate is our prediction with equations of motion?
Now suppose we try to predict the trajectory of the tennis ball with only the equations of motion, how accurate will it be?
Equations of Motion Predicted Trajectory vs Real (Image by Author)
As you can see, the difference can accumulate and become quite significant, in this case, they are off by more than 1000 meters.
Observation
How do we improve this prediction with Kalman filter?
Kalman filter takes advantage of the fact that you don’t need to wait till the tennis hits the ground to know its position. If you have a camera with you, for example, you can take a picture of the tennis ball every 10 seconds and estimate its position from it so that you can update your prediction from the observation. (So don’t ever forget to bring your phone!)
Suppose at time t, we take a picture of tennis ball, what will it look like?
Taking a Picture of the Tennis Ball at Time t(Image by Author)
It’s not going to give you a perfect observation (nothing can be perfect). But you can get a good estimate from it. Below is the mathematical representation of the observation:
Observed Tennis Ball Position at time t
In this case, you have the actual state of the ball for which you don’t know the exact values, but you can observe it with an observation model, H.
H represents our camera, since from the picture taken by a camera you can only find position y at time t, it’s a horizontal unit vector. If, say, we have a radar that can actually observe the velocity as well, we can use [1, 1, 0] as observation model.
The third part represents the inaccuracies associated with the observation, and it’s a normal random variable with mean 0 and standard deviation sigma z.
Kalman Filter
Apply Kalman filter to improve the predictions with observations.
Kalman filter provides us with a recipe for using the observation to improve the prediction.
- You start with your predicted state(position, velocity, acceleration) of the tennis ball and their predicted covariances (accuracy in prediction), the predicted covariance is updated during each step along with state proportional to how much random force is on the tennis ball.
- Calculate the difference between the predicted position and the observation.
- Calculate the gain warranted from the difference based on the covariance of the tennis ball’s state and the accuracy of the observation.
— here the idea is that if the observation is perfect (R=0), then the gain should be 1, so that your updated estimate is basically the observation.
— if the variance of the position is high (the prediction is not very accurate), even if the observation is not that accurate, it’s a good idea to give a bit more weight to it since compared to the accuracy of the prediction it’s not so bad.
- Apply the gain to the predicted position and predicted covariance to update them.
Results
How does Kalman filter perform?
As you can see, the purpose of Kalman filter is to improve your predictions with periodic observations. If you have observations, great, let’s use them, if not, just continue the prediction process.
Below is a comparison of performance using Kalman filters to improve prediction with observations every 50, 10, 5 seconds:
As you can see, more frequent observations definitely improve predicted values.
Conclusion
What’s next?
By going through this simple application of Kalman filter, I hope my readers have gained an intuitive understanding of the Kalman filter estimation improvement process. As we can see from the example, how accurate the prediction is ultimately depends on the model used to generate the prediction. Kalman filter can help improve the prediction with appropriately chosen error models for the prediction(sigma a) and the observation (sigma z). There are many improvements that can be made to the Kalman filter, we’ll talk about them in the future.