Close Navigation
Learn more about IBKR accounts

How To Make A Kalman Filter in R for Pairs Trading

Posted September 26, 2019
Kris Longmore
Robot Wealth

Excerpt

The Kalman filter is a state space model for estimating an unknown (‘hidden’) variable using observations of related variables and models of those relationships. The Kalman filter is underpinned by Bayesian probability theory and enables an estimate of the hidden variable in the presence of noise.

There are plenty of tutorials online that describe the mathematics of the Kalman filter, so I won’t repeat those here (this article is a wonderful read). Instead, I’ll show you how to implement the Kalman filter framework to provide a dynamic estimate of the hedge ratio in a pairs trading strategy. I’ll provide just enough math as is necessary to follow the implementation.

To implement our Kalman filter, we need four variables:

  1. A vector of our observed variable
  2. A vector of our hidden variable
  3. A state transition model (which describes how the hidden variable evolves from one state to the next)
  4. An observation model (a matrix of coefficients for the other variable – we use a hedge coefficient and an intercept)

For our hedge ratio/pairs trading application, the observed variable is one of our price series p1 and the hidden variable is our hedge ratio, β. The observed and hidden variables are related by the familiar spread equation:p1=β∗p2+ϵ, where ϵ is noise (in our pairs trading framework, we are essentially making bets on the mean reversion of ϵ). In the Kalman framework, the other price series, p2 provides our observation model.

We also need to define a state transition model that describes the evolution of β from one time period to the next. If we assume that β follows a random walk, then our state transition model is simply βt=βt−1+ω

Visit Robot Wealth website to read the rest of the article, and to download the sample R code.

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 Robot Wealth and is being posted with its permission. The views expressed in this material are solely those of the author and/or Robot Wealth 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.