# Rolling volatility estimation using GARCH family of models in python

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).

• Are you just asking for code / how to get Python to do this? – gung - Reinstate Monica May 7 '15 at 18:57
• 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 :) – Pter May 7 '15 at 19:22
• 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. – Pter May 7 '15 at 19:32
• quant.stackexchange.com/questions/16730/… – gliptak Jul 16 '17 at 21:32

Here is an example notebook:

# In[1]:

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

# In[2]:

start = datetime.datetime(1995, 1, 1)
del df['Open']
del df['High']
del df['Low']
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')
res.summary()

# 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]:

df.tail()

• 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? – KOB Jan 9 '19 at 10:17
• 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 – develarist Apr 27 at 1:12