pairs trading · 2020. 12. 28. · design of a pairs trading strategy we first focus on pairs...

63
Pairs Trading Prof. Daniel P. Palomar MAFS5310 - Portfolio Optimization with R MSc in Financial Mathematics The Hong Kong University of Science and Technology (HKUST) Fall 2020-21

Upload: others

Post on 11-Mar-2021

8 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Pairs Trading

Prof. Daniel P. Palomar

MAFS5310 - Portfolio Optimization with RMSc in Financial Mathematics

The Hong Kong University of Science and Technology (HKUST)Fall 2020-21

Page 2: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Outline1 Cointegration

2 Basic Idea of Pairs Trading

3 Design of Pairs Trading

Pairs selectionCointegration testOptimum threshold

4 LS Regression and Kalman for Pairs Trading

5 From Pairs Trading to Statistical Arbitrage (StatArb)∗

VECMOptimization of mean-reverting portfolio (MRP)

6 Summary

Page 3: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Outline1 Cointegration

2 Basic Idea of Pairs Trading

3 Design of Pairs Trading

Pairs selectionCointegration testOptimum threshold

4 LS Regression and Kalman for Pairs Trading

5 From Pairs Trading to Statistical Arbitrage (StatArb)∗

VECMOptimization of mean-reverting portfolio (MRP)

6 Summary

Page 4: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

CointegrationCointegration is a very interesting property that can be exploited in finance for trading.Idea: While it may be difficult to predict individual stocks, it may be easier to predictrelative behavior of stocks.Illustrative example: A drunk man is wandering the streets (random walk) with a dog.Both paths of man and dog are nonstationary and difficult to predict, but the distancebetween them is mean-reverting and stationary.

D. Palomar (HKUST) Pairs Trading 4 / 63

Page 5: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Correlation vs. cointegration

Everybody is familiar with the concept of correlation between two random variables:correlation is high when they co-movecorrelation is zero when they move independently

So what is cointegration?cointegration is high when two quantities move together or remain close to each othercointegration is inexistent if the two quantities do not stay together

Clear? You can see why this concept may be difficult to grasp at first, but the truth isthat it’s easy.1In the financial context:

Cointegration of (log-)prices yt refers to long-term co-movements.Correlation of (log-)returns ∆yt = yt − yt−1 characterizes short-term co-movements in(log-)prices yt.

1Y. Feng and D. P. Palomar, A Signal Processing Perspective on Financial Engineering. Foundations andTrends in Signal Processing, Now Publishers, 2016.

D. Palomar (HKUST) Pairs Trading 5 / 63

Page 6: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Correlation vs. cointegrationExample of high correlation with no cointegration:

0 20 40 60 80 100 120 140 160 180 200−1

0

1

2

3

4

5

y1ty2ty1t − y2t

D. Palomar (HKUST) Pairs Trading 6 / 63

Page 7: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Correlation vs. cointegrationIndeed the returns are highly correlated, see scatter plot:

−1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1−1

−0.8

−0.6

−0.4

−0.2

0

0.2

0.4

0.6

0.8

1

Log−returns of stock 1

Lo

g−

retu

rns o

f sto

ck 2

D. Palomar (HKUST) Pairs Trading 7 / 63

Page 8: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Correlation vs. cointegrationOpposite example of high cointegration with no correlation:

0 20 40 60 80 100 120 140 160 180 200−3.5

−3

−2.5

−2

−1.5

−1

−0.5

0

0.5

1

y1ty2ty1t − y2t

D. Palomar (HKUST) Pairs Trading 8 / 63

Page 9: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Correlation vs. cointegrationIndeed the returns are not correlated, see scatter plot:

−1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1−1

−0.8

−0.6

−0.4

−0.2

0

0.2

0.4

0.6

0.8

1

Log−returns of stock 1

Lo

g−

retu

rns o

f sto

ck 2

D. Palomar (HKUST) Pairs Trading 9 / 63

Page 10: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Cointegration

A time series is called integrated of order p, denoted as I(p), if the time series obtainedby differencing the time series p times is weakly stationary,while by differencing the time series p − 1 times is not weakly stationary.

Example: stock log-prices yt are integrated of order I(1) becauselog-prices are not stationarybut log-returns yt − yt−1 are stationary (at least for some period of time).

A multivariate time series is said to be cointegrated if it has at least one linearcombination being integrated of a lower order, e.g., yt is not stationary but wTyt isstationary for some weights w.

D. Palomar (HKUST) Pairs Trading 10 / 63

Page 11: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

CointegrationConsider the following two nonstationary time series (e.g., log-prices of stocks):

y1t = γxt + w1t

y2t = xt + w2t

with a stochastic common trend defined as a random walk:xt = xt−1 + wt

where w1t, w2t, wt are i.i.d. residual terms mutually independent.The coefficient γ is the secret ingredient here.If γ is known, then we can define the so-called “spread”

zt = y1t − γy2t = w1t − γw2t

which is stationary and mean reverting.Interestingly, the differences (i.e., log-returns) ∆y1t and ∆y2t can have an arbitrarily smallcorrelation: ρ = 1/

(√1 + 2σ2

1/σ2√

1 + 2σ22/σ2

).

D. Palomar (HKUST) Pairs Trading 11 / 63

Page 12: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Cointegration

The log-prices y1t and y2t are cointegrated and the spread zt = y1t − γy2t is stationary(assume γ = 1):

0 20 40 60 80 100 120 140 160 180 200−3.5

−3

−2.5

−2

−1.5

−1

−0.5

0

0.5

1

y1ty2ty1t − y2t

D. Palomar (HKUST) Pairs Trading 12 / 63

Page 13: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Outline1 Cointegration

2 Basic Idea of Pairs Trading

3 Design of Pairs Trading

Pairs selectionCointegration testOptimum threshold

4 LS Regression and Kalman for Pairs Trading

5 From Pairs Trading to Statistical Arbitrage (StatArb)∗

VECMOptimization of mean-reverting portfolio (MRP)

6 Summary

Page 14: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Basic Idea of Pairs Trading

Recall that if two time series are cointegrated, then in the long term they remain close toeach other.In other words, the spread zt = y1t − γy2t is mean reverting.This mean-reverting property of the spread can be exploited for trading and it iscommonly referred to as “pairs trading” or “statistical arbitrage”.The idea behind pairs trading is to

short-sell the relatively overvalued stocks and buy the relatively undervalued stocks,unwind the position when they are relatively fairly valued.

D. Palomar (HKUST) Pairs Trading 14 / 63

Page 15: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Trading the spread

Suppose the spread zt = y1t − γy2t is mean-reverting with zero mean.Stat-arb trading:

if spread is low (zt < −s0), then stock 1 is undervalued and stock 2 overvalued:buy the spread (i.e., buy stock 1 and short-sell stock 2)unwind the positions when it reverts to zero after i time steps zt+i = 0

if spread is high (zt > s0), then stock 1 is overvalued and stock 2 undervalued:short-sell the spread (i.e., short-sell stock 1 and buy stock 2)unwind the positions when it reverts to zero after i time steps zt+i = 0

The profit, say, from buying low and unwinding at zero is zt+i − zt = s0. So easy!Indeed zt+i − zt = −γ(y2,t+i − y2t) + (y1,t+i − y1t), so the whole process is like having

used a portfolio with weigths w =[

1−γ

].

Recall that the return of a portfolio w is wT∆yt.

D. Palomar (HKUST) Pairs Trading 15 / 63

Page 16: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Trading the spreadIllustration on how to trade the spread zt = y1t − γy2t:2

s0

0

−s0

t

zt

Buy to unwind

Sell

Buy to unwind

Sell

Buy

Sell to unwind

2G. Vidyamurthy, Pairs Trading: Quantitative Methods and Analysis. John Wiley & Sons, 2004.D. Palomar (HKUST) Pairs Trading 16 / 63

Page 17: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Pairs trading or statistical arbitrageStatistical arbitrage can be used in practice with profits:3

0 20 40 60 80 100 120 140 160 180 200

−0.5

0

0.5

Spre

ad

(a)

0 20 40 60 80 100 120 140 160 180 200−1

−0.5

0

0.5

1

Posi

tion

(b)

0 20 40 60 80 100 120 140 160 180 2000

10

20

30

40

P&L

(c)

3M. Avellaneda and J.-H. Lee, “Statistical arbitrage in the US equities market,” Quantitative Finance,vol. 10, no. 7, pp. 761–782, 2010.

D. Palomar (HKUST) Pairs Trading 17 / 63

Page 18: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

But how to discover cointegrated pairs and γ?

One interesting approach is based on a VECM modeling of the universe of stocks: Fromthe parameter β contained in the low-rank matrix Π = αβT one can extract acointegration subspace. After that, one can design some portfolio within thatcointegration subspace.4

A simpler approach to discover pairs is by brute force, i.e., try exhaustively differentcombinations of pairs of stocks and see if they are cointegrated.But, given a potential pair, how do we obtain the “secret” γ?Easy! Just a simple LS regression!

Recall thatγ is needed to form the spread to be traded (i.e., portfolio)the spread mean µ is needed to determine the thresholds for entering a trade and unwindlater the position.

4Z. Zhao and D. P. Palomar, “Mean-reverting portfolio with budget constraint,” IEEE Trans. SignalProcess., vol. 66, no. 9, pp. 2342–2357, 2018.

D. Palomar (HKUST) Pairs Trading 18 / 63

Page 19: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Outline1 Cointegration

2 Basic Idea of Pairs Trading

3 Design of Pairs Trading

Pairs selectionCointegration testOptimum threshold

4 LS Regression and Kalman for Pairs Trading

5 From Pairs Trading to Statistical Arbitrage (StatArb)∗

VECMOptimization of mean-reverting portfolio (MRP)

6 Summary

Page 20: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Design of a pairs trading strategy

We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as theexample to introduce the main steps of statistical arbitrage.In practice, pairs trading contains three main steps5:

Pairs selection: identify stock pairs that could potentially be cointegrated.Cointegration test: test whether the identified stock pairs are indeed cointegrated or not.Trading strategy design: study the spread dynamics and design proper trading rules.

5G. Vidyamurthy, Pairs Trading: Quantitative Methods and Analysis. John Wiley & Sons, 2004.D. Palomar (HKUST) Pairs Trading 20 / 63

Page 21: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Outline1 Cointegration

2 Basic Idea of Pairs Trading

3 Design of Pairs Trading

Pairs selectionCointegration testOptimum threshold

4 LS Regression and Kalman for Pairs Trading

5 From Pairs Trading to Statistical Arbitrage (StatArb)∗

VECMOptimization of mean-reverting portfolio (MRP)

6 Summary

Page 22: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Pairs selection: normalized price distance

Normalized price distance6 (as a rough proxy to measure cointegration):

NPD ≜T∑

t=1(p1t − p2t)2

where the normalized price p1t of stock 1 is given by p1t = p1t/p10. The normalized pricesof stock 2 defined similarly.One can easily (i.e., cheaply) compute the NPD for all the possible combination of pairsand select some pairs with smallest NPD as the potentially cointegrated pairs.Later one can use a more refined measure of cointegration (more computationallydemanding).

6E. Gatev, W. N. Goetzmann, and K. G. Rouwenhorst, “Pairs trading: Performance of a relative-valuearbitrage rule,” Review of Financial Studies, vol. 19, no. 3, pp. 797–827, 2006.

D. Palomar (HKUST) Pairs Trading 22 / 63

Page 23: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Outline1 Cointegration

2 Basic Idea of Pairs Trading

3 Design of Pairs Trading

Pairs selectionCointegration testOptimum threshold

4 LS Regression and Kalman for Pairs Trading

5 From Pairs Trading to Statistical Arbitrage (StatArb)∗

VECMOptimization of mean-reverting portfolio (MRP)

6 Summary

Page 24: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Least Squares (LS) regressionIf the spread zt is stationary, it can be written as7

zt = y1t − γy2t = µ + ϵt

whereµ represents the equilibrium value andϵt is a zero-mean residual.

Equivalently, it can be written as

y1t = µ + γy2t + ϵt

which now has the typical form of linear regression.Least squares (LS) regression over T observations:

minimizeµ,γ

T∑t=1

(y1t − (µ + γy2t))2

7G. Vidyamurthy, Pairs Trading: Quantitative Methods and Analysis. John Wiley & Sons, 2004.D. Palomar (HKUST) Pairs Trading 24 / 63

Page 25: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Cointegration test

LS regression is used to estimate the parameters µ and γ, obtaining the estimates µ andγ.If y1t and y2t are I(1) and are cointegrated, then the estimates converge to the true valuesas the number of observations goes to infinity8.Using the estimated parameters µ and γ, we can compute the residuals

ϵt = y1t − γy2t − µ.

Then, one has to decide whether the spread is stationary, i.e., ϵt is stationary. In practice,the estimated residuals are used ϵt

There are many well-defined mathematical tests for the stationarity of ϵt, e.g., augmentedDicky-Fuller (ADF) test, Johansen test, etc.

8R. F. Engle and C. W. J. Granger, “Co-integration and error correction: Representation, estimation, andtesting,” Econometrica: Journal of the Econometric Society, pp. 251–276, 1987.

D. Palomar (HKUST) Pairs Trading 25 / 63

Page 26: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Outline1 Cointegration

2 Basic Idea of Pairs Trading

3 Design of Pairs Trading

Pairs selectionCointegration testOptimum threshold

4 LS Regression and Kalman for Pairs Trading

5 From Pairs Trading to Statistical Arbitrage (StatArb)∗

VECMOptimization of mean-reverting portfolio (MRP)

6 Summary

Page 27: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Optimum threshold

Once some identified pairs have passed the cointegration test, one still needs to decidethe entry and exit thresholds to open and unwind the positions, respectively.For the sake of concreteness, we focus on studying the entry threshold:

open positions when the spread diverges from its long-term mean by s0unwind the position when it reverts to its mean

Thus, the key problem now is how to design the value of s0 such that the total profit ismaximized.Total profit:

profit of each trade × number of trades

profit of each trade is s0number of trades is related to the zero crossings, which can be analized theoretically as wellas empirically.

We focus now on estimating the number of trades.

D. Palomar (HKUST) Pairs Trading 27 / 63

Page 28: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Optimum threshold s0: Parametric approach∗

Suppose the spread follows a standard Normal distribution.The probability that the spread deviates above from the mean by s0 or more is

1 − Φ(s0)

where Φ(·) is the c.d.f. of the standard Normal distribution.For a path with T days, the number of tradable events is

T(1 − Φ(s0)).

For each trade, the profit is s0 and then the total profit is s0T(1 − Φ(s0)).Then the optimal threshold is s⋆

0 = arg maxs0 {s0T(1 − Φ(s0))}.In practice, one cannot know the true distribution but can estimate the distributionparameters.Then one can compute the total profit based on estimated distribution.

D. Palomar (HKUST) Pairs Trading 28 / 63

Page 29: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Optimum threshold s0: Parametric approach∗

Optimal threshold s⋆0 maximizes the total profit:

0 1 2 30

0.1

0.2

0.3

0.4

0.5

0.6

0.7

s0

(a)

Pro

babi

lity

of tr

ades

Theoretical

Parametric

0 1 2 30

0.5

1

1.5

2

2.5

3

s0

(b)

Pro

fit o

f eac

h tra

de

0 0.5 1 1.5 2 2.5 30

0.05

0.1

0.15

0.2

0.25

s0

(c)

Tota

l pro

fit

Theoretical

Parametric

D. Palomar (HKUST) Pairs Trading 29 / 63

Page 30: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Optimum threshold s0: Non-parametric approach∗

Suppose the observed sample path has length T: z1, z2, . . . , zT.We consider J discretized threshold values as s0 ∈ {s01, s02, . . . , s0J} and the empiricaltrading frequency for the threshold s0j is

fj =∑T

t=1 1{zt>s0j}

T .

The empirical values fj may not be a smoothed enough and the resulted profit functionmay not be accurate enough.Smooth the trading frequency function by regularization:

minimizef

J∑j=1

(fj − fj)2 + λJ−1∑j=1

(fj − fj+1)2

D. Palomar (HKUST) Pairs Trading 30 / 63

Page 31: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Optimum threshold s0: Non-parametric approach∗

The problem can be rewritten as

minimizef

∥f − f∥22 + λ ∥Df∥2

2

where

D =

1 −1

1 −1. . . . . .

1 −1

∈ R(J−1)×J.

Setting the derivative of the objective w.r.t. f to zero yields the optimal solutionf⋆ = (I + λDTD)−1f.The optimal threshold is the one maximizes the total profit:

s⋆0 = arg max

s0j∈{s01,s02,...,s0J}{s0jfj} .

D. Palomar (HKUST) Pairs Trading 31 / 63

Page 32: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Optimum threshold s0: Non-parametric approach∗

Optimal threshold s⋆0 maximizes the total profit:

0 1 2 30

0.1

0.2

0.3

0.4

0.5

s0

(a)

Pro

babi

lity

of tr

ades

Theoretical

NonParam: empirical

NonParam: regularized

0 1 2 30

0.5

1

1.5

2

2.5

3

s0

(b)

Pro

fit o

f eac

h tra

de

0 0.5 1 1.5 2 2.5 30

0.05

0.1

0.15

0.2

s0

(c)

Tota

l pro

fit

Theoretical

NonParam: empirical

NonParam: regularized

D. Palomar (HKUST) Pairs Trading 32 / 63

Page 33: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Outline1 Cointegration

2 Basic Idea of Pairs Trading

3 Design of Pairs Trading

Pairs selectionCointegration testOptimum threshold

4 LS Regression and Kalman for Pairs Trading

5 From Pairs Trading to Statistical Arbitrage (StatArb)∗

VECMOptimization of mean-reverting portfolio (MRP)

6 Summary

Page 34: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

LS regression for pairs tradingIf the spread zt is stationary, it can be written as

zt = y1t − γy2t = µ + ϵt

whereµ represents the equilibrium value andϵt is a zero-mean residual.

Equivalently, it can be written asy1t = µ + γy2t + ϵt

which now has the typical form for linear regression.Least squares (LS) regression over T observations:

minimizeµ,γ

T∑t=1

(y1t − (µ + γy2t))2

By stacking the T observations in the vectors y1 and y2, we can finally write:minimize

µ,γ∥y1 − (µ1 + γy2)∥2

D. Palomar (HKUST) Pairs Trading 34 / 63

Page 35: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

LS regression for pairs trading

Using the estimated parameters µ and γ, we can compute the residualsϵt = y1t − µ − γy2t.Then, one has to decide whether the cointegration is acceptable or not so move to thetrading part.There are many well-defined mathematical tests for the stationarity of ϵt, e.g., augmentedDicky-Fuller (ADF) test, Johansen test, etc.Total profit:

profit of each trade × number of trades

profit of each trade is s0number of trades is related to the zero crossings, which can be analized theoretically as wellas empirically.

Ideally, we want residuals with large amplitude (variance) as well as a strong meanreversion because they directly affect the profit.

D. Palomar (HKUST) Pairs Trading 35 / 63

Page 36: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

LS regression for pairs tradingOne good case:

Aug 012000

Sep 012000

Oct 022000

Nov 012000

Dec 012000

Jan 022001

Feb 012001

Apr 022001

May 012001

Jun 012001

Jul 022001

Aug 012001

Sep 042001

Nov 012001

Dec 032001

Jan 022002

Jan 312002

Z−score and trading signal for EWH vs EWZ 2000−08−01 / 2002−01−31

−3

−2

−1

0

1

2

−3

−2

−1

0

1

2

Z−scoresignal

Aug 012000

Sep 012000

Oct 022000

Nov 012000

Dec 012000

Jan 022001

Feb 012001

Apr 022001

May 012001

Jun 012001

Jul 022001

Aug 012001

Sep 042001

Nov 012001

Dec 032001

Jan 022002

Jan 312002

Cum P&L 2000−08−01 / 2002−01−31

1.0

1.5

2.0

2.5

1.0

1.5

2.0

2.5

D. Palomar (HKUST) Pairs Trading 36 / 63

Page 37: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

LS regression for pairs tradingBut also a bad case:

Jul 032000

Aug 012000

Sep 012000

Oct 022000

Nov 012000

Dec 012000

Jan 022001

Feb 012001

Apr 022001

May 012001

Jun 012001

Jul 022001

Aug 012001

Sep 042001

Nov 012001

Dec 032001

Z−score and trading signal for EWY vs EWT 2000−07−03 / 2001−12−31

−2

0

2

4

6

8

−2

0

2

4

6

8Z−scoresignal

Jul 032000

Aug 012000

Sep 012000

Oct 022000

Nov 012000

Dec 012000

Jan 022001

Feb 012001

Apr 022001

May 012001

Jun 012001

Jul 022001

Aug 012001

Sep 042001

Nov 012001

Dec 032001

Cum P&L 2000−07−03 / 2001−12−31

1.0

1.2

1.4

1.6

1.8

2.0

1.0

1.2

1.4

1.6

1.8

2.0

D. Palomar (HKUST) Pairs Trading 37 / 63

Page 38: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

LS regression for pairs trading

The problem with the LS regression is that it assumes that µ and γ are constant.In practice, they can change with time, resulting in a spread that drifts from equilibrimnever to revert back with huge potential losses.Thus, in practice, µ and γ are time-varying and have to be tracked.How to track time-varying parameters?

Of course… Kalman!!!Well, you can also try a rolling regression or exponential smoothing, but Kalman worksbetter.

D. Palomar (HKUST) Pairs Trading 38 / 63

Page 39: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Kalman for pairs trading

Recall the previous static relationship for cointegrated series y1t and y2t:

y1t = µ + γy2t + ϵt

Let’s make it time-varying:y1t = µt + γty2t + ϵt

Let’s further assume that the parameters µt and γt change slowly over time:

µt+1 = µt + η1t

γt+1 = γt + η2t

Obviously, this fits nicely the Kalman framework!

D. Palomar (HKUST) Pairs Trading 39 / 63

Page 40: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Interlude: The Kalman filter

Kalman filter consist of two equations that model the time-varying hidden state xt andthe observations yt:

xt+1 = Ttxt + ηtyt = Ztxt + ϵt

The observation equation yt = Ztxt + ϵt relates the observation yt to the hidden state xtas a linear relationship, where Zt is the time-varying observation matrix and ϵt is azero-mean Gaussian error ϵt ∼ N (0, R) with covariance matrix R.The state transition equation xt+1 = Ttxt + ηt expresses the transition of the hiddenstate from xt to xt+1 as a linear relationship, where Tt is the time-varying transitionmatrix and ηt is a zero-mean Gaussian error ηt ∼ N (0, Q) with covariance matrix Q.The Kalman filter is extremely versatile in modeling a variety of real-life processes.9

9J. Durbin and S. J. Koopman, Time Series Analysis by State Space Methods, 2nd Ed. Oxford UniversityPress, 2012.

D. Palomar (HKUST) Pairs Trading 40 / 63

Page 41: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Kalman for pairs tradingKalman filter (state transition equation and observation equation):

xt+1 = Txt + ηty1t = Ztxt + ϵt

wherext ≜

[µtγt

]is the hidden state

T ≜[

1 00 1

]is the state transition matrix

ηt ∼ N (0, Q) is the i.i.d. state transition noise with Q =[

σ21 0

0 σ22

]Zt ≜

[1 y2t

]is the observation coefficient matrix

ϵt ∼ N(0, σ2

ϵ

)is the i.i.d. observation noise

Note that this is a time-varying Kalman filter since Zt is time-varying.Parameters σ2

1, σ22, σ2

ϵ can be estimated using the EM algorithm using historical data forcalibration.The hidden state path xt gives the sought time-varying coefficients.

D. Palomar (HKUST) Pairs Trading 41 / 63

Page 42: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Kalman for pairs tradingLog-prices of ETFs EWH and EWZ:

Aug 012000

Nov 012000

Feb 012001

Jun 012001

Sep 042001

Jan 022002

Apr 012002

Jul 012002

Oct 012002

Jan 022003

Apr 012003

Jul 012003

Oct 012003

Dec 312003

Log−prices 2000−08−01 / 2003−12−31

1.4

1.6

1.8

2.0

2.2

2.4

1.4

1.6

1.8

2.0

2.2

2.4

EWHEWZ

D. Palomar (HKUST) Pairs Trading 42 / 63

Page 43: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Kalman for pairs tradingTracking of µ and γ by LS, rolling LS, and Kalman:

Aug 012000

Nov 012000

Feb 012001

May 012001

Aug 012001

Nov 012001

Feb 012002

May 012002

Aug 012002

Nov 012002

Feb 032003

May 012003

Aug 012003

Nov 032003

Tracking of mu 2000−08−01 / 2003−12−31

0.4

0.6

0.8

1.0

0.4

0.6

0.8

1.0

mu.LSmu.rolling.LSmu.Kalman

Aug 012000

Nov 012000

Feb 012001

May 012001

Aug 012001

Nov 012001

Feb 012002

May 012002

Aug 012002

Nov 012002

Feb 032003

May 012003

Aug 012003

Nov 032003

Tracking of gamma 2000−08−01 / 2003−12−31

0.3

0.4

0.5

0.6

0.3

0.4

0.5

0.6gamma.LSgamma.rolling.LSgamma.Kalman

D. Palomar (HKUST) Pairs Trading 43 / 63

Page 44: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Kalman for pairs trading

Spreads achieved by LS, rolling LS, and Kalman:

Aug 012000

Nov 012000

Feb 012001

Jun 012001

Sep 042001

Jan 022002

Apr 012002

Jul 012002

Oct 012002

Jan 022003

Apr 012003

Jul 012003

Oct 012003

Dec 312003

Spreads 2000−08−01 / 2003−12−31

−0.15

−0.10

−0.05

0.00

0.05

0.10

0.15

−0.15

−0.10

−0.05

0.00

0.05

0.10

0.15LSrolling.LSKalman

D. Palomar (HKUST) Pairs Trading 44 / 63

Page 45: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Kalman for pairs tradingTrading of spread from LS:

Aug 012000

Nov 012000

Feb 012001

May 012001

Aug 012001

Nov 012001

Feb 012002

May 012002

Aug 012002

Nov 012002

Feb 032003

May 012003

Aug 012003

Nov 032003

Z−score and trading on spread based on LS 2000−08−01 / 2003−12−31

−3

−2

−1

0

1

2

−3

−2

−1

0

1

2Z−scoresignal

Aug 012000

Nov 012000

Feb 012001

May 012001

Aug 012001

Nov 012001

Feb 012002

May 012002

Aug 012002

Nov 012002

Feb 032003

May 012003

Aug 012003

Nov 032003

Cum P&L for spread based on LS 2000−08−01 / 2003−12−31

1.0

1.5

2.0

2.5

1.0

1.5

2.0

2.5

D. Palomar (HKUST) Pairs Trading 45 / 63

Page 46: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Kalman for pairs tradingTrading of spread from rolling LS:

Aug 012000

Nov 012000

Feb 012001

May 012001

Aug 012001

Nov 012001

Feb 012002

May 012002

Aug 012002

Nov 012002

Feb 032003

May 012003

Aug 012003

Nov 032003

Z−score and trading on spread based on rolling−LS 2000−08−01 / 2003−12−31

−3

−2

−1

0

1

2

−3

−2

−1

0

1

2Z−scoresignal

Aug 012000

Nov 012000

Feb 012001

May 012001

Aug 012001

Nov 012001

Feb 012002

May 012002

Aug 012002

Nov 012002

Feb 032003

May 012003

Aug 012003

Nov 032003

Cum P&L for spread based on rolling−LS 2000−08−01 / 2003−12−31

1.0

1.5

2.0

2.5

3.0

1.0

1.5

2.0

2.5

3.0

D. Palomar (HKUST) Pairs Trading 46 / 63

Page 47: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Kalman for pairs tradingTrading of spread from Kalman:

Aug 012000

Oct 022000

Dec 012000

Feb 012001

Apr 022001

Jun 012001

Aug 012001

Oct 012001

Dec 032001

Feb 012002

Apr 012002

Jun 032002

Aug 012002

Oct 012002

Dec 022002

Feb 032003

Mar 312003

Z−score and trading on spread based on Kalman 2000−08−01 / 2003−03−31

−3

−2

−1

0

1

2

−3

−2

−1

0

1

2Z−scoresignal

Aug 012000

Oct 022000

Dec 012000

Feb 012001

Apr 022001

Jun 012001

Aug 012001

Oct 012001

Dec 032001

Feb 012002

Apr 012002

Jun 032002

Aug 012002

Oct 012002

Dec 022002

Feb 032003

Mar 312003

Cum P&L for spread based on Kalman 2000−08−01 / 2003−03−31

1.5

2.0

2.5

3.0

3.5

4.0

1.5

2.0

2.5

3.0

3.5

4.0

D. Palomar (HKUST) Pairs Trading 47 / 63

Page 48: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Kalman for pairs tradingWealth comparison:

Aug 012000

Nov 012000

Feb 012001

May 012001

Aug 012001

Nov 012001

Feb 012002

May 012002

Aug 012002

Nov 012002

Feb 032003

Cum P&L 2000−08−01 / 2003−03−31

1.0

1.5

2.0

2.5

3.0

3.5

4.0

1.0

1.5

2.0

2.5

3.0

3.5

4.0LSrolling.LSKalman

D. Palomar (HKUST) Pairs Trading 48 / 63

Page 49: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Kalman filter in finance

The Kalman filter can and has been used in many aspects of financial time-seriesmodeling as one could expect.10

Examples of univariate time series: rate of inflation, national income, level ofunemployment, etc.Typical models include: local model, trend-cycle decompositions, seasonality, etc.Examples of multivariate time series: inflation and national income.Multiple time series allows for more sophisticated models including common factors,cointegration, etc.Also data irregularities can be easily handled, e.g., missing observations, outliers, mixedfrequencies.Plenty of applications for nonlinear and non-Gaussian models as well, e.g., GARCHmodeling and stochastic volatility modeling.

10A. Harvey and S. J. Koopman, “Unobserved components models in economics and finance: The role of theKalman filter in time series econometrics,” IEEE Control Systems Magazine, vol. 29, no. 6, pp. 71–81, 2009.

D. Palomar (HKUST) Pairs Trading 49 / 63

Page 50: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Outline1 Cointegration

2 Basic Idea of Pairs Trading

3 Design of Pairs Trading

Pairs selectionCointegration testOptimum threshold

4 LS Regression and Kalman for Pairs Trading

5 From Pairs Trading to Statistical Arbitrage (StatArb)∗

VECMOptimization of mean-reverting portfolio (MRP)

6 Summary

Page 51: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

From pairs trading to statistical arbitrage

Pairs trading focuses on finding cointegration between two stocks.A more general idea is to extend this statistical arbitrage from two stocks to more stocks.

The idea is still based on cointegration:Try to construct a linear combination of the log-prices of multiple (more than two)stocks such that it is a cointegrated mean-reversion process.

In the case of two assets, the spread is zt = y1t − γy2t, which can be understood as a

portfolio with weights: w =[

1−γ

].

In the general case of many assets, one has to properly design the portfolio w.

D. Palomar (HKUST) Pairs Trading 51 / 63

Page 52: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Outline1 Cointegration

2 Basic Idea of Pairs Trading

3 Design of Pairs Trading

Pairs selectionCointegration testOptimum threshold

4 LS Regression and Kalman for Pairs Trading

5 From Pairs Trading to Statistical Arbitrage (StatArb)∗

VECMOptimization of mean-reverting portfolio (MRP)

6 Summary

Page 53: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

VECM

Denote the log-prices of multiple stocks as yt and the log-returns as rt = ∆yt = yt − yt−1.Most of the multivariate time-series models attempt to model the log-returns rt (becausethe log-prices are nonstationary whereas the log-returns are weakly stationary, at leastover some time horizon).However, it turns out that differencing the log-prices may destroy part of the structure.The VECM11 tries to fix that issue by including an additional term in the model:

rt = ϕ0 + Πyt−1 +p−1∑i=1

Φirt−i + wt,

where the term Πyt−1 is called error correction term.

11R. F. Engle and C. W. J. Granger, “Co-integration and error correction: Representation, estimation, andtesting,” Econometrica: Journal of the Econometric Society, pp. 251–276, 1987.

D. Palomar (HKUST) Pairs Trading 53 / 63

Page 54: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

VECM - Matrix Π

The matrix Π is of extreme importance.Notice that from the model rt = ϕ0 + Πyt−1 +

∑p−1i=1 Φirt−i + wt one can conclude that

Πyt must be stationary even though yt is not!!!If that happens, it is said that yt is cointegrated.There are three possibilities for Π:

rank (Π) = 0: This implies Π = 0, thus yt is not cointegrated (so no mystery here) and theVECM reduces to a VAR model on the log-returns.rank (Π) = N: This implies Π is invertible and thus yt must be stationary already.0 < rank (Π) < N: This is the interestinc case and Π can be decomposed as Π = αβT withα, β ∈ RN×r with full column rank. This means that yt has r linearly independentcointegrated components, i.e., βTyt, each of which can be used for pairs trading.

D. Palomar (HKUST) Pairs Trading 54 / 63

Page 55: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Outline1 Cointegration

2 Basic Idea of Pairs Trading

3 Design of Pairs Trading

Pairs selectionCointegration testOptimum threshold

4 LS Regression and Kalman for Pairs Trading

5 From Pairs Trading to Statistical Arbitrage (StatArb)∗

VECMOptimization of mean-reverting portfolio (MRP)

6 Summary

Page 56: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Mean-reverting portfolio (MRP)

In the case of two assets, the spread is zt = y1t − γy2t, which can be understood as a

portfolio with weights: w =[

1−γ

].

In the general case of many assets, one has to properly design the portfolio w.One interesting approach is based on a VECM modeling of the universe of stocks:

From the parameter β contained in the low-rank matrix Π = αβT one can simply use anycolumn of β (even all of them)Even better, β defines a cointegration subspace and we can then optimize the portfoliowithin that cointegration subspace.12,13

12Z. Zhao and D. P. Palomar, “Mean-reverting portfolio with budget constraint,” IEEE Trans. SignalProcess., vol. 66, no. 9, pp. 2342–2357, 2018.

13Z. Zhao, R. Zhou, and D. P. Palomar, “Optimal mean-reverting portfolio with leverage constraint forstatistical arbitrage in finance,” IEEE Trans. Signal Process., vol. 67, no. 7, pp. 1681–1695, 2019.

D. Palomar (HKUST) Pairs Trading 56 / 63

Page 57: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Mean-reverting portfolio (MRP)

Consider the log-prices yt and use β to extract several spreads st = βTyt.Let’s now use a portfolio w to extract the best mean-reverting spread from st aszt = wTst.To design the the portfolio w we have two main objectives (recall that total profit equals:profit of each trade × number of trades):

we want large variance (profit of each trade): wTM0w, whereMi = E

[(st − E [st]) (st+i − E [st+i])T

]we want strong mean reversion (number of trades): many proxies exist like the Portmanteaustatistics or crossing statistics.

D. Palomar (HKUST) Pairs Trading 57 / 63

Page 58: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Mean-reverting portfolio (MRP)

For example, if we use the Portmanteau statistics as a proxy for the mean reversion, theproblem formulation becomes:

minimizew

∑pi=1

(wTMiwwTM0w

)2

subject to wTM0w = νw ∈ W.

Using other proxies, the formulation can be expressed more generally as14

minimizew

wTHw + λ∑p

i=1(wTMiw

)2

subject to wTM0w = νw ∈ W.

14Z. Zhao and D. P. Palomar, “Mean-reverting portfolio with budget constraint,” IEEE Trans. SignalProcess., vol. 66, no. 9, pp. 2342–2357, 2018.

D. Palomar (HKUST) Pairs Trading 58 / 63

Page 59: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

MRP in practiceObserve several stock log-prices and the spreads obtained from β:

Lo

g-p

rice

s3.0

3.5

4.0

4.5

5.0

5.5APAAXPCATCOFFCXIBMMMM

s1

5.5

6.0

s2

5.0

5.2

5.4

2012 2013 2014

s3

5.2

5.4

5.6

D. Palomar (HKUST) Pairs Trading 59 / 63

Page 60: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

MRP in practiceObserve several stock log-prices and the spreads obtained from β:

Sp

rea

ds

-0.4-0.2

00.20.4

MRP-cro (prop.)Spread s

1

RO

I

-0.02

0

0.02MRP-cro (prop.) - SR=3.2471

RO

I

-0.01

0

0.01Spread s

1 - SR=2.8579

2012 2013 2014

Cu

m.

P&

L

0

2

4

6

8MRP-cro (prop.)Spread s

1

D. Palomar (HKUST) Pairs Trading 60 / 63

Page 61: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Outline1 Cointegration

2 Basic Idea of Pairs Trading

3 Design of Pairs Trading

Pairs selectionCointegration testOptimum threshold

4 LS Regression and Kalman for Pairs Trading

5 From Pairs Trading to Statistical Arbitrage (StatArb)∗

VECMOptimization of mean-reverting portfolio (MRP)

6 Summary

Page 62: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Summary

First of all, we have discovered the concept of cointegration.We have learned the basic idea of pairs trading for cointegrated assets:

searching for a cointegrated spread is the first stepmaking sure that the chosen spread remains cointegrated is key (cointegrated tests)obtaining the cointegration ratio γ and the entering and exiting thresholds are importantdetails.

We have learned of the use of Kalman (initially developed for tracking missiles) filteringfor improved pairs trading.We have briefly explored the extension of pairs trading (for two stocks) to statisticalarbitrage (for more than two stocks):

VECM modeling is an important multivariate time-series modeling toolsophisticated portfolio designs on the cointegration subspace are possible.

D. Palomar (HKUST) Pairs Trading 62 / 63

Page 63: Pairs Trading · 2020. 12. 28. · Design of a pairs trading strategy We first focus on pairs trading (i.e., statistical arbitrage between two stocks) as the example to introduce

Thanks

For more information visit:

https://www.danielppalomar.com