Table of Contents

Introduction

The Generalized Autoregressive Conditional Heteroskedasticity (GARCH) model is a statistical technique used to model and predict volatility in financial and economic time series. It was developed by Robert Engle in 1982 as an extension of the Autoregressive Conditional Heteroskedasticity (ARCH) model proposed by Andrew Lo and Craig MacKinlay in 1988.

The GARCH model allows capturing the presence of conditional heteroscedasticity in time series data, that is, the presence of fluctuations in the variance of a time series as a function of time. This is especially useful in financial data analysis, where volatility can be an important measure of risk.

The GARCH model has become a fundamental tool in the analysis of financial time series and has been used in a wide variety of applications, from risk management to forecasting prices of shares and other financial values.

Definition of GARCH Models

Definition 1. A GARCH(p,q)\text{GARCH}(p,q) model with order (p1,q0)(p≥1,q≥0) is of the form

{Xt=σtεtσt2=ω+i=1pαiXti2+j=1qβjσtj2 \begin{equation} \begin{cases} X_t = \sigma_t \varepsilon_t\\ \sigma_{t}^2 = \omega + \sum_{i=1}^{p} \alpha_i X_{t-i}^2 + \sum_{j=1}^{q} \beta_j \sigma_{t-j}^2 \end{cases} \end{equation}

where ω0,αi0,βj0,αp>0\omega ≥0,\alpha_i ≥0,\beta_j ≥0,\alpha_p >0 ,and βq>0\beta_q >0 are constants,εtiid(0,1)\varepsilon_t \sim iid(0,1), and εt\varepsilon_t is independent of {Xk;kt1}\{X_k;k ≤ t − 1 \}. A stochastic process XtX_t is called a GARCH(p,q)\text{GARCH}(p, q ) process if it satisfies Eq. (1).

In practice, it has been found that for some time series, the ARCH(p)\text{ARCH}(p) model defined by (1) will provide an adequate fit only if the order pp is large. By allowing past volatilities to affect the present volatility in (1), a more parsimonious model may result. That is why we need GARCH models. Besides, note the condition that the order p1p ≥ 1. The GARCH model in Definition 1 has the properties as follows.

Proposition 1. If XtX_t is a GARCH(p,q)\text{GARCH}(p, q) process defined in (1) and i=1pαi+j=1qβj<1\sum_{i=1}^{p} \alpha_{i} + \sum_{j=1}^{q} \beta_j <1,then the following propositions hold.

  • Xt2X_{t}^2 follows the ARMA(m,q)\text{ARMA}(m, q ) model

Xt2=ω+i=1m(αi+βi)Xti2+ηtj=1qβjηtjX_{t}^2=\omega +\sum_{i=1}^{m} (\alpha_i + \beta_i )X_{t-i}^2 + \eta_t − \sum_{j=1}^q \beta_j \eta_{t-j}

where αi=0\alpha_i =0 for i>p,βj=0i >p,βj =0 for j>q,m=max(p,q)j >q,m=max(p,q), and ηt=σt2(εt21)\eta_t =\sigma_{t}^2 (\varepsilon_{t}^2 −1).

  • XtX_t is a white noise with

E(X)=0,E(Xt+hXt)=0  for any  h0,Var(Xt)=ω1i=1m(αi+βi)E(X)=0, E(X_{t+h} X_t )=0 \ \ \text{for} \ any \ \ h \neq 0, Var(X_t)= \frac{\omega}{1-\sum_{i=1}^{m} (\alpha_i + \beta_i )}

  • σt2\sigma_{t}^2 is the conditional variance of XtX_t , that is, we have

E(XtFt1)=0,σt2=Var(Xt2Ft1).E(X_t|\mathscr{F}_{t−1}) = 0, \sigma_{t}^2 = Var(X_{t}^2|\mathscr{F}_{t−1}).

  • Model (1) reflects the fat tails and volatility clustering.

Although an asset return series can usually be seen as a white noise, there exists such a return series so that it may be autocorrelated. What is more, a given original time series is not necessarily a return series, and at the same time, its values may be negative. If a time series is autocorrelated, we must first build an adequate model (e.g., an ARMA model) for the series in order to remove any autocorrelation in it. Then check whether the residual series has an ARCH effect, and if yes then we further model the residuals. In other words, if a time series YtY_t is autocorrelated and has ARCH effect, then a GARCH model that can capture the features of YtY_tt should be of the form

where Eq. (2) is referred to as the mean equation (model) and Eq. (3) is known as the volatility (variance) equation (model), and ZtZ_t is a representative of exogenous regressors. If YtY_t is a return series, then typically Yt=r+XtY_t = r + X_t where rr is a constant that means the expected returns is fixed.

Advantages and disadvantages of the Generalized Autoregressive Conditional Heteroskedasticity (GARCH) Model

AdvantagesDisadvantages
1. 1. Flexible model: The GARCH model is flexible and can fit different types of time series data with different volatility patterns.1. Requires a large amount of data: The GARCH model requires a large amount of data to accurately estimate the model parameters.
2. Ability to model volatility: The GARCH model is capable of modeling the volatility and heteroscedasticity of a time series, which can improve the accuracy of forecasts.2. Sensitive to the model specification: The GARCH model is sensitive to the model specification and can be difficult to estimate if incorrectly specified.
3. It incorporates past information: The GARCH model incorporates past information on the volatility of the time series, which makes it useful for predicting future volatility.3. It can be computationally expensive: The GARCH model can be computationally expensive, especially if more complex models are used.
4. Allows the inclusion of exogenous variables: The GARCH model can be extended to include exogenous variables, which can improve the accuracy of the predictions.4. It does not consider extreme events: The GARCH model does not consider extreme or unexpected events in the time series, which can affect the accuracy of the predictions in situations of high volatility.
5. The GARCH model makes it possible to model conditional heteroscedasticity, that is, the variation of the variance of a time series as a function of time and of the previous values of the time series itself.5. The GARCH model assumes that the time series errors are normally distributed, which may not be true in practice. If the errors are not normally distributed, the model may produce inaccurate estimates of volatility.
6. The GARCH model can be used to estimate the value at risk (VaR) and the conditional value at risk (CVaR) of an investment portfolio.

The Generalized Autoregressive Conditional Heteroskedasticity (GARCH) model can be applied in several fields

The Generalized Autoregressive Conditional Heteroskedasticity (GARCH) model can be applied in a wide variety of areas where time series volatility is required to be modeled and predicted. Some of the areas in which the GARCH model can be applied are:

  1. Financial markets: the GARCH model is widely used to model the volatility (risk) of returns on financial assets such as stocks, bonds, currencies, etc. It allows you to capture the changing nature of volatility.

  2. Commodity prices: the prices of raw materials such as oil, gold, grains, etc. they exhibit conditional volatility that can be modeled with GARCH.

  3. Credit risk: the risk of non-payment of loans and bonds also presents volatility over time that suits GARCH well.

  4. Economic time series: macroeconomic indicators such as inflation, GDP, unemployment, etc. they have conditional volatility modelable with GARCH.

  5. Implicit volatility: the GARCH model allows estimating the implicit volatility in financial options.

  6. Forecasts: GARCH allows conditional volatility forecasts to be made in any time series.

  7. Risk analysis: GARCH is useful for measuring and managing the risk of investment portfolios and assets.

  8. Finance: The GARCH model is widely used in finance to model the price volatility of financial assets, such as stocks, bonds, and currencies.

  9. Economics: The GARCH model is used in economics to model the volatility of the prices of goods and services, inflation, and other economic indicators.

  10. Environmental sciences: The GARCH model is applied in environmental sciences to model the volatility of variables such as temperature, precipitation, and air quality.

  11. Social sciences: The GARCH model is used in the social sciences to model the volatility of variables such as crime, migration, and employment.

  12. Engineering: The GARCH model is applied in engineering to model the volatility of variables such as the demand for electrical energy, industrial production, and vehicular traffic.

  13. Health sciences: The GARCH model is used in health sciences to model the volatility of variables such as the number of cases of infectious diseases and the prices of medicines.

The GARCH Model is applicable in any context where it is required to model and forecast heterogeneous conditional volatility in time series, especially in finance and economics.

Loading libraries and data

Tip

Statsforecast will be needed. To install, see instructions.

Next, we import plotting libraries and configure the plotting style.

import matplotlib.pyplot as plt
from statsmodels.graphics.tsaplots import plot_acf
from statsmodels.graphics.tsaplots import plot_pacf
plt.style.use('fivethirtyeight')
plt.rcParams['lines.linewidth'] = 1.5
dark_style = {
    'figure.facecolor': '#212946',
    'axes.facecolor': '#212946',
    'savefig.facecolor':'#212946',
    'axes.grid': True,
    'axes.grid.which': 'both',
    'axes.spines.left': False,
    'axes.spines.right': False,
    'axes.spines.top': False,
    'axes.spines.bottom': False,
    'grid.color': '#2A3459',
    'grid.linewidth': '1',
    'text.color': '0.9',
    'axes.labelcolor': '0.9',
    'xtick.color': '0.9',
    'ytick.color': '0.9',
    'font.size': 12 }
plt.rcParams.update(dark_style)

from pylab import rcParams
rcParams['figure.figsize'] = (18,7)

Read Data

Let’s pull the S&P500 stock data from the Yahoo Finance site.

import pandas as pd
import time
from datetime import datetime

ticker = '^GSPC'
period1 = int(time.mktime(datetime(2015, 1, 1, 23, 59).timetuple()))
period2 = int(time.mktime(datetime.now().timetuple()))
interval = '1d' # 1d, 1m

query_string = f'https://query1.finance.yahoo.com/v7/finance/download/{ticker}?period1={period1}&period2={period2}&interval={interval}&events=history&includeAdjustedClose=true'

SP_500 = pd.read_csv(query_string)
SP_500.head()
DateOpenHighLowCloseAdj CloseVolume
02015-01-022058.8999022072.3601072046.0400392058.1999512058.1999512708700000
12015-01-052054.4399412054.4399412017.3399662020.5799562020.5799563799120000
22015-01-062022.1500242030.2500001992.4399412002.6099852002.6099854460110000
32015-01-072005.5500492029.6099852005.5500492025.9000242025.9000243805480000
42015-01-082030.6099852064.0800782030.6099852062.1398932062.1398933934010000
df=SP_500[["Date","Close"]]

The input to StatsForecast is always a data frame in long format with three columns: unique_id, ds and y:

  • The unique_id (string, int or category) represents an identifier for the series.

  • The ds (datestamp) column should be of a format expected by Pandas, ideally YYYY-MM-DD for a date or YYYY-MM-DD HH:MM:SS for a timestamp.

  • The y (numeric) represents the measurement we wish to forecast.

df["unique_id"]="1"
df.columns=["ds", "y", "unique_id"]
df.head()
dsyunique_id
02015-01-022058.1999511
12015-01-052020.5799561
22015-01-062002.6099851
32015-01-072025.9000241
42015-01-082062.1398931
print(df.dtypes)
ds            object
y            float64
unique_id     object
dtype: object

We can see that our time variable (ds) is in an object format, we need to convert to a date format

df["ds"] = pd.to_datetime(df["ds"])

Explore data with the plot method

Plot a series using the plot method from the StatsForecast class. This method prints a random series from the dataset and is useful for basic EDA.

from statsforecast import StatsForecast

StatsForecast.plot(df)

The Augmented Dickey-Fuller Test

An Augmented Dickey-Fuller (ADF) test is a type of statistical test that determines whether a unit root is present in time series data. Unit roots can cause unpredictable results in time series analysis. A null hypothesis is formed in the unit root test to determine how strongly time series data is affected by a trend. By accepting the null hypothesis, we accept the evidence that the time series data is not stationary. By rejecting the null hypothesis or accepting the alternative hypothesis, we accept the evidence that the time series data is generated by a stationary process. This process is also known as stationary trend. The values of the ADF test statistic are negative. Lower ADF values indicate a stronger rejection of the null hypothesis.

Augmented Dickey-Fuller Test is a common statistical test used to test whether a given time series is stationary or not. We can achieve this by defining the null and alternate hypothesis.

Null Hypothesis: Time Series is non-stationary. It gives a time-dependent trend. Alternate Hypothesis: Time Series is stationary. In another term, the series doesn’t depend on time.

ADF or t Statistic < critical values: Reject the null hypothesis, time series is stationary. ADF or t Statistic > critical values: Failed to reject the null hypothesis, time series is non-stationary.

Let’s check if our series that we are analyzing is a stationary series. Let’s create a function to check, using the Dickey Fuller test

from statsmodels.tsa.stattools import adfuller

def Augmented_Dickey_Fuller_Test_func(series , column_name):
    print (f'Dickey-Fuller test results for columns: {column_name}')
    dftest = adfuller(series, autolag='AIC')
    dfoutput = pd.Series(dftest[0:4], index=['Test Statistic','p-value','No Lags Used','Number of observations used'])
    for key,value in dftest[4].items():
       dfoutput['Critical Value (%s)'%key] = value
    print (dfoutput)
    if dftest[1] <= 0.05:
        print("Conclusion:====>")
        print("Reject the null hypothesis")
        print("The data is stationary")
    else:
        print("Conclusion:====>")
        print("The null hypothesis cannot be rejected")
        print("The data is not stationary")
Augmented_Dickey_Fuller_Test_func(df["y"],'S&P500')
Dickey-Fuller test results for columns: S&P500
Test Statistic          -0.814971
p-value                  0.814685
No Lags Used            10.000000
                          ...    
Critical Value (1%)     -3.433341
Critical Value (5%)     -2.862861
Critical Value (10%)    -2.567473
Length: 7, dtype: float64
Conclusion:====>
The null hypothesis cannot be rejected
The data is not stationary

In the previous result we can see that the Augmented_Dickey_Fuller test gives us a p-value of 0.864700, which tells us that the null hypothesis cannot be rejected, and on the other hand the data of our series are not stationary.

We need to differentiate our time series, in order to convert the data to stationary.

Return Series

Since the 1970s, the financial industry has been very prosperous with advancement of computer and Internet technology. Trade of financial products (including various derivatives) generates a huge amount of data which form financial time series. For finance, the return on a financial product is most interesting, and so our attention focuses on the return series. If PtP_t is the closing price at time t for a certain financial product, then the return on this product is

Xt=(PtPt1)Pt1log(Pt)log(Pt1).X_t = \frac{(P_t − P_{t−1})}{P_{t−1}} ≈ log(P_t ) − log(P_{t−1}).

It is return series {Xt}\{X_t \} that have been much independently studied. And important stylized features which are common across many instruments, markets, and time periods have been summarized. Note that if you purchase the financial product, then it becomes your asset, and its returns become your asset returns. Now let us look at the following examples.

We can estimate the series of returns using the pandas, DataFrame.pct_change() function. The pct_change() function has a periods parameter whose default value is 1. If you want to calculate a 30-day return, you must change the value to 30.

df['return'] = 100 * df["y"].pct_change()
df.dropna(inplace=True, how='any')
df.head()
dsyunique_idreturn
12015-01-052020.5799561-1.827811
22015-01-062002.6099851-0.889347
32015-01-072025.90002411.162984
42015-01-082062.13989311.788828
52015-01-092044.8100591-0.840381
import plotly.express as px
fig = px.line(df, x=df["ds"], y="return",title="SP500 Return Chart",template = "plotly_dark")
fig.show()

Creating Squared Returns

df['sq_return'] = df["return"].mul(df["return"])
df.head()
dsyunique_idreturnsq_return
12015-01-052020.5799561-1.8278113.340891
22015-01-062002.6099851-0.8893470.790938
32015-01-072025.90002411.1629841.352532
42015-01-082062.13989311.7888283.199906
52015-01-092044.8100591-0.8403810.706240

Returns vs Squared Returns

from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig = make_subplots(rows=1, cols=2)

fig.add_trace(go.Scatter(x=df["ds"], y=df["return"],
                         mode='lines',
                         name='return'),
row=1, col=1
)


fig.add_trace(go.Scatter(x=df["ds"], y=df["sq_return"],
                         mode='lines',
                         name='sq_return'), 
    row=1, col=2
)

fig.update_layout(height=600, width=800, title_text="Returns vs Squared Returns", template = "plotly_dark")
fig.show()

from scipy.stats import probplot, moment
from statsmodels.tsa.stattools import adfuller, q_stat, acf
import numpy as np
import seaborn as sns

def plot_correlogram(x, lags=None, title=None):    
    lags = min(10, int(len(x)/5)) if lags is None else lags
    fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(14, 8))
    x.plot(ax=axes[0][0], title='Return')
    x.rolling(21).mean().plot(ax=axes[0][0], c='k', lw=1)
    q_p = np.max(q_stat(acf(x, nlags=lags), len(x))[1])
    stats = f'Q-Stat: {np.max(q_p):>8.2f}\nADF: {adfuller(x)[1]:>11.2f}'
    axes[0][0].text(x=.02, y=.85, s=stats, transform=axes[0][0].transAxes)
    probplot(x, plot=axes[0][1])
    mean, var, skew, kurtosis = moment(x, moment=[1, 2, 3, 4])
    s = f'Mean: {mean:>12.2f}\nSD: {np.sqrt(var):>16.2f}\nSkew: {skew:12.2f}\nKurtosis:{kurtosis:9.2f}'
    axes[0][1].text(x=.02, y=.75, s=s, transform=axes[0][1].transAxes)
    plot_acf(x=x, lags=lags, zero=False, ax=axes[1][0])
    plot_pacf(x, lags=lags, zero=False, ax=axes[1][1])
    axes[1][0].set_xlabel('Lag')
    axes[1][1].set_xlabel('Lag')
    fig.suptitle(title+ f'Dickey-Fuller: {adfuller(x)[1]:>11.2f}', fontsize=14)
    sns.despine()
    fig.tight_layout()
    fig.subplots_adjust(top=.9)
plot_correlogram(df["return"], lags=30, title="Time Series Analysis plot \n")

Ljung-Box Test

Ljung-Box is a test for autocorrelation that we can use in tandem with our ACF and PACF plots. The Ljung-Box test takes our data, optionally either lag values to test, or the largest lag value to consider, and whether to compute the Box-Pierce statistic. Ljung-Box and Box-Pierce are two similar test statisitcs, Q , that are compared against a chi-squared distribution to determine if the series is white noise. We might use the Ljung-Box test on the residuals of our model to look for autocorrelation, ideally our residuals would be white noise.

  • Ho : The data are independently distributed, no autocorrelation.
  • Ha : The data are not independently distributed; they exhibit serial correlation.

The Ljung-Box with the Box-Pierce option will return, for each lag, the Ljung-Box test statistic, Ljung-Box p-values, Box-Pierce test statistic, and Box-Pierce p-values.

If p<α(0.05)p<\alpha (0.05) we reject the null hypothesis.

from statsmodels.stats.diagnostic import acorr_ljungbox

ljung_res = acorr_ljungbox(df["return"], lags= 40, boxpierce=True)

ljung_res.head()
lb_statlb_pvaluebp_statbp_pvalue
149.2222732.285409e-1249.1551832.364927e-12
262.9913482.097020e-1462.8992342.195861e-14
363.9449448.433622e-1463.8506638.834380e-14
474.3436522.742989e-1574.2210242.911751e-15
580.2348627.494100e-1680.0934988.022242e-16

Split the data into training and testing

Let’s divide our data into sets 1. Data to train our GARCH model 2. Data to test our model

For the test data we will use the last 30 day to test and evaluate the performance of our model.

df=df[["ds","unique_id","return"]]
df.columns=["ds", "unique_id", "y"]
train = df[df.ds<='2023-05-31'] # Let's forecast the last 30 days
test = df[df.ds>'2023-05-31']
train.shape, test.shape
((2116, 3), (83, 3))

Now let’s plot the training data and the test data.

sns.lineplot(train,x="ds", y="y", label="Train")
sns.lineplot(test, x="ds", y="y", label="Test")
plt.show()

Implementation of GARCH with StatsForecast

To also know more about the parameters of the functions of the GARCH Model, they are listed below. For more information, visit the documentation

p : int
    Number of lagged versions of the series.
q: int
    Number of lagged versions of the volatility.
alias : str
    Custom name of the model.
prediction_intervals : Optional[ConformalIntervals]
    Information to compute conformal prediction intervals.
    By default, the model will compute the native prediction
    intervals.

Load libraries

from statsforecast import StatsForecast 
from statsforecast.models import GARCH

Instantiating Models

Import and instantiate the models. Setting the argument is sometimes tricky. This article on Seasonal periods by the master, Rob Hyndmann, can be useful.season_length.

season_length = 7 # Dayly data 
horizon = len(test) # number of predictions biasadj=True, include_drift=True,

models = [GARCH(1,1),
          GARCH(1,2),
          GARCH(2,2),
          GARCH(2,1),
          GARCH(3,1),
          GARCH(3,2),
          GARCH(3,3), 
          GARCH(1,3), 
          GARCH(2,3)]

We fit the models by instantiating a new StatsForecast object with the following parameters:

models: a list of models. Select the models you want from models and import them.

  • freq: a string indicating the frequency of the data. (See pandas’ available frequencies.)

  • n_jobs: n_jobs: int, number of jobs used in the parallel processing, use -1 for all cores.

  • fallback_model: a model to be used if a model fails.

Any settings are passed into the constructor. Then you call its fit method and pass in the historical data frame.

sf = StatsForecast(df=train,
                   models=models,
                   freq='C', # custom business day frequency
                   n_jobs=-1)

Cross-validation

We have built different GARCH models, so we need to determine which is the best model to then be able to train it and thus be able to make the predictions. To know which is the best model we go to the Cross Validation.

With time series data, Cross Validation is done by defining a sliding window across the historical data and predicting the period following it. This form of cross-validation allows us to arrive at a better estimation of our model’s predictive abilities across a wider range of temporal instances while also keeping the data in the training set contiguous as is required by our models.

The following graph depicts such a Cross Validation Strategy:

Perform time series cross-validation

Cross-validation of time series models is considered a best practice but most implementations are very slow. The statsforecast library implements cross-validation as a distributed operation, making the process less time-consuming to perform. If you have big datasets you can also perform Cross Validation in a distributed cluster using Ray, Dask or Spark.

The cross_validation method from the StatsForecast class takes the following arguments.

  • df: training data frame

  • h (int): represents h steps into the future that are being forecasted. In this case, 12 months ahead.

  • step_size (int): step size between each window. In other words: how often do you want to run the forecasting processes.

  • n_windows(int): number of windows used for cross validation. In other words: what number of forecasting processes in the past do you want to evaluate.

crossvalidation_df = sf.cross_validation(df=train,
                                         h=horizon,
                                         step_size=6,
                                         n_windows=5)

The crossvaldation_df object is a new data frame that includes the following columns:

  • unique_id: index. If you dont like working with index just run crossvalidation_df.resetindex()
  • ds: datestamp or temporal index
  • cutoff: the last datestamp or temporal index for the n_windows.
  • y: true value
  • "model": columns with the model’s name and fitted value.
crossvalidation_df
dscutoffyGARCH(1,1)GARCH(1,2)GARCH(2,2)GARCH(2,1)GARCH(3,1)GARCH(3,2)GARCH(3,3)GARCH(1,3)GARCH(2,3)
unique_id
12023-01-032023-01-02-0.4049621.9069131.8864131.9083571.9309151.9132081.9043801.9034941.8858971.873233
12023-01-042023-01-02-1.202064-0.824837-0.838750-0.827394-0.824686-0.835927-0.822887-0.841358-0.833009-0.828155
12023-01-052023-01-021.746133-0.665798-0.653746-0.666613-0.680718-0.663415-0.677103-0.649123-0.661879-0.665392
12023-05-292023-02-031.304909-0.389434-0.350367-0.313831-0.191451-0.192649-0.186990-0.184408-0.188771-0.185092
12023-05-302023-02-030.001660-0.340987-0.296400-0.278776-0.158889-0.157296-0.160854-0.154074-0.156617-0.158255
12023-05-312023-02-03-0.6108620.3166390.2748530.2489950.1350920.1349110.1325320.1355330.1348440.133779
from datasetsforecast.losses import rmse

def compute_cv_rmse(crossvalidation_df):
    """Compute MAE for all models generated"""
    res = {}
    for mod in models: 
        res[mod] = rmse(crossvalidation_df['actual'], crossvalidation_df[str(mod)])
    return pd.Series(res)
crossvalidation_df.rename(columns = {'y' : 'actual'}, inplace = True) # rename actual values 
rmse_cv = crossvalidation_df.groupby(['unique_id', 'cutoff']).apply(compute_cv_rmse)

mae = rmse_cv.groupby('unique_id').mean()
mae.style.highlight_min(color = 'red', axis = 1)
 GARCH(1,1)GARCH(1,2)GARCH(2,2)GARCH(2,1)GARCH(3,1)GARCH(3,2)GARCH(3,3)GARCH(1,3)GARCH(2,3)
unique_id         
11.5986131.7216201.5472101.3756461.4420021.4191951.3732991.3772531.374233

Note: This result can vary depending on the data and period you use to train and test the model, and the models you want to test. This is an example, where the objective is to be able to teach a methodology for the use of StatsForecast, and in particular the GARCH model and the parameters used in Cross Validation to determine the best model for this example.

In the previous result it can be seen that the best model is the model GARCH(1,1)\text{GARCH}(1,1)

With this result found using Cross Validation to determine which is the best model, we are going to continue training our model, to then make the predictions.

Fit the Model

season_length = 7 # Dayly data 
horizon = len(test) # number of predictions biasadj=True, include_drift=True,

models = [GARCH(1,1)]
sf = StatsForecast(df=train,
                   models=models,
                   freq='C', # custom business day frequency
                   n_jobs=-1)
sf.fit()
StatsForecast(models=[GARCH(1,1)])

Let’s see the results of our Theta model. We can observe it with the following instruction:

result=sf.fitted_[0,0].model_
result
{'p': 1,
 'q': 1,
 'coeff': array([0.11715665, 0.261296  , 0.64151786]),
 'message': 'Optimization terminated successfully',
 'y_vals': array([-0.61086244]),
 'sigma2_vals': array([0.81241364]),
 'fitted': array([        nan,  2.22688619, -0.75658963, ..., -0.20975516,
         0.83589047,  0.1360351 ]),
 'actual_residuals': array([        nan, -3.11623338,  1.91957389, ...,  1.51466384,
        -0.83423013, -0.74689754])}

Let us now visualize the residuals of our models.

As we can see, the result obtained above has an output in a dictionary, to extract each element from the dictionary we are going to use the .get() function to extract the element and then we are going to save it in a pd.DataFrame().

residual=pd.DataFrame(result.get("actual_residuals"), columns=["residual Model"])
residual
residual Model
0NaN
1-3.116233
21.919574
21131.514664
2114-0.834230
2115-0.746898
from scipy import stats

fig, axs = plt.subplots(nrows=2, ncols=2)

# plot[1,1]
residual.plot(ax=axs[0,0])
axs[0,0].set_title("Residuals");

# plot
sns.distplot(residual, ax=axs[0,1]);
axs[0,1].set_title("Density plot - Residual");

# plot
stats.probplot(residual["residual Model"], dist="norm", plot=axs[1,0])
axs[1,0].set_title('Plot Q-Q')

# plot
plot_acf(residual,  lags=35, ax=axs[1,1],color="fuchsia")
axs[1,1].set_title("Autocorrelation");

plt.show();

Forecast Method

If you want to gain speed in productive settings where you have multiple series or models we recommend using the StatsForecast.forecast method instead of .fit and .predict.

The main difference is that the .forecast doest not store the fitted values and is highly scalable in distributed environments.

The forecast method takes two arguments: forecasts next h (horizon) and level.

  • h (int): represents the forecast h steps into the future. In this case, 12 months ahead.

  • level (list of floats): this optional parameter is used for probabilistic forecasting. Set the level (or confidence percentile) of your prediction interval. For example, level=[90] means that the model expects the real value to be inside that interval 90% of the times.

The forecast object here is a new data frame that includes a column with the name of the model and the y hat values, as well as columns for the uncertainty intervals. Depending on your computer, this step should take around 1min. (If you want to speed things up to a couple of seconds, remove the AutoModels like ARIMA and Theta)

Y_hat = sf.forecast(horizon, fitted=True)

Y_hat.head()
dsGARCH(1,1)
unique_id
12023-06-011.393380
12023-06-02-0.640599
12023-06-05-0.508753
12023-06-06-0.947621
12023-06-070.798889
Y_hat = sf.forecast(horizon, fitted=True, level=[95])

Y_hat.head()
dsGARCH(1,1)GARCH(1,1)-lo-95GARCH(1,1)-hi-95
unique_id
12023-06-011.393380-0.0488362.835595
12023-06-02-0.640599-2.7897341.508536
12023-06-05-0.508753-2.3272461.309740
12023-06-06-0.947621-2.4763940.581153
12023-06-070.798889-0.8713542.469133
values=sf.forecast_fitted_values()
values.head()
dsyGARCH(1,1)GARCH(1,1)-lo-95GARCH(1,1)-hi-95
unique_id
12015-01-05-1.827811NaNNaNNaN
12015-01-06-0.8893472.226886-0.8581475.311919
12015-01-071.162984-0.756590-3.8416232.328444
12015-01-081.788828-0.636398-3.7214312.448635
12015-01-09-0.840381-1.472993-4.5580261.612040

Adding 95% confidence interval with the forecast method

sf.forecast(h=horizon, level=[95])
dsGARCH(1,1)GARCH(1,1)-lo-95GARCH(1,1)-hi-95
unique_id
12023-06-011.393380-0.0488362.835595
12023-06-02-0.640599-2.7897341.508536
12023-06-05-0.508753-2.3272461.309740
12023-09-21-0.199650-1.7801581.380858
12023-09-22-0.161219-1.4251791.102742
12023-09-250.136796-0.9169911.190583
Y_hat=Y_hat.reset_index()
Y_hat.head()
unique_iddsGARCH(1,1)GARCH(1,1)-lo-95GARCH(1,1)-hi-95
012023-06-011.393380-0.0488362.835595
112023-06-02-0.640599-2.7897341.508536
212023-06-05-0.508753-2.3272461.309740
312023-06-06-0.947621-2.4763940.581153
412023-06-070.798889-0.8713542.469133
# Merge the forecasts with the true values
test['unique_id'] = test['unique_id'].astype(int)
Y_hat1 = test.merge(Y_hat, how='left', on=['unique_id', 'ds'])
Y_hat1
dsunique_idyGARCH(1,1)GARCH(1,1)-lo-95GARCH(1,1)-hi-95
02023-06-0110.9854451.393380-0.0488362.835595
12023-06-0211.453442-0.640599-2.7897341.508536
22023-06-051-0.200358-0.508753-2.3272461.309740
802023-09-261-1.473453NaNNaNNaN
812023-09-2710.022931NaNNaNNaN
822023-09-2810.589317NaNNaNNaN
fig, ax = plt.subplots(1, 1)
plot_df = pd.concat([train, Y_hat1]).set_index('ds').tail(200)
plot_df[['y', "GARCH(1,1)"]].plot(ax=ax, linewidth=2)
ax.set_title(' Forecast', fontsize=22)
ax.set_ylabel('Year ', fontsize=20)
ax.set_xlabel('Timestamp [t]', fontsize=20)
ax.legend(prop={'size': 15})
ax.grid(True)
plt.show()

Predict method with confidence interval

To generate forecasts use the predict method.

The predict method takes two arguments: forecasts the next h (for horizon) and level.

  • h (int): represents the forecast h steps into the future. In this case, 30 dayly ahead.

  • level (list of floats): this optional parameter is used for probabilistic forecasting. Set the level (or confidence percentile) of your prediction interval. For example, level=[95] means that the model expects the real value to be inside that interval 95% of the times.

The forecast object here is a new data frame that includes a column with the name of the model and the y hat values, as well as columns for the uncertainty intervals.

This step should take less than 1 second.

sf.predict(h=horizon)
dsGARCH(1,1)
unique_id
12023-06-011.393380
12023-06-02-0.640599
12023-06-05-0.508753
12023-09-21-0.199650
12023-09-22-0.161219
12023-09-250.136796
forecast_df = sf.predict(h=horizon, level=[80,95]) 

forecast_df.head(10)
dsGARCH(1,1)GARCH(1,1)-lo-95GARCH(1,1)-lo-80GARCH(1,1)-hi-80GARCH(1,1)-hi-95
unique_id
12023-06-011.393380-0.0488360.4503652.3363942.835595
12023-06-02-0.640599-2.789734-2.0458430.7646451.508536
12023-06-05-0.508753-2.327246-1.6978020.6802961.309740
12023-06-12-1.251548-6.549860-4.7159282.2128324.046764
12023-06-130.479689-3.951083-2.4174373.3768154.910460
12023-06-14-0.318133-3.508017-2.4038861.7676202.871751

We can join the forecast result with the historical data using the pandas function pd.concat(), and then be able to use this result for graphing.

df_plot=pd.concat([df, forecast_df]).set_index('ds').tail(220)
df_plot
unique_idyGARCH(1,1)GARCH(1,1)-lo-95GARCH(1,1)-lo-80GARCH(1,1)-hi-80GARCH(1,1)-hi-95
ds
2023-03-151-0.698088NaNNaNNaNNaNNaN
2023-03-1611.756201NaNNaNNaNNaNNaN
2023-03-171-1.101946NaNNaNNaNNaNNaN
2023-09-21NaNNaN-0.199650-1.780158-1.2330880.8337891.380858
2023-09-22NaNNaN-0.161219-1.425179-0.9876780.6652411.102742
2023-09-25NaNNaN0.136796-0.916991-0.5522380.8258311.190583
def plot_forecasts(y_hist, y_true, y_pred, models):
    _, ax = plt.subplots(1, 1, figsize = (20, 7))
    y_true = y_true.merge(y_pred, how='left', on=['unique_id', 'ds'])
    df_plot = pd.concat([y_hist, y_true]).set_index('ds').tail(12*10)
    df_plot[['y'] + models].plot(ax=ax, linewidth=2 )
    colors = ['green']
  # Specify graph features:
    ax.fill_between(df_plot.index, 
                df_plot['GARCH(1,1)-lo-80'], 
                df_plot['GARCH(1,1)-hi-80'],
                alpha=.20,
                color='lime',
                label='GARCH(1,1)_level_80')
    ax.fill_between(df_plot.index, 
                df_plot['GARCH(1,1)-lo-95'], 
                df_plot['GARCH(1,1)-hi-95'],
                alpha=.2,
                color='white',
                label='GARCH(1,1)_level_95')
    ax.set_title('', fontsize=22)
    ax.set_ylabel("Return", fontsize=20)
    ax.set_xlabel('Month-Days', fontsize=20)
    ax.legend(prop={'size': 15})
    ax.grid(True)
    plt.show()
plot_forecasts(train, test, forecast_df, models=["GARCH(1,1)" ])

Model Evaluation

We can now compute the accuracy of the forecast using an appropiate accuracy metric. Here we’ll use the Root Mean Squared Error (RMSE). To do this, we first need to install datasetsforecast, a Python library developed by Nixtla that includes a function to compute the RMSE.

!pip install datasetsforecast

The function to compute the RMSE takes two arguments:

  1. The actual values.
  2. The forecasts, in this case, GARCH.

Now we are going to evaluate our model with the results of the predictions, we will use different types of metrics MAE, MAPE, MASE, RMSE, SMAPE to evaluate the accuracy.

from datasetsforecast.losses import mae, mape, mase, rmse, smape
def evaluate_performace(y_hist, y_true, y_pred, model):
    y_true = y_true.merge(y_pred, how='left', on=['unique_id', 'ds'])
    evaluation = {}
    evaluation[model] = {}
    for metric in [mase, mae, mape, rmse, smape]:
        metric_name = metric.__name__
        if metric_name =='mase':
            evaluation[model][metric_name] = metric(y_true['y'].values, 
                                                y_true[model].values, 
                                                y_hist['y'].values, seasonality=7)
        else:
            evaluation[model][metric_name] = metric(y_true['y'].values, y_true[model].values)
    return pd.DataFrame(evaluation).T
evaluate_performace(train, test, Y_hat, model="GARCH(1,1)")
maemapemasermsesmape
GARCH(1,1)0.886834372.562147NaN1.108626136.528289

Acknowledgements

We would like to thank Naren Castellon for writing this tutorial.

References

  1. Changquan Huang • Alla Petukhina. Springer series (2022). Applied Time Series Analysis and Forecasting with Python.
  2. Bollerslev, T. (1986). Generalized autoregressive conditional heteroskedasticity. Journal of econometrics, 31(3), 307-327.
  3. Engle, R. F. (1982). Autoregressive conditional heteroscedasticity with estimates of the variance of United Kingdom inflation. Econometrica: Journal of the econometric society, 987-1007..
  4. James D. Hamilton. Time Series Analysis Princeton University Press, Princeton, New Jersey, 1st Edition, 1994.
  5. Nixtla Parameters.
  6. Pandas available frequencies.
  7. Rob J. Hyndman and George Athanasopoulos (2018). “Forecasting principles and practice, Time series cross-validation”..
  8. Seasonal periods- Rob J Hyndman.