Mastering ARCH and GARCH Models in Python: From Theory to Forecasting

This guide explains the limitations of ARIMA‑type models for handling changing variance, introduces ARCH and GARCH as solutions, and walks through Python implementations—including data generation, model fitting, and forecasting—complete with code snippets and visualizations.

Model Perspective
Model Perspective
Model Perspective
Mastering ARCH and GARCH Models in Python: From Theory to Forecasting

Variance Issue

Autoregressive models such as AR, ARIMA, and SARIMA can model stationary, trending, or seasonal univariate time‑series, but they cannot capture changes in variance over time.

Classic ways to handle variance changes include transformations like logarithms or Box‑Cox. In finance, time‑varying variance is called volatility or heteroscedasticity. When variance is time‑dependent, autoregressive conditional heteroscedasticity (ARCH) can be used.

What is an ARCH model?

ARCH (Autoregressive Conditional Heteroscedasticity) explicitly models time‑varying variance by expressing the variance of each time step as a function of past residuals. An ARCH(p) model assumes the data‑generating process is an MA(p) process.

What is a GARCH model?

GARCH (Generalized ARCH) extends ARCH by adding lagged variance terms. The model is denoted GARCH(p, q), where p is the number of lagged variance terms and q is the number of lagged residual terms. GARCH(0, q) reduces to ARCH(q).

p: number of lagged variance terms

q: number of lagged residual terms

Python implementation of ARCH and GARCH

First, create a synthetic dataset with increasing variance.

# create dataset
from random import gauss, seed
from matplotlib import pyplot
seed(1)
# generate increasing‑variance white noise
data = [gauss(0, i*0.01) for i in range(0, 100)]
pyplot.plot(data)
pyplot.show()

Check autocorrelation of squared observations.

# check correlations of squared observations
from random import gauss, seed
from matplotlib import pyplot
from statsmodels.graphics.tsaplots import plot_acf
import numpy as np
seed(1)
data = [gauss(0, i*0.01) for i in range(0, 100)]
squared_data = np.array([x**2 for x in data])
plot_acf(squared_data)

ARCH model example

Steps: define, train, predict.

# example of ARCH model
from random import gauss, seed
from matplotlib import pyplot
from arch import arch_model
seed(1)
data = [gauss(0, i*0.01) for i in range(0, 100)]
n_test = 10
train, test = data[:-n_test], data[-n_test:]
model = arch_model(train, mean='Zero', vol='ARCH', p=15)
model_fit = model.fit()
yhat = model_fit.forecast(horizon=n_test)
# actual variance (increasing)
var = [i*0.01 for i in range(0, 100)]
pyplot.plot(var[-n_test:])
pyplot.plot(yhat.variance.values[-1, :])
pyplot.show()

GARCH model example

# example of GARCH model
from random import gauss, seed
from matplotlib import pyplot
from arch import arch_model
seed(1)
data = [gauss(0, i*0.01) for i in range(0, 100)]
n_test = 10
train, test = data[:-n_test], data[-n_test:]
model = arch_model(train, mean='Zero', vol='GARCH', p=15, q=15)
model_fit = model.fit()
yhat = model_fit.forecast(horizon=n_test)
var = [i*0.01 for i in range(0, 100)]
pyplot.plot(var[-n_test:])
pyplot.plot(yhat.variance.values[-1, :])
pyplot.show()

Reference: https://www.kancloud.cn/apachecn/ml-mastery-zh/1952457

Pythonforecastingtime seriesArchGARCHvolatility
Model Perspective
Written by

Model Perspective

Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.