Machine Learning in Pairs Trading Sraegies Yuxing Chen (Joseph) Deparmen of Saisics Sanford Universiy Email: osephc5@sanford.edu Weiluo Ren (David) Deparmen of Mahemaics Sanford Universiy Email: weiluo@sanford.edu Xiaoxiong Lu Deparmen of Elecrical Engineering Sanford Universiy Email: lxx@sanford.edu Keywords: pairs rading mean revering Ornsein-Uhlenbec process porfolio rebalancing Kalman filer Kalman smooher EM.Inroducion Pairs rading consiss of long posiion in one financial produc shor posiion in anoher produc we focus he form of saisical arbirage insead of rend following; hese sraegies are mare neural have low ris. Choose wo securiies denoe heir prices as S S. Then he spread is S S where is a carefully chosen consan depending on ime. The simples case is ha ; he spread becomes simply difference beween wo prices. We assume ha he spread is a mean revering process meaning if deviaions of spread from is mean occur his deviaion will evenually vanish. Then when deviaions arise we long he relaively cheap securiies shor sell he relaively expensive securiies hen wai for he spread will go bac o is mean level o mae profi. This is he basic idea behind many pairs rading sraegies including ours. The quesion now becomes how o model he meanrevering process of spread so ha enering exiing rading signal can be developed from ha model. In his paper Ornsein-Uhlenbec process is used as he underlying model of spread: dx ( ) ( X( )) d dw( ) (.) where X () is he spread a ime measures he speed of X () reurning o is mean level is he volailiy of spread. In his proec wo approaches are applied. One is saring from difference of daily reurns insead of spread of prices inegraing his process using a linear regression o esimae coefficiens. Anoher one is assuming a spread model which is a laen O-U process plus some noise building signals based on predicion generaed from Kalman filer; E-M algorihm modified for Kalman smooher/filer is applied o esimae coefficiens in he spread model. In Secion 3 models algorihms are given in a bacwards order firs saring from models hen inroducing algorihms in order o esimae parameers in models. Very brief summaries of real procedures are given in laer par of Secion 3 showing he order of how algorihms should be implemens..porfolio Rebalancing & Linear Regression Approach The advanage of his approach is simpliciy: linear model is convenien o be inerpreed if anyhing goes wrong i is easy o spo he source of problem.. Assumpions Porfolio Rebalancing We assume ha he daily reurns of wo financial producs saisfy he following sochasic differenial equaion: ds( ) ds( ) d dx () (.) S ( ) S ( ) The drif erm is he rend of spread of daily reurns; is a consan which canno change much along ime; X () is a mean revering process. In pracice his equaion says ha if we long $ securiies one shor selling $ securiies wo he daily reurn of our porfolio should be mean revering given he condiion ha he magniude of flucuaion of X () which is usually he case. From he above explanaion we can see why canno change much along ime. Since / ( ) / ( ) are weighs wihin our porfolio if changes frequenly in a large magniude meaning ha porfolio needs rebalancing frequenly weighs change much. Then he profi canno cover he cos of rebalancing. I is beer o run a regression o find a 0 eep as a consan for a shor period of ime e.g. 5 or 0 days chec wheher X () from ds( ) ds( ) dx () has mean-revering propery. S ( ) S ( )
. The O-U Model of Spread As saed in Secion we use O-U process (.) o model he dynamic of he spread X () hus we have dx ( ) ( X( )) d dw( ) (.) Inegraing he above equaion we have [] X ( ) e X ( ) ( e ) A( ) 0 0 0 0 ( 0 s) A e dw s () (.3) 0 ow le end o infiniy he equilibrium probabiliy disribuion of X () is normal wih E[ X ( )] Var[ X ( )] (.4) Wih (.3) (.4) we are able o esimae he parameers in he O-U process..3 Linear Regression for Esimaing Weighs Parameers Le us denoe R S S S Run a linear regression of window wih lengh 60. R agains R on a moving R 0 R 60 noe ha / ( ) / ( ) is he weigh of porfolio also ha we may run he above regression every 5 days as indicaed in Secion.. Then we use he sum of residuals o obain he discree version of X X 60 Then use hese X linear regression again o esimae parameers as below. Thus log( b) 5 m a / ( b) Var( ) b Moreover we are able o ge he equilibrium sard deviaion from (3.4) now. eq : Var( X ( )) (.5) A his sage we can use he sardized version of X () called Z-score as rading signal. This facor measures how far X () deviaes from is mean level is a valid measure across all securiies since i is dimensionless. More deails of signal will be given laer..4 Summary of he Procedure A summary of he whole procedure will be given i displays he order wihin he implemenaion of he rading sraegy. Firs run a linear regression on daily reurns on a moving window o ge new weigh (performing rebalancing if necessary); hen use o sum o discree version of X run anoher regression of X o obain parameers in he O-U process z-score as rading signal. The buying selling rules are buy o open if s i s bo sell o open if s i s close long posiion if s i s close shor posiion if s i s.5 Bac-Tes Resuls We use closing price (daily daa) of wo chosen fuure conracs in China fuure mare. The daily reurn plo is shown as below. so sc bc X a bx 60 By (.3) we have a( e ) b e Var( ) e Figure. Daily reurns of wo securiies
The plo of (updaed every five rading days) is Figure. which indicaes he weigh of porfolio The plo of cumulaive profi summary are given; we consider he ransacion cos of buying/selling plus slippage is 0 basis poins. The Observaion Process We assume he spread process { y } is he observaion of { x } wih Gaussian noise y x D (3.3) where { } are also i.i.d Gaussian (0) independen of { }. The Trading Signal Here we define x E[ x F ] l l (3.4) (3.5) l E[( x x ) Fl ] x x (3.6) Annualized Reurn Rae Figure.3 Cumulaive profi Volailiy Sharpe Maximal Drawdown 8.0% 7.59%.4 5.57% 3.Kalman Filer EM Algorihm Approach 3. The Spread Model The Sae Process We sudied { y } he simples spread S S wih he assumpion ha i is a noisy observaion of a laen meanrevering sae process { x }. Here ss for one day x is some variable a ime for 0 saisfying he following mean-revering dynamic (discree version of OU process). x x ( a bx ) (3.) where 0 b 0 a { } is i.i.d Gaussian (0). Thus in he above equaion is independen of all x. And he process mean revers o a/ bwih "speed" b. Therefore we can rewrie (3.) as follows x A Bx C (3.) The condiional expecaion given observed informaion F eiher from an exping window or a moving window. l If y x ransacion cos + premium here he premium is he profi ha we wan o ensure when ener a posiion hen he spread is regarded as oo large meaning he securiies if relaively expensive han securiies ; we would ae a long posiion in he spread porfolio (shor selling one uni longing one uni of produc ) expecing ha he spread will shrin evenually. Similarly if y x ransacion cos - premium hen he spread is lower han he expecaion significanly; we would ae a shor posiion in he spread porfolio. We close posiions when y x where is a predeermined hreshold. 3. Kalman Filer Equiped wih sae equaion (3.) observaion equaion (3.) our nex sep is o esimae he hidden process. x In order o esimae he predicion of he nex day a ime we will sar from ime 0 he iniializaion x y ; recall he definiion (3.4) o 0 0 0 0 D (3.6).Then perform he following procedure ieraively unil. In he predicion sep we would compue he "predicion" x A Bx (3.7) B C (3.8) The sae esimae. x is hidden which needs observaions o The opimal Kalman gain is K / ( D ) (3.9)
Then we can compue our esimaion wih he new observaion y in he following updae sep: x x x K [ y x ] (3.0) R D K K (3.) Repea he above process o obain x x. E[( x x ) F ] E[( x x ) ] (3.5) E[( x x )( x x )] (3.6) They can be compued bacwards meaning ha x [5]. can be obained from x Le us use firs few seps o illusrae how EM algorihm wors in he form of Kalman smooher/filer. Figure 3. Kalman filer The above algorihm is based on nowing ( A B C D ) bu is unnown now. We need an exra raining se o esimae he parameers A B C D before using Kalman filer hese parameers are esimaed via EM algorihm. 3.3 Esimaion using EM Algorihm x Before predicing We now use he EM algorihm o esimae y0 y... y. ( A B C D ) based on he observaions Recall he general form of EM algorihm ( P is a probabiliy measure wih parameer ): Sep (E-sep) Sep (M-sep) dp Compue E log F dp Updae o by maximizing condiional expecaion. dp arg max E log F dp Here we are going o use Shumwau Soffer [6] smooher o implemen EM algorihm. We define smoohers for (noe ha hey have he same form of definiion for filering when ): x E[ x F ] (3.4) Figure 3. EM in he form of smooher/filer From he above figure he operaion rules wih blue arrows indicaing smooher filer are nown; so now we go o he rule of updaing A B C D using X. ( ) Given A B C D iniial values for he Kalman Filer he updae ( A B C D ) are calculae as follows: where  B C [( x A Bx ) F ] (3.7) (3.7) (3.9) D y x F (3.0) [( ) ] 0 E x F x [ ] [ ]
E x x F x x x [ ] [ ] x x x 0 3.4 Summary of Algorihms As shown in Figure 3. we sar from X : X : 0 coninuing updaing. Afer 300 predicion x as rading signal. we use he Figure 3.4 Acual Spread vs Predicion Besides he EM algorihm in [4] is implemenaed using exping window as above which would cause heavy compuaional burden. So we also implemen he EM using moving window for updaing as well. 3.5 Resul of Implemenaion We pair wo fuure conracs of agriculural producs in China mare esimae he process of spread. Wih Kalman Filer EM Algorihm we can predic he hidden sae x using an exping window ploed in Figure 3.3. Figure 3.5 Cumulaive Profis Figure 3.3 Acual Spread vs Predicion As shown in Figure 3.3 he acual spread y is osillaing around our predicion. When y deviaes from x significanly we would expec ha he spread will shrin evenually we will ae advanage of his o mae profi. Figure 3.4 is he predicion made from EM on a moving window. Comparison of cumulaive profis of using wo windows is given as well (Figure 3.5). 4.Conclusion Analysis We are being on meaning reversion of spread hus i is necessary o chec wheher he spread beween wo securiies has such propery in hisorical daa. If he spread has obvious upward or downward rend hen loss may incurr; seing a loss cuing limi can be implemened. I seems ha he smooher/filer approach has beer reurns (0.4% 8.79%) compared wih he linear regression approach. The cos of rebalancing in he laer one may be he reason. We have o realize ha linear regression is simple i is easy o be inerpreed while smooher/filer approach is a blac box. Reference [] Saisical Arbirage in he U.S. Equiies Mare Marco Avellaneda Jeong-Hyun Lee. [] Sanford Weisberg Applied Linear Regression hird ediion John Wiley & Sons Inc. [3] David Serbin Trend following signal confirmaion using non-price indicaors. [4] Rober J. Ellio John van der Hoe William P. Malcolm Pairs Trading Quaniaive Finance Vol. 5(3) pp. 7-76. [5] Ellio R.J. L. Aggoun J.B. Moore Hidden Marov Models. (Springer Verlag 995) [6] Shumway R.H. D.S. Soffer. An Approach o Time Series Smoohing Fore-casing using he EM Algorihm. Journal of Time Series 3 [4] (98) 53-64.