calc_cvar: Calculate the Value at Risk (_VaR_) or the Conditional Value...

View source: R/HighFreq.R

calc_cvarR Documentation

Calculate the Value at Risk (VaR) or the Conditional Value at Risk (CVaR) of an xts time series of returns, using R code.

Description

Calculate the Value at Risk (VaR) or the Conditional Value at Risk (CVaR) of an xts time series of returns, using R code.

Usage

calc_cvar(tseries, method = "var", confi = pnorm(-2))

Arguments

tseries

An xts time series of returns with multiple columns.

method

A string specifying the type of risk measure (the default is method = "var" - see Details).

confi

The confidence level for calculating the quantile (the default is confi = pnorm(-2) = 0.02275).

Details

The function calc_cvar() calculates the Value at Risk (VaR) or the Conditional Value at Risk (CVaR) of an xts time series of returns, using R

The Value at Risk (VaR) and the Conditional Value at Risk (CVaR) are measures of the tail risk of returns.

If method = "var" then calc_cvar() calculates the Value at Risk (VaR) as the quantile of the returns as follows:

\alpha = \int_{-\infty}^{\mathrm{VaR}(\alpha)} \mathrm{f}(r) \, \mathrm{d}r

Where \alpha is the confidence level for calculating the quantile, and \mathrm{f}(r) is the probability density (distribution) of returns.

If method = "cvar" then calc_cvar() calculates the Value at Risk (VaR) as the Expected Tail Loss (ETL) of the returns as follows:

\mathrm{CVaR} = \frac{1}{\alpha} \int_{0}^\alpha \mathrm{VaR}(p) \, \mathrm{d}p

Where \alpha is the confidence level for calculating the quantile.

Value

A vector with the risk measures of the columns of the input time series tseries.

Examples

## Not run: 
# Calculate VTI and XLF returns
returns <- na.omit(rutils::etfenv$returns[, c("VTI", "XLF")])
# Calculate VaR
all.equal(HighFreq::calc_cvar(returns), 
  sapply(returns, quantile, probs=pnorm(-2)), check.attributes=FALSE)
# Calculate CVaR
all.equal(HighFreq::calc_cvar(returns, method="cvar", confi=0.02), 
  sapply(returns, function(x) mean(x[x < quantile(x, 0.02)])), 
  check.attributes=FALSE)

## End(Not run)


algoquant/HighFreq documentation built on Feb. 9, 2024, 8:15 p.m.