python - 使用 Python Statsmodels 进行向量自回归

标签 python statistics time-series statsmodels causality

我正在尝试在 python 中实现多维 Granger 因果关系。就此而言,我使用的是 Statsmodels 的 Vector Autoregression,但是当我尝试从中获取系数时,它会返回一个空矩阵。谁能告诉我到底出了什么问题?

import numpy as np
from statsmodels.tsa.vector_ar import var_model
def multi_dim_granger(X_ts,Y_ts,order=5,test='F-test'):
    """Multivariate Granger cusality.
    input:
        X_ts: the first vector time series. 
              TxK matrix with T being the time instance and K is the dimension  
        Y_ts: the second vector time series. 
              TxK matrix with T being the time instance and K is the dimension  

        order: the maximum number of lags for fitting a VAR process
        test: the statistical test to check for the residual covariance matrix
    """
    ts=np.hstack((X,Y))
    print ts.shape
    VAR_model=var_model.VAR(ts)
    ts=VAR_model.fit(ic='aic',maxlags=order)
    return ts.coefs
X=np.random.randn(1000,2)
Y=(np.arange(4000)*np.random.randn(4000)).reshape((1000,4))
multi_dim_granger(X,Y)

最佳答案

您可以使用 VARResults 实例的 test_causality 方法来检验 Granger 因果关系。请参阅文档 here和示例 here .

关于python - 使用 Python Statsmodels 进行向量自回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26585825/

相关文章:

python - Python中的方差膨胀因子

arrays - 如何创建正交数组?

python - 如何计算两个 float 列表的 p 值?

r - 为什么时间序列没有断点

python - Python 中的类型推导

python - 用列表理解替换 for 循环

python - 装饰器来代替重复的面向对象代码?

r - 在 XTS 中添加列名称并更改 XTS 中的日期

python - FB Prophet 按分钟预测

python - 如果无论我如何训练网络或扩展其容量,我的网络都不会过度拟合,这意味着什么?