<jupyter_start><jupyter_text>test the stationarity1. DIcky fuller test2.GRANGER Causilityevaluating forecast METRIC.MAE.MSE.RSMEanthor two .AIC(AKAIKE INFORAMTION CRITERION)  perform poor on new unseen data.BIC(BAYESIAN INFORMATION CRITERION)<jupyter_code>import pandas as pd
import numpy as np
%matplotlib inline

# Ignore harmless warnings
import warnings
warnings.filterwarnings("ignore")

# Load a seasonal dataset
df1 = pd.read_csv('tser_lec/Data/airline_passengers.csv',index_col='Month',parse_dates=True)
df1.index.freq = 'MS'

# Load a nonseasonal dataset
df2 = pd.read_csv('tser_lec/Data/DailyTotalFemaleBirths.csv',index_col='Date',parse_dates=True)
df2.index.freq = 'D'
df1.plot()
from statsmodels.tsa.stattools import adfuller
print('Augmented Dickey-Fuller Test on Airline Data')
dftest = adfuller(df1['Thousands of Passengers'],autolag='AIC')
dftest
print('Augmented Dickey-Fuller Test on Airline Data')

dfout = pd.Series(dftest[0:4],index=['ADF test statistic','p-value','# lags used','# observations'])

for key,val in dftest[4].items():
    dfout[f'critical value ({key})']=val
print(dfout)
#apply on statioanry data
df2.head()
df2['30-Day-SMA'] = df2['Births'].rolling(window=30).mean()
df2['30-Day-Std'] = df2['Births'].rolling(window=30).std()

df2[['Births','30-Day-SMA','30-Day-Std']].plot();
print('Augmented Dickey-Fuller Test on Daily Female Births')
dftest2 = adfuller(df2['Births'],autolag='AIC')
dfout2 = pd.Series(dftest2[0:4],index=['ADF test statistic','p-value','# lags used','# observations'])

for key,val in dftest2[4].items():
    dfout2[f'critical value ({key})']=val
print(dfout2)
#p=0(so it is stationary)<jupyter_output><empty_output><jupyter_text>Function for running the augmented Dickey-Fuller test¶<jupyter_code>from statsmodels.tsa.stattools import adfuller

def adf_test(series,title=''):
    """
    Pass in a time series and an optional title, returns an ADF report
    """
    print(f'Augmented Dickey-Fuller Test: {title}')
    result = adfuller(series.dropna(),autolag='AIC') # .dropna() handles differenced data
    
    labels = ['ADF test statistic','p-value','# lags used','# observations']
    out = pd.Series(result[0:4],index=labels)

    for key,val in result[4].items():
        out[f'critical value ({key})']=val
        
    print(out.to_string())          # .to_string() removes the line "dtype: float64"
    
    if result[1] <= 0.05:
        print("Strong evidence against the null hypothesis")
        print("Reject the null hypothesis")
        print("Data has no unit root and is stationary")
    else:
        print("Weak evidence against the null hypothesis")
        print("Fail to reject the null hypothesis")
        print("Data has a unit root and is non-stationary")
adf_test(df1['Thousands of Passengers'])
df1['Thousands of Passengers'].plot()
df2.plot()
adf_test(df2['Births'])<jupyter_output>Augmented Dickey-Fuller Test: 
ADF test statistic       -4.808291
p-value                   0.000052
# lags used               6.000000
# observations          358.000000
critical value (1%)      -3.448749
critical value (5%)      -2.869647
critical value (10%)     -2.571089
Strong evidence against the null hypothesis
Reject the null hypothesis
Data has no unit root and is stationary<jupyter_text>Granger Causality Tests<jupyter_code>df3 = pd.read_csv('tser_lec/Data/samples.csv',index_col=0,parse_dates=True)
df3.index.freq = 'MS'
df3[['a','d']].plot(figsize=(12,8));
df3['a'].iloc[2:].plot(figsize=(16,5),legend=True);
df3['d'].shift(2).plot(legend=True);<jupyter_output><empty_output><jupyter_text>Run the test¶<jupyter_code>from statsmodels.tsa.stattools import grangercausalitytests
# Add a semicolon at the end to avoid duplicate output
grangercausalitytests(df3[['a','d']],maxlag=3);;# causality
# Add a semicolon at the end to avoid duplicate output
grangercausalitytests(df3[['b','d']],maxlag=3);# no causality
#p<0.05<jupyter_output>Granger Causality
number of lags (no zero) 1
ssr based F test:         F=1.5225  , p=0.2197  , df_denom=116, df_num=1
ssr based chi2 test:   chi2=1.5619  , p=0.2114  , df=1
likelihood ratio test: chi2=1.5517  , p=0.2129  , df=1
parameter F test:         F=1.5225  , p=0.2197  , df_denom=116, df_num=1

Granger Causality
number of lags (no zero) 2
ssr based F test:         F=0.4350  , p=0.6483  , df_denom=113, df_num=2
ssr based chi2 test:   chi2=0.9086  , p=0.6349  , df=2
likelihood ratio test: chi2=0.9051  , p=0.6360  , df=2
parameter F test:         F=0.4350  , p=0.6483  , df_denom=113, df_num=2

Granger Causality
number of lags (no zero) 3
ssr based F test:         F=0.5333  , p=0.6604  , df_denom=110, df_num=3
ssr based chi2 test:   chi2=1.7018  , p=0.6365  , df=3
likelihood ratio test: chi2=1.6895  , p=0.6393  , df=3
parameter F test:         F=0.5333  , p=0.6604  , df_denom=110, df_num=3<jupyter_text>Evaluating forecast accuracy<jupyter_code>import numpy as np
import pandas as pd
%matplotlib inline
np.random.seed(42)
df = pd.DataFrame(np.random.randint(20,30,(50,2)),columns=['test','predictions'])
df.plot(figsize=(12,4));
from statsmodels.tools.eval_measures import mse, rmse, meanabs
MSE = mse(df['test'],df['predictions'])
RMSE = rmse(df['test'],df['predictions'])
MAE = meanabs(df['test'],df['predictions'])

print(f'Model  MSE: {MSE:.3f}')
print(f'Model RMSE: {RMSE:.3f}')
print(f'Model  MAE: {MAE:.3f}')
import pandas as pd
import numpy as np
%matplotlib inline

df = pd.read_csv('tser_lec/Data/airline_passengers.csv',index_col='Month',parse_dates=True)
df.index.freq = 'MS'
df.head()
df.head()
df.plot()
from statsmodels.graphics.tsaplots import month_plot,quarter_plot

month_plot(df['Thousands of Passengers']);
dfq = df['Thousands of Passengers'].resample(rule='Q').mean()

quarter_plot(dfq);
df3 = pd.read_csv('tser_lec/Data/macrodata.csv',index_col=0,parse_dates=True)
df3['realgdp'].plot()
quarter_plot(df3['realgdp']);<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup