Plotting Historical Cointegration Values between two pairs

久未见 提交于 2019-12-03 08:27:31

I assume you want to test for expanding cointegration? Note that you should use sm.tsa.coint to test for cointegration. You could test for historical cointegrating relationship between realgdp and realdpi using pandas like so

import pandas as pd
import statsmodels.api as sm

data = sm.datasets.macrodata.load_pandas().data

def rolling_coint(x, y):
    yy = y[:len(x)]
    # returns only the p-value
    return sm.tsa.coint(x, yy)[1]

historical_coint = pd.expanding_apply(data.realgdp, rolling_coint, 
                                      min_periods=36, 
                                      args=(data.realdpi,))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!