Problem: Correct usage of GARCH(1,1)

Aim of research: Forecasting volatility/variance.

Tools used: Python 3.3 with arch library

I am trying to obtain out-of-sample estimation of volatility using a fitted GARCH (or other model from the library), so I can compare it with other approaches - like recurrent neural networks.

However, I haven't found a way, how to use the fitted model in similar way to the R as mentioned here, where they fit the model and then obtain rolling forecast one period ahead. I have looked through many examples and tutorial, but they always use in-sample estimates, when they already have the residuals as in example with in-sample estimation. I would like to "feed" the model historical data and obtain n periods ahead prediction (e.g. day ahead).

I believe the answer is very simple, however, I have not found it. Also, admittedly, I am quite new into using these types of models and python for statistical learning (I mostly used machine learning in the past).

  • $\begingroup$ Are you just asking for code / how to get Python to do this? $\endgroup$ May 7, 2015 at 18:57
  • $\begingroup$ Yes and also no, I am asking 1) is there any similarly simple method as in R? 2) What would be the best way to implement this in python? However, if there is no general way how to do this in python, I would be glad for the any hint how to compute that manually :) $\endgroup$
    – Pter
    May 7, 2015 at 19:22
  • $\begingroup$ However, I would like to know the general way, not just the equations for GARCH. In the code snippet in R, it seems it can be used for any type of model - e.g. GJR-GARCH. I need to automatically evaluate more models, but I do not know how in python. $\endgroup$
    – Pter
    May 7, 2015 at 19:32
  • $\begingroup$ quant.stackexchange.com/questions/16730/… $\endgroup$
    – gliptak
    Jul 16, 2017 at 21:32

1 Answer 1


Here is an example notebook:

# In[1]:

import pandas as pd
import numpy as np
import pandas_datareader as pdr
import datetime
import arch

# In[2]:

start = datetime.datetime(1995, 1, 1)
df = pdr.data.DataReader('SPY', 'yahoo', start=start)
del df['Open']
del df['High']
del df['Low']
del df['Adj Close']
del df['Volume']

# In[3]:

df['log_price'] = np.log(df['Close'])
df['pct_change'] = df['log_price'].diff()

# In[4]:

df['stdev21'] = df['pct_change'].rolling(window=21, center=False).std()
df['hvol21'] = df['stdev21'] * (252**0.5) # Annualize.
#df['variance'] = df['hvol21']**2
df = df.dropna() # Remove rows with blank cells.

# In[5]:


# In[6]:

returns = df['pct_change'] * 100
am = arch.arch_model(returns)

# In[7]:

res = am.fit(disp='off')

# In[8]:

df['forecast_vol'] = 0.1 * np.sqrt(res.params['omega'] + res.params['alpha[1]'] * res.resid**2 + res.conditional_volatility**2 * res.params['beta[1]'])

# In[9]:

  • 1
    $\begingroup$ So df['forecast_vol'] is the model's approximation of the rolling volatility for the dates it was fed the returns for (training data), correct? If this is the case, how can the model's parameters then be used to forecast the volatility, say, 10 days into the future from today? $\endgroup$
    – KOB
    Jan 9, 2019 at 10:17
  • $\begingroup$ In the second to last line of code (# In[8]), why are you multiplying the Garch forecasts by 0.1? and this code is merely just predicting the same training data that the model was trained on? out-of-sample performance is what matters, not in-sample $\endgroup$
    – develarist
    Apr 27, 2020 at 1:12

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.