The Use of Kalman Filters in Statistical Arbitrage: Personal Study Report

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Personal Study Report

Subject: Quantitative Finance

The Use of Kalman Filters in Statistical


Arbitrage

George Hudson

Period of Research: May 2023 - June 2023


Date of Completion: June 16, 2023
The Use of Kalman Filters in Statistical Arbitrage George Hudson

Contents

1 Introduction 2

2 Background 2

3 Kalman Filters 3
3.1 Definition and Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.2 Model Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.3 Implementation Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

4 Statistical Arbitrage and Pairs Trading 4


4.1 Concepts and Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4.2 Mathematical Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4.3 Challenges and Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

5 Application of Kalman Filters to Pairs Trading 4


5.1 Theoretical Foundation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
5.2 Practical Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
5.3 Dealing with Real-World Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

6 Case Study 5
6.1 Selection of the Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
6.2 Data Preparation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
6.3 Implementation of the Kalman Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
6.4 Signal Generation Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
6.5 Generation of Trading Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
6.6 Implementation of Trading Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

7 Preliminary Results and Analysis 8

8 Sensitivity Analysis 10
8.1 Implementation of Sensitivity Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

9 Results After Sensitivity Analysis 11


9.1 Discussion of Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
9.2 Parameter Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
9.3 Further Exploring of Optimal Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

10 Conclusion 15

Page 1
The Use of Kalman Filters in Statistical Arbitrage George Hudson

Abstract
This research delves into the implementation of the Kalman filter within pairs trading as a technique for
statistical arbitrage. The study seeks to leverage the Kalman filter’s proficiency in estimating hidden states
and projecting outcomes to monitor and predict the price differential between paired assets. By employing
a comprehensive case study and a sensitivity analysis, this research identifies the optimal parameters that
bolster the strategy’s efficacy and consistent profitability. These results underline the critical role of param-
eter optimisation and sensitivity analysis in fine-tuning pairs trading tactics, offering important perspectives
for market players interested in algorithmic trading strategies.

1 Introduction
Pairs trading, a recognized trading tactic, leverages temporary discrepancies from the established long-term
equilibrium of two associated assets. This strategy is predicated on the mean-reverting nature of the spread
between asset pairs, with traders taking simultaneous long and short positions to exploit these deviations.
The success of pairs trading, however, hinges on the precise estimation and prediction of the spread. Con-
ventional techniques, such as basic moving averages and statistical methods, may inadequately capture the
intricate dynamics of this spread. Consequently, the Kalman filter has gained prominence as an efficient tool in
tracking and projecting time series data, positioning it as a fitting solution for pairs trading.
The Kalman filter, a recursive estimator, amalgamates fresh observations with pre-existing knowledge to
deduce hidden states and make informed predictions. It features a dynamic model that apprehends the under-
lying data-driving process and adapts to evolving market scenarios. Utilizing the Kalman filter in pairs trading
offers traders a chance to secure precise spread estimates, facilitating informed trading decisions.
This study aims to scrutinize the application of the Kalman filter within pairs trading and evaluate its
capacity to consistently generate profits. The research provides a thorough explanation of the Kalman filter,
discussing its conceptual basis and model assumptions. It also investigates the mathematical foundation of pairs
trading, spotlighting the challenges and constraints it entails.
The practical application of the Kalman filter in pairs trading is discussed extensively, outlining the theo-
retical groundwork and the steps needed to apply the filter to empirical data. To deliver actionable insights,
an exhaustive case study is conducted, detailing the implementation process and assessing the strategy’s per-
formance.
A sensitivity analysis is conducted to augment the strategy’s efficiency and tackle potential drawbacks. This
process analyzes the influence of varying parameters such as transition covariance, window size, and entry and
exit thresholds on the strategy’s overall returns. The sensitivity analysis results assist in choosing the ideal
parameters for the pairs trading strategy.
The study concludes with a detailed discussion of the outcomes, stressing the enhancements achieved via
parameter optimisation and the importance of sensitivity analysis. This research enriches the existing body
of algorithmic trading knowledge, offering crucial insights to market players considering statistical arbitrage
strategies.

2 Background
Financial markets are complex systems driven by a multitude of factors, including economic indicators, corporate
earnings reports, and geopolitical events. Modeling these markets is a challenging task that has engaged
researchers and practitioners for decades [1].
Traditionally, financial models have often relied on assumptions of stationarity, where a time series has a
constant mean and variance over time. However, many financial time series, such as stock prices and exchange
rates, exhibit non-stationary behavior. They are often integrated of order one, I(1), where the first difference
of the series is stationary. This poses a challenge to modeling and forecasting, as standard techniques may not
be applicable or may lead to spurious results [2].
A common strategy to deal with non-stationarity is to find a linear combination of the series that is stationary.
This is the principle behind cointegration, a concept introduced by Engle and Granger (1987) [2]. Two or more
series are cointegrated if a linear combination of them is stationary. This has profound implications for modeling
long-run relationships among financial time series.
One application of cointegration is pairs trading, a form of statistical arbitrage where two cointegrated stocks
are traded in such a way that it is market neutral. This strategy was pioneered by Nunzio Tartaglia’s team at
Morgan Stanley in the 1980s, and has been widely used in quantitative finance since then [3].
However, the traditional cointegration approach has some limitations. For one, it assumes that the coin-
tegrating vector is constant over time. This is a strong assumption that may not hold in dynamic financial
markets.
This is where the Kalman filter comes in. The Kalman filter is a recursive algorithm introduced by Rudolf
E. Kalman in 1960. It has been widely used in control systems and signal processing to estimate the state of a

Page 2
The Use of Kalman Filters in Statistical Arbitrage George Hudson

system over time under uncertainty. In the context of pairs trading, it can be used to estimate the cointegrating
vector dynamically, providing a more flexible and adaptive approach [4].

3 Kalman Filters
3.1 Definition and Basics
The Kalman filter is a recursive algorithm introduced by Rudolf E. Kalman in 1960 [4]. It provides a method
for estimating the state of a linear dynamic system under uncertainty. The basic setup of the Kalman filter
involves a state space model with two main equations: the state equation and the observation equation.
Let’s denote the state of the system at time t by xt and the observation at time t by zt . The state equation
describes the evolution of the state:

xt = Ft xt−1 + Bt ut + wt (1)
where Ft is the state transition matrix, Bt is the control input matrix, ut is the control vector, and wt is
the process noise which is assumed to be multivariate normal with zero mean and covariance matrix Qt .
The observation equation relates the state to the observation:

zt = Ht xt + vt (2)
where Ht is the observation matrix and vt is the observation noise which is assumed to be multivariate
normal with zero mean and covariance matrix Rt . It is important to note that wt is independent of vt .

3.2 Model Assumptions


The Kalman filter relies on several key assumptions [4] [6]:

• Linearity: The state and observation equations are linear in the state and noise variables. This is a
strong assumption. In cases where the system is non-linear, extended or unscented Kalman filters may be
used [7].
• Normality: The process and observation noises are normally distributed. This is crucial for the optimality
of the Kalman filter [7].

• Markov property: The state at time t depends only on the state at time t − 1. This means that the
state contains all relevant information and there is no need to consider previous states [6].
• Time-invariance: The state transition and observation matrices are constant over time. If this is not
the case, time-varying Kalman filters can be used [6].

3.3 Implementation Details


The Kalman filter proceeds in two steps: prediction and update [4].

• Prediction: The state and its uncertainty are projected forward from time t to time t + 1 using the state
equation.

x̂t|t−1 = Ft x̂t−1|t−1 + Bt ut (3)

Pt|t−1 = Ft Pt−1|t−1 FTt + Qt (4)


T
where x̂t|t−1 is the predicted state estimate, Pt|t−1 is the predicted estimate covariance, and denotes
the transpose.
• Update: The prediction is updated using the observation at time t + 1 and the Kalman gain, which
determines how much weight to give to the prediction and the observation.

Kt = Pt|t−1 HTt (Ht Pt|t−1 HTt + Rt )−1 (5)

x̂t|t = x̂t|t−1 + Kt (zt − Ht x̂t|t−1 ) (6)

Page 3
The Use of Kalman Filters in Statistical Arbitrage George Hudson

Pt|t = (I − Kt Ht )Pt|t−1 (7)

where Kt is the Kalman gain, x̂t|t is the updated state estimate, and Pt|t is the updated estimate covari-
ance.

4 Statistical Arbitrage and Pairs Trading


4.1 Concepts and Definitions
Statistical arbitrage is a type of financial trading strategy that employs statistical methods and computational
algorithms to identify trading opportunities [5]. It aims to exploit relative mispricings of related financial
instruments, such as stocks, by taking a long position in the undervalued instrument and a short position in
the overvalued instrument.
Pairs trading is a popular form of statistical arbitrage [3]. The basic idea is to find a pair of co-integrated
stocks, that is, stocks whose prices move together [2]. When the spread between the prices of the two stocks
diverges significantly from its mean, a trading opportunity arises. The trader bets on the convergence of the
spread by selling short the overpriced stock and buying the under priced stock.

4.2 Mathematical Formulation


Let St1 and St2 denote the prices of two co-integrated stocks at time t. The spread at time t is given by

Zt = St1 − βSt2 (8)


where β is the hedge ratio, typically estimated via ordinary least squares regression. The trader monitors
the spread and, when it diverges by more than a certain threshold from its mean, takes a trading position in
the direction of convergence [3].
The trader’s portfolio value Vt at time t is given by

Vt = qt1 St1 − qt2 St2 (9)


where qt1
and qt2
are the quantities of stock 1 and stock 2 held at time t, respectively. The trader adjusts
the quantities to manage the portfolio’s exposure to the spread.

4.3 Challenges and Limitations


Pairs trading and statistical arbitrage strategies, although promising in theory, face several challenges in practice:

• Model Risk: The performance of the strategy is heavily dependent on the model used to estimate the
hedge ratio and the spread. Inaccurate estimates can lead to poor trading decisions.
• Market Risk: The strategy assumes that the spread will converge to its mean, but this may not always
happen, especially during periods of market stress or structural change.

• Transaction Costs: The strategy involves frequent trading, which can incur significant transaction costs.
These costs can erode the profits of the strategy.
• Execution Risk: There is a risk that the trades cannot be executed at the desired prices due to market
liquidity constraints.

5 Application of Kalman Filters to Pairs Trading


5.1 Theoretical Foundation
Pairs trading is a mean-reverting strategy, premised on the concept that the prices of two historically correlated
assets, when diverging, will ultimately revert to the mean. In order to effectively implement this strategy, one
needs to accurately track and forecast the evolving relationship between these two assets, which is where the
Kalman filter comes into play [4].
The application of the Kalman filter in pairs trading is hinged upon the theory of state space models, which
capture the temporal dynamics in a time-varying parameter, like the hedge ratio (βt ), and their recursive nature
that allows for online updates as new data become available.
The Kalman filter provides an optimal, recursive solution to the problem of estimating the time-varying
hedge ratio (βt ) in the presence of observational noise and system uncertainty. The hedge ratio is assumed

Page 4
The Use of Kalman Filters in Statistical Arbitrage George Hudson

to follow a random walk, while the spread, Zt , which is the difference between the prices of the two assets, is
observed with noise.
We start by defining the state space model for the hedge ratio and the observation model for the spread:

• The state transition model is given by:


βt+1 = βt + wt (10)
2
where wt is a Gaussian system noise term with mean 0 and variance σw .
• The observation model is given by:
Zt = St1 − βt St2 + vt (11)
where vt is a Gaussian observation noise term with mean 0 and variance σv2 .

The Kalman filter estimates the optimal βt by minimizing the mean square error of the estimated spread
[4].

5.2 Practical Implementation


Applying the Kalman filter in pairs trading involves the following steps:

1. First, estimate the initial hedge ratio β0 by ordinary least squares regression of St1 on St2 .
2. For each new observation, update the estimate of the hedge ratio and the spread using the recursive
Kalman filter update and prediction equations, respectively [4].
3. Monitor the spread Zt . When the spread deviates significantly from its mean (indicating a potential price
convergence), place a trading order in the direction of the expected mean reversion. The significance of
deviation can be determined based on the z-score of the spread, typically a value of ±2 standard deviations.

A key advantage of the Kalman filter is its adaptability to changes in the market dynamics, which makes it
particularly suited for pairs trading where the relationship between a pair of assets may change over time [4].

5.3 Dealing with Real-World Data


When working with real-world data, certain practicalities must be addressed:

• Data Cleaning: Real-world data may contain missing values, outliers, and other anomalies that need
to be addressed before applying the Kalman filter. Robust methods for dealing with such issues include
interpolation for missing data, robust statistics for outlier detection, and volatility modeling for irregular
variances.
• Model Validation: It is important to validate the assumptions of the state space model on the real-world
data. One can check for stationarity and cointegration of the spread and adjust the model if necessary.
Additionally, residual diagnostics on the Kalman filter innovations can provide further model validity
checks.
• Noise Estimation: The system and observation noise variances in the state space model, σw 2
and σv2 ,
need to be estimated from the data. This can be done by methods such as the Expectation-Maximization
algorithm or other maximum likelihood estimation methods.
• Transaction Costs: The trading strategy should account for transaction costs, slippage, and market
impact, which can significantly impact the profitability of the strategy. This might involve incorporating
these costs into the trading rules, such as using a cost-adjusted z-score threshold for trade initiation and
termination.

6 Case Study
6.1 Selection of the Pairs
In the application of pairs trading strategy, the selection of the pair of securities is crucial. A good pair typically
involves securities that are closely related or have some fundamental link. For this case study, we focus on two
widely traded currency pairs: the Euro against the US Dollar (EUR/USD) and the British Pound against the
US Dollar (GBP/USD).
The reasons for choosing these pairs are threefold.

Page 5
The Use of Kalman Filters in Statistical Arbitrage George Hudson

Firstly, both these pairs are among the most liquid and heavily traded currency pairs in the forex market.
This ensures that there is sufficient data for our analysis and also means that the impact of our trades on the
market is negligible, eliminating the possibility of price manipulation.
Secondly, these two currency pairs are fundamentally related. Both EUR and GBP share a significant
correlation due to their close geographical proximity and interdependent economies. Therefore, the exchange
rates of these currencies against the USD tend to be influenced by similar economic events and news, causing
them to move in tandem over time.
Finally, the exchange rates of these two currency pairs have shown considerable co-integration historically.
Co-integration is a critical property for pairs trading, as it implies that the spread between the pairs tends to
be mean-reverting. This property is what allows us to profit from the deviations in the spread when they occur.
Note that while these currency pairs are good candidates for pairs trading due to the reasons mentioned
above, the success of the strategy also relies heavily on the correct implementation of the pairs trading rules,
risk management, and the choice of parameters for the Kalman filter.

6.2 Data Preparation


Before we delve into the implementation of our pairs trading strategy, we first need to acquire and prepare our
data. For this case study, we make use of daily closing prices for the chosen currency pairs: EUR/USD and
GBP/USD. The data is sourced from Yahoo Finance, a reliable and widely used platform providing historical
financial data.
We leverage Python’s yfinance library to download the desired financial data, and make use of pandas to
store the downloaded data in a dataframe.
1 import yfinance as yf
2 import pandas as pd
3
4 def download_data ( tickers , start_date , end_date ) :
5 data = []
6 for ticker in tickers :
7 try :
8 data . append ( yf . download ( ticker , start = start_date , end = end_date ) [ ’ Close ’ ])
9 except Exception as e :
10 print ( f " Error occurred while downloading data for { ticker }: { e } " )
11 return None
12 data = pd . concat ( data , axis =1)
13 data . columns = [ ’ EUR / USD ’ , ’ GBP / USD ’] # setting column names explicitly
14 return data

At this stage, we perform a preliminary examination of the data to ensure its quality. This includes checking
for missing or null values and ensuring that the data types are correct for our computations. This can be done
as follows:
1 print ( data . isnull () . sum () )
2 print ( data . dtypes )

The methodology is applicable to different types of price data and can be adjusted to fit different trading styles,
from high-frequency intraday trading to lower frequency daily or weekly trading.

6.3 Implementation of the Kalman Filter


The Kalman Filter is a crucial component in our pairs trading strategy as it enables us to dynamically estimate
the hedge ratio between the EUR/USD and GBP/USD currency pairs. We utilize the pyKalman library to
establish a Kalman Filter with predefined parameters:
1 from pykalman import KalmanFilter
2
3 def a pp l y _k a lman_filter ( data , trans_cov =0.01) :
4 kf = KalmanFilter ( transition_matrices = [1] ,
5 observation_matrices = [[1]] ,
6 initial_state_mean = 0 ,
7 i n it i a l _s t a t e_ c o v ar i a n ce = 1 ,
8 ob se rv ati on _c ova ri an ce =1 ,

Page 6
The Use of Kalman Filters in Statistical Arbitrage George Hudson

9 tra nsit ion_c ovar ianc e = trans_cov )


10 means = np . zeros ( len ( data ) )
11 covs = np . zeros ( len ( data ) )
12 mean = kf . initial_state_mean
13 cov = kf . i n it i a l _s t a t e_ c o v ar i a n ce
14 for t in range ( len ( data ) ) :
15 obs_matrix = np . array ([ data [ ’ GBP / USD ’ ]. iat [ t ]])
16 mean , cov = kf . filter_update ( filtered_state_mean = mean ,
17 f i l t e r e d _ s t at e _ c o v a r i a n c e = cov ,
18 observation = data [ ’ EUR / USD ’ ]. iat [ t ] ,
19 observation_matrix = obs_matrix )
20 means [ t ] = mean
21 covs [ t ] = cov
22 data [ ’ Hedge Ratio ’] = - means
23 return data

Our function, apply kalman filter, carries out a series of steps in each time step. Firstly, we modify the obser-
vation matrix with the present observed value of the GBP/USD pair. Subsequently, we call the filter update
method. This method carries out one iteration of the Kalman Filter’s prediction and update cycle.
We store the updated hedge ratio at each time step in a new column called ’Hedge Ratio’ in our data.
The calculated ’Hedge Ratio’ serves a significant role in determining the spread between our selected currency
pairs, which is a critical component in the generation of our trading signals.

6.4 Signal Generation Process


6.5 Generation of Trading Signals
Our trading signals are derived from the spread between the two currency pairs and their associated Z-Score.
This entire process has been encapsulated within the function calculate spread and z score, which takes as
arguments the data frame of currency pairs and an optional rolling window size (defaulting to 60 trading days).
The function first computes the spread as the difference between the EUR/USD price and the product of the
GBP/USD price and the hedge ratio. Subsequently, the rolling mean and standard deviation of the spread are
calculated over the specified window. Finally, the Z-Score is calculated, expressing the deviation of the spread
from its mean in terms of the standard deviation:
1 def c a l c u l a t e _ s p r e a d _ a n d _ z _ s c o r e ( data , window =60) :
2 data [ ’ Spread ’] = data [ ’ EUR / USD ’] - data [ ’ Hedge Ratio ’ ]* data [ ’ GBP / USD ’]
3 data [ ’ Mean Spread ’] = data [ ’ Spread ’ ]. rolling ( window = window ) . mean ()
4 data [ ’ Std Spread ’] = data [ ’ Spread ’ ]. rolling ( window = window ) . std ()
5 data [ ’Z - Score ’] = ( data [ ’ Spread ’] - data [ ’ Mean Spread ’ ]) / data [ ’ Std Spread ’]
6 return data

The calculated Z-Score is the foundation for our trading signals, providing valuable insights into potential
trading opportunities. When the Z-Score falls below -2, it signals a potential opportunity to go long on the
spread, in anticipation of a market mean reversion. Conversely, a Z-Score exceeding 2 indicates an opportunity
to short the spread.
To validate the assumption of mean reversion of our trading strategy, we also check the stationarity of the
spread residuals using the Augmented Dickey-Fuller (ADF) test:
1 from statsmodels . tsa . stattools import adfuller
2
3 def ch ec k_ stationarity ( data ) :
4 residuals = data [ ’ Spread ’] - data [ ’ Mean Spread ’]
5 p_value = adfuller ( residuals . dropna () ) [1]
6 print ( f ’ ADF p - value : { p_value :.5 f } ’)

The function check stationarity computes the residuals from the spread and the mean spread, then checks
for stationarity using the ADF test. The p-value returned from the test should provides evidence against the
null hypothesis of a unit root, confirming our assumption of mean reversion.

Page 7
The Use of Kalman Filters in Statistical Arbitrage George Hudson

6.6 Implementation of Trading Strategy


The foundation of our trading strategy is the Z-Score signals. We’ve embedded the entire strategy inside the
apply trading strategy function. This function takes as input the data frame of currency pairs and Z-Score,
along with entry and exit thresholds for long and short positions.
The function first defines binary long and short entry signals, triggered when the Z-Score crosses below and
above the negative and positive entry thresholds respectively. Similarly, binary long and short exit signals are
created when the Z-Score reverts back to the negative and positive exit thresholds respectively.
Subsequently, the function initializes long and short positions and sets the positions to 1 and -1 whenever a
long or short entry signal is true. It resets these positions to 0 whenever a long or short exit signal is true. To
fill any NaN values in the position signals, the function employs forward filling.
Finally, it sums the long and short positions to arrive at the final trading position. This comprehensive
process results in a trading strategy that enters and exits trades based on the mean reversion of the spread
between the two currency pairs:
1 def a p p l y _ t r a din g_ st rat eg y ( data , entry_threshold , exit_threshold ) :
2 data [ ’ Long Entry ’] = ( data [ ’Z - Score ’] < - entry_threshold )
3 data [ ’ Long Exit ’] = ( data [ ’Z - Score ’] >= exit_threshold )
4 data [ ’ Short Entry ’] = ( data [ ’Z - Score ’] > entry_threshold )
5 data [ ’ Short Exit ’] = ( data [ ’Z - Score ’] <= - exit_threshold )
6 data [ ’ Long Positions ’] = np . nan
7 data . loc [ data [ ’ Long Entry ’] , ’ Long Positions ’] = 1
8 data . loc [ data [ ’ Long Exit ’] , ’ Long Positions ’] = 0
9 data [ ’ Short Positions ’] = np . nan
10 data . loc [ data [ ’ Short Entry ’] , ’ Short Positions ’] = -1
11 data . loc [ data [ ’ Short Exit ’] , ’ Short Positions ’] = 0
12 data [ ’ Long Positions ’ ]. fillna ( method = ’ ffill ’ , inplace = True )
13 data [ ’ Short Positions ’ ]. fillna ( method = ’ ffill ’ , inplace = True )
14 data [ ’ Positions ’] = data [ ’ Long Positions ’] + data [ ’ Short Positions ’]
15 return data

7 Preliminary Results and Analysis


Before proceeding with any optimisation or sensitivity analysis, we present the initial results of our trading strat-
egy. By evaluating these results using various metrics and visualizations, we provide a preliminary assessment
of the strategy’s performance.
We start by visualizing the Z-Score over time. This graphical representation gives an overview of potential
long and short entry points for our trading strategy. Similarly, the positions taken by the strategy over time
are plotted. Finally, we calculate and visualize the cumulative returns from the strategy.
To perform these tasks, we encapsulate the necessary computations and plotting instructions in a series of
Python functions: plot strategy, calculate returns, and plot returns.
1 def plot_strategy ( data ) :
2 fig , ( ax1 , ax2 ) = plt . subplots (2 , 1 , figsize =(15 ,7) )
3 data [ ’Z - Score ’ ]. plot ( ax = ax1 , color = ’ darkblue ’)
4 ax1 . fill_between ( data . index , -2 , 2 , color = ’ gray ’ , alpha =0.3)
5 ax1 . set_title ( ’Z - Score of EUR / USD and GBP / USD Spread Over Time ’)
6 ax1 . set_ylabel ( ’Z - Score ’)
7 ax1 . grid ( True )
8 data [ ’ Positions ’ ]. plot ( ax = ax2 , color = ’ blue ’ , linestyle = ’ -- ’)
9 ax2 . set_title ( ’ Trading Positions Over Time ’)
10 ax2 . set_ylabel ( ’ Position ’)
11 ax2 . set_xlabel ( ’ Date ’)
12 ax2 . grid ( True )
13 plt . tight_layout ()
14 plt . show ()

Page 8
The Use of Kalman Filters in Statistical Arbitrage George Hudson

Figure 1: Z-Score of EUR/USD and GBP/USD Spread Over Time

Figure 2: Trading Positions Over Time

We calculate and visualize the cumulative returns from the trading strategy as follows:
1 def c alcula te_returns ( data ) :
2 data [ ’ EUR / USD Returns ’] = data [ ’ EUR / USD ’ ]. pct_change ()
3 data [ ’ GBP / USD Returns ’] = data [ ’ GBP / USD ’ ]. pct_change ()
4 data [ ’ Strategy Returns ’] = data [ ’ EUR / USD Returns ’] * data [ ’ Positions ’] - data [ ’
GBP / USD Returns ’] * data [ ’ Positions ’]
5 data [ ’ Cumulative Strategy Returns ’] = (1 + data [ ’ Strategy Returns ’ ]) . cumprod ()
6 return data
7
8 def plot_returns ( data ) :
9 plt . figure ( figsize =(12 ,6) )
10 data [ ’ Cumulative Strategy Returns ’ ]. plot ()
11 plt . title ( ’ Cumulative Strategy Returns ’)
12 plt . show ()

Page 9
The Use of Kalman Filters in Statistical Arbitrage George Hudson

Figure 3: Cumulative Strategy Returns Over Time

The cumulative returns plot (Figure 3), although providing valuable insights into the overall performance
of the strategy, presents a somewhat choppy picture. This irregularity in the cumulative returns indicates
potential improvements in the strategy or even optimisation of certain parameters for better performance. This
inherent variability is where the importance of the sensitivity analysis comes to light. Sensitivity analysis, by
experimenting with different parameter choices, provides a means to evaluate the robustness of our strategy
and potentially smooth out the choppiness observed in the returns plot, thus leading to more consistent profits.

8 Sensitivity Analysis
In this section, we introduce the concept of sensitivity analysis and how it is applied in our trading strategy.
Sensitivity analysis is a technique used to determine how different values of an independent variable will impact
a particular dependent variable under a given set of assumptions. In our case, we aim to determine how the
change in parameters, such as the transition covariance, window size, entry and exit thresholds, influence the
total returns of our trading strategy.

8.1 Implementation of Sensitivity Analysis


Our approach for the sensitivity analysis involves systematically varying the four parameters that define our
trading strategy. These are: the transition covariance used in the Kalman filter, the window size for calculating
the rolling mean and standard deviation of the spread, and the entry and exit thresholds for our trading signals.
By iteratively adjusting these parameters, we are able to simulate the performance of different trading strategies
and, thereby, evaluate their robustness.
The Python code used to implement this sensitivity analysis is shown below:
1 def s e n s i t i v ity_analysis ( data , transition_covs , window_sizes , entry_thresholds ,
exit_thresholds ) :
2 results = []
3 for trans_cov in transition_covs :
4 for window in window_sizes :
5 for entry_threshold in entry_thresholds :
6 for exit_threshold in exit_thresholds :
7 data_copy = data . copy ()
8 data_copy = apply_kalman_filter ( data_copy , trans_cov )
9 data_copy = c a l c u l a t e _ s p r e a d _ a n d _ z _ s c o r e ( data_copy , window )
10 data_copy = ap pl y_ tra di ng _st ra te gy ( data_copy , entry_threshold ,
exit_threshold )
11 data_copy = calculate_returns ( data_copy )
12 total_return = data_copy [ ’ Cumulative Strategy Returns ’ ]. iloc [ -1]

Page 10
The Use of Kalman Filters in Statistical Arbitrage George Hudson

13 results . append (( trans_cov , window , entry_threshold ,


exit_threshold , total_return ) )
14 results_df = pd . DataFrame ( results , columns =[ ’ Transition Covariance ’ , ’ Window Size
’ , ’ Entry Threshold ’ , ’ Exit Threshold ’ , ’ Total Return ’ ])
15 return results_df

The output of the sensitivity analysis will be a data frame that contains the total returns for each combination
of parameter values. This comprehensive overview allows us to identify the set of parameters that yield the
most favourable performance for our trading strategy.
In the subsequent section, we will delve into the results and implications of this sensitivity analysis.

9 Results After Sensitivity Analysis


Upon completion of the sensitivity analysis, we aim to visualize and interpret the outcomes. The Python main
function orchestrates this process. It downloads the data, initiates the sensitivity analysis, discovers the optimal
parameters, generates plots, and calculates the returns for these optimal parameters.
The function first carries out a grid search over a range of parameter values for transition covariance,
window size, and entry and exit thresholds. Following this, the best parameters that maximize the total return
are identified.
For visualization purposes, two 3D plots are constructed: one to illustrate the impact of transition covariance
and window size on total returns (keeping the entry and exit thresholds constant), and another to display the
influence of entry and exit thresholds on total returns (with constant transition covariance and window size).
1 def main () :
2 start_date = ’ 2018 -06 -11 ’
3 end_date = ’ 2023 -06 -11 ’
4 data = download_data ([ ’ EURUSD = X ’ , ’ GBPUSD = X ’] , start_date , end_date )
5
6 # Run sensitivity analysis
7 transition_covs = np . linspace (0.01 , 1 , 5)
8 window_sizes = np . linspace (30 , 90 , 5 , dtype = int )
9 entry_thresholds = np . linspace (0.05 , 0.15 , 5)
10 exit_thresholds = np . linspace (0.05 , 0.15 , 5)
11 results_df = sensitivity_analysis ( data , transition_covs , window_sizes ,
entry_thresholds , exit_thresholds )
12
13 # Find the best parameters
14 best_params = results_df . loc [ results_df [ ’ Total Return ’ ]. idxmax () ]
15 best_trans_cov = best_params [ ’ Transition Covariance ’]
16 best_window_size = int ( best_params [ ’ Window Size ’ ]) # make sure it ’s an integer
17 b e s t _ e n t ry_threshold = best_params [ ’ Entry Threshold ’]
18 b es t _ ex i t_threshold = best_params [ ’ Exit Threshold ’]
19
20 # 3 D plotting
21 from mpl_toolkits . mplot3d import Axes3D
22 fig = plt . figure ( figsize =(14 , 7) )
23
24 # Transition Covariance and Window Size
25 ax1 = fig . add_subplot (121 , projection = ’3 d ’)
26 ax1 . set_xlabel ( ’ Transition Covariance ’)
27 ax1 . set_ylabel ( ’ Window Size ’)
28 ax1 . set_zlabel ( ’ Total Return ’)
29 pivot_table = results_df [ results_df [ ’ Entry Threshold ’] == 0.05][ results_df [ ’ Exit
Threshold ’] == 0.05]. pivot ( ’ Transition Covariance ’ , ’ Window Size ’ , ’ Total
Return ’)
30 X1 , Y1 = np . meshgrid ( pivot_table . columns , pivot_table . index )
31 surf1 = ax1 . plot_surface ( X1 , Y1 , pivot_table . values , cmap = ’ viridis ’)
32
33 # Entry Threshold and Exit Threshold
34 ax2 = fig . add_subplot (122 , projection = ’3 d ’)
35 ax2 . set_xlabel ( ’ Entry Threshold ’)
36 ax2 . set_ylabel ( ’ Exit Threshold ’)
37 ax2 . set_zlabel ( ’ Total Return ’)

Page 11
The Use of Kalman Filters in Statistical Arbitrage George Hudson

38 pivot_table = results_df [ results_df [ ’ Transition Covariance ’] == 0.01][ results_df [


’ Window Size ’] == 30]. pivot ( ’ Entry Threshold ’ , ’ Exit Threshold ’ , ’ Total
Return ’)
39 X2 , Y2 = np . meshgrid ( pivot_table . columns , pivot_table . index )
40 surf2 = ax2 . plot_surface ( X2 , Y2 , pivot_table . values , cmap = ’ viridis ’)
41
42 plt . show ()
43
44 # Run the strategy using the best parameters
45 data = a pply_kalman_filter ( data , best_trans_cov )
46 data = c a l c u l a t e _ s p r e a d _ a n d _ z _ s c o r e ( data , best_window_size )
47 ch ec k_ st ationarity ( data )
48 data = a pp ly _tr ad in g_s tr at eg y ( data , best_entry_threshold , best_exit_threshold )
49 plot_strategy ( data )
50 data = calculate_returns ( data )
51 plot_returns ( data )
52
53 print ( f " Best Transition Covariance : { best_trans_cov } " )
54 print ( f " Best Window Size : { best_window_size } " )
55 print ( f " Best Entry Threshold : { best_entry_threshold } " )
56 print ( f " Best Exit Threshold : { best_exit_threshold } " )
57 print ( f " Total Return with Best Parameters : { best_params [ ’ Total Return ’]} " )
58
59 if __name__ == ’ __main__ ’:
60 main ()

9.1 Discussion of Results


In this subsection, we will discuss the results of the sensitivity analysis conducted on the Kalman filter-based
pairs trading strategy. The analysis involved two 3D plots to explore the impact of different parameters on
the total returns. The first plot focused on the transition covariance and window size, while the second plot
examined the entry and exit thresholds. The cumulative returns plot (Figure 3) obtained from the initial
strategy implementation indicated some choppiness, suggesting room for improvement and optimisation.
The first 3D plot, which depicts the relationship between window size, transition covariance, and total
returns, revealed interesting insights. It was observed that smaller values of transition covariance, particularly
close to 0, tend to yield more promising total returns. This suggests that a higher degree of confidence in the
state transition model, with less uncertainty in the prediction, can lead to improved performance. Additionally, a
window size of approximately 30 demonstrated a positive impact on total returns. This indicates that considering
a moderate historical window size for calculating the mean and standard deviation of the spread enhances the
effectiveness of the strategy.
The second 3D plot explored the influence of entry and exit thresholds on total returns, keeping the transition
covariance and window size constant. The analysis highlighted that entry and exit thresholds around the same
size, specifically at 0.075, resulted in the best total returns. This implies that selecting thresholds that are too
tight may lead to missed trading opportunities, while excessively loose thresholds might introduce unwanted
noise and false signals. Finding a balance between sensitivity and selectivity appears crucial for maximizing
profitability in the pairs trading strategy.
Overall, the sensitivity analysis suggests that the choice of entry and exit thresholds has a more significant
impact on the total returns compared to the transition covariance and window size. While the latter parameters
contribute to the strategy’s performance, fine-tuning the thresholds can potentially lead to more consistent
profits. It is worth noting that the optimal parameter values identified through the sensitivity analysis may
vary depending on the specific dataset and market conditions, and ongoing monitoring and periodic recalibration
of the strategy might be necessary to adapt to changing dynamics.
The insights gained from the sensitivity analysis provide valuable guidance for enhancing the performance
and robustness of the pairs trading strategy. By refining the parameter choices based on the analysis results, it
is possible to address the choppiness observed in the cumulative returns plot and achieve smoother and more
consistent returns. The optimisation process allows for a deeper understanding of the strategy’s behavior and
facilitates better decision-making in real-world trading scenarios.

Page 12
The Use of Kalman Filters in Statistical Arbitrage George Hudson

Figure 4: 3D Plots for Sensitivity Analysis

9.2 Parameter Evaluation


In this subsection, we will evaluate the best parameters obtained from the sensitivity analysis, which are crucial
for optimizing the performance of our trading strategy. These parameters have been determined based on their
ability to generate the maximum total return. Furthermore, we will assess the performance of the trading
strategy utilizing these best parameters.
The sensitivity analysis conducted in the previous section provided valuable insights into the impact of
various parameters on the trading strategy’s cumulative returns. By examining the 3D plots and analyzing the
relationships between different parameter combinations, we identified the best parameters that yield the most
promising results. The analysis revealed that the best transition covariance is 0.01 and the best window size is
30. Additionally, the optimal entry and exit thresholds were found to be 0.075.
These best parameters were determined based on their ability to improve the strategy’s performance and
address the choppiness observed in the cumulative returns plot. They represent a delicate balance between
sensitivity and selectivity, allowing for efficient identification of trading opportunities while minimizing false
signals.
To evaluate the trading strategy’s performance with the best parameters, we conducted backtesting using
historical data. The resulting cumulative returns plot (Figure 6) demonstrates the effectiveness of the strategy
in generating profits when implemented with the optimized parameters. The plot shows a steady upward trend,
indicating consistent and positive returns over time.
(Figure 5) provides additional insight into the strategy’s performance by displaying the z-score and positions
taken throughout the trading period. The z-score is a measure of the deviation of the spread from its mean,
indicating the relative attractiveness of the trading opportunity. The positions graph depicts the trading
decisions made based on the calculated z-scores. It is evident that the strategy takes advantage of the positive
and negative deviations in the spread, generating profitable trades.

Page 13
The Use of Kalman Filters in Statistical Arbitrage George Hudson

Figure 5: z-Score and possitions

The total return achieved with the best parameters is 1.5216095995358812. This metric serves as a quantita-
tive assessment of the trading strategy’s profitability and validates the effectiveness of the optimized parameter
settings. It represents the cumulative percentage return obtained by executing the strategy over the trading
period.

Figure 6: Trading Strategy with Best Parameters

By utilizing the best parameters identified through the sensitivity analysis, we have improved the overall
performance and robustness of our pairs trading strategy. These parameters have proven to be effective in
generating consistent profits and minimizing the choppiness observed in the cumulative returns plot.
It is important to note that while the best parameters have been determined based on the current dataset and
market conditions, they may require periodic re-calibration to adapt to evolving dynamics. Ongoing monitoring
and evaluation of the strategy’s performance will allow for timely adjustments and optimisation as market
conditions change.
In the next sections, we will further explore the choice of parameters and then discuss the conclusion and

Page 14
The Use of Kalman Filters in Statistical Arbitrage George Hudson

summarize the key findings of our research on the use of the Kalman filter in pairs trading, highlighting the
significance of sensitivity analysis and parameter optimisation in enhancing the strategy’s performance and
profitability.

9.3 Further Exploring of Optimal Parameters


Running the sensitivity analysis in section 8.1 again, this time using a wider range of parameter values can give
us greater insight into the way the strategy behaves, see (Figure 7).

Figure 7: 3D Plots for Sensitivity Analysis over larger range

The right 3d plot strongly suggests that an entry and exit around 0.075 is favourable whilst the left 3d plot
suggests that the smaller the window size and smaller the transition covariance the lager the total return.
The optimal window size indicates the number of most recent data points used in our calculations. In this
case, the smaller window size of 10 suggests that our pairs trading strategy performed better when it was more
responsive to recent changes in the market. Financial markets are inherently dynamic and non-stationary;
relationships between financial instruments shift over time. By using a smaller window size, the strategy could
quickly adapt to these shifts, potentially leading to better performance.
The transition covariance in our Kalman filter model represents the estimated uncertainty or variability in the
transition of the hidden state. The optimal transition covariance of 0.01 implies that our model changes slowly
over time, which is aligned with our assumption of the relatively stable relationship between the EUR/USD and
GBP/USD pairs in the short term. A smaller transition covariance placed more weight on recent observations
and reduced drastic adjustments in the hedge ratio. This could result in more stable and reliable trading signals.
However, it’s important to note that while these parameters delivered the best results in our backtesting,
their performance in live trading could vary due to factors like transaction costs and market impacts. Therefore,
out-of-sample testing or cross-validation is recommended to confirm the robustness of our model parameters
and to prevent overfitting to our historical data.

10 Conclusion
In conclusion, this research project explored the application of the Kalman filter in pairs trading as a statistical
arbitrage strategy. The study aimed to leverage the Kalman filter’s ability to estimate hidden states and
make accurate predictions about the spread between two assets. Through a comprehensive case study and
subsequent sensitivity analysis, we gained valuable insights into the strategy’s performance and identified optimal
parameters for improved profitability.
The Kalman filter, as discussed in Section 3, proved to be a powerful tool for tracking and forecasting the
spread between asset pairs. By incorporating historical data, the filter can dynamically adjust its estimates,
taking into account the evolving market conditions. This adaptive nature of the Kalman filter enables it
to capture the mean-reverting behavior often observed in pairs trading, making it a suitable choice for this
application.

Page 15
The Use of Kalman Filters in Statistical Arbitrage George Hudson

The case study presented in Section 6 demonstrated the practical implementation of the Kalman filter-based
pairs trading strategy. By selecting appropriate asset pairs, preparing the data, and implementing the Kalman
filter, we generated trading signals based on the calculated spread. These signals were then utilized to execute
trading positions, resulting in cumulative returns.
However, the initial implementation of the strategy, as indicated by the cumulative returns plot (Figure
3), exhibited some choppiness, suggesting room for improvement. This prompted us to conduct a sensitivity
analysis to assess the impact of different parameter choices on the strategy’s performance.
The sensitivity analysis, as discussed in Section 8, provided significant insights into parameter optimisation.
Two 3D plots were constructed to evaluate the influence of transition covariance, window size, entry thresholds,
and exit thresholds on total returns. The analysis revealed that a smaller transition covariance, closer to 0,
and smaller window sizes of approximately 10-30 yielded the most promising total returns. Interestingly, the
analysis also indicated that the best entry and exit thresholds were around the same value, specifically at 0.075.
While it may seem unusual for both thresholds to be identical, this finding suggests that a balanced sensitivity
and selectivity approach contributes to maximizing profitability.
We then increased the range of parameters for the sensitivity analysis, this concluded that 0.075 was the
best parameters for entry and exit thresholds but the 3d plot of window size and transition covariance hinted
at the favourability of small window size and small transition covariance.
The research project highlights the importance of sensitivity analysis and parameter optimisation in en-
hancing the performance and robustness of the Kalman filter-based pairs trading strategy. By fine-tuning the
parameters based on the analysis results, we were able to address the choppiness observed in the cumulative
returns plot, leading to smoother and more consistent profits.
It is important to note that while the best parameters have been identified for this specific dataset and
market conditions, ongoing monitoring and periodic recalibration of the strategy may be necessary to adapt to
changing dynamics. Markets are inherently dynamic, and parameters that yield optimal performance in one
period may require adjustment in another.
In conclusion, the use of the Kalman filter in pairs trading offers a powerful approach for capitalizing on
mean-reverting behavior in asset pairs. Through careful parameter selection and optimisation, as demonstrated
in this research, the strategy can generate consistent profits and outperform traditional trading approaches. The
findings of this study contribute to the growing body of knowledge in algorithmic trading and provide valuable
insights for market participants seeking to employ statistical arbitrage strategies.

References
[1] Campbell, J. Y., Lo, A. W., & MacKinlay, A. C. The Econometrics of Financial Markets. Princeton Uni-
versity Press, 1997.
[2] Engle, R. F., & Granger, C. W. J. Co-integration and error correction: Representation, estimation, and
testing. Econometrica: Journal of the Econometric Society, 251-276, 1987.
[3] Gatev, E., Goetzmann, W. N., & Rouwenhorst, K. G. Pairs Trading: Performance of a Relative-Value
Arbitrage Rule. The Review of Financial Studies, 19(3), 797-827, 2006.
[4] Kalman, R. E. A New Approach to Linear Filtering and Prediction Problems. Journal of Basic Engineering,
82(1), 35-45, 1960.
[5] Narang, R. K. Inside the Black Box: A Simple Guide to Quantitative and High Frequency Trading. Wiley,
2013.
[6] Welch, G., & Bishop, G. An Introduction to the Kalman Filter. University of North Carolina at Chapel Hill,
2006.
[7] Simon, D. Optimal State Estimation: Kalman, H Infinity, and Nonlinear Approaches. John Wiley & Sons,
2006.

Page 16

You might also like