In this notebook we will show how to perform a Granger Causality analysis, and demonstrate how poorly it performs on our simulated data.


In [4]:
import os,sys
import numpy
%matplotlib inline
import matplotlib.pyplot as plt
import statsmodels.tsa.stattools

from dcm_sim import sim_dcm_dataset

sys.path.insert(0,'../')
from utils.graph_utils import show_graph_from_adjmtx,show_graph_from_pattern

# first we simulate some data using our DCM model, with the same HRF across all regions

_,data_conv,params=sim_dcm_dataset(verbose=True)

A=params['A']
B=params['B']
C=params['C']

data=data_conv[range(0,data_conv.shape[0],int(1./params['stepsize']))]


A
[[-1.  0.  0.  0.  0.]
 [ 0. -1.  0.  0.  0.]
 [ 0.  1. -1.  0.  0.]
 [ 0.  1.  0. -1.  0.]
 [ 0.  0.  0.  0. -1.]]
B
[[ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 1.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 1.  0.  0.  0.  0.]]
C
[[ 1.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 0.]]

Now compute Granger causality across all pairs of timeseries


In [5]:
gc=numpy.zeros(A.shape)
for i in range(A.shape[0]):
    for j in range(A.shape[0]):
        if i==j:  # don't compute self-connectivity
            continue
        result=statsmodels.tsa.stattools.grangercausalitytests(data[:,[i,j]],1)
        if result[1][0]['params_ftest'][1]<0.05:
            gc[i,j]=1


Granger Causality
('number of lags (no zero)', 1)
ssr based F test:         F=9.1313  , p=0.0027  , df_denom=296, df_num=1
ssr based chi2 test:   chi2=9.2239  , p=0.0024  , df=1
likelihood ratio test: chi2=9.0845  , p=0.0026  , df=1
parameter F test:         F=9.1313  , p=0.0027  , df_denom=296, df_num=1

Granger Causality
('number of lags (no zero)', 1)
ssr based F test:         F=0.3328  , p=0.5644  , df_denom=296, df_num=1
ssr based chi2 test:   chi2=0.3362  , p=0.5620  , df=1
likelihood ratio test: chi2=0.3360  , p=0.5621  , df=1
parameter F test:         F=0.3328  , p=0.5644  , df_denom=296, df_num=1

Granger Causality
('number of lags (no zero)', 1)
ssr based F test:         F=0.0275  , p=0.8684  , df_denom=296, df_num=1
ssr based chi2 test:   chi2=0.0278  , p=0.8676  , df=1
likelihood ratio test: chi2=0.0278  , p=0.8676  , df=1
parameter F test:         F=0.0275  , p=0.8684  , df_denom=296, df_num=1

Granger Causality
('number of lags (no zero)', 1)
ssr based F test:         F=4.3450  , p=0.0380  , df_denom=296, df_num=1
ssr based chi2 test:   chi2=4.3890  , p=0.0362  , df=1
likelihood ratio test: chi2=4.3571  , p=0.0369  , df=1
parameter F test:         F=4.3450  , p=0.0380  , df_denom=296, df_num=1

Granger Causality
('number of lags (no zero)', 1)
ssr based F test:         F=2.0748  , p=0.1508  , df_denom=296, df_num=1
ssr based chi2 test:   chi2=2.0959  , p=0.1477  , df=1
likelihood ratio test: chi2=2.0886  , p=0.1484  , df=1
parameter F test:         F=2.0748  , p=0.1508  , df_denom=296, df_num=1

Granger Causality
('number of lags (no zero)', 1)
ssr based F test:         F=4.3860  , p=0.0371  , df_denom=296, df_num=1
ssr based chi2 test:   chi2=4.4304  , p=0.0353  , df=1
likelihood ratio test: chi2=4.3979  , p=0.0360  , df=1
parameter F test:         F=4.3860  , p=0.0371  , df_denom=296, df_num=1

Granger Causality
('number of lags (no zero)', 1)
ssr based F test:         F=8.5122  , p=0.0038  , df_denom=296, df_num=1
ssr based chi2 test:   chi2=8.5985  , p=0.0034  , df=1
likelihood ratio test: chi2=8.4772  , p=0.0036  , df=1
parameter F test:         F=8.5122  , p=0.0038  , df_denom=296, df_num=1

Granger Causality
('number of lags (no zero)', 1)
ssr based F test:         F=4.7968  , p=0.0293  , df_denom=296, df_num=1
ssr based chi2 test:   chi2=4.8454  , p=0.0277  , df=1
likelihood ratio test: chi2=4.8066  , p=0.0284  , df=1
parameter F test:         F=4.7968  , p=0.0293  , df_denom=296, df_num=1

Granger Causality
('number of lags (no zero)', 1)
ssr based F test:         F=1.3658  , p=0.2435  , df_denom=296, df_num=1
ssr based chi2 test:   chi2=1.3796  , p=0.2402  , df=1
likelihood ratio test: chi2=1.3764  , p=0.2407  , df=1
parameter F test:         F=1.3658  , p=0.2435  , df_denom=296, df_num=1

Granger Causality
('number of lags (no zero)', 1)
ssr based F test:         F=5.3406  , p=0.0215  , df_denom=296, df_num=1
ssr based chi2 test:   chi2=5.3947  , p=0.0202  , df=1
likelihood ratio test: chi2=5.3466  , p=0.0208  , df=1
parameter F test:         F=5.3406  , p=0.0215  , df_denom=296, df_num=1

Granger Causality
('number of lags (no zero)', 1)
ssr based F test:         F=1.6741  , p=0.1967  , df_denom=296, df_num=1
ssr based chi2 test:   chi2=1.6911  , p=0.1935  , df=1
likelihood ratio test: chi2=1.6863  , p=0.1941  , df=1
parameter F test:         F=1.6741  , p=0.1967  , df_denom=296, df_num=1

Granger Causality
('number of lags (no zero)', 1)
ssr based F test:         F=0.0097  , p=0.9214  , df_denom=296, df_num=1
ssr based chi2 test:   chi2=0.0098  , p=0.9210  , df=1
likelihood ratio test: chi2=0.0098  , p=0.9210  , df=1
parameter F test:         F=0.0097  , p=0.9214  , df_denom=296, df_num=1

Granger Causality
('number of lags (no zero)', 1)
ssr based F test:         F=0.3514  , p=0.5538  , df_denom=296, df_num=1
ssr based chi2 test:   chi2=0.3550  , p=0.5513  , df=1
likelihood ratio test: chi2=0.3548  , p=0.5514  , df=1
parameter F test:         F=0.3514  , p=0.5538  , df_denom=296, df_num=1

Granger Causality
('number of lags (no zero)', 1)
ssr based F test:         F=8.8805  , p=0.0031  , df_denom=296, df_num=1
ssr based chi2 test:   chi2=8.9705  , p=0.0027  , df=1
likelihood ratio test: chi2=8.8385  , p=0.0029  , df=1
parameter F test:         F=8.8805  , p=0.0031  , df_denom=296, df_num=1

Granger Causality
('number of lags (no zero)', 1)
ssr based F test:         F=0.6261  , p=0.4294  , df_denom=296, df_num=1
ssr based chi2 test:   chi2=0.6325  , p=0.4264  , df=1
likelihood ratio test: chi2=0.6318  , p=0.4267  , df=1
parameter F test:         F=0.6261  , p=0.4294  , df_denom=296, df_num=1

Granger Causality
('number of lags (no zero)', 1)
ssr based F test:         F=0.0269  , p=0.8699  , df_denom=296, df_num=1
ssr based chi2 test:   chi2=0.0272  , p=0.8691  , df=1
likelihood ratio test: chi2=0.0272  , p=0.8691  , df=1
parameter F test:         F=0.0269  , p=0.8699  , df_denom=296, df_num=1

Granger Causality
('number of lags (no zero)', 1)
ssr based F test:         F=0.5649  , p=0.4529  , df_denom=296, df_num=1
ssr based chi2 test:   chi2=0.5706  , p=0.4500  , df=1
likelihood ratio test: chi2=0.5701  , p=0.4502  , df=1
parameter F test:         F=0.5649  , p=0.4529  , df_denom=296, df_num=1

Granger Causality
('number of lags (no zero)', 1)
ssr based F test:         F=11.1816 , p=0.0009  , df_denom=296, df_num=1
ssr based chi2 test:   chi2=11.2950 , p=0.0008  , df=1
likelihood ratio test: chi2=11.0868 , p=0.0009  , df=1
parameter F test:         F=11.1816 , p=0.0009  , df_denom=296, df_num=1

Granger Causality
('number of lags (no zero)', 1)
ssr based F test:         F=2.8170  , p=0.0943  , df_denom=296, df_num=1
ssr based chi2 test:   chi2=2.8456  , p=0.0916  , df=1
likelihood ratio test: chi2=2.8321  , p=0.0924  , df=1
parameter F test:         F=2.8170  , p=0.0943  , df_denom=296, df_num=1

Granger Causality
('number of lags (no zero)', 1)
ssr based F test:         F=0.1653  , p=0.6846  , df_denom=296, df_num=1
ssr based chi2 test:   chi2=0.1670  , p=0.6828  , df=1
likelihood ratio test: chi2=0.1670  , p=0.6828  , df=1
parameter F test:         F=0.1653  , p=0.6846  , df_denom=296, df_num=1

In [6]:
show_graph_from_adjmtx(gc,numpy.zeros(B.shape),numpy.zeros(C.shape),title='Granger')
show_graph_from_adjmtx(A,B,C,title='True model')


[(1, 0), (1, 2), (1, 3), (1, 4), (2, 1), (3, 1), (4, 0), (4, 1)]
Black: input
Red: unmodulated
[(0, 2), (0, 4), (1, 2), (1, 3), ('u', 0)]
Black: input
Red: unmodulated
Blue: modulated
Out[6]:
<networkx.classes.digraph.DiGraph at 0x10a47a650>

Exercise: Try running this notebook a few times, and watch how the GC results vary from run to run.


In [ ]: