Skip to content

Instantly share code, notes, and snippets.

@h3ik0th
Created September 16, 2021 16:28
Show Gist options
  • Save h3ik0th/b10bf6328a8c2f69eae84652b5cb306d to your computer and use it in GitHub Desktop.
Save h3ik0th/b10bf6328a8c2f69eae84652b5cb306d to your computer and use it in GitHub Desktop.
# 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:
gct_Tyrion = grangercausalitytests(ar_iCT, maxlag=MAXLAG, verbose=False)
p_values_Tyrion = [round(gct_Tyrion[i+1][0][TEST][1],4) for i in lags_max]
# test Cersei and get her p-values:
gct_Cersei = grangercausalitytests(ar_iTC, maxlag=MAXLAG, verbose=False)
p_values_Cersei = [round(gct_Cersei[i+1][0][TEST][1],4) for i in lags_max]
#combine Tyrion and Cersei's p-values in a dictionary
dict_p = {lags_max[L]: [p_values_Tyrion[L], p_values_Cersei[L]] for L in lags_max}
df_p = pd.DataFrame.from_dict(dict_p).T
df_p = df_p.iloc[1:, :]
df_p = df_p.rename(columns={0:"Tyrion", 1:"Cersei"})
df_p
# plot Tyrion and Cersei's p-values
ax = df_p["Tyrion"].plot(color="blue", label="Tyrion as causal agent", legend=True, title="Granger Causality p-values: Tyrion & Cersei", figsize=(12,6))
df_p["Cersei"].plot(color="red", label="Cersei as causal agent", style="-", legend=True, ax=ax)
ax.autoscale(axis="x",tight=True)
ax.set(xlabel="lag", ylabel="p-value")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment