0
$\begingroup$

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.

$\endgroup$

2 Answers 2

0
$\begingroup$

Given mu for mean,and var for variance, next step forecast is:

mu=prediction.mean.iloc[-1,0]
var=prediction.variance.iloc[-1,0]
$\endgroup$
2
  • $\begingroup$ Thank you so much. I do believe there exists a more efficient method without for loop, but this definitely works. $\endgroup$
    – Xtiaan
    Jun 12, 2020 at 11:43
  • 1
    $\begingroup$ there's no for loop. "mu for mean" means "mu variable" is used for predicting the mean of the process, not a for loop. $\endgroup$
    – alexprice
    Jun 12, 2020 at 15:48
1
$\begingroup$

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)

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

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")
```
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.