The VaR is found numerically using numerical root finding
via uniroot
(of the stats
package), whereas the ES is obtained through
numerical integration, where firstly the VaR at the corresponding
confidence level is computed using VaR_calc
, and where
subsequently integrate
(of the stats
package) is
used at the tail of the distribution.
In detail, let \(f(x)\) be the probability density function (pdf) of a standardized random
variable with mean zero and variance one. Without the need to state a
cumulative distribution function (cdf) mathematically, we can define it in R numerically
by integrating over \(f\) from -Inf
to some quantile x
using
integrate
. To then find a quantile for a given cumulative
probability, we can use uniroot
to find the quantile,
where the numerical cdf function minus the set cumulative probability equals
zero. This way, a numerical VaR can be found.
On the other hand, a numerical ES for a (continuous) random variable with mean zero and variance one
follows the alternative definition of the ES
$$\text{ES}_{\alpha}=(1-\alpha)^{-1}\int_{-\infty}^{\text{VaR}_{\alpha}}x f(x) dx,$$
where \(\alpha\), usually 0.99 or 0.975, is the confidence level,
\(\text{VaR}_{\alpha}\) is the VaR at the same \(\alpha\). Using the previous
approach, \(\text{VaR}_{\alpha}\) can be easily identified. Then, in R,
a function \(g(x)=x f(x)\) can also be defined easily. Ultimately, only
integrate
needs to be applied from -Inf
to the
corresponding VaR as the upper bound to the function \(g\). The resulting numerical
integral is then divided by \((1-\alpha)\).