50% off: Unlimited data and AI learning.
State of Data and AI Literacy Report 2025

qrmtools (version 0.0-17)

risk_measures: Risk Measures

Description

Computing risk measures.

Usage

## Value-at-risk
VaR_np(x, level, names = FALSE, type = 1, ...)
VaR_t(level, loc = 0, scale = 1, df = Inf)
VaR_t01(level, df = Inf)
VaR_GPD(level, shape, scale)
VaR_Par(level, shape, scale = 1)
VaR_GPDtail(level, threshold, p.exceed, shape, scale)

## Expected shortfall ES_np(x, level, method = c(">", ">="), verbose = FALSE, ...) ES_t(level, loc = 0, scale = 1, df = Inf) ES_t01(level, df = Inf) ES_GPD(level, shape, scale) ES_Par(level, shape, scale = 1) ES_GPDtail(level, threshold, p.exceed, shape, scale)

## Range value-at-risk RVaR_np(x, level, ...)

## Multivariate geometric value-at-risk and expectiles gVaR(x, level, start = colMeans(x), method = if(length(level) == 1) "Brent" else "Nelder-Mead", ...) gEX(x, level, start = colMeans(x), method = if(length(level) == 1) "Brent" else "Nelder-Mead", ...)

Value

VaR_np(), ES_np(), RVaR_np() estimate value-at-risk, expected shortfall and range value-at-risk non-parametrically. For expected shortfall, if method = ">="

(method = ">", the default), losses greater than or equal to (strictly greater than) the nonparametric value-at-risk estimate are averaged; in the former case, there might be no such loss, in which case NaN is returned. For range value-at-risk, losses greater than the nonparametric VaR estimate at level

level[1] and less than or equal to the nonparametric VaR estimate at level level[2] are averaged.

VaR_t(), ES_t() compute value-at-risk and expected shortfall for the t (or normal) distribution. VaR_t01(),

ES_t01() compute value-at-risk and expected shortfall for the standardized t (or normal) distribution, so scaled t

distributions to have mean 0 and variance 1; note that they require a degrees of freedom parameter greater than 2.

VaR_GPD(), ES_GPD() compute value-at-risk and expected shortfall for the generalized Pareto distribution (GPD).

VaR_Par(), ES_Par() compute value-at-risk and expected shortfall for the Pareto distribution.

gVaR(), gEX() compute the multivariate geometric value-at-risk and expectiles suggested by Chaudhuri (1996) and Herrmann et al. (2018), respectively.

Arguments

x

gVaR(), gEX():

matrix of (rowwise) multivariate losses.

VaR_np(), ES_np(), RVaR_np():

if x is a matrix then rowSums() is applied first (so value-at-risk and expected shortfall of the sum is computed).

otherwise:

vector of losses.

level

RVaR_np():

vector of length 1 or 2 giving the lower and upper confidence level; if of length 1, it is interpreted as the lower confidence level and the upper one is taken to be 1.

gVaR(), gEX():

vector or matrix of (rowwise) confidence levels α (all in [0,1]).

otherwise:

confidence level α[0,1].

names

see ?quantile.

type

see ?quantile.

loc

location parameter μ.

shape

VaR_GPD(), ES_GPD():

GPD shape parameter ξ, a real number.

VaR_Par(), ES_Par():

Pareto shape parameter θ, a positive number.

scale

VaR_t(), ES_t():

t scale parameter σ, a positive number.

VaR_GPD(), ES_GPD():

GPD scale parameter β, a positive number.

VaR_Par(), ES_Par():

Pareto scale parameter κ, a positive number.

df

degrees of freedom, a positive number; choose df = Inf for the normal distribution. For the standardized t distributions, df has to be greater than 2.

threshold

threhold u (used to estimate the exceedance probability based on the data x).

p.exceed

exceedance probability; typically mean(x > threshold) for x being the data modeled with the peaks-over-threshold (POT) method.

start

vector of initial values for the underlying optim().

method

ES_np():

character string indicating the method for computing expected shortfall.

gVaR(), gEX():

the optimization method passed to the underlying optim().

verbose

logical indicating whether verbose output is given (in case the mean is computed over (too) few observations).

...

VaR_np():

additional arguments passed to the underlying quantile().

ES_np(), RVaR_np():

additional arguments passed to the underlying VaR_np().

gVaR(), gEX():

additional arguments passed to the underlying optim().

Author

Marius Hofert

Details

The distribution function of the Pareto distribution is given by F(x)=1(κ/(κ+x))θ, x0, where θ>0, κ>0.

References

McNeil, A. J., Frey, R. and Embrechts, P. (2015). Quantitative Risk Management: Concepts, Techniques, Tools. Princeton University Press.

Chaudhuri, P. (1996). On a geometric notion of quantiles for multivariate data. Journal of the American Statistical Assosiation 91(434), 862--872.

Herrmann, K., Hofert, M. and Mailhot, M. (2018). Multivariate geometric expectiles. Scandinavian Actuarial Journal, 2018(7), 629--659.

Examples

Run this code
### 1 Univariate measures ######################################################

## Generate some losses and (non-parametrically) estimate VaR_alpha and ES_alpha
set.seed(271)
L <- rlnorm(1000, meanlog = -1, sdlog = 2) # L ~ LN(mu, sig^2)
## Note: - meanlog = mean(log(L)) = mu, sdlog = sd(log(L)) = sig
##       - E(L) = exp(mu + (sig^2)/2), var(L) = (exp(sig^2)-1)*exp(2*mu + sig^2)
##         To obtain a sample with E(L) = a and var(L) = b, use:
##         mu = log(a)-log(1+b/a^2)/2 and sig = sqrt(log(1+b/a^2))
VaR_np(L, level = 0.99)
ES_np(L,  level = 0.99)

## Example 2.16 in McNeil, Frey, Embrechts (2015)
V <- 10000 # value of the portfolio today
sig <- 0.2/sqrt(250) # daily volatility (annualized volatility of 20%)
nu <- 4 # degrees of freedom for the t distribution
alpha <- seq(0.001, 0.999, length.out = 256) # confidence levels
VaRnorm <- VaR_t(alpha, scale = V*sig, df = Inf)
VaRt4 <- VaR_t(alpha, scale = V*sig*sqrt((nu-2)/nu), df = nu)
ESnorm <- ES_t(alpha, scale = V*sig, df = Inf)
ESt4 <- ES_t(alpha, scale = V*sig*sqrt((nu-2)/nu), df = nu)
ran <- range(VaRnorm, VaRt4, ESnorm, ESt4)
plot(alpha, VaRnorm, type = "l", ylim = ran, xlab = expression(alpha), ylab = "")
lines(alpha, VaRt4, col = "royalblue3")
lines(alpha, ESnorm, col = "darkorange2")
lines(alpha, ESt4, col = "maroon3")
legend("bottomright", bty = "n", lty = rep(1,4), col = c("black",
       "royalblue3", "darkorange3", "maroon3"),
       legend = c(expression(VaR[alpha]~~"for normal model"),
                  expression(VaR[alpha]~~"for "*t[4]*" model"),
                  expression(ES[alpha]~~"for normal model"),
                  expression(ES[alpha]~~"for "*t[4]*" model")))


### 2 Multivariate measures ####################################################

## Setup
library(copula)
n <- 1e4 # MC sample size
nu <- 3 # degrees of freedom
th <- iTau(tCopula(df = nu), tau = 0.5) # correlation parameter
cop <- tCopula(param = th, df = nu) # t copula
set.seed(271) # for reproducibility
U <- rCopula(n, cop = cop) # copula sample
theta <- c(2.5, 4) # marginal Pareto parameters
stopifnot(theta > 2) # need finite 2nd moments
X <- sapply(1:2, function(j) qPar(U[,j], shape = theta[j])) # generate X
N <- 17 # number of angles (rather small here because of run time)
phi <- seq(0, 2*pi, length.out = N) # angles
r <- 0.98 # radius
alpha <- r * cbind(alpha1 = cos(phi), alpha2 = sin(phi)) # vector of confidence levels

## Compute geometric value-at-risk
system.time(res <- gVaR(X, level = alpha))
gvar <- t(sapply(seq_len(nrow(alpha)), function(i) {
    x <- res[[i]]
    if(x[["convergence"]] != 0) # 0 = 'converged'
        warning("No convergence for alpha = (", alpha[i,1], ", ", alpha[i,2],
                ") (row ", i, ")")
    x[["par"]]
})) # (N, 2)-matrix

## Compute geometric expectiles
system.time(res <- gEX(X, level = alpha))
gex <- t(sapply(seq_len(nrow(alpha)), function(i) {
    x <- res[[i]]
    if(x[["convergence"]] != 0) # 0 = 'converged'
        warning("No convergence for alpha = (", alpha[i,1], ", ", alpha[i,2],
                ") (row ", i, ")")
    x[["par"]]
})) # (N, 2)-matrix

## Plot geometric VaR and geometric expectiles
plot(gvar, type = "b", xlab = "Component 1 of geometric VaRs and expectiles",
     ylab = "Component 2 of geometric VaRs and expectiles",
     main = "Multivariate geometric VaRs and expectiles")
lines(gex, type = "b", col = "royalblue3")
legend("bottomleft", lty = 1, bty = "n", col = c("black", "royalblue3"),
       legend = c("geom. VaR", "geom. expectile"))
lab <- substitute("MC sample size n ="~n.*","~t[nu.]~"copula with Par("*th1*
                  ") and Par("*th2*") margins",
                  list(n. = n, nu. = nu, th1 = theta[1], th2 = theta[2]))
mtext(lab, side = 4, line = 1, adj = 0)

Run the code above in your browser using DataLab