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 August 2016

```
library(tseries)
library(zoo)
prices = zoo(read.csv('stocks.csv',header=TRUE,sep=','))
## convert prices of first two stocks to returns and adjust length
y1=tail(diff(log(coredata(prices[,1]))),4100)
y2=tail(diff(log(coredata(prices[,2]))),4100)
TT=length(y1)
y=cbind(y1,y2)
value = 1000 # portfolio value
p = 0.01 # probability
```

Last updated June 2018

```
import numpy as np
p = np.loadtxt('stocks.csv',delimiter=',',skiprows=1)
p = p[:,[0,1]] # consider two stocks
## convert prices to returns, and adjust length
y1 = np.diff(np.log(p[:,0]), n=1, axis=0)
y2 = np.diff(np.log(p[:,1]), n=1, axis=0)
y1 = y1[len(y1)-4100:]
y2 = y2[len(y2)-4100:]
y = np.stack([y1,y2], axis = 1)
T = len(y1)
value = 1000 # portfolio value
p = 0.01 # probability
```

Last updated August 2016

```
ys = sort(y1) # sort returns
op = TT*p # p percent smallest
VaR1 = -ys[op]*value
print(VaR1)
```

Last updated June 2018

```
ys = np.sort(y1) # sort returns
op = int(T*p) # p percent smallest
VaR1 = -ys[op - 1] * value
print(VaR1)
```

Last updated 2011

```
w = matrix(c(0.3,0.7)) # vector of portfolio weights
yp = y %*% w # obtain portfolio returns
yps = sort(yp)
VaR2 = -yps[op]*value
print(VaR2)
```

Last updated June 2018

```
w = [[0.3], [0.7]] # vector of portfolio weights
yp = np.squeeze(np.matmul(y, w)) # portfolio returns
yps = np.sort(yp)
VaR2= -yps[op - 1] * value
print(VaR2)
```

Last updated 2011

```
ES1 = -mean(ys[1:op])*value
print(ES1)
```

Last updated June 2018

```
ES1 = -np.mean(ys[:op]) * value
print(ES1)
```

Last updated 2011

```
sigma = sd(y1) # estimate volatility
VaR3 = -sigma * qnorm(p) * value
print(VaR3)
```

Last updated June 2018

```
sigma = np.std(y1, ddof=1) # estimate volatility
VaR3 = -sigma * stats.norm.ppf(p) * value
print(VaR3)
```

Last updated 2011

```
sigma = sqrt(t(w) %*% cov(y) %*% w)[1] # portfolio volatility
## Note: the trailing [1] is to convert a single element matrix to float
VaR4 = -sigma * qnorm(p)*value
print(VaR4)
```

Last updated June 2018

```
## portfolio volatility
sigma = np.sqrt(np.mat(np.transpose(w))*np.mat(np.cov(y,rowvar=False))*np.mat(w))[0,0]
## Note: [0,0] is to pull the first element of the matrix out as a float
VaR4 = -sigma * stats.norm.ppf(p) * value
print(VaR4)
```

Last updated August 2016

```
library(QRM)
scy1=(y1)*100 # scale the returns
res=fit.st(scy1)
sigma1=res$par.ests[3]/100 # rescale the volatility
nu=res$par.ests[1]
VaR5 = - sigma1 * qt(df=nu,p=p) * value
print(VaR5)
```

Last updated June 2018

```
scy1 = y1 * 100 # scale the returns
res = stats.t.fit(scy1)
sigma = res[2]/100 # rescale volatility
nu = res[0]
VaR5 = -sigma*stats.t.ppf(p,nu)*value
print(VaR5)
```

Last updated June 2018

```
sigma = sd(y1)
ES2 = sigma*dnorm(qnorm(p))/p * value
print(ES2)
```

Last updated June 2018

```
sigma = np.std(y1, ddof=1)
ES2 = sigma * stats.norm.pdf(stats.norm.ppf(p)) / p * value
print(ES2)
```

Last updated 2011

```
VaR = -qnorm(p)
integrand = function(q){q*dnorm(q)}
ES = -sigma*integrate(integrand,-Inf,-VaR)$value/p*value
print(ES)
```

Last updated June 2018

```
from scipy.integrate import quad
VaR = -stats.norm.ppf(p)
integrand = lambda q: q * stats.norm.pdf(q)
ES = -sigma * quad(integrand, -np.inf, -VaR)[0] / p * value
print(ES)
```

Last updated June 2018

```
WE=20
for (t in seq(TT-5,TT)){
t1=t-WE+1
window= y1[t1:t] # estimation window
sigma=sd(window)
VaR6 = -sigma * qnorm(p) * value
print(VaR6)
}
```

Last updated June 2018

```
WE = 20
for t in range(T-5,T+1):
t1 = t-WE
window = y1[t1:t] # estimation window
sigma = np.std(window, ddof=1)
VaR6 = -sigma*stats.norm.ppf(p)*value
print (VaR6)
```

Last updated August 2016

```
lambda = 0.94;
s11 = var(y1[1:30]); # initial variance
for (t in 2:TT){
s11 = lambda * s11 + (1-lambda) * y1[t-1]^2
}
VaR7 = -qnorm(p) * sqrt(s11) * value
print(VaR7)
```

Last updated June 2018

```
lmbda = 0.94
s11 = np.var(y1[0:30], ddof = 1) # initial variance
for t in range(1, T):
s11 = lmbda*s11 + (1-lmbda)*y1[t-1]**2
VaR7 = -np.sqrt(s11)*stats.norm.ppf(p)*value
print(VaR7)
```

Last updated 2011

```
s = cov(y) # initial covariance
for (t in 2:TT){
s = lambda*s + (1-lambda)*y[t-1,] %*% t(y[t-1,])
}
sigma = sqrt(t(w) %*% s %*% w)[1] # portfolio vol
## Note: [1] is to convert single element matrix to float
VaR8 = -sigma * qnorm(p) * value
print(VaR8)
```

Last updated June 2018

```
## s is the initial covariance
s = np.cov(y, rowvar = False)
for t in range(1,T):
s = lmbda*s+(1-lmbda)*np.transpose(np.asmatrix(y[t-1,:]))*np.asmatrix(y[t-1,:])
sigma = np.sqrt((np.transpose(w)*s*w)[0,0])
## Note: [0,0] is to pull the first element of the matrix out as a float
VaR8 = -sigma * stats.norm.ppf(p) * value
print(VaR8)
```

Last updated 2011

```
library(fGarch)
g = garchFit(~garch(1,1),y1,include.mean=F,trace=F)
omega = g@fit$matcoef[1,1]
alpha = g@fit$matcoef[2,1]
beta = g@fit$matcoef[3,1]
sigma2 = omega + alpha*y[TT]^2 + beta*g@h.t[TT] # calc sigma2 for t+1
VaR9 = -sqrt(sigma2) * qnorm(p) * value
print(VaR9)
```

Last updated June 2018

```
from arch import arch_model
am = arch_model(y1, mean = 'Zero', vol='Garch', p=1, o=0, q=1, dist='Normal')
res = am.fit(update_freq=5)
omega = res.params[0]
alpha = res.params[1]
beta = res.params[2]
## computing sigma2 for t+1
sigma2 = omega + alpha*y1[T-1]**2 + beta * res.conditional_volatility[-1]**2
VaR9 = -np.sqrt(sigma2) * stats.norm.ppf(p) * value
print(VaR9)
## Note: arch_model's GARCH optimization has issues with convergence
```