Computing VaR and ES for normal distribution
The standard function qnorm() calculates quantiles of a normal distribution from the probability p, the mean, and standard deviation, and thus can be used to calculate value-at-risk (VaR). The function ESnorm() from the QRM package calculates the expected shortfall (ES) for a normal distribution from the probability p, location parameter mu, and scale parameter sd:
qnorm(p, mean = 0, sd = 1)
ESnorm(p, mu = 0, sd = 1)
Common numeric values for p include 0.95 and 0.99 for confidence levels of 95% and 99%, respectively.
In this exercise, you will compute and display VaR and ES for a normal distribution \(N(\mu, \sigma^2)\) with mean \(\mu\) and standard deviation \(\sigma\). In the process, you will use the new functions for sequence generation and adding straight lines to a plot. You can read about their arguments by typing in ?seq and ?abline to your console.
The variables mu and sigma contain the estimated mean and standard deviation of the Dow Jones index returns for 2008-2009 contained in djx. All three objects are available in your workspace.
This exercise is part of the course
Quantitative Risk Management in R
Exercise instructions
- Fill in
seq()to make a sequence of 100 x-values going from \(-4\sigma\) to \(4\sigma\) and assign toxvals. - Fill in
dnorm()to compute the density of a \(N(\mu, \sigma^2)\) distribution atxvalsand assign tondens. - Plot
ndensagainstxvalsusingtype = "l". - Use
qnorm()andESnorm()to compute the 99% VaR and 99% ES of the distribution and assign toVaR99andES99, respectively. - Fill in
abline()to create vertical lines forVaR99andES99in red and green, respectively.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Make a sequence of 100 x-values going from -4*sigma to 4*sigma
___ <- seq(from = -4*sigma, to = 4*sigma, length.out = ___)
# Compute the density of a N(mu, sigma^2) distribution at xvals
___ <- dnorm(___, mean = ___, sd = ___)
# Plot ndens against xvals
# Compute the 99% VaR and 99% ES of a N(mu, sigma^2) distribution
___ <- qnorm(___, mean = ___, sd = ___)
___ <- ESnorm(___, mu = ___, sd = ___)
# Draw vertical lines at VaR99 and ES99 in red and green
abline(v = ___, col = "red")
abline(v = ___, col = "green")