ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes...

42
ALGORITHMIC TRADING Hidden Markov Models

Transcript of ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes...

Page 1: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

ALGORITHMIC TRADINGHidden Markov Models

Page 2: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

Overview

a) Introduction

b) Methodology

c) Programming codes

d) Conclusion

Page 3: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

a) Introduction

Markov Chain• Named after Andrey Markov, Russian mathematician.

• Mathematical system that undergoes transitions from one state to another state.

• From a finite or countable number of possible states in a chainlike manner.

• Next state depends ONLY on the current state and not the past.

Page 4: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

a) Introduction

Simple 2 States Markov Chain Models

Page 5: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

a) Introduction

• System is in a certain state at each step.• States are changing randomly between steps.• Steps can be time or others measurements.• Random process is simply to map states to steps.

Page 6: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

a) Introduction

1

2

Markov Property – The state at next step or all future steps, given its current state depends only on the current state.

Transition Probabilities – Probability of transiting to various states.

Page 8: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

a) Introduction

Transition Probability Matrix• Simple matrix holding all the state changing probabilities.

•Eg: : Imagine that the sequence of rainy and sunny days is such that each day’s weather depends only on the previous day’s, and the transition probabilities are given by the following table.

Page 9: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

a) Introduction

Day t+1Day t Rainy SunnyRainy 0.9 0.1Sunny 0.6 0.4

if today is rainy, the probability that tomorrow will be rainy is 0.9; if today is sunny, that probability is 0.6. The weather is then a two-state Markov chain, with t.p.m. Γ given by:

Γ = 0.9 0.1 0.6 0.4

Page 10: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

a) Introduction to HMM

Hidden Markov Model• it is a Markov chain but the states are hidden, not observable.

• In a normal Markov chain, t.p.m are the only parameters. In HMM, although state is not directly visible BUT the output which is dependent on the state is visible!

•Thus the sequence of output reveals something about the sequence of states.

Page 11: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

a) Introduction to HMM

Therefore, what is available to the observer is another stochastic process linked to the Markov chain.

The underlying Markov chain a.k.a the regime or state, will be directly affecting the distribution of the observed process.

Page 14: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

a) Introduction to HMM

Probabilistic parameters of a hidden Markov model x — statesy — possible observationsa — state transition probabilitiesb — output probabilities

Page 15: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

END OF INTRODUCTION!

METHODOLOGY

Page 16: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

Methodology Main Idea

Total No. of data: 1400=1000+400

Page 17: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

Markov Chain

s2: Cloudy

s1: Sunny

s3: Rainy

a22 = ?

a11 = ? a33 = ?

a12 = ?

a21 = ?

a23 = ?

a32 = ?

a13 = ?

a31 = ?

Page 18: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

Model—λ = {A,B,Π}A = {a11, a12, ..., aNN} : a transition probability matrix

A, where each aij represents the probability of moving from state si to state sj

B = bi(ot) : a sequence of observation likelihoods, also called emission probabilities, expressing the probability of an observation ot being generated from a state si at time t

Π = {π1, π2, ..., πN} : an initial probability distribution, where πi indicates the probability of starting in state si.

Page 19: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

Model—Other ParametersS = {s1, s2, ..., sN} a set of N hidden states,Q = {q1, q2, ..., qT } a state sequence of length T taking

values from S,O = {o1, o2, ..., oT } an observation sequence consisting

of T observations, taking values from the discrete alphabet

V = {v1, v2, ..., vM},

Page 20: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

Model—Likelihood : P(O|λ)Forward αt(i) = P(o1, o2, . . . , ot, qt = si|λ)

Backward βt(i) = P(ot+1, ot+2, . . . , oT |qt = si, λ)

Page 21: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

Forward—L2

Page 22: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

Backward—L2

Page 23: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

Model—Decoding : Viterbi Algo

Page 24: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

Model—Learning: Re-estimationξt(i, j) = P(qt = si, qt+1 = sj |O, λ)

γt(i) = P(qt = si|O, λ)

Page 25: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

Model—Learning: Re-estimation

Page 26: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

Applying HMM(Discrete)Model: Window length=100; N=3; M=; Initial parameter estimation: Random λ = {A,B,Π} Final model

Page 27: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

Applying HMM(Discrete)TestOut of sample

Generating trading signals

Ot+1 & Ot : Trading strategy

Page 28: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

Content

• A Ideas

• B Code Structure

• C Results

• D Limitations and TODOs

Page 29: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

Section A - Ideas

• Parameter Estimation

• Prediction

• Trading Strategy

Page 30: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

Parameter estimation-HMM

• A, B, pie

• 3 steps: likelihood, decoding, learning

• Greeks

• Updating, Iteration

Page 31: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

the Choice of B• Discrete - not smooth

• Time homogenous, why?

• a. Memory cost N * M * T (3 * 1000 * 100) in one iteration step

• b. Difficult to update

• Why continuous dist. can do this?

• a. Less parameters, less cost

• b. Update the time varying B directly

Page 32: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

Parameter Updating

• How to compute updated B?

• Function get_index and its inverse function

• How to choose size of B? Extend the min and max, prepare to for extreme value.

• set a max, min, then interval length, set a function

Page 33: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

Convergence Condition

• P converges to Po, set the eps

• Local maximum, not global

Page 34: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

Prediction

• Find the q from last window -> the most likely state(t + 1)

• Find the B from last window -> the most likely obs given state

• Combine them to get most likely o(t + 1)

• Drawback: previous B to get new obs

Page 35: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

Trading Strategy

• o(t + 1) > o(t) buy, otherwise sell

• set an indicator function I(buy or sell), which value is +1 when buy, -1 when sell

• Valuation: daily P&L = I * (true value(t + 1) - true value(t))

• Sum daily P&L to get accumulate P&L, plot

Page 36: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

section c - results

P&L, window length = 30, state number = 3

Page 37: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

Section b - Code Structure

double update data {input = window length data

output = a, b, pie in one step iteration, by using HMM}

int main {generate a b pie( window length, state numbers) in uniformly distributionupdate data( 1~ window length) compute p, qupdate until p convergesget best a, b, pie @ time 1 ~ window lengthuse a, b, pie @ time period 1 as initialization, run update data iteration until p convergesiteration until end of data

prediction:q at last windowb at last windowto get most likely price @ time t + 1, say o(t + 1)our strategybacktesting and draw p & l (in Matlab for convenience)}

Page 38: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

section c - results

• P&L, window length = 50, state number = 3

Page 39: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

section c - results

• P&L, window length = 70, state number = 3

Page 40: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

section c - results

P&L, window length = 100, state number = 3

Page 41: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

section d - drawbacks and todos

• Drawbacks:

• a. Iteration: Why we can use paras from last window to initialize paras of this window?

• b. Prediction: Why can use previous B to get next observation?

• c. Matrix B: time homogenous, does not make sense

• d. Discrete Distribution: Not smooth, not continuous...

• e. How many shares we will buy/sell?

Page 42: ALGORITHMIC TRADING Hidden Markov Models. Overview a)Introduction b)Methodology c)Programming codes d)Conclusion.

todos

• a. Continuous Distribution and GMM

• b. More Trading Strategy: Computing expectation price rather than the most likely one

• because their are too many probabilities in a vector; their differences are slight -> more errors

• c. Transaction Cost: when o(t + 1) - o(t) >(<) c, buy/sell