# Kalman Filter and Pairs Trading

**Imagine this nightmare scenario**: you are a statistical arbitrage trader at a proprietary trading desk or hedge fund. As such, you routinely hold an inventory of ETF exposure that you must hedge.

The previous night, you instructed your overnight traders to **calculate the hedge ratios** for a matrix of ETF’s.

The next morning before the market opens, your junior quant traders eagerly present their results for your inspection. You load the hedge ratios into your trading platform and wait for the open.

When the market first opens for trading, you re-balance your hedges according to the new ratios. Afterwards, you watch in horror as your hedges do not perform as expected. **What went wrong?**

**Every good trader knows they have to adapt** when conditions in the market change, so why do we demand otherwise from our trading models? The traders in our example relied on static hedge ratios to power their trading logic. As a result, they opened themselves up to what is known as parameter risk.

Updating your parameters as new information becomes available is one way to protect yourself from this under-appreciated trading risk. A great model for accomplishing this in a trading scenario is the Kalman Filter.

**Demystifying Kalman Filter**

Kalman Filter is an online (real-time) algorithm that estimates a system state as it receives new information. The algorithm is named after the developer of its theory, Rudolf E. Kalman. One of the first application of the Kalman Filter was on the Apollo project. The Kalman Filter is used to this very day in guidance and navigation systems, particularly aircraft, missiles, and spacecraft.

**HOW ARE KALMAN FILTERS USED IN PAIRS TRADING?**

Kalman Filters are optimal estimation algorithms. The two main benefits are:

The variables of interest (i.e. submarines deep under water space rocket engine) can only be measured indirectly.

Measurements are available from various sensors but are subject to noise (i.e. radar, accelerometers, financial markets)

The second benefit is why the Kalman Filter is an excellent algorithm to incorporate within your pairs trading models. As stated above, when conditions in the market change it is critical that your trading models adjust to various market conditions that expose your PnL to parameter risk (i.e. stale hedge ratios).

Hence, **Kalman Filters** are useful when dealing with a linear model such as pairs trading, which in its simplest form reduces down to trading the residual of a linear regression:

**Y _{t} = ß_{t} * X_{t} + e_{t}**

Where **Y _{t}** is the current price of the first stock,

**X**is the current price of the second

_{t}**ß**stock, is our current hedge ratio and

_{t}**e**is the current spread price we are trading. We could also estimate the hedge ratio using the log changes in

_{t}**X**and

**Y**, instead of their levels. This would be more likely to be the case in a High Frequency Trading scenario, where all we care about are price changes. The Kalman Filter allows us to vary the hedge ratio over time. For example, suppose we assume the hedge ratio follows a random walk, i.e.

**ß _{t} = ß_{t-1} + w_{t}**

Where **ß _{t}** is the current state of the hedge ratio,

**ß**is the last state and

_{t - 1}**w**is random white noise with mean of zero and volatility.

_{t}The Kalman Filter was designed for estimating the “hidden state” of a linear Gaussian model like Pairs Trading. The filter is based off of a system of equations:

**TransitionEquation : x _{t + 1} = A_{t} x_{t} + w_{t}**

**ObservationEquation : z _{t} = H_{t} x_{t} + e_{t}**

**Where:**

- x
_{t}is the current hidden state (e.g. our hedge ratio) - A
_{t}is the transition matrix (e.g. the identity matrix, I ) - z
_{t}is the latest observation matrix (e.g. the log change of stock x) - w
_{t}, e_{t}are Gaussian white noise with mean zero and variances

**SliceMatrix provides automated trading solutions such as Hedge Ratios, Anomaly Detectors, Portfolio Stress Testers and more. For more ****information about SliceMatrix user friendly interfaces solutions and trading tools, please contact sales at** sales@slicematrix.com or **CLICK ON THE BUTTON BELOW to request an Online Demonstration/Presentation **