I am currently attempting to calculate the halflife of a mean reverting series using python programming language and the theory of the Ornstein–Uhlenbeck process.
I have a series which when plotted looks like:
Which obviously looks rather mean reverting. I am carrying out the following using python code to find the halflife (FYI the series shown above is held in the variable (z_array):
import numpy as np
import statsmodels.api as sm
#set up lagged series of z_array and return series of z_array
z_lag = np.roll(z_array,1)
z_lag[0] = 0
z_ret = z - z_lag
z_ret[0] = 0
#run OLS regression to find regression coefficient to use as "theta"
model = sm.OLS(z_ret,z_lag)
res = model.fit()
#calculate halflife
halflife = -log(2) / res.params[0]
print 'Halflife = ',halflife
The code runs fine, however for this series I am getting a halflife of 680.5 days - I can see from the chart that this looks very wrong. Full reversions are happening within a fraction of that time frame.
Could someone please advise me as to where I am going wrong with this?
Any help much appreciated!