Popular Posts

Tuesday, July 4, 2017

An attempt at an anti-rocketsurgery explanation of Kalman Filter

This post is for providing some intuition for people who are interested in Kalman Filters, trying to learn what it is but lost their way in equations and for dummies explanations involving matrix notation!

Wikipedia definition:

Kalman filtering, also known as linear quadratic estimation (LQE), is an algorithm that uses a series of measurements observed over time, containing statistical noise and other inaccuracies, and produces estimates of unknown variables that tend to be more accurate than those based on a single measurement alone, by using Bayesian inference and estimating a joint probability distribution over the variables for each time-frame. The filter is named after Rudolf E. Kálmán, one of the primary developers of its theory.

Quantopian Definition:

The Kalman filter is an algorithm that uses noisy observations of a system over time to estimate the parameters of the system (some of which are unobservable) and predict future observations. At each time step, it makes a prediction, takes in a measurement, and updates itself based on how the prediction and measurement compare.

Use in trading:

Options are endless as you may have several measurements and predictions involving uncertainty. These days, for retail quants, a common use is for updating the hedge ratio in mean reversion strategies.

My explanation:

There is a guy called Rich who owns 5.000 cows. This guy is very detail oriented and he always wants to know the most accurate location of his cows. He knows that he can never be exact about the location unless he sees with his own eyes, so he wants to hear two things, where the cow is and how many meters this estimation can be off. For this purpose he has hired three cowboys. The old cowboy, the spotter and Mr. Kalman.



Old cowboy knows everything about the cows but always sits in the house and says things like if a cow has left the farm 2 hours ago and it is raining, she should be near the big rock by the river now. The spotter, instead of sitting in the house, goes on a hill next to the farm and tells where the cow is. Mr. Kalman listens these guys and makes the final decision on the cows location.

Old cowboy knows the past very well, so he has this general understanding of the behavior and the daily cycle of a cow. The spotter only counts on what he sees. However, Mr. Kalman knows that both are not exact in their assessments and he needs a method for balancing the information to come up with a good estimate of the cows location.

At the first day of the work, Mr Kalman knows that the cow was next to the big rock by the river one hour ago but he also knows that he is not certain. She may as well be 300 meters to the north of the big rock.

Mr. Kalman: Ok old cowboy, we know that cow was around the big rock, tell me where she is now.
Old Cowboy: She is at 100 meters to the north of the big rock
Mr. Kalman: Are you certain?
Old Cowboy: I may be off by 50 meters, she may be at 50 meters north to the rock
Mr Kalman: Thank you Old Cowboy
Mr Kalman: Ok spotter, tell me where the cow is
Spotter: She is at 500 meters to the north of the big rock
Mr Kalman: Are you certain?
Spotter: I may be off by 100 meters, she may be at 400 meters to the north of the rock
Mr Kalman: Thank you spotter

Mr. Kalman decides making the assumption that the spotter generally corrects the old cowboy as the spotter actually sees where the cow is. He may have a bad eye, there may be fog etc, but it is still a reflection of the reality. Then he thinks “if the spotter was 100% sure of the cows position, I should have taken it as a fact and ignore the old cowboy, however, as the spotter is not sure, I should find a way to incorporate the old cowboys estimation and make my final decision”

He comes up with a way of calculating the share of the old cowboy in total uncertainty:

Old cowboys uncertainty / (Old cowboys uncertainty + Spotters uncertainty) = 50 / (50 + 100) = 0.33

He says, “hmm, the old cowboy is more certain than the spotter, I can use this ratio as my trust in the spotters estimate, if the spotter was 100% certain and hence spotters uncertainty was zero, then my trust in spotters estimate would have been 1, which is not the case here” Then he looks for a way to adjust spotters estimate taking into consideration his trust in the spotters estimate.

He thinks again “If I say the position of the cow was next to the big rock one hour ago and the spotter says that now it is 500 meters to the north and I do not trust him 100%, I may multiply 500 with my trust in the spotters estimate which is 0.33". So he comes to the conclusion that the cow is 165 meters to the north of the big rock.

Then he says “wait a minute, what about my uncertainty of the cows position one hour ago, it was 300 meters, I need to calculate this as well taking into consideration the new information from the old man and the spotter. He says, “If the spotter was 100% certain, I might have consider to be certain and remove all uncertainty but this is not the case, the spotter is bringing in more uncertainty than the old man. As my trust in the spotters estimate is 0.33, this is what I should be taking out of the uncertainty one hour ago, so 0.67 of the uncertainty should stay, which is 200 meters”

Mr. Kalman then goes and reports to his boss, “the cow is 165 meters north to the big rock by the river with an uncertainty of 200 meters.

Here,

- The big rock is the estimate of the previous state (the location of the cow)
- 300 is the error of the previous state estimation
- Old Guy is the model of the cows motion and errors of this old guy are normally distributed, he is mostly consistent in his uncertainty in the long run
- Old guy is the transition model or you can call the prediction
- Spotter is the noisy sensor doing the measurement
- Spotters is the observation model or you can call the correction
- Mr. Kalman’s trust in spotters estimate is the Kalman Gain(KG), 0.33 in our case
- 165 is the estimate of the current state
    - New Position = Old position + KG(spotters estimate - old position)
    - 0 + 0.33(500 - 0) = 165
- 200 is the error of the current state
    - Uncertainty now = (1 - trust in spotters estimate) x uncertainty one hour ago
    - (1-0.33) x 300 = 200
- Farm owner is the rich guy, nothing to do with math
- Cow has left the farm and traveling the world now in the form of processed meat

If you want the same guys including the cow in a trading setting like updating the hedge ratio in mean reversion strategies, let me know for a future post.

If you find this post useful, please like it on Quantocracy!