Copyright 2011, 2016, 2018 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. The latest version of the Matlab code only uses functions from Matlab toolboxes.

The GARCH functionality in the econometric toolbox in Matlab is trying to be too clever, but can't deliver and could well be buggy. If you want to try that, here are the docs (estimate). Besides, it can only do univariate GARCH and so can't be used in Chapter 3. Kevin Sheppard's MFE toolbox is much better, while not as user friendly, it is much better written and is certainly more comprehensive. It can be downloaded here and the documentation here is quite detailed.

Last updated June 2018

```
library(tseries)
p = zoo(read.csv('index.csv', header=TRUE, sep=','))
y=diff(log(p))*100
y=y-mean(y)
## We multiply returns by 100 and de-mean them
library(fGarch)
garchFit(~ garch(1,0), data = y,include.mean=FALSE)
garchFit(~ garch(4,0), data = y,include.mean=FALSE)
garchFit(~ garch(4,1), data = y,include.mean=FALSE)
garchFit(~ garch(1,1), data = y,include.mean=FALSE,cond.dist="std",trace=F)
res=garchFit(~ garch(1,1), data = y,include.mean=FALSE,cond.dist="sstd",trace=F)
plot(res)
## plot(res) shows various graphical analysis, works in command line
```

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

Last updated June 2018

```
## normal APARCH(1,1)
print(garchFit(~ aparch(1,1),data=y,include.mean=FALSE,trace=F))
## fixing delta at 2 (or to any value)
print(garchFit(~ aparch(1,1),data=y,include.mean=FALSE,trace=F,include.delta=F,delta=2))
## Student-t conditional distribution
print(garchFit(~ aparch(1,1),data=y,include.mean=FALSE,cond.dist="std",trace=F))
## normal APARCH(2,2)
print(garchFit(~ aparch(2,2),data=y,include.mean=FALSE,trace=F))
```

Last updated June 2018

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