Chapter 2. Univariate Volatility Modeling (in Python/Julia)


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


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.1/2.2: ARCH and GARCH estimation in Julia
Last updated June 2018

## No package for ARCH/GARCH estimation as of June 2018
## We have a mini-package for estimating GARCH(1,1) and t-GARCH(1,1)
using CSV;
p = CSV.read("index.csv", nullable = false)
y = diff(log.(p[:,1]))*100
y = y - mean(y)
using FRFGarch;
## Ensure that the folder FRFGarch is in the folder returned when LOAD_PATH is keyed into Julia command line
## The package must be in ../FRFGarch/src/FRFGarch.jl
res = GARCHfit(y, "GARCH");
println(res.par)
res_t = GARCHfit(y, "t-GARCH");
println(res_t.par)
## GARCH estimation will be slightly different from other languages
## this is due to GARCHfit choosing initial conditional vol = sample vol
		

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
		
Listing 2.3/2.4: Advanced ARCH and GARCH estimation in Julia
Last updated June 2018

## We use the same mini-package to estimate APARCH(1,1)
res_AP = GARCHfit(y, "APARCH");
println(res_AP.par)
## GARCH estimation will be slightly different from other languages
## this is due to GARCHfit choosing initial conditional vol = sample vol