introduction to algorithmic trading strategies...

43
Introduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic Spread Methods Haksun Li [email protected] www.numericalmethod.com

Upload: trinhnguyet

Post on 20-May-2018

248 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Introduction to Algorithmic Trading StrategiesLecture 5

Pairs Trading by Stochastic Spread Methods

Haksun [email protected]

www.numericalmethod.com

Page 2: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Outline First passage time Kalman filter Maximum likelihood estimate EM algorithm

2

Page 3: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

References As the emphasis of the basic co‐integration methods of most papers are 

on the construction of a synthetic mean‐reverting asset, the stochastic spread methods focuses on the dynamic of the price of the synthetic asset.

Most referenced academic paper: Elliot, van der Hoek, and Malcolm, 2005, Pairs Trading Model the spread process as a state‐space version of Ornstein‐Uhlenbeck

process Jonathan Chiu, Daniel Wijaya Lukman, Kourosh Modarresi, Avinayan

Senthi Velayutham. High‐frequency Trading. Stanford University. 2011 The idea has been conceived by a lot of popular pairs trading books

Technical analysis and charting for the spread, Ehrman, 2005, The Handbook of Pairs Trading

ARMA model, HMM ARMA model, some non‐parametric approach, and a Kalman filter model, Vidyamurthy, 2004, Pairs Trading: Quantitative Methods and Analysis

3

Page 4: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Spread as a Mean‐Reverting Process

The long term mean =  .

The rate of mean reversion =  .

4

Page 5: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Sum of Power Series We note that

5

Page 6: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Unconditional Mean

6

Page 7: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Long Term Mean

7

Page 8: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Unconditional Variance

8

Page 9: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Long Term Variance

9

Page 10: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Observations and Hidden State Process The hidden state process is:

1

0, 0 1 The observations:

We want to compute the expected state from observations. | |

10

Page 11: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

First Passage Time Standardized Ornstein‐Uhlenbeck process 2

First passage time , inf 0, 0| 0

The pdf of  , has a maximum value at

ln 1 3 4 3

11

Page 12: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

A Sample Trading Strategy

Buy when  unwind after time 

Sell when  unwind after time 

12

Page 13: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Kalman Filter The Kalman filter is an efficient recursive filter that estimates the state of a dynamic system from a series of incomplete and noisy measurements.

13

Page 14: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Conceptual Diagram

prediction at time t Update at time t+1

as new measurements come in

correct for better estimation

14

Page 15: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

A Linear Discrete System

: the state transition model applied to the previous state

: the control‐input model applied to control vectors : the noise process drawn from multivariate Normal distribution

15

Page 16: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Observations and Noises

: the observation model mapping the true states to observations

: the observation noise

16

Page 17: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Discrete System Diagram

17

Page 18: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Prediction predicted a prior state estimate | |

predicted a prior estimate covariance | |

18

Page 19: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Update measurement residual |

residual covariance |

optimal Kalman gain |

updated a posteriori state estimate | |

updated a posteriori estimate covariance | |

19

Page 20: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Computing the ‘Best’ State Estimate Given  ,  ,  ,  , we define the conditional variance Σ | ≡ E |

Start with  | ,  .

20

Page 21: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Predicted (a Priori) State Estimation |

|

21

Page 22: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Predicted (a Priori) Variance |

|

|

|

|

22

Page 23: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Minimize Posteriori Variance Let the Kalman updating formula be | | |

We want to solve for K such that the conditional variance is minimized. Σ | E |

23

Page 24: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Solve for K E |

E | | |

E | | |

E 1 | |

1 E | |

1 Σ |

24

Page 25: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

First Order Condition for k

|

|

|

25

Page 26: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Optimal Kalman Filter

|

|

26

Page 27: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Updated (a Posteriori) State Estimation So, we have the “optimal” Kalman updating rule. | | |

||

||

27

Page 28: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Updated (a Posteriori) Variance Σ | E | 1 Σ |

1 |

|Σ |

|

|

|

Σ ||

|

| |

|

| |

|

| |

|

Σ |

28

Page 29: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Parameter Estimation We need to estimate the parameters from the observable data before we can use the Kalman filter model.

We need to write down the likelihood function in terms of  , and then maximize w.r.t.  .

29

Page 30: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Likelihood Function A likelihood function (often simply the likelihood) is a function of the parameters of a statistical model, defined as follows: the likelihood of a set of parameter values given some observed outcomes is equal to the probability of those observed outcomes given those parameter values.

30

Page 31: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Maximum Likelihood Estimate We find  such that  is maximized given the observation.

31

Page 32: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Example Using the Normal Distribution We want to estimate the mean of a sample of size 

drawn from a Normal distribution.

exp

32

Page 33: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Log‐Likelihood

Maximizing the log‐likelihood is equivalent to maximizing the following. ∑

First order condition w.r.t., ∑

33

Page 34: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Nelder‐Mead After we write down the likelihood function for the Kalman model in terms of  , we can run any multivariate optimization algorithm, e.g., Nelder‐Mead, to search for  . ma ;

The disadvantage is that it may not converge well, hence not landing close to the optimal solution.

34

Page 35: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Marginal Likelihood For the set of hidden states,  , we write ; | ∑ , |

Assume we know the conditional distribution of  , we could instead maximize the following. ma E | , , or

ma E log | ,

The expectation is a weighted sum of the (log‐) likelihoods weighted by the probability of the hidden states.

35

Page 36: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

The Q‐Function Where do we get the conditional distribution of from?

Suppose we somehow have an (initial) estimation of the parameters,  . Then the model has no unknowns. We can compute the distribution of  .

| ,

36

Page 37: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

EM Intuition Suppose we know  , we know completely about the mode; we can find 

Suppose we know  , we can estimate  , by, e.g., maximum likelihood.

What do we do if we don’t know both  and  ?

37

Page 38: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Expectation‐Maximization Algorithm Expectation step (E‐step): compute the expected value of the log‐likelihood function, w.r.t., the conditional distribution of  under  and  . | E

| ,log | ,

Maximization step (M‐step): find the parameters,  , that maximize the Q‐value. argmax |

38

Page 39: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

EM Algorithms for Kalman Filter Offline: Shumway and Stoffer smoother approach, 1982

Online: Elliott and Krishnamurthy filter approach, 1999

39

Page 40: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

A Trading Algorithm From  ,  , …,  , we estimate  . Decide whether to make a trade at  , unwind at 

, or some time later, e.g.,  . As  arrives, estimate  . Repeat.

40

Page 41: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Results (1)

41

Page 42: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Results (2)

42

Page 43: Introduction to Algorithmic Trading Strategies …numericalmethod.com/papers/course1/lecture5.pdfIntroduction to Algorithmic Trading Strategies Lecture 5 Pairs Trading by Stochastic

Results (3)

43