Measuring Risk

Quantitative Risk Management in Python

Jamsheed Shorish

CEO, Shorish Research

The Loss Distribution

  • Forex Example:
    • Portfolio value in U.S. dollars is USD 100
    • Risk factor = EUR / USD exchange rate
    • Portfolio value in EURO if 1 EUR = 1 USD: USD 100 x EUR 1 / USD 1 = EUR 100.
    • Portfolio value in EURO if r EUR = 1 USD: = USD 100 x EUR r / 1 USD = EUR 100 x r
    • Loss = EUR 100 - EUR 100 x r = EUR 100 x (1 - r)
  • Loss distribution: Random realizations of r => distribution of portfolio losses in the future

Histogram of forex losses

Quantitative Risk Management in Python

Maximum loss

  • What is the maximum loss of a portfolio?
  • Losses cannot be bounded with 100% certainty
  • Confidence Level: replace 100% certainty with likelihood of upper bound
  • Can express questions like "What is the maximum loss that would take place 95% of the time?"
    • Here the confidence level is 95%.
Quantitative Risk Management in Python

Value at Risk (VaR)

  • VaR: statistic measuring maximum portfolio loss at a particular confidence level
  • Typical confidence levels: 95%, 99%, and 99.5% (usually represented as decimals)
  • Forex Example: If 95% of the time EUR / USD exchange rate is at least 0.40, then:
    • portfolio value is at least USD 100 x 0.40 EUR / USD = EUR 40,
    • portofio loss is at most EUR 40 - EUR 100 = EUR 60,
    • so the 95% VaR is EUR 60.

Time series of EUR/USD exchange rate with 0.40 cutoff

Quantitative Risk Management in Python

Conditional Value at Risk (CVaR)

  • CVaR: measures expected loss given a minimum loss equal to the VaR
  • Equals expected value of the tail of the loss distribution:
    • $$\textnormal{CVaR}(\alpha) := \frac{1}{1-\alpha}\mathbb{E} \int_{\textnormal{VaR}(\alpha)}^{\bar{x}} x f(x) dx,$$
    • $f(\cdot)$ = loss distribution pdf
    • $\bar{x}$ = upper bound of the loss (can be infinity)
    • $\textnormal{VaR}(\alpha)$ = VaR at the $\alpha$ confidence level.
  • Forex Example:
    • 95% CVaR = expected loss for 5% of cases when portfolio value smaller than EUR 40

Plot of the loss distribution, showing the VaR and CVaR tail dependence

Quantitative Risk Management in Python

Deriving the VaR

  1. Specify confidence level, e.g. 95% (0.95)
  2. Create Series of loss observations
  3. Compute loss.quantile() at specified confidence level
  4. VaR = computed .quantile() at desired confidence level

  5. scipy.stats loss distribution: percent point function .ppf() can also be used

loss = pd.Series(observations)

VaR_95 = loss.quantile(0.95) print("VaR_95 = ", VaR_95)
Var_95 = 1.6192834157254088
Quantitative Risk Management in Python

Deriving the CVaR

  1. Specify confidence level, e.g. 95% (0.95)
  2. Create or use sample from loss distribution
  3. Compute VaR at a specified confidence level, e.g. 0.95.
  4. Compute CVaR as expected loss (Normal distribution: scipy.stats.norm.expect() does this).
losses = pd.Series(scipy.stats.norm.rvs(size=1000))

VaR_95 = scipy.stats.norm.ppf(0.95)
CVaR_95 = (1/(1 - 0.95))*scipy.stats.norm.expect(lambda x: x, lb = VaR_95)
print("CVaR_95 = ", CVaR_95)
CVaR_95 = 2.153595332530393
Quantitative Risk Management in Python

Visualizing the VaR

  • Loss distribution histogram for 1000 draws from N(1,3)

Histogram of normally-distributed losses

Quantitative Risk Management in Python

Visualizing the VaR

  • Loss distribution histogram for 1000 draws from N(1,3)
    • VaR$_{95}$ = 5.72, i.e. VaR at 95% confidence

Histogram of losses with the 95% VaR

Quantitative Risk Management in Python

Visualizing the VaR

  • Loss distribution histogram for 1000 draws from N(1,3)
    • VaR$_{95}$ = 5.72, i.e. VaR at 95% confidence
    • VaR$_{99}$ = 7.81, i.e. VaR at 99% confidence

Histogram of losses with the 95% and 99% VaR

Quantitative Risk Management in Python

Visualizing the VaR

  • Loss distribution histogram for 1000 draws from N(1,3)
    • VaR$_{95}$ = 5.72, i.e. VaR at 95% confidence
    • VaR$_{99}$ = 7.81, i.e. VaR at 99% confidence
    • VaR$_{99.5}$ = 8.78, i.e. VaR at 99.5% confidence
  • The VaR measure increases as the confidence level rises

Histogram of losses with the 95%, 99% and 99.5% VaR

Quantitative Risk Management in Python

Let's practice!

Quantitative Risk Management in Python

Preparing Video For Download...