26  Value at Risk test

Setup
library(dplyr)
library(latex2exp)
library(backports)
library(ggplot2)
library(knitr)

The Value at Risk (VaR) of a random variable \(X\) is a measure of risk that represents the value of \(X\) such that the probability that \(X\) is greater than this value is equal to \(\alpha\). Formally,
\[ \mathbb{P}(X \le \text{VaR}^{\alpha}(X)) = \alpha \text{.} \] If a the random value \(X\) has known distribution \(F_X\) and quantile \(q_X\) functions, the Value at Risk will be defined as \[ \text{VaR}^{\alpha}(X) = q_X(\alpha) \text{,} \] where \(q_X\) is the Quantile function of \(X\).

Value at Risk
#' @param q_X Quantile function of X.
#' @param alpha Probability
VaR <- function(q_X, alpha){
  q_X(alpha)
}
# Confidence level
alpha <- 0.05
# Quantile functions
q_X1 <- function(p) qnorm(p)
q_X2 <- function(p) qlogis(p)
q_X3 <- function(p) qt(p, df = 5)
# VaR
VaR_X1 <- VaR(q_X1, alpha)
VaR_X2 <- VaR(q_X2, alpha)
VaR_X3 <- VaR(q_X3, alpha)
Figure 26.1: Value at Risk for different distributions with \(\alpha = 0.05\).

Another important measure of risk is the Expected shortfall (ES) that, given a certain level \(\alpha\), represents the expected value of the random variable \(X\) when \(X > \text{VaR}^{\alpha}\). More precisely, \[ \text{ES}^{\alpha}(X) = \frac{1}{\alpha} \int_{0}^{\alpha} \text{VaR}^{\gamma}(X) d\gamma \] The Expected shortfall is also called conditional value at risk (CVaR) or superquantile.

Expected Shortfall
#' @param q_X Quantile function of X.
#' @param alpha Probability
ES <- function(q_X, alpha){
  integrate(function(p) q_X(p), lower = 0, upper = alpha)$value / alpha
}
# Expected Shortfalls 
ES_X1 <- ES(q_X1, alpha)
ES_X2 <- ES(q_X2, alpha)
ES_X3 <- ES(q_X3, alpha)
Distribution \(\alpha\) \(\text{VaR}^{\alpha}(X)\) \(\text{ES}^{\alpha}(X)\)
Normal 0.05 -1.644854 -2.062713
Student-t 0.05 -2.015048 -2.890129
Logistic 0.05 -2.944439 -3.970305
Table 26.1: Value at Risk (VaR) and Expected Shortfall for different distributions.

Example 26.1 Let’s consider a process \(Y_t\) following an ARMA(2,2)-GARCH(1,1), i.e.  \[ \begin{aligned} & {} Y_t = \mu + \phi_1 Y_{t-1} + \phi_2 Y_{t-2} + \theta_1 e_{t-1} + \theta_2 e_{t-2} + e_t \\ & e_t = \sigma_t u_t \\ & \sigma_t^2 = \omega + \alpha_1 e_{t-1}^{2} + \beta_1 \sigma_{t-1}^{2} \end{aligned} \] with \(u_t \sim \mathcal{N}(0,1)\). The conditional expectation \(\mathbb{E}\{Y_t \mid \mathcal{F}_{t-1}\}\) is equal to \[ \mu_t = \mu + \phi_1 Y_{t-1} + \phi_2 Y_{t-2} + \theta_1 e_{t-1} + \theta_2 e_{t-2} \] while the conditional variance \[ \mathbb{V}\{Y_t \mid \mathcal{F}_{t-1}\} = \sigma_t^2 \] Since \(u_t\) is normally distributed also the conditional distribution of \(Y_t\) given \(\mathcal{F}_t\) is normal, i.e.  \[ Y_{t \mid t-1} \sim \mathcal{N}(\mu_t, \sigma^2_t) \] Therefore, the conditional Value at Risk (VaR) with confidence level \(\alpha\) \[ \text{VaR}_{t \mid t-1}^{\alpha} = \mu_t + \sqrt{\sigma^2_t} q(\alpha) \] where \(q(\alpha)\) is the quantile with probability \(\alpha\) of a standard normal.

26.1 Test on the number of violations

Let’s define a violation of the conditional VaR with level \(\alpha\) as \[ V_t = \mathbb{1}_{[Y_t \le \text{VaR}_{t \mid t-1}^{\alpha}]} \] The number of violations \(V_t\) are Bernoulli distributed, i.e.  \[ V_t \sim \text{Bernoulli}(\alpha) \text{,} \] with mean and variance equal to \[ \mathbb{E}\{V_t\} = \alpha \text{,}\quad \mathbb{V}\{V_t\} = \alpha (1-\alpha) \]

26.1.1 Asymptotic variance

If the residuals are IID, then we can standardize \(V_t\) and apply the central limit theorem (CLT) to obtain that \[ \text{T}_1^{\alpha} = \frac{1}{\sqrt{n}} \sum_{i = 1}^{n} \left(\frac{V_i - \alpha}{\sqrt{\alpha(1-\alpha)}} \right) \underset{n\to\infty}{\overset{\text{d}}{\longrightarrow}} \mathcal{N}(0, 1) \text{,} \] converges in distribution to a standard normal. Under the null hypothesis \(\mathcal{H}_0\), i.e.  \[ \mathcal{H}_0: \mathbb{P}\{Y_t \le \text{VaR}_{t|t-1}^{\alpha}) = \alpha \] that is equivalent to say that the VaR is correctly specified we reject \(\mathcal{H}_0\) if \[ \mathcal{H}_0 \text{ is rejected} \iff \text{T}_1^{\alpha} < q_{\alpha/2} \text{,}\quad\text{OR}\quad \text{T}_1^{\alpha} > |q_{\alpha/2}| \] where \(q_{\alpha}\) is the quantile with level \(\alpha \in (0,1)\) of a standard normal.

26.1.2 Empirical variance

Let’s define the total number of violations of the conditional VaR as \[ S_n = \sum_{i = 1}^{n} V_i \text{,} \] and let’s use it to estimate the empirical variance of the number of violation. Instead of using the theoretical variance \(\alpha(1-\alpha)\), let’s compute \[ \alpha(1-\alpha) \longrightarrow \frac{S_n}{n} \left(1 - \frac{S_n}{n} \right) \text{.} \] Hence, we obtain a new statistic \[ \text{T}_2^{\alpha} = \frac{1}{\sqrt{n}} \sum_{i = 1}^{n} \left(\frac{V_i - \alpha}{\sqrt{\frac{S_n}{n} \left(1 - \frac{S_n}{n} \right)}} \right) \underset{n\to\infty}{\overset{\text{p}}{\longrightarrow}} \text{T}_1^{\alpha} \underset{n\to\infty}{\overset{\text{d}}{\longrightarrow}} \mathcal{N}(0, 1) \text{,} \] that converges in probability to \(\text{T}_1^{\alpha}\) and therefore in distribution to a standard normal. In general, the relation between \(\text{T}_1^{\alpha}\) and \(\text{T}_2^{\alpha}\) is \[ \text{T}_2^{\alpha} = \frac{\sqrt{\alpha(1-\alpha)}}{\sqrt{\frac{N_t}{t} \left(1 - \frac{N_t}{t} \right)}} \text{T}_1^{\alpha} \text{.} \] Under the null hypothesis \(\mathcal{H}_0\), i.e.  \[ \mathcal{H}_0: \mathbb{P}\{Y_t \le \text{VaR}_{t|t-1}^{\alpha}) = \alpha \] we reject \(\mathcal{H}_0\) if \[ \mathcal{H}_0 \text{ is rejected} \iff \text{T}_2^{\alpha} < q_{\alpha/2} \text{,}\quad\text{OR}\quad \text{T}_2^{\alpha} > |q_{\alpha/2}| \] where \(q_{\alpha}\) is the quantile with level \(\alpha \in (0,1)\) of a standard normal.

Example 26.2 Let’s consider an AR(2)-GARCH(1,2) process of the form \[ \begin{aligned} & {} Y_t = \mu + \phi_1 Y_{t-1} + \phi_2 Y_{t-2} + e_t \\ & e_t = \sigma_t u_t \\ & \sigma_t^2 = \omega + \alpha_1 e_{t-1}^{2} + \beta_1 \sigma_{t-1}^{2} + \beta_2 \sigma_{t-2}^{2} \end{aligned} \] where \(u_t \sim \mathcal{t}(25)\), namely we simulate Student-t residuals with distribution close to the normal one.

AR(2)-GARCH(1,2) simulation
set.seed(1)   # random seed 
# *************************************************
#                      Inputs 
# *************************************************
# Number of steps ahead 
t_bar <- 5000 
# Long-term mean
mu <- 0.5
# AR parameters 
phi <- c(phi1 = 0.34, phi2 = 0.14)
# Intercept GARCH 
omega <- 0.4
# ARCH parameters
alpha <- c(alpha1=0.25)
# GARCH parameters
beta <- c(beta1=0.25, beta2=0.15)
# *************************************************
#                   Simulation 
# *************************************************
# Long term mean GARCH variance 
e_sigma2 <- omega / (1 - sum(alpha) - sum(beta))
# Long term mean AR
e_Xt <- mu / (1 - sum(phi))
# Initialization 
Xt <- rep(e_Xt, 2)
sigma2_t <- rep(e_sigma2, 2)
mu_t <- rep(e_Xt, 2)
# Simulated standardized residuals 
u_t <- rt(t_bar, 25)
# Simulated residuals 
eps <- u_t * sigma2_t
for(t in 3:t_bar){
  # ARCH component 
  sum_arch <- alpha[1]*eps[t-1]^2 
  # GARCH component 
  sum_garch <- beta[1]*sigma2_t[t-1] + beta[2]*sigma2_t[t-2] 
  # Conditional variance
  sigma2_t[t] <- omega + sum_arch + sum_garch
  # Simulated residuals 
  eps[t] <- sqrt(sigma2_t[t]) * u_t[t]
  # Conditional mean
  mu_t[t] <- mu + phi[1]*Xt[t-1] + phi[2]*Xt[t-2]
  # Simulated AR component 
  Xt[t] <- mu_t[t] + eps[t]
}

Then, we compute the VaR with \(\alpha = 0.05\) as in Example 26.1 using the quantile \(q_{\alpha}\) of a standard normal instead of the true distribution.

AR(2)-GARCH(1,2) VaR
# *************************************************
#                      Inputs 
# *************************************************
# confidence level 
alpha <- 0.05   
# *************************************************
# Quantile standard normal
q_alpha <- qnorm(alpha)
# Value at risk
VaR_alpha <- mu_t + sqrt(sigma2_t) * q_alpha
# Empiric quantile 
q_alpha_emp <- quantile(u_t, probs = alpha)
# Empiric Value at risk
VaR_emp <- mu + sqrt(sigma2_t) * q_alpha_emp
Figure 26.2: AR(2)-GARCH(1,2) simulation with theoric (red) VaR at \(\alpha = 0.05\).

Finally, let’s perform a test on the number of violations.

VaR test
# Violation of the VaR
Vt <- ifelse(Xt < VaR_alpha, 1, 0)
Vt[1:3] <- 0
# Theoric variance
v_theoric <- alpha*(1-alpha)
# Empiric variance
v_empiric <- (sum(Vt)/t_bar)*(1 - sum(Vt)/t_bar)
# Standardized number of violations
Sn <- sum((Vt - alpha)/sqrt(v_theoric))
# Statistic test (NV_1)
T1 <- (1/sqrt(t_bar)) * Sn
# Statistic test (NV_2)
T2 <- sqrt(v_theoric/v_empiric) * T1
# Rejection level 
t_alpha <- qnorm(alpha / 2)
\[n\] \[\alpha\] \[\frac{N_n}{n}\] \[t_{\alpha/2}\] \[\text{T}_1^{\alpha}\] \[\text{T}_2^{\alpha}\] \[t_{\alpha/2}\] \[\mathcal{H}_0(\text{T}_1)\] \[\mathcal{H}_0(\text{T}_2)\]
5000 5% 5.6% -1.96 1.947 1.845 1.96 Non-Rejected Non-Rejected
Table 26.2: Test for a Student-\(t\) with 25 degrees of freedom at \(\alpha = 0.05\) on the number of violations of the theoric VaR at \(\alpha = 0.05\).

Example 26.3 Let’s consider an AR(2)-GARCH(1,2) process of the form \[ \begin{aligned} & {} Y_t = \mu + \phi_1 Y_{t-1} + \phi_2 Y_{t-2} + e_t \\ & e_t = \sigma_t u_t \\ & \sigma_t^2 = \omega + \alpha_1 e_{t-1}^{2} + \beta_1 \sigma_{t-1}^{2} + \beta_2 \sigma_{t-2}^{2} \end{aligned} \] where \(u_t \sim \mathcal{t}(5)\), namely we simulate Student-t residuals with distribution far away with respect to the normal one.

AR(2)-GARCH(1,2) simulation
set.seed(1)   # random seed 
# *************************************************
#                      Inputs 
# *************************************************
# Number of steps ahead 
t_bar <- 5000 
# Long-term mean
mu <- 0.5
# AR parameters 
phi <- c(phi1 = 0.34, phi2 = 0.14)
# Intercept GARCH 
omega <- 0.4
# ARCH parameters
alpha <- c(alpha1=0.25)
# GARCH parameters
beta <- c(beta1=0.25, beta2=0.15)
# *************************************************
#                   Simulation 
# *************************************************
# Long term mean GARCH variance 
e_sigma2 <- omega / (1 - sum(alpha) - sum(beta))
# Long term mean AR
e_Xt <- mu / (1 - sum(phi))
# Initialization 
Xt <- rep(e_Xt, 2)
sigma2_t <- rep(e_sigma2, 2)
mu_t <- rep(e_Xt, 2)
# Simulated standardized residuals 
u_t <- rt(t_bar, 5)
# Simulated residuals 
eps <- u_t * sigma2_t
for(t in 3:t_bar){
  # ARCH component 
  sum_arch <- alpha[1]*eps[t-1]^2 
  # GARCH component 
  sum_garch <- beta[1]*sigma2_t[t-1] + beta[2]*sigma2_t[t-2] 
  # Conditional variance
  sigma2_t[t] <- omega + sum_arch + sum_garch
  # Simulated residuals 
  eps[t] <- sqrt(sigma2_t[t]) * u_t[t]
  # Conditional mean
  mu_t[t] <- mu + phi[1]*Xt[t-1] + phi[2]*Xt[t-2]
  # Simulated AR component 
  Xt[t] <- mu_t[t] + eps[t]
}

Then, we compute the VaR with \(\alpha = 0.05\) as in Example 26.1 using the quantile \(q_{\alpha}\) of a standard normal instead of the true distribution.

AR(2)-GARCH(1,2) VaR
# *************************************************
#                      Inputs 
# *************************************************
# confidence level 
alpha <- 0.05   
# *************************************************
# Quantile standard normal
q_alpha <- qnorm(alpha)
# Value at risk
VaR_alpha <- mu_t + sqrt(sigma2_t) * q_alpha
# Empiric quantile 
q_alpha_emp <- quantile(u_t, probs = alpha)
# Empiric Value at risk
VaR_emp <- mu + sqrt(sigma2_t) * q_alpha_emp
Figure 26.3: AR(2)-GARCH(1,2) simulation with theoric (red) VaR at \(\alpha = 0.05\).

Finally, let’s perform a test on the number of violations.

VaR test
# Violation of the VaR
Vt <- ifelse(Xt < VaR_alpha, 1, 0)
Vt[1:3] <- 0
# Theoric variance
v_theoric <- alpha*(1-alpha)
# Empiric variance
v_empiric <- (sum(Vt)/t_bar)*(1 - sum(Vt)/t_bar)
# Standardized number of violations
Sn <- sum((Vt - alpha)/sqrt(v_theoric))
# Statistic test (NV_1)
T1 <- (1/sqrt(t_bar)) * Sn
# Statistic test (NV_2)
T2 <- sqrt(v_theoric/v_empiric) * T1
# Rejection level 
t_alpha <- qnorm(alpha / 2)
\[n\] \[\alpha\] \[\frac{N_n}{n}\] \[t_{\alpha/2}\] \[\text{T}_1^{\alpha}\] \[\text{T}_2^{\alpha}\] \[t_{\alpha/2}\] \[\mathcal{H}_0(\text{T}_1)\] \[\mathcal{H}_0(\text{T}_2)\]
5000 5% 8.06% -1.96 9.928 7.949 1.96 Rejected Rejected
Table 26.3: Test for a Student-\(t\) with 5 degrees of freedom at \(\alpha = 0.05\) on the number of violations of the theoric VaR at \(\alpha = 0.05\).