Professional Documents
Culture Documents
The Use of Kalman Filters in Statistical Arbitrage: Personal Study Report
The Use of Kalman Filters in Statistical Arbitrage: Personal Study Report
The Use of Kalman Filters in Statistical Arbitrage: Personal Study Report
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
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
8 Sensitivity Analysis 10
8.1 Implementation of Sensitivity Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
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 .
• 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].
• Prediction: The state and its uncertainty are projected forward from time t to time t + 1 using the state
equation.
Page 3
The Use of Kalman Filters in Statistical Arbitrage George Hudson
where Kt is the Kalman gain, x̂t|t is the updated state estimate, and Pt|t is the updated estimate covari-
ance.
• 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.
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 Kalman filter estimates the optimal βt by minimizing the mean square error of the estimated spread
[4].
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].
• 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.
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.
Page 6
The Use of Kalman Filters in Statistical Arbitrage George Hudson
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.
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
Page 8
The Use of Kalman Filters in Statistical Arbitrage George Hudson
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
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.
Page 10
The Use of Kalman Filters in Statistical Arbitrage George Hudson
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.
Page 11
The Use of Kalman Filters in Statistical Arbitrage George Hudson
Page 12
The Use of Kalman Filters in Statistical Arbitrage George Hudson
Page 13
The Use of Kalman Filters in Statistical Arbitrage George Hudson
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.
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.
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