Close Navigation
Learn more about IBKR accounts
How to Create Kalman Filter in Python – Part V

How to Create Kalman Filter in Python – Part V

Posted March 16, 2021
Rekhit Pachanekar

See Part IPart II , Part III and Part IV of this series to get started with the Statistical terms and concepts used in Kalman Filter.

Estimate uncertainty update

In the Kalman filter tutorial, we saw that the Kalman gain was dependent on the uncertainty in the estimation. Now, as we know that with every successive step, the Kalman filter continuously updates the anticipated value so that we get the estimated value as close to the actual value of a variable, thus, we have to see how this uncertainty in the error can be reduced.

While the derivation of the equation is lengthy, we are only concerned about the equation.

Thus, the estimate uncertainty update equation tells us that the estimate uncertainty of current state varies from the previous estimate uncertainty by the factor of (1 – Kalman gain). We can also call this the covariance update equation.

This brings us to the last equation of the Kalman filter tutorial, which we will see below.

Estimate uncertainty extrapolation

The reason why the Kalman filter is popular is because it continuously updates its state depending on the anticipated and measured current value. Recall that in the second equation we had extrapolated the state of the estimate. Similarly, the estimate uncertainty of the current error is used to anticipate the uncertainty in the error in the next state.

Ok. That was a simple, no equations way to describe the Kalman filter. If you are confused, let us go through the process and see what we have learned so far.

For input, we have measured value. Initially, we use certain parameters for the Kalman gain as well as the expected value. We will also make a note of the estimate uncertainty.

Now we use the Kalman filter equation to find the next expected value.

In the next iteration, depending on how accurate our expected variable was, we make changes to the uncertainty estimate which in turn would modify our Kalman gain.

Thus, we get a new expected value which will be used as our current estimate in the next phase.

In this way, with each step, we would get closer to anticipating the actual value with a reasonable amount of success.

That is all there is to it. We would reiterate in this Kalman filter tutorial that the reason the Kalman filter is popular is because it only needs the previous value as input and depending on the uncertainty in the measurement, the resulting value is expected. In the real world, the Kalman filter is used by implementing matrix operations as the complexity increases when we take real-world situations. If you are interested in the math part of the Kalman filter, you can go through this resource to find many examples illustrating the individual equations of the Kalman filter.

When it comes to trading, Kalman filter forms an important component in the pairs trading strategy. Let us build a simple pairs trading strategy in Python now.

Stay tuned for the next installment, in which the Rekhit will review pairs trading using Kalman Filter in Python.

Download the full code:

All data and information provided in this article are for informational purposes only. QuantInsti® makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information in this article and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.