ARCH Model
Step-by-step guide on using the ARCH Model
with Statsforecast
.
Table of Contents
- Introduction
- ARCH Models
- Loading libraries and data
- Explore data with the plot method
- Split the data into training and testing
- Implementation of ARCH with StatsForecast
- Cross-validation
- Model evaluation
- References
Introduction
Financial time series analysis has been one of the hottest research
topics in the recent decades. In this guide, we illustrate the stylized
facts of financial time series by real financial data. To characterize
these facts, new models different from the Box- Jenkins ones are needed.
And for this reason, ARCH models were firstly proposed by R. F. Engle in
1982 and have been extended by a great number of scholars since then. We
also demonstrate how to use Python and its libraries to implement
ARCH
.
As we have known, there are lot of time series that possess the ARCH effect, that is, although the (modeling residual) series is white noise, its squared series may be autocorrelated. What is more, in practice, a large number of financial time series are found having this property so that the ARCH effect has become one of the stylized facts from financial time series.
Stylized Facts of Financial Time Series
Now we briefly list and describe several important stylized facts (features) of financial return series:
-
Fat (heavy) tails: The distribution density function of returns often has fatter (heavier) tails than the tails of the corresponding normal distribution density.
-
ARCH effect: Although the return series can often be seen as a white noise, its squared (and absolute) series may usually be autocorrelated, and these autocorrelations are hardly negative.
-
Volatility clustering: Large changes in returns tend to cluster in time, and small changes tend to be followed by small changes.
-
Asymmetry: As we have know , the distribution of asset returns is slightly negatively skewed. One possible explanation could be that traders react more strongly to unfavorable information than favorable information.
Definition of ARCH Models
Specifically, we give the definition of the ARCH model as follows.
Definition 1. An model with order is of the form
where , and are constants, , and is independent of . A stochastic process is called an process if it satisfies Eq. (1).
By Definition 1, (and ) is independent of . Besides, usually it is further assumed that . Sometimes, however, we need to further suppose that follows a standardized (skew) Student’s T distribution or a generalized error distribution in order to capture more features of a financial time series.
Let denote the information set generated by , namely, the sigma field . It is easy to see that is independent of for any . According to Definition 1 and the properties of the conditional mathematical expectation, we have that
and
This implies that is the conditional variance of and it evolves according to the previous values of like an model. And so Model (1) is named an model.
As an example of models, let us consider the model
Explicitly, the unconditional mean
Additionally, the ARCH(1) model can be expressed as
that is,
where . It can been shown that is a new white noise, which is left as an exercise for reader. Hence, if , Eq. (4) is a stationary model for the series Xt2. Thus, the unconditional variance
that is,
Moreover, for , in light of the properties of the conditional mathematical expectation and by (2), we have that
In conclusion, if , we have that:
-
Any process defined by Eqs.(3) follows a white noise .
-
Since is an process defined by (4), , which reveals the ARCH effect.
-
It is clear that for any ,and with Eq.(4),for any :
which reflects the volatility clustering, that is, large (small) volatility is followed by large (small) one.
In addition, we are able to prove that Xt defined by Eq. (3) has heavier tails than the corresponding normal distribution. At last, note that these properties of the ARCH(1) model can be generalized to ARCH(p) models.
Advantages and disadvantages of the Autoregressive Conditional Heteroskedasticity (ARCH) model:
Advantages | Disadvantages |
---|---|
- The ARCH model is useful for modeling volatility in financial time series, which is important for investment decision making and risk management. | - The ARCH model assumes that the forecast errors are independent and identically distributed, which may not be realistic in some cases. |
- The ARCH model takes heteroscedasticity into account, which means that it can model time series with variances that change over time. | - The ARCH model can be difficult to fit to data with many parameters, which may require large amounts of data or advanced estimation techniques. |
- The ARCH model is relatively easy to use and can be implemented with standard econometrics software. | - The ARCH model does not take into account the possible relationship between the mean and the variance of the time series, which may be important in some cases. |
Note:
The ARCH model is a useful tool for modeling volatility in financial time series, but like any econometric model, it has limitations and should be used with caution depending on the specific characteristics of the data being modeled.
Autoregressive Conditional Heteroskedasticity (ARCH) Applications
-
Finance - The ARCH model is widely used in finance to model volatility in financial time series, such as stock prices, exchange rates, interest rates, etc.
-
Economics - The ARCH model can be used to model volatility in economic data, such as GDP, inflation, unemployment, among others.
-
Engineering - The ARCH model can be used in engineering to model volatility in data related to energy, climate, pollution, industrial production, among others.
-
Social Sciences - The ARCH model can be used in the social sciences to model volatility in data related to demography, health, education, among others.
-
Biology - The ARCH model can be used in biology to model volatility in data related to evolution, genetics, epidemiology, among others.
Loading libraries and data
Tip
Statsforecast will be needed. To install, see instructions.
Next, we import plotting libraries and configure the plotting style.
Read Data
Let’s pull the S&P500 stock data from the Yahoo Finance site.
Date | Open | High | Low | Close | Adj Close | Volume | |
---|---|---|---|---|---|---|---|
0 | 2015-01-02 | 2058.899902 | 2072.360107 | 2046.040039 | 2058.199951 | 2058.199951 | 2708700000 |
1 | 2015-01-05 | 2054.439941 | 2054.439941 | 2017.339966 | 2020.579956 | 2020.579956 | 3799120000 |
2 | 2015-01-06 | 2022.150024 | 2030.250000 | 1992.439941 | 2002.609985 | 2002.609985 | 4460110000 |
3 | 2015-01-07 | 2005.550049 | 2029.609985 | 2005.550049 | 2025.900024 | 2025.900024 | 3805480000 |
4 | 2015-01-08 | 2030.609985 | 2064.080078 | 2030.609985 | 2062.139893 | 2062.139893 | 3934010000 |
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.
ds | y | unique_id | |
---|---|---|---|
0 | 2015-01-02 | 2058.199951 | 1 |
1 | 2015-01-05 | 2020.579956 | 1 |
2 | 2015-01-06 | 2002.609985 | 1 |
3 | 2015-01-07 | 2025.900024 | 1 |
4 | 2015-01-08 | 2062.139893 | 1 |
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.
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
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 is the closing price at time t for a certain financial product, then the return on this product is
It is return series 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.
ds | y | unique_id | return | |
---|---|---|---|---|
1 | 2015-01-05 | 2020.579956 | 1 | -1.827811 |
2 | 2015-01-06 | 2002.609985 | 1 | -0.889347 |
3 | 2015-01-07 | 2025.900024 | 1 | 1.162984 |
4 | 2015-01-08 | 2062.139893 | 1 | 1.788828 |
5 | 2015-01-09 | 2044.810059 | 1 | -0.840381 |
Creating Squared Returns
ds | y | unique_id | return | sq_return | |
---|---|---|---|---|---|
1 | 2015-01-05 | 2020.579956 | 1 | -1.827811 | 3.340891 |
2 | 2015-01-06 | 2002.609985 | 1 | -0.889347 | 0.790938 |
3 | 2015-01-07 | 2025.900024 | 1 | 1.162984 | 1.352532 |
4 | 2015-01-08 | 2062.139893 | 1 | 1.788828 | 3.199906 |
5 | 2015-01-09 | 2044.810059 | 1 | -0.840381 | 0.706240 |
Returns vs Squared Returns
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 we reject the null hypothesis.
lb_stat | lb_pvalue | bp_stat | bp_pvalue | |
---|---|---|---|---|
1 | 49.222273 | 2.285409e-12 | 49.155183 | 2.364927e-12 |
2 | 62.991348 | 2.097020e-14 | 62.899234 | 2.195861e-14 |
3 | 63.944944 | 8.433622e-14 | 63.850663 | 8.834380e-14 |
4 | 74.343652 | 2.742989e-15 | 74.221024 | 2.911751e-15 |
5 | 80.234862 | 7.494100e-16 | 80.093498 | 8.022242e-16 |
Split the data into training and testing
Let’s divide our data into sets
- Data to train our
ARCH
model - 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.
Now let’s plot the training data and the test data.
Implementation of ARCH with StatsForecast
To also know more about the parameters of the functions of the
ARCH Model
, they are listed below. For more information, visit the
documentation
Load libraries
Building Model
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.
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.
Fit the Model
Let’s see the results of our ARCH model. We can observe it with the following instruction:
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 Model | |
---|---|
0 | NaN |
1 | NaN |
2 | -1.071760 |
… | … |
2109 | 1.495833 |
2110 | -2.222391 |
2111 | 0.248641 |
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.
ds | ARCH(2) | |
---|---|---|
unique_id | ||
1 | 2023-05-25 | 1.681836 |
1 | 2023-05-26 | -0.777028 |
1 | 2023-05-29 | -0.677960 |
… | … | … |
1 | 2023-09-20 | 0.136752 |
1 | 2023-09-21 | 0.082173 |
1 | 2023-09-22 | -0.450958 |
ds | y | ARCH(2) | |
---|---|---|---|
unique_id | |||
1 | 2015-01-05 | -1.827811 | NaN |
1 | 2015-01-06 | -0.889347 | NaN |
1 | 2015-01-07 | 1.162984 | 2.234745 |
1 | 2015-01-08 | 1.788828 | -0.667577 |
1 | 2015-01-09 | -0.840381 | -0.752437 |
Adding 95% confidence interval with the forecast method
ds | ARCH(2) | ARCH(2)-lo-95 | ARCH(2)-hi-95 | |
---|---|---|---|---|
unique_id | ||||
1 | 2023-05-25 | 1.681836 | -0.419322 | 3.782995 |
1 | 2023-05-26 | -0.777028 | -3.939044 | 2.384989 |
1 | 2023-05-29 | -0.677960 | -3.907244 | 2.551323 |
… | … | … | … | … |
1 | 2023-09-20 | 0.136752 | -0.795371 | 1.068876 |
1 | 2023-09-21 | 0.082173 | -0.852268 | 1.016615 |
1 | 2023-09-22 | -0.450958 | -1.337117 | 0.435202 |
unique_id | ds | ARCH(2) | |
---|---|---|---|
0 | 1 | 2023-05-25 | 1.681836 |
1 | 1 | 2023-05-26 | -0.777028 |
2 | 1 | 2023-05-29 | -0.677960 |
… | … | … | … |
84 | 1 | 2023-09-20 | 0.136752 |
85 | 1 | 2023-09-21 | 0.082173 |
86 | 1 | 2023-09-22 | -0.450958 |
ds | unique_id | y | ARCH(2) | |
---|---|---|---|---|
0 | 2023-05-25 | 1 | 0.875758 | 1.681836 |
1 | 2023-05-26 | 1 | 1.304909 | -0.777028 |
2 | 2023-05-30 | 1 | 0.001660 | -0.968701 |
… | … | … | … | … |
84 | 2023-09-26 | 1 | -1.473453 | NaN |
85 | 2023-09-27 | 1 | 0.022931 | NaN |
86 | 2023-09-28 | 1 | 0.589317 | NaN |
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, 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=[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.
ds | ARCH(2) | |
---|---|---|
unique_id | ||
1 | 2023-05-25 | 1.681836 |
1 | 2023-05-26 | -0.777028 |
1 | 2023-05-29 | -0.677960 |
… | … | … |
1 | 2023-09-20 | 0.136752 |
1 | 2023-09-21 | 0.082173 |
1 | 2023-09-22 | -0.450958 |
ds | ARCH(2) | ARCH(2)-lo-95 | ARCH(2)-lo-80 | ARCH(2)-hi-80 | ARCH(2)-hi-95 | |
---|---|---|---|---|---|---|
unique_id | ||||||
1 | 2023-05-25 | 1.681836 | -0.419322 | 0.307963 | 3.055710 | 3.782995 |
1 | 2023-05-26 | -0.777028 | -3.939044 | -2.844559 | 1.290504 | 2.384989 |
1 | 2023-05-29 | -0.677960 | -3.907244 | -2.789475 | 1.433555 | 2.551323 |
… | … | … | … | … | … | … |
1 | 2023-09-20 | 0.136752 | -0.795371 | -0.472731 | 0.746235 | 1.068876 |
1 | 2023-09-21 | 0.082173 | -0.852268 | -0.528825 | 0.693172 | 1.016615 |
1 | 2023-09-22 | -0.450958 | -1.337117 | -1.030386 | 0.128471 | 0.435202 |
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.
unique_id | y | ARCH(2) | ARCH(2)-lo-95 | ARCH(2)-lo-80 | ARCH(2)-hi-80 | ARCH(2)-hi-95 | |
---|---|---|---|---|---|---|---|
ds | |||||||
2023-03-21 | 1 | 1.298219 | NaN | NaN | NaN | NaN | NaN |
2023-03-22 | 1 | -1.646322 | NaN | NaN | NaN | NaN | NaN |
2023-03-23 | 1 | 0.298453 | NaN | NaN | NaN | NaN | NaN |
… | … | … | … | … | … | … | … |
2023-09-20 | NaN | NaN | 0.136752 | -0.795371 | -0.472731 | 0.746235 | 1.068876 |
2023-09-21 | NaN | NaN | 0.082173 | -0.852268 | -0.528825 | 0.693172 | 1.016615 |
2023-09-22 | NaN | NaN | -0.450958 | -1.337117 | -1.030386 | 0.128471 | 0.435202 |
Let’s plot the same graph using the plot function that comes in
Statsforecast
, as shown below.
Cross-validation
In previous steps, we’ve taken our historical data to predict the future. However, to asses its accuracy we would also like to know how the model would have performed in the past. To assess the accuracy and robustness of your models on your data perform 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.
In this case, we want to evaluate the performance of each model for the
last 5 months (n_windows=5)
, forecasting every second months
(step_size=12)
. Depending on your computer, this step should take
around 1 min.
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.
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 indexcutoff:
the last datestamp or temporal index for the n_windows.y:
true value"model":
columns with the model’s name and fitted value.
ds | cutoff | y | ARCH(2) | |
---|---|---|---|---|
unique_id | ||||
1 | 2022-12-21 | 2022-12-20 | -0.605272 | 1.889850 |
1 | 2022-12-22 | 2022-12-20 | -2.492167 | -0.850434 |
1 | 2022-12-23 | 2022-12-20 | -1.113775 | -0.742012 |
… | … | … | … | … |
1 | 2023-05-22 | 2023-01-23 | 0.015503 | 0.135570 |
1 | 2023-05-23 | 2023-01-23 | -1.122203 | 0.081367 |
1 | 2023-05-24 | 2023-01-23 | -0.731860 | -0.446374 |
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.
The function to compute the RMSE takes two arguments:
- The actual values.
- The forecasts, in this case,
ARCH
.
As you have noticed, we have used the cross validation results to perform the evaluation of our model.
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.
mae | mape | mase | rmse | smape | |
---|---|---|---|---|---|
ARCH(2) | 0.935485 | 1064.149021 | NaN | 1.152612 | 138.403076 |
Acknowledgements
We would like to thank Naren Castellon for writing this tutorial.
References
- Changquan Huang • Alla Petukhina. Springer series (2022). Applied Time Series Analysis and Forecasting with Python.
- Engle, R. F. (1982). Autoregressive conditional heteroscedasticity with estimates of the variance of United Kingdom inflation. Econometrica: Journal of the econometric society, 987-1007..
- James D. Hamilton. Time Series Analysis Princeton University Press, Princeton, New Jersey, 1st Edition, 1994.
- Nixtla Parameters.
- Pandas available frequencies.
- Rob J. Hyndman and George Athanasopoulos (2018). “Forecasting principles and practice, Time series cross-validation”..
- Seasonal periods- Rob J Hyndman.