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")
```