# Chapter 5. Implementing Risk Forecasts

##### Listing 5.1: Download stock prices in R Last edited: August 2016

library("tseries")
library(zoo)
p1 = get.hist.quote(instrument = "msft",start = "2000-01-01",end = "2016-08-30",quote="AdjClose")
p2 = get.hist.quote(instrument = "ibm",  start = "2000-01-01",end = "2016-08-30",quote="AdjClose")
y1=tail(diff(log(coredata(p1))),4100)
y2=tail(diff(log(coredata(p2))),4100)
TT=length(y1)
y=cbind(y1,y2)
value = 1000
p = 0.01

##### Listing 5.2: Download stock prices in Matlab Last edited: August 2016

stocks =hist_stock_data('01012000','30082016','msft','ibm')
y1=diff(log(p1));
y2=diff(log(p2));
y1=y1(length(y1)-4100+1:end);
y2=y2(length(y2)-4100+1:end);
y=[y1 y2];
T=length(y1)
value = 1000;
p = 0.01;


##### Listing 5.3: Univariate HS in R Last edited: August 2016

ys = sort(y1)
op = TT*p
VaR1 = -ys[op]*value

##### Listing 5.4: Univariate HS in Matlab Last edited: 2011

ys = sort(y1);
op = T*p;
VaR1 = -ys(op)*value


##### Listing 5.5: Multivariate HS in R Last edited: 2011

w = matrix(c(0.3,0.7))
yp = y %*% w
yps = sort(yp)
VaR2 = -yps[op]*value

##### Listing 5.6: Multivariate HS in Matlab Last edited: 2011

w = [0.3; 0.7]
yp = y*w;
yps = sort(yp);
VaR2 = -yps(op)*value


##### Listing 5.7: Univariate ES in R Last edited: 2011

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

##### Listing 5.8: Univariate ES in Matlab Last edited: 2011

ES1 = -mean(ys(1:op))*value


##### Listing 5.9: Normal VaR in R Last edited: 2011

sigma = sd(y1)
VaR3 = -sigma * qnorm(p) * value

##### Listing 5.10: Normal VaR in Matlab Last edited: 2011

sigma = std(y1)
VaR3 = -sigma * norminv(p) * value


##### Listing 5.11: Portfolio normal VaR in R Last edited: 2011

sigma = sqrt(t(w) %*% cov(y) %*% w)
VaR4 = -sigma * qnorm(p)*value

##### Listing 5.12: Portfolio normal VaR in Matlab Last edited: 2011

sigma = sqrt(w' * cov(y) * w);
VaR4 = - sigma * norminv(p) *  value


##### Listing 5.13: Student-t VaR in R Last edited: August 2016

library(QRM)
scy1=(y1)*100
res=fit.st(scy1)
sigma1=res$par.ests[3]/100 nu=res$par.ests[1]
VaR5 = - sigma1 * qt(df=nu,p=p) *  value

##### Listing 5.14: Student-t VaR in Matlab Last edited: 2011

scy1=y1*100;
res=mle(scy1,'distribution','tlocationscale')
sigma1 = res(2)/100
nu = res(3)
VaR5 = - sigma1 * tinv(p,nu) * value


##### Listing 5.15: Normal ES in R Last edited: 2011

ES2 = sigma*dnorm(qnorm(p))/p * value

##### Listing 5.16: Normal ES in Matlab Last edited: 2011

ES2=sigma*normpdf(norminv(p))/p * value


##### Listing 5.17: Direct integration ES in R Last edited: 2011

VaR = -qnorm(p)
integrand = function(q){q*dnorm(q)}
ES = -sigma*integrate(integrand,-Inf,-VaR)$value/p*value  ##### Listing 5.18: Direct integration ES in Matlab Last edited: 2011  VaR = -norminv(p) ES = -sigma*quad(@(q) q.*normpdf(q),-6,-VaR)/p*value  ##### Listing 5.19: MA Normal VaR in R Last edited: August 2016  WE=20 t=TT-WE+1 window= y1[t1:TT] sigma=sd(window) VaR6 = -sigma * qnorm(p) * value  ##### Listing 5.20: MA Normal VaR in Matlab Last edited: 2011  WE=20 for t=T-5:T t1=t-WE+1; window=y1(t1:t); sigma=std(window); VaR6 = -sigma * norminv(p) * value end  ##### Listing 5.21: EWMA VaR in R Last edited: August 2016  lambda = 0.94; s11 = var(y1[1:30]); for (t in 2:TT){ s11 = lambda * s11 + (1-lambda) * y1[t-1]^2 } VaR7 = -qnorm(p) * sqrt(s11) * value  ##### Listing 5.22: EWMA VaR in Matlab Last edited: 2011  lambda = 0.94; s11 = var(y1(1:30)); for t = 2:T s11 = lambda * s11 + (1-lambda) * y1(t-1)^2; end VaR7 = -norminv(p) * sqrt(s11) * value  ##### Listing 5.23: 2 asset EWMA Normal VaR in R Last edited: August 2016  s = cov(y) for (t in 2:TT){ s = lambda * s + (1-lambda) * y[t-1,] %*% t(y[t-1,]) } sigma = sqrt(t(w) %*% s %*% w) VaR8 = -sigma * qnorm(p) * value  ##### Listing 5.24: 2 asset EWMA VaR in Matlab Last edited: 2011  s = cov(y); for t = 2:T s = lambda * s + (1-lambda) * y(t-1,:)' * y(t-1,:); end sigma = sqrt(w' * s * w); VaR8 = - sigma * norminv(p) * value  ##### Listing 5.25: GARCH in R Last edited: August 2016  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]
VaR9 = -sqrt(sigma2) * qnorm(p) * value

##### Listing 5.26: GARCH in Matlab Last edited: August 2017

[parameters,ll,ht]=tarch(y1,1,0,1)
omega = parameters(1)
alpha = parameters(2)
beta = parameters(3)
sigma2 = omega + alpha * y1(end)^2 + beta * ht(end)
VaR9 = -sqrt(sigma2) * norminv(p) * value