import pandas as pd
from datetime import datetime
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.tsa.arima.model import ARIMA
from statsmodels.tsa.stattools import adfuller

6: AR Model#

def parser(s):
    return datetime.strptime(s, '%Y-%m-%d')
def perform_adf_test(series):
    result = adfuller(series)
    print('ADF Statistic: %f' % result[0])
    print('p-value: %f' % result[1])
#get data
series = pd.read_csv('../data/catfish.csv', parse_dates=[0], index_col=0)
series = series.asfreq(pd.infer_freq(series.index))
series = series.loc[datetime(2004,1,1):]
series = series.diff().diff().dropna()
#check stationarity
perform_adf_test(series)
ADF Statistic: -7.162321
p-value: 0.000000
plt.plot(series)
[<matplotlib.lines.Line2D at 0x7f56935c2190>]
../../../_images/29b94e030c303edcb2b26b2638983e3e9e8d6d80e20bd121465088558d31f8e7.png
plot_pacf(series, lags=10)
plt.show()
/home/ubuntu/Documents/Projects/STI_FX_Intervention/.venv/lib/python3.9/site-packages/statsmodels/graphics/tsaplots.py:348: FutureWarning: The default method 'yw' can produce PACF values outside of the [-1,1] interval. After 0.13, the default will change tounadjusted Yule-Walker ('ywm'). You can use this method now by setting method='ywm'.
  warnings.warn(
../../../_images/fdf5857f1417a82fed697c32ab6995ee23f33eeb3b51072e5a1753fa6517d061.png

AR(4) seems to be reasonable enough#

ARIMA?
ar_model = ARIMA(series, order=(4,0,0))
ar_model_fit = ar_model.fit()
print(ar_model_fit.summary())
                               SARIMAX Results                                
==============================================================================
Dep. Variable:                  Total   No. Observations:                  106
Model:                 ARIMA(4, 0, 0)   Log Likelihood                -957.805
Date:                Wed, 22 Mar 2023   AIC                           1927.610
Time:                        12:55:45   BIC                           1943.591
Sample:                    03-01-2004   HQIC                          1934.087
                         - 12-01-2012                                         
Covariance Type:                  opg                                         
==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------
const        -19.0749     59.076     -0.323      0.747    -134.862      96.712
ar.L1         -0.9029      0.105     -8.620      0.000      -1.108      -0.698
ar.L2         -0.7355      0.121     -6.098      0.000      -0.972      -0.499
ar.L3         -0.6742      0.110     -6.119      0.000      -0.890      -0.458
ar.L4         -0.5772      0.080     -7.258      0.000      -0.733      -0.421
sigma2      4.064e+06   6.27e+05      6.479      0.000    2.83e+06    5.29e+06
===================================================================================
Ljung-Box (L1) (Q):                   0.46   Jarque-Bera (JB):                 0.42
Prob(Q):                              0.50   Prob(JB):                         0.81
Heteroskedasticity (H):               0.85   Skew:                             0.15
Prob(H) (two-sided):                  0.63   Kurtosis:                         3.00
===================================================================================

Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step).
plt.plot(series)
plt.plot(ar_model_fit.fittedvalues)
plt.title('AR(%s) Fit'%4, fontsize=16)

plt.tight_layout()
../../../_images/e2de188e85f929935bf74fb3dc27dc81d8e6956550cccab3282cea2008ba755a.png