Wednesday, 2 March 2016

Time Series Analysis

We can assess whether the time series is prone to mean reversion through the hypothesis that it is statistically significantly different from a random walk. For a mean reverting time series, the change in the value in the next time period is proportional to the difference between mean and present price. This is a continuous time series, known as an Ornstein-Uhlenbeck process.


Screen Shot 2016-02-23 at 10.33.12 AM.png
θ is the rate of reversion to the mean,  μ, and σ is the variance of the process and Wt is a Wiener Process or Brownian Motion.


Python libraries pandas and statsmodels is useful to test whether the time series follows the Ornstein-Uhlenbeck process. Here I will be trying out R instead.


The Augmented Dickey-Fuller (ADF) test uses this Orsentein-Uhlenbeck process as an assumption and tests for the presence of a unit root in an autoregressive time series. If the process has a unit root, then it is a non-stationary time series. ie If y = 0 then it is a random walk process.


The linear lag model of order p indicates the change in the price is proportional to a constant, time, the previous p values and an error term. β will be 0 if there is a long term drift in the price.
Screen Shot 2016-02-23 at 12.08.01 PM.png
The ADF test follows these steps:
  1. set p = 1 which is sufficient according to trading research
  1. calculate the test statistic, DFτ
  2. use the distribution of this test statistic along with the selected critical value to decide whether to reject the null hypothesis that y = 0 (hence α = β = 0 and non stationary) ie when DFτ is smaller than the critical values at whichever chosen significance levels.
Screen Shot 2016-02-23 at 12.08.44 PM.png


We firstly download Yahoo finance data on Amazon OHLCV data as a .csv file, from 1/1/2010 to 23/4/2016.


Screen Shot 2016-02-24 at 1.51.11 PM.png
Screen Shot 2016-02-24 at 1.51.54 PM.png
We therefore cannot reject the null and conclude that a mean reversion strategy would most likely not work on this stock as it does not seem to exhibit mean reversion over the past 6 years.


Another approach can be to determine whether the time series is stationary - the mean and variance of the stochastic process does not change over time and therefore there is no trend. This occurs when the joint probability distribution is constant. The Hurst Exponent can help us in this way by providing a scalar value. Here we are measuring the rate in which prices diverge from their initial value to detect whether the nature of the time series is mean reverting or a random walk or a trend. If it is a stationary series then this rate is slower than a Geometric Brownian Motion (random walk).


More on the Hurst Exponent: http://www.jstor.org/stable/3213900?seq=1#page_scan_tab_contents


To get to this scalar value we use the variance of a log time series ( time lag T ) to assess the rate of diffusive behaviour.
Screen Shot 2016-02-24 at 5.50.42 PM.png
Where the brackets ⟨ and ⟩ refer to the average over all values of t.
For a random walk, when T is large the variance of T is proportional to T:
Screen Shot 2016-02-24 at 5.51.29 PM.png
If we find behaviour that differs from this, then we have identified either a trending or a mean-reverting series. This is because then the price changes are autocorrelated.
We then change it to the below equation with the Hurst Exponent value H:


Screen Shot 2016-02-24 at 5.51.53 PM.png
• H < 0.5 - The time series is mean reverting
• H = 0.5 - The time series is a Geometric Brownian Motion
• H > 0.5 - The time series is trending
The closer H is to 0 the higher it is mean reverting and the closer to 1 the higher it is trending.


This can be seen in R, where I create a random generation of numbers that follow GBM:
Screen Shot 2016-02-26 at 12.34.37 PM.png


Screen Shot 2016-02-26 at 11.51.56 AM.png
Screen Shot 2016-02-26 at 11.52.22 AM.png
Screen Shot 2016-02-26 at 11.52.44 AM.png
Screen Shot 2016-02-26 at 11.52.50 AM.png
We can conclude that Amazon’s adjusted close stock price over the past 6 years has followed a trend.


Equities are typically not an asset class that experiences mean reversion though we can create a portfolio of stocks with prices that are stationary. The pairs trade as Mary blogged about before for instance relies on mean reversion where relative prices diverge and converge to the mean. JP Morgan and BoA were used before and now I will demonstrate the research into their cointegrating nature to select such a portfolio.


This below plot is from Yahoo Finance and shows their respective price histories for the period Feb 28th 2015 to Feb 28th 2016.


Screen Shot 2016-02-29 at 8.36.56 PM.png
A scatterplot allows us to visually determine whether this pair are closely integrated.
Screen Shot 2016-03-01 at 10.44.50 PM.png
Screen Shot 2016-02-29 at 8.59.10 PM.png
The pairs trade should work with a linear model for a relationship between the two stock prices as supported by the scatterplot.
Screen Shot 2016-02-26 at 1.52.01 PM.png
Where y is the price of JPM Morgan stock and x is the price of BAC stock at time t.







By using the residuals of the linear regression we can perform the Cointegrated Augmented Dickey-Fuller Test to determine the optimal hedge ratio and test for stationarity under this beta required to create a linear combination. If so, then this pair trade assumption is fulfilled.

Update: I am experiencing a technical bug in performing this test in R and I will post the results as soon as I can. 

No comments:

Post a Comment