I am trying to implement multidimensional Granger causality in python. For that matter I am using Vector Autoregression from Statsmodels, but when I try to get coeffcients out of it, it returns me an empty matrix. Can somebody tell me what is wrong exactly?
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)
You can use the test_causality method of the VARResults instance to test Granger causality. See the documentation here and the example here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With