# Chapter 2. Univariate Volatility Modeling

### R and Matlab

Copyright 2011 - 2023 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: www.gnu.org/licenses.

##### Listing 2.1/2.2

##### ARCH and GARCH estimation in R

```
library(rugarch)
p = read.csv('index.csv')
y=diff(log(p$Index))*100
y=y-mean(y)
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)
```

##### Listing 2.1/2.2

##### % ARCH and GARCH estimation in MATLAB

```
p = csvread('index.csv', 1, 0);
y=diff(log(p))*100;
y=y-mean(y);
tarch(y,1,0,0); % ARCH(1)
tarch(y,4,0,0); % ARCH(4)
tarch(y,4,0,1); % GARCH(4,1)
tarch(y,1,0,1); % GARCH(1,1)
tarch(y,1,0,1,'STUDENTST'); % t-GARCH(1,1)
```

##### Listing 2.3/2.4

##### Advanced ARCH and GARCH estimation in R

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

```
aparch(y,1,1,1); % APARCH(1,1)
aparch(y,2,2,1); % APARCH(2,1)
aparch(y,1,1,1,'STUDENTST'); % t-APARCH(1,1)
```

##### Financial Risk Forecasting

Market risk forecasting with R, Julia, Python and Matlab. Code, lecture slides, implementation notes, seminar assignments and questions.© All rights reserved, Jon Danielsson,