Fixed and Rolling GARCH Forecasts with the Python arch Package
Summary
The document explains how to extract one-step-ahead conditional mean and variance forecasts from the Python arch package, and why forecast output can contain many missing values. For a single forecast, select the final row and forecast-horizon column from the mean or variance result. Setting reindex to false produces a compact result instead of aligning forecasts to the full input series.
It also distinguishes fixed-window from rolling-window forecasting. A fixed window fits through a chosen final observation and forecasts after that point; a rolling window repeatedly refits over a fixed-length sample using first_obs and last_obs. The examples show how forecast horizon and fitting boundaries affect the output. These are package usage patterns rather than a comparison of forecasting accuracy, and the chosen window, model specification, and horizon still need to match the intended evaluation design.
Key ideas
- The forecast mean and variance are returned as indexed tables, so a single value can be selected by row and horizon column.
- The reindex setting controls whether forecast output is aligned to the full input series or kept compact.
- A fixed-window forecast fits through a specified final observation and forecasts beyond it.
- A rolling-window forecast repeatedly estimates the model over a fixed-length sample.
- The first and last observation settings define the estimation window used for each fit.
Tags
Full text
# How to obtain one-step ahead forecast in Python based on GARCH?
# How to obtain one-step ahead forecast in Python based on GARCH?
I am trying to produce one-step ahead forecast using GARCH in Python using a fixed windows method. I ultimately want to put the code below in a for loop, but this code snippet does not perform as I expect.
```
egarch = arch_model(train_data, mean='zero', lags=0, vol='EGARCH', p=1, o=1, q=1, dist='normal')
egarch_fit = egarch.fit();
prediction = egarch_fit.forecast(horizon=1);
prediction.variance
```
I expected the final line to print a numeric value, the one step ahead prediction based on train_data. However, the output I get is a dataframe with many NaN values and the prediction. How do I obtain the numerical forecasts? And how come the length of the returned dataframe is equal to the original sample size and not the size of train_data?
I read the reference (https://arch.readthedocs.io/en/latest/univariate/forecasting.html#arch.univariate.base.ARCHModelForecast) and it makes me believe that a for loop is not necessary to get one-step ahead forecasts based on a fixed window. However, I do not know how to obtain such a result. Furthermore, the code in the reference returns a dataframe, but I prefer a simple array of predictions.
## Answer by alexprice (score 0, accepted)
https://quant.stackexchange.com/a/54837
Given mu for mean,and var for variance, next step forecast is:
```
mu=prediction.mean.iloc[-1,0]
var=prediction.variance.iloc[-1,0]
```
## Answer by Pleb (score 3)
https://quant.stackexchange.com/a/71575
#### For anybody still wondering how to produce forecasts using the arch package:
Kevin Sheppard, the author of the `arch` package, has "recently" uploaded an extensive applied documentation on how to use different features/methods provided in the package. This includes different forecasting methods (see chapter 1.3 in his documentation), which can be extended to alternative models. To reiterate from the documentation, you can do a fixed window forecast as follows (pp. 29 -30):
> Fixed-windows forecasting uses data up to a specified date to generate all forecasts after that date. This can be implemented by passing the entire data in when initializing the model and then using `last_obs` when calling `fit`. `forecast()` will, by default, produce forecasts after this final date. `res = am.fit(last_obs="2011-1-1", update_freq=5) forecasts = res.forecast(horizon=5, reindex=False) `
```
res = am.fit(last_obs="2011-1-1", update_freq=5)
forecasts = res.forecast(horizon=5, reindex=False)
```
or a rolling window forecast (pp. 30 - 31):
> Rolling window forecasts use a fixed sample length and then produce one-step from the final observation. These can be implemented using first_obs and last_obs. `index = returns.index start_loc = 0 end_loc = np.where(index >= "2010-1-1")[0].min() forecasts = {} for i in range(20): sys.stdout.write(".") sys.stdout.flush() res = am.fit(first_obs=i, last_obs=i + end_loc, disp="off") temp = res.forecast(horizon=3, reindex=False).variance fcast = temp.iloc[0] forecasts[fcast.name] = fcast `
```
index = returns.index
start_loc = 0
end_loc = np.where(index >= "2010-1-1")[0].min()
forecasts = {}
for i in range(20):
sys.stdout.write(".")
sys.stdout.flush()
res = am.fit(first_obs=i, last_obs=i + end_loc, disp="off")
temp = res.forecast(horizon=3, reindex=False).variance
fcast = temp.iloc[0]
forecasts[fcast.name] = fcast
```
Quick summary of internal parameters:
- `last_obs` is the last included observation in your fixed window (or to estimate your model).
- `update_freq` the frequency at which the model refits the new data and produces forecasts.
- `horizon` returns the number of analytical forecasts at each time-period.
- `reindex = False` returns the smallest DataFrame that contains all of the results. If `True`, then returns the forecast results as the same shape of data used to fit the model. This might have been OP's problem regarding the shape of the output.
- `disp` displays optimization results.
- `first_obs` is the first observation to use when estimating the model.
For the above snippets to work, here is the preliminary code (p. 27):
```
import datetime as dt
import sys
import arch.data.sp500
import numpy as np
import pandas as pd
from arch import arch_model
data = arch.data.sp500.load()
market = data["Adj Close"]
returns = 100 * market.pct_change().dropna()
am = arch_model(returns, vol="Garch", p=1, o=0, q=1, dist="Normal")
```
```Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.