绘制两对之间的历史协整值 [英] Plotting Historical Cointegration Values between two pairs

查看:71
本文介绍了绘制两对之间的历史协整值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是python中的示例ADF测试,用于检查两对之间的协整.但是最终结果仅给出了用于协整的数值.如何获得协整的历史结果.

Here is the sample ADF test in python to check for Cointegration between two pairs. However the final result gives only the numeric value for co-integration. How to get the historical results of Co-integration.

来自 http://www.leinenbock.com/adf-test-in -python/

import numpy as np
import statsmodels.api as stat
import statsmodels.tsa.stattools as ts

x = np.random.normal(0,1, 1000)
y = np.random.normal(0,1, 1000)

def cointegration_test(y, x):
    result = stat.OLS(y, x).fit()    
    return ts.adfuller(result.resid)

推荐答案

我假设您要测试扩展的协整性?请注意,您应该使用sm.tsa.coint进行协整测试.您可以使用像这样的熊猫来测试realgdp和realdpi之间的历史协整关系

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,))

这篇关于绘制两对之间的历史协整值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆