我有一个时间序列的 pandas 数据帧df1,我检查我的时间序列是否是静止的,如果不是,我确实想检查是否存在协整,以防万一创建它们的 VECM。这是为了避免区分以使它们静止:
from statsmodels.tsa.stattools import adfuller #to check unit root in time series
threshold=0.05 #significance level
coint = []
for column in df1.columns:
result=adfuller(df1[column])
if result[1]>threshold:
print('P-value for ' + column + ' is: ' + str(result[1]))
coint.append(column)
l = coint
combin = [comb for comb in combinations(l, 2)] # this is a list of all the pairwise columns which are non-stationary
在我检查成对列之间的协整之后:
import statsmodels.api as sm
df = df1.copy()
for comb in combin:
i = comb[0]
j = comb[1]
x, y = df[j], df[i]
var1 = adfuller(df[i])
var2 = adfuller(df[j])
x = sm.add_constant(x)
result = sm.OLS(y,x).fit()
b = result.params[1]
x = df[j]
diff = adfuller(y - b*x)
print("The p-value for the ADF test on " + i + " is ", var1[1])
print("The p-value for the ADF test on " + j + " is ", var2[1])
print("The p-value for the ADF test on the difference is ", diff[1])
if diff[1]<0.05:
print('We have cointegration')
else: print('We do not have cointegration')
我的问题是:
一旦验证了 2 个变量是协整的,我如何从它们构建协整变量?换句话说,我如何创建 VECM?
上面的协整测试是成对的,但我可能有多个协整,是否有一个库允许我在构建 VECM 后测试多个变量?