Skip to content

Instantly share code, notes, and snippets.

# check if the VAR residuals are white noise or still contain a signal
# a Durbin-Watson test statistic of 2.0 (as opposed to 0 or 4) would indicate no detectable autocorrelation
dw = durbin_watson(res.resid)
print(f'Durbin-Watson statistic (Tyrion): {dw[0]:.2f}')
print(f'Durbin-Watson statistic (Cersei): {dw[1]:.2f}')
if (1.9 <= dw[0] <= 2.1) and (1.9 <= dw[1] <= 2.1):
print("test passed: no discernible signal in the residuals")
# run the VAR model again, for the differenced time series, and add the order of differencing
# to the lags that had minimized the AIC in the preceding VAR1 model (Dave Giles 2011, step #8)
VARmodel2 = VAR(df_iTC) # df_iTC contains the integrated / differenced time series we prepared in our testing of stationarity
resVAR2 = VARmodel2.fit(maxlags=lag_min_aic + n_diff_Tyrion, ic='aic')
lag_order = resVAR2.k_ar
print("VAR2 lag-order: " + str(lag_order))
resVAR2.summary()
# test for cointegration using Engle-Granger
# null hypothesis: Tyrion and Cersei are NOT cointegrated
score, pvalue, _ = coint(df["Tyrion"], df["Cersei"], maxlag=lag_min_aic)
pvalue
print(f'cointegration: score: {score:.2f}')
print(f'cointegration: p-value: {pvalue:.10f}')
print("cointegrated - there MUST be Granger causality" if pvalue < ALPHA else "NOT cointegrated (uncertain about Granger causality)")
# run the granger causality test for Cersei as causal agent
print("lags (min AIC): " + str(lag_min_aic))
gct_Cersei_caus = grangercausalitytests(x=ar_iTC, verbose=True, maxlag=lag_min_aic)
# swap Tyrion's and Cersei's columns
ar_iCT = ar_iTC.copy()
ar_iCT[:, [1,0]] = ar_iCT[:, [0,1]]
# run the granger causality test for Tyrion as causal agent
print("lags (min AIC): " + str(lag_min_aic))
gct_Tyrion_caus = grangercausalitytests(x=ar_iCT, verbose=True, maxlag=lag_min_aic)
# going off the rails:
# how does causality develop over a far more expansive range of lags than the VAR model justifies?
# causality tests up to a lag of 20 weeks
MAXLAG = 20
TEST = "ssr_ftest" # choose the F-test as criterion
lags_max = range(0,MAXLAG)
# test Tyrion and get his p-values:
# convert "Week" object/string to date and set datetime index
df.rename(columns={"Week": "Date"}, inplace=True)
df.Date = df["Date"]
df.Date = pd.to_datetime(df.Date)
df.set_index(df.Date)
df[["Tyrion", "Cersei"]] = df[["Tyrion", "Cersei"]].apply(pd.to_numeric, errors="ignore")
df.info()
df = pd.read_csv("got_Cersei_Tyrion.csv")
df
# Stationarity and SARIMA model
import pandas as pd
import datetime as dt
import numpy as np
import matplotlib.pyplot as plt
import pmdarima as pmd
import statsmodels.api as sm