DNB_ES

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 ES. For VaR 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 ES figure obtained is slightly different from the one in [AP14].

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

#(The WORST possible ES is simply the sum of marginal ES figures)

# STEP 1.  CREATION OF THE MATRIX X

# alpha = confidence level at which ES 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)

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

N=2.5*10^6

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

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

S2=scan("market.txt")

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

S3=scan("asset.txt")

# 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,1,length=N+1)[1:N])

# 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,1,length=N+1)[1:N])

# 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,1,length=N+1)[1:N])

#Final matrix X

X=cbind(S1,S2,S3,X4,X5,X6)

# STEP 2-3.  ITERATIVE REARRANGEMENT

# epsilon = desired accuracy

epsilon=0.01

# ES = recursive value for the best possible ES

ES=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 ES is less than epsilon

while(delta >epsilon ){

#EStemp = auxiliary variable

EStemp=ES

for(j in 1:d) {

# rearrangement procedure

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

# computation of the best ES in one iteration

#Y auxiliary variable representing the vector of the row-wise sums of X in increasing order

Y=sort(rowSums(X))

ES =sum(Y[(floor(N*alpha)+1):N])/N/(1-alpha)

delta=abs(ES-EStemp) 

}

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

print(ES)