Get Started

Historical value at risk

Drawdown is a measure of sustained losses over time, but what about simple single-day movements?

Value at Risk, often referred to as VaR, is a way to estimate the risk of a single day negative price movement. VaR can be measured for any given probability, or confidence level, but the most commonly quoted tend to be VaR(95) and VaR(99). Historical VaR is the simplest method to calculate VaR, but relies on historical returns data which may not be a good assumption of the future. Historical VaR(95), for example, represents the minimum loss that your portfolio or asset has sustained in the worst 5% of cases.

Below, you will calculate the historical VaR(95) of the USO oil ETF. Returns data is available (in percent) in the variable StockReturns_perc.

This is a part of the course

“Introduction to Portfolio Risk Management in Python”

View Course

Exercise instructions

  • Calculate VaR(95), the worst 5% of USO returns (StockReturns_perc), and assign it to var_95.
  • Sort StockReturns_perc and assign it to sorted_rets.
  • Plot the histogram of sorted returns (sorted_rets).

Hands-on interactive exercise

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

# Calculate historical VaR(95)
var_95 = ____(StockReturns_perc, ____)
print(var_95)

# Sort the returns for plotting
sorted_rets = ____

# Plot the probability of each sorted return quantile
____(sorted_rets, density=True, stacked=True)

# Denote the VaR 95 quantile
plt.axvline(x=var_95, color='r', linestyle='-', label="VaR 95: {0:.2f}%".format(var_95))
plt.show()
Edit and Run Code

This exercise is part of the course

Introduction to Portfolio Risk Management in Python

IntermediateSkill Level
4.5+
11 reviews

Evaluate portfolio risk and returns, construct market-cap weighted equity portfolios and learn how to forecast and hedge market risk via scenario generation.

In this chapter, you will learn two different methods to estimate the probability of sustaining losses and the expected values of those losses for a given asset or portfolio of assets.

Exercise 1: Estimating tail riskExercise 2: Historical drawdownExercise 3: Historical value at risk
Exercise 4: Historical expected shortfallExercise 5: VaR extensionsExercise 6: Changing VaR and CVaR quantilesExercise 7: Parametric VaRExercise 8: Scaling risk estimatesExercise 9: Random walksExercise 10: A random walk simulationExercise 11: Monte Carlo simulationsExercise 12: Monte Carlo VaRExercise 13: Understanding risk

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