Get Started

Comparing CVaR and VaR

The conditional value at risk (CVaR), or expected shortfall (ES), asks what the average loss will be, conditional upon losses exceeding some threshold at a certain confidence level. It uses VaR as a point of departure, but contains more information because it takes into consideration the tail of the loss distribution.

You'll first compute the 95% VaR for a Normal distribution of portfolio losses, with the same mean and standard deviation as the 2005-2010 investment bank portfolio_losses. You'll then use the VaR to compute the 95% CVaR, and plot both against the Normal distribution.

The portfolio_losses are available in your workspace, as well as the norm Normal distribution from scipy.stats.

This is a part of the course

“Quantitative Risk Management in Python”

View Course

Exercise instructions

  • Compute the mean and standard deviation of portfolio_losses and assign them to pm and ps, respectively.
  • Find the 95% VaR using norm's .ppf() method--this takes arguments loc for the mean and scale for the standard deviation.
  • Use the 95% VaR and norm's .expect() method to find the tail_loss, and use it to compute the CVaR at the same level of confidence.
  • Add vertical lines showing the VaR (in red) and the CVaR (in green) to a histogram plot of the Normal distribution.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Compute the mean and standard deviation of the portfolio returns
pm = portfolio_losses.____
ps = portfolio_losses.____

# Compute the 95% VaR using the .ppf()
VaR_95 = norm.ppf(0.95, loc = ____, scale = ____)
# Compute the expected tail loss and the CVaR in the worst 5% of cases
tail_loss = norm.____(lambda x: x, loc = ____, scale = ____, lb = VaR_95)
CVaR_95 = (1 / (1 - 0.95)) * ____

# Plot the normal distribution histogram and add lines for the VaR and CVaR
plt.hist(norm.rvs(size = 100000, loc = pm, scale = ____), bins = 100)
plt.axvline(x = VaR_95, c='r', label = "VaR, 95% confidence level")
plt.axvline(x = ____, c='g', label = "CVaR, worst 5% of outcomes")
plt.legend(); plt.show()

This exercise is part of the course

Quantitative Risk Management in Python

AdvancedSkill Level
3.8+
5 reviews

Learn about risk management, value at risk and more applied to the 2008 financial crisis using Python.

Now it’s time to expand your portfolio optimization toolkit with risk measures such as Value at Risk (VaR) and Conditional Value at Risk (CVaR). To do this you will use specialized Python libraries including pandas, scipy, and pypfopt. You’ll also learn how to mitigate risk exposure using the Black-Scholes model to hedge an options portfolio.

Exercise 1: Measuring RiskExercise 2: VaR for the Normal distributionExercise 3: Comparing CVaR and VaR
Exercise 4: Which risk measure is "better"?Exercise 5: Risk exposure and lossExercise 6: What's your risk appetite?Exercise 7: VaR and risk exposureExercise 8: CVaR and risk exposureExercise 9: Risk management using VaR & CVaRExercise 10: VaR from a fitted distributionExercise 11: Minimizing CVaRExercise 12: CVaR risk management and the crisisExercise 13: Portfolio hedging: offsetting riskExercise 14: Black-Scholes options pricingExercise 15: Options pricing and the underlying assetExercise 16: Using options for hedging

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