Time Series Modelling: ARIMA & GARCH
Model S&P 500 returns data using the ARIMA and GARCH models
In this article you will learn how to:
Implement an ARIMA model
Implement a GARCH model
Fit the models to real life stock data
Introduction
Autoregressive Integrated Moving Average (ARIMA) and Generalized Autoregressive Conditional Heteroskedasticity (GARCH) are two widely used models in financial time series modelling.
Autoregressive Integrated Moving Average (ARIMA) models are used to model and forecast a time series process.
Generalized Autoregressive Conditional Heteroskedasticity (GARCH) models are used to model volatility and capture volatility clustering and time-varying volatility in financial time series.
This article will focus on the Python implementation and not the theory.
Load Imports
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from arch import arch_model
import yfinance as yf
from statsmodels.tsa.ar_model import AutoReg
from statsmodels.tsa.arima.model import ARIMA
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
Download Data
ticker = "SPY"
start_date = "2020-01-01"
end_date = "2023-01-01"
spy_data = yf.download(ticker, start=start_date, end=end_date)
data = np.log(spy_data["Adj Close"] / spy_data["Adj Close"].shift(1)).dropna()
Visualise Data
fig, ax = plt.subplots(2, 1, figsize=(10, 8))
ax[0].plot(spy_data["Adj Close"])
ax[0].set_title("SPY Closing Prices")
ax[1].plot(data, color="orange")
ax[1].set_title("SPY Log Returns")
plt.tight_layout()
plt.show()
Plot Autocorrelation Function
The autocorrelation function (ACF) shows how a time series is correlated with its own past observations at different lags.
It is used to identify the order (q) of the MA component of the ARIMA model.
Autocorrelation at lag k indicates correlation up to lag k, suggesting that we can model it using a MA process of order k.
In the figure below, there is significant autocorrelation at lag 9 which suggests it can be modelled with a MA process of order 9.
plot_acf(data, lags=20)
plt.title('Autocorrelation Function (ACF)')
plt.show()
Plot Partial Autocorrelation Function
The partial autocorrelation function (PACF) quantifies the correlation between a time series and its past values at a specific lag, excluding the influence of shorter lags.
The PACF is used to identify the order (p) of the AR component of the ARIMA model.
Significant partial autocorrelation at lag k indicates a direct relationship with the observation at lag k, suggesting the presence of a AR process of order k.
In the figure below, there is significant partial autocorrelation at lag 9 which suggests it can be modelled with a AR process of order 9.
plot_pacf(data, lags=20)
plt.title('Partial Autocorrelation Function (PACF)')
plt.show()
Fit ARIMA Model
Although the section above suggests we use an ARIMA model of order p=9, q=9. We shall use orders p=1 and q=1 for simplicity of this article.
arima_model = ARIMA(endog=data, order=(1, 0, 1))
res = arima_model.fit()
print(res.summary())
To interpret how well our model fits the data, we look at the P>|z| (p values) column. We can see that our AR coefficient (ar.L1) and MA coefficient (ma.L1) have a P>|z| of 0.000 which shows they are statistically significant and hence our model fits the data well.
plt.plot(data, label='Original Data')
plt.plot(res.fittedvalues, label='Fitted Values', color='orange')
plt.title(f'Original Data and Fitted ARIMA model')
plt.xticks(rotation=45)
plt.legend()
plt.show()
Garch Model
To model the volatility of the returns data, we use the GARCH model.
For simplicity of this article, we use the orders p=1 and q=1.
garch_model = arch_model(data, p=1, q=1)
res = garch_model.fit()
print(res.summary())
We can see that the P>|t| values for our model coefficients are around 0 and they are statistically significant.
res.plot(annualize='D')
plt.xticks(rotation=45)
plt.plot(data, label='Original Data')
plt.plot(res.conditional_volatility, label='Fitted Volatility', color='orange')
plt.xticks(rotation=45)
In this article you have learnt how to:
Implement an ARIMA model
Implement a GARCH model
Fit the models to real life stock data
Other Social Media:
Twitter: @Quant_prep