Skip to main content

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!

Comments

Popular posts from this blog

Two Strategies you can start trading tomorrow - Time of Day effects in FX continued

My latest post at http://quantsjourney.blogspot.co.uk/2017/09/time-of-day-effects-in-fx.html was on time of day effects in FX and I was claiming that you can actually make money with simple strategies depending on time of day. Below you will find 2 very simple strategies you can play with and make some money. Do not forget sending my 20%, I know I can trust you. I will now test these strategies with M15 bars with my quick and dirty backtester on 15 currency pairs including majors. Backtesting period is 01.01.2007 - 05.05.2017. The charts are profit in pips for the pairs where the strategy has positive P&L, these are not equity curves with equally spaced time, the horizontal axis is number of trades. But obviously equity curves will be similar with some gaps due to days with no trades. I almost did not optimize these strategies, there is room for improvement. Strategy one: Short at GMT 09:15 am, do this on Wednesday, Thursday and Friday, close after 5 hours. Check out EURU

Struggling Quant Episode 1: How I lost USD 500.000 while figuring out the link between questions, math, stats, coding and trading

Say that you are 30 years old and you have a good 25 years to work hard. Instead of going down the easy way of working for someone else during the day and killing time in the evenings and weekends, you have chosen the hard path of quantitative trading and started the heavy work. How many profitable trading ideas you can find in 25 years. Lets say you can figure out and test ideas in 10 days, 36 ideas a year and 900 ideas in 25 years right. Since this is my post, I am rounding 900 to 1000. Assuming a hit rate of 5%, 50 profitable ideas in 25 years, 2 per year and lets also say a profitable idea makes 20% per year. Depending on your initial capital, you will make X USD in 25 years. Lets say this X is USD 12.500.000 for me. USD 500.000 per year in average. This is my potential if I start doing things right now, right? So if I do things wrong for a year, I lose USD 500.000, this is what I did last year. I could not figure out how to operationalize all the scientific literature,

Trading Decisions of Your Stone Age Grandpa can Make You Money in FOREX

Why Ferrari or Rolex does not price their products at 149.999 or 12.999 but most of the items you see in your supermarket is priced like 4.99? Because they never like to be positioned as a bargain. Did you know that we tend to chose the price with less syllables even if the two prices have the same written lenght? These are some of the pricing strategies used by marketers. This is a very interesting topic and you can even find yourself deep in neuroscience while reading about it. Check this site, it gives 42 pricing methods to influence your brain, crazy, https://www.nickkolenda.com/psychological-pricing-strategies/ We also deal with prices when trading and I believe there are some sub-concious forces in play. Knowing some articles on things like the effect of round numbers in trading, I see some more potential here and I find this worth digging more not as standalone trading strategies but more as filters. At the end of the day, no one knows how the price feed effects the primit