DNB case

Sample code for the DNB example described in:

Aas, K. and G. Puccetti (2014). Bounds for total economic capital: the DNB case study. Extremes, 17(4), 693-715 paper  SSRN

This is the code for VaR. For ES risk measure see this code

Supplementary files (either put them in your R working directory [RMenu->Misc->Change working directory]

either insert the physical address of the files in " "):

"credit.txt" = 2.5 million simulated samples for credit risk 

"market.txt" = 2.5 million simulated samples for market risk

"asset.txt" = 2.5 million simulated samples for ownership risk

Disclaimer

The samples provided are artificial - not the original ones used by DNB - thus the VaR figure obtained is slightly different from the one in [AP14].

#Algorithm in R to compute the WORST possible VaR for the sum of six random losses for the DNB portfolio as described in Section 3.2 of [AP14]

# STEP 1.  CREATION OF THE MATRIX X

# alpha = confidence level at which VaR is computed

alpha=0.9997

# N = cardinality of the support of the approximation of EACH of the marginal distribution 

# (the larger N, the more accurate, the more time expensive the algorithm)

N=2.5*10^6

# In the computation of the worst bound only M data are used for each marginal risk

M=N*(1-alpha)

# d = dimensionality of the risk portfolio (this cannot be changed in this specific example)

d=6;

#  Marginal Loss 1:  CREDIT RISK; obtained from a simulated sample included in the file "credit.txt"

S1=scan("credit.txt")

# discretisation of Loss 1 above its alpha-quantile (for simulated samples just means taking the N*(1-alpha) greatest losses)

X1=sort(S1)[(N*alpha+1):N]

# Marginal Loss 2: MARKET RISK; obtained from simulated sample included in the file "market.txt"

S2=scan("market.txt")

# discretisation of Loss 2 above its alpha-quantile (for simulated samples just means taking the (N*alpha) greatest losses)

X2=sort(S2)[(N*alpha+1):N]

# Marginal Loss 3: OWNERSHIP RISK; obtained from simulated sample included in the file "ownership.txt"

S3=scan("asset.txt")

# discretisation of Loss 3 above its alpha-quantile (for simulated samples just means taking the (N*alpha) greatest losses)

X3=sort(S3)[(N*alpha+1):N]

# Marginal Loss 4: OPERATIONAL RISK; obtained from a LogNormal distribution

Q4=function(x){qlnorm(x, meanlog = 6.4741049, sdlog = 0.7213475, lower.tail = TRUE, log.p = FALSE)}

X4=Q4(seq(alpha,1,length=M))

# Marginal Loss 5: BUSINESS RISK; obtained from a LogNormal distribution

Q5=function(x){qlnorm(x, meanlog = 6.445997, sdlog = 0.574740, lower.tail = TRUE, log.p = FALSE)}

X5=Q5(seq(alpha,1,length=M))

# Marginal Loss 6: INSURANCE RISK; obtained from a LogNormal distribution

Q6=function(x){qlnorm(x, meanlog = 6.0534537, sdlog = 0.2489763, lower.tail = TRUE, log.p = FALSE)}

X6=Q6(seq(alpha,1,length=M))

#Final matrix X

X=cbind(X1,X2,X3,X4,X5,X6)

# STEP 2-3.  ITERATIVE REARRANGEMENT

# epsilon = desired accuracy

epsilon=0.01

# VaR = recursive value for the worst possible VaR

VaR=Inf

# delta = difference between two consecutive estimates 

delta=Inf

# iteratively rearrange each column of the matrix until the difference between

# two consecutive estimates of the worst VaR is less than epsilon

while(delta >epsilon ){

#VaRtemp = auxiliary variable

VaRtemp=VaR

for(j in 1:d) {

# rearrangement procedure

X[,j]=sort(X[,j],decreasing=TRUE)[rank(rowSums(X[,c(1:d)[-j]]))] }

# computation of the worst VaR in one iteration

VaR= min(rowSums(X))

delta=abs(VaR-VaRtemp) 

}

# Output=worst possible VaR at the fixed level of accuracy

print(VaR)

#Algorithm in R to compute the BEST possible VaR for the sum of six random losses for the DNB portfolio as described in Section 3.2 of [AP14]

# STEP 1.  CREATION OF THE MATRIX X

# alpha = confidence level at which VaR is computed

alpha=0.9997

# N = cardinality of the support of the approximation of EACH of the marginal distribution 

# (the larger N, the more accurate, the more time expensive the algorithm)

N=2.5*10^6

# In the computation of the best bound K data are used for each marginal risk

K=N*alpha

# d = dimensionality of the risk portfolio (this cannot be changed in this specific example)

d=6;

#  Marginal Loss 1:  CREDIT RISK; obtained from a simulated sample included in the file "credit.txt"

S1=scan("credit.txt")

# discretisation of Loss 1 below its alpha-quantile (for simulated samples just means taking the (N*alpha) smallest losses)

X1=sort(S1)[1:(N*alpha)]

# Marginal Loss 2: MARKET RISK; obtained from simulated sample included in the file "market.txt"

S2=scan("market.txt")

# discretisation of Loss 2 above its alpha-quantile (for simulated samples just means taking the (N*alpha) greatest losses)

X2=sort(S2)[1:(N*alpha)]

# Marginal Loss 3: OWNERSHIP RISK; obtained from simulated sample included in the file "ownership.txt"

S3=scan("asset.txt")

# discretisation of Loss 3 above its alpha-quantile (for simulated samples just means taking the (N*alpha) greatest losses)

X3=sort(S3)[1:(N*alpha)]

# Marginal Loss 4: OPERATIONAL RISK; obtained from a LogNormal distribution

Q4=function(x){qlnorm(x, meanlog = 6.4741049, sdlog = 0.7213475, lower.tail = TRUE, log.p = FALSE)}

X4=Q4(seq(0,alpha,length=K))

# Marginal Loss 5: BUSINESS RISK; obtained from a LogNormal distribution

Q5=function(x){qlnorm(x, meanlog = 6.445997, sdlog = 0.574740, lower.tail = TRUE, log.p = FALSE)}

X5=Q5(seq(0,alpha,length=K))

# Marginal Loss 6: INSURANCE RISK; obtained from a LogNormal distribution

Q6=function(x){qlnorm(x, meanlog = 6.0534537, sdlog = 0.2489763, lower.tail = TRUE, log.p = FALSE)}

X6=Q6(seq(0,alpha,length=K))

#Final matrix X

X=cbind(X1,X2,X3,X4,X5,X6)

# STEP 2-3.  ITERATIVE REARRANGEMENT

# epsilon = desired accuracy

epsilon=0.01

# VaR = recursive value for the best possible VaR

VaR=Inf

# delta = difference between two consecutive estimates 

delta=Inf

# iteratively rearrange each column of the matrix until the difference between

# two consecutive estimates of the best VaR is less than epsilon

while(delta >epsilon ){

#VaRtemp = auxiliary variable

VaRtemp=VaR

for(j in 1:d) {

# rearrangement procedure

X[,j]=sort(X[,j],decreasing=TRUE)[rank(rowSums(X[,c(1:d)[-j]]))] }

# computation of the worst VaR in one iteration

VaR= max(rowSums(X))

delta=abs(VaR-VaRtemp) 

}

# Output=best possible VaR at the fixed level of accuracy

print(VaR)