Get Started

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 is a part of the course

“Quantitative Risk Management in R”

View Course

Exercise instructions

  • Fill in seq() to make a sequence of 100 x-values going from \(-4\sigma\) to \(4\sigma\) and assign to xvals.
  • Fill in dnorm() to compute the density of a \(N(\mu, \sigma^2)\) distribution at xvals and assign to ndens.
  • Plot ndens against xvals using type = "l".
  • Use qnorm() and ESnorm() to compute the 99% VaR and 99% ES of the distribution and assign to VaR99 and ES99, respectively.
  • Fill in abline() to create vertical lines for VaR99 and ES99 in 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")

This exercise is part of the course

Quantitative Risk Management in R

BeginnerSkill Level
4.8+
8 reviews

Work with risk-factor return series, study their empirical properties, and make estimates of value-at-risk.

In this chapter, the concept of value-at-risk and simple methods of estimating VaR based on historical simulation are introduced.

Exercise 1: Value-at-risk and expected shortfallExercise 2: Computing VaR and ES for normal distribution
Exercise 3: International equity portfolioExercise 4: Examining risk factors for international equity portfolioExercise 5: Historical simulationExercise 6: Estimating VaR and ESExercise 7: Option portfolio and Black ScholesExercise 8: Compute Black-Scholes price of an optionExercise 9: Equity and implied volatility risk factorsExercise 10: Historical simulation for the option exampleExercise 11: Historical simulation of losses for option portfolioExercise 12: Estimating VaR and ES for option portfolioExercise 13: Computing VaR for weekly lossesExercise 14: Wrap-up

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free