 # Chapter 2. Univariate Volatility Modeling (in R/Python)

Copyright 2011 - 2019 Jon Danielsson. This code is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. The GNU General Public License is available at: https://www.gnu.org/licenses/.

The original 2011 R code will not fully work on a recent R because there have been some changes to libraries. At least two R packages support estimating GARCH style models. rugarch by Alexios Ghalanos and fGarch. For our purposes there nothing to separate them but rugarch is regularly maintained, but fGarch appears not to be.

##### Listing 2.1/2.2: ARCH and GARCH estimation in R Last updated August 2019

library(rugarch)
y=diff(log(p\$Index))*100
y=y-mean(y)
## We multiply returns by 100 and de-mean them
spec1 = ugarchspec(variance.model = list( garchOrder = c(1, 1)),
mean.model = list( armaOrder = c(0,0),include.mean = FALSE))
res1 = ugarchfit(spec = spec1, data = y)
spec2 = ugarchspec(variance.model = list( garchOrder = c(1, 0)),
mean.model = list( armaOrder = c(0,0),include.mean = FALSE))
res2 = ugarchfit(spec = spec2, data = y)
spec3 = ugarchspec(variance.model = list( garchOrder = c(1, 1)),
mean.model = list( armaOrder = c(0,0),include.mean = FALSE),
distribution.model = "std")
res3 = ugarchfit(spec = spec3, data = y)
## plot(res) shows various graphical analysis, works in command line

##### Listing 2.1/2.2: ARCH and GARCH estimation in Python Last updated June 2018

import numpy as np
p = np.loadtxt('index.csv', delimiter = ',', skiprows = 1)
y = np.diff(np.log(p), n=1, axis=0)*100
y = y-np.mean(y)
from arch import arch_model
## using Kevin Sheppard's ARCH package for Python
## ARCH(1)
am = arch_model(y, mean = 'Zero', vol='Garch', p=1, o=0, q=0, dist='Normal')
am.fit(update_freq=5)
## ARCH(4)
am = arch_model(y, mean = 'Zero', vol='Garch', p=4, o=0, q=0, dist='Normal')
am.fit(update_freq=5)
## GARCH(4,1)
am = arch_model(y, mean = 'Zero', vol='Garch', p=4, o=0, q=1, dist='Normal')
am.fit(update_freq=5)
## GARCH(1,1)
am = arch_model(y, mean = 'Zero', vol='Garch', p=1, o=0, q=1, dist='Normal')
am.fit(update_freq=5)
## t-GARCH(1,1)
am = arch_model(y, mean = 'Zero', vol='Garch', p=1, o=0, q=1, dist='StudentsT')
am.fit(update_freq=5)
## comment out all the lines except one to see its output


##### Listing 2.3/2.4: Advanced ARCH and GARCH estimation in R Last updated August 2019

library(rugarch)
## normal APARCH(1,1)
spec4 = ugarchspec(variance.model = list(model="apARCH", garchOrder = c(1, 1)),
mean.model = list( armaOrder = c(0,0),include.mean = FALSE))
res4 = ugarchfit(spec = spec4, data = y)
#show(res4)
spec5 = ugarchspec(variance.model = list(model="apARCH", garchOrder = c(1, 1)),
mean.model = list( armaOrder = c(0,0),include.mean = FALSE), fixed.pars=list(delta=2))
res5 = ugarchfit(spec = spec5, data = y)
show(res5)

##### Listing 2.3/2.4: Advanced ARCH and GARCH estimation in Python Last updated June 2018

## Python does not have a proper APARCH package at present
## To be introduced in scikits.statsmodels