This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# difference Tyrion and check again for stationarity: | |
sT = df["Tyrion"].diff(n_diff_Tyrion).dropna() | |
is_Stationary(sT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The integrated Tyrion & Cersei, after differencing | |
df_iTC = pd.DataFrame({"Tyrion":sT, "Cersei":sC}) | |
ar_iTC = df_iTC.to_numpy() | |
df_iTC.describe() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
OlderNewer