Chapter 5. Implementing Risk Forecasts (in R/Python)


Copyright 2011 - 2020 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 5.1/5.2: Download stock prices in R
Last updated July 2020

p = read.csv('stocks.csv')
## Calculate returns. Note that first column is dates
y = apply(log(p),2,diff)
## Specify portfolio value and VaR probability
portfolio_value = 1000
p = 0.01
		
Listing 5.1/5.2: Download stock prices in Python
Last updated July 2020

import numpy as np
from scipy import stats
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)
y = np.stack([y1,y2], axis = 1)
T = len(y1)
value = 1000   # portfolio value
p = 0.01   # probability
		

Listing 5.3/5.4: Univariate HS in R
Last updated July 2020

y1 = y[,1]           # select one asset
ys = sort(y1)        # sort returns
op = ceiling(length(y1)*p) # p percent smallest, rounded up
VaR1 = -ys[op]*portfolio_value
print(VaR1)
		
Listing 5.3/5.4: Univariate HS in Python
Last updated July 2020

from math import ceil
ys = np.sort(y1) # sort returns
op = ceil(T*p)   # p percent smallest
VaR1 = -ys[op - 1] * value
print(VaR1)
		

Listing 5.5/5.6: Multivariate HS in R
Last updated July 2020

w = matrix(c(0.3,0.2,0.5)) # vector of portfolio weights
## The number of columns of the left matrix must be the same as the number of rows of the right matrix
yp = y %*% w         # obtain portfolio returns
yps = sort(yp)
VaR2 = -yps[op]*portfolio_value
print(VaR2)
		
Listing 5.5/5.6: Multivariate HS in Python
Last updated July 2020

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)
		

Listing 5.7/5.8: Univariate ES in R
Last updated August 2019

ES1 = -mean(ys[1:op])*portfolio_value
print(ES1)
		
Listing 5.7/5.8: Univariate ES in Python
Last updated June 2018

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

Listing 5.9/5.10: Normal VaR in R
Last updated August 2019

sigma = sd(y1) # estimate volatility
VaR3 = -sigma * qnorm(p) * portfolio_value
print(VaR3)
		
Listing 5.9/5.10: Normal VaR in Python
Last updated June 2018

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

Listing 5.11/5.12: Portfolio normal VaR in R
Last updated August 2019

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)*portfolio_value
print(VaR4)
		
Listing 5.11/5.12: Portfolio normal VaR in Python
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)
		

Listing 5.13/5.14: Student-t VaR in R
Last updated July 2020

library(QRM)
scy1 = (y1)*100      # scale the returns
res = fit.st(scy1)
sigma1 = unname(res$par.ests['sigma']/100) # rescale the volatility
nu = unname(res$par.ests['nu'])
## Note: We are removing the names of the fit.st output using unname()
VaR5 = - sigma1 * qt(df=nu,p=p) *  portfolio_value
print(VaR5)
		
Listing 5.13/5.14: Student-t VaR in Python
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)
		

Listing 5.15/5.16: Normal ES in R
Last updated June August 2019

sigma = sd(y1)
ES2 = sigma*dnorm(qnorm(p))/p * portfolio_value
print(ES2)
		
Listing 5.15/5.16: Normal ES in Python
Last updated June 2018

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

Listing 5.17/5.18: Direct integration ES in R
Last updated July 2020

VaR = -qnorm(p)
integrand = function(q){q*dnorm(q)}
ES = -sigma*integrate(integrand,-Inf,-VaR)$value/p*portfolio_value
print(ES)
		
Listing 5.17/5.18: Direct integration ES in Python
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)
		

Listing 5.19/5.20: MA normal VaR in R
Last updated June August 2019

WE=20
for (t in seq(length(y1)-5,length(y1))){
  t1=t-WE+1
  window= y1[t1:t] # estimation window
  sigma=sd(window)
  VaR6 = -sigma * qnorm(p) * portfolio_value
  print(VaR6)
}
		
Listing 5.19/5.20: MA normal VaR in Python
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)
		

Listing 5.21/5.22: EWMA VaR in R
Last updated July 2020

lambda = 0.94
s11 = var(y1) # initial variance, using unconditional
for (t in 2:length(y1)){
  s11 = lambda * s11  + (1-lambda) * y1[t-1]^2
}
VaR7 = -qnorm(p) * sqrt(s11) * portfolio_value
print(VaR7)
		
Listing 5.21/5.22: EWMA VaR in Python
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)
		

Listing 5.23/5.24: Three-asset EWMA VaR in R
Last updated August 2019

s = cov(y)           # initial covariance
for (t in 2:dim(y)[1]){
  s = lambda*s + (1-lambda)*y[t-1,] %*% t(y[t-1,])
}
sigma = sqrt(t(w) %*% s %*% w)[1] # portfolio vol
## Note: trailing[1] is to convert single element matrix to float
VaR8 = -sigma * qnorm(p) * portfolio_value
print(VaR8)
		
Listing 5.23/5.24: Two-asset EWMA VaR in Python
Last updated July 2020

## 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)
		

Listing 5.25/5.26: Univariate GARCH in R
Last updated July 2020

library(rugarch)
spec = ugarchspec(variance.model = list( garchOrder = c(1, 1)),
                  mean.model = list( armaOrder = c(0,0),include.mean = FALSE))
res = ugarchfit(spec = spec, data = y1)
omega = res@fit$coef['omega']
alpha = res@fit$coef['alpha1']
beta = res@fit$coef['beta1']
sigma2 = omega + alpha * tail(y1,1)^2 + beta * tail(res@fit$var,1)
VaR9 = -sqrt(sigma2) * qnorm(p) * portfolio_value
names(VaR9)="VaR"
print(VaR9)
		
Listing 5.25/5.26: GARCH VaR in Python
Last updated July 2020

from arch import arch_model
am = arch_model(y1, mean = 'Zero', vol='Garch', p=1, o=0, q=1, dist='Normal', rescale = False)
res = am.fit(update_freq=5, disp = "off")
omega = res.params.loc['omega']
alpha = res.params.loc['alpha[1]']
beta = res.params.loc['beta[1]']
## 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)