Below is an example with 3 timeseries that (I believe) should be co-integrated by definition. However only one of the co-integration relations gives a co-integrated timeseries.
np.random.seed(1234)
nums = np.random.normal(size=10000)
z = np.cumsum(nums)
p = .3*z + np.random.normal(size=10000)
q = .6*z + np.random.normal(size=10000)
r = .2*z + np.random.normal(size=10000)
ordering1 = np.stack((p,q,r),axis=1)
from statsmodels.tsa.vector_ar.vecm import coint_johansen
jres1 = coint_johansen(ordering1, 0, 1)
d1 = np.dot(ordering1,jres1.evec[0])
plt.plot(d1)
d2 = np.dot(ordering1,jres1.evec[1])
plt.plot(d2)
d3 = np.dot(ordering1,jres1.evec[2])
plt.plot(d3)
from statsmodels.tsa.stattools import adfuller
def ADF(v, max_d=1, level='10%'):
adf = adfuller(v, max_d)
if adf[0] < adf[4][level]:
return True
else:
return False
print(ADF(d1))
print(ADF(d2))
print(ADF(d3))
My questions are:
Why is only one timeseries co-integrated in the ADF test, this example should have no problem finding co-integrated series?
How can I know which of the possible co-integration relations I should use in order to get the most co-integrated timeseries. In the example it was number three but my tests has shown that this is by far not always the case.