Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@h3ik0th
h3ik0th / GoT_grangercausality
Created September 16, 2021 15:37
GoT_grangercausality_01.py
import statsmodels.api as sm
from statsmodels.stats.stattools import durbin_watson
from statsmodels.tsa.stattools import grangercausalitytests, adfuller, kpss, coint
from pmdarima.arima import ndiffs # for order of differencing
from statsmodels.tsa.api import VAR
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import warnings
# plot Tyrion and Cersei's fame:
ax = df["Tyrion"].plot(color="blue", label="Tyrion", legend=True, title="Tyrion & Cersei", figsize=(12,6))
df["Cersei"].plot(color="red", label="Cersei", style="-", legend=True, ax=ax)
ax.autoscale(axis="x",tight=True)
ax.set(xlabel="week", ylabel="fame")
# should we expect Granger causality, based on cointegration?
# test for cointegration using Engle-Granger
# null hypothesis: Tyrion and Cersei are NOT cointegrated
# we choose lags up to 10, the typical length of a season, during
# which the interested audience received news about Tyrion and Cersei;
score, pvalue, _ = coint(df["Tyrion"], df["Cersei"], maxlag=10)
pvalue
print(f'cointegration: score: {score:.2f}')
# Are Tyrion and Cersei stationary processes?
# ADF and KPSS tests:
def ADF_test(x):
adf_result = adfuller(x.values)
t_stat, p_value, _, _, critical_values, _ = adf_result
print(f'ADF statistic: {t_stat:.2f}')
#for key, value in critical_values.items():
# Tyrion: get the recommended order of differencing
is_Stationary(df["Tyrion"])
n_diff_Tyrion = nDiff(df["Tyrion"])
print("Tyrion's recommended order of differencing: " + str(n_diff_Tyrion))
# difference Tyrion and check again for stationarity:
sT = df["Tyrion"].diff(n_diff_Tyrion).dropna()
is_Stationary(sT)
# difference Cersei by the same order as Tyrion and check her stationarity:
sC = df["Cersei"].diff(n_diff_Tyrion).dropna()
is_Stationary(sC)
n_diff_Cersei = nDiff(sC)
# The integrated Tyrion & Cersei, after differencing
df_iTC = pd.DataFrame({"Tyrion":sT, "Cersei":sC})
ar_iTC = df_iTC.to_numpy()
df_iTC.describe()
# plot Tyrion and Cersei after differencing:
ax = sT.plot(color="blue", label="Tyrion", legend=True, title="differenced: Tyrion & Cersei", figsize=(12,6))
sC.plot(color="red", label="Cersei", style="-", legend=True, ax=ax)
ax.autoscale(axis="x",tight=True)
ax.set(xlabel="week", ylabel="fame")
MAXLAG = 52
VARmodel1 = VAR(df_TC) # use the time series BEFORE differencing
lags = range(0, MAXLAG)
ic_dict = dict(enumerate(lags))
# fill a dictionary with the information criteria which the VAR model computes at each lag
for L in lags:
res = VARmodel1.fit(L)
ic_dict[L] = [res.aic, res.bic, res.hqic]
# find lag at which the respective information criterion has its minimum: