I want to compute the Expected Shortfall from a distribution of returns.

I have no closed solution for my distribution of returns, so I wonder if I can simply compute ES by taking the mean of all the values below a certain quantile. From my understanding, this seems to be what the analytical formula also does.

But the problem I have is that the output of the analytical formula for all combinations of mean and standard deviation is always the additive inverse of the numerical approximation. I suspect I somehow used the formula incorrectly. In any case, my main questions are:

  1. Can I compute the ES the way I do it in the code below? I.e. simply taking the mean of the values below the e.g 95% level? Does this work no matter my return distribution?
  2. What did i do wrong in the analytical formula?
# imports
import numpy as np
import matplotlib.pyplot as plt
import scipy; from scipy import stats

# generating the distribution 
mean = 6
std  = 1
dist = scipy.stats.norm.rvs(loc=mean, scale=std, size=1_000_000)

# computing VaR and ES: is this correct?
VAR = np.quantile(a=dist, q=0.05)
ES = np.mean(dist[np.where(dist < VAR)])

# plotting and printing
plt.hist(dist, bins=np.linspace(-20,20,100), density=True)
plt.vlines(x=VAR, ymin=0, ymax=.5, label="VAR 95", color="red")
plt.vlines(x=ES, ymin=0, ymax=.5, label="ES 95", color="green")
print(f"VAR: {VAR}")
print(f"ES: {ES}")

# verifying the ES analytically and printing aswell
def analytical_normal_es(mu, sigma, level):
    return -mu + sigma * scipy.stats.norm.pdf(x=scipy.stats.norm.ppf(level)) / level
solES = analytical_normal_es(mean,std,0.05)
print(f"solES: {solES}")


VAR: 4.354602809080357
ES: 3.9394240486966927
solES: -3.9372871924925747

enter image description here

  • 2
    $\begingroup$ ES is a loss measure. Simply take -1 times the average left tail. Yes, your approximation is sufficient. $\endgroup$ Commented Apr 7, 2022 at 17:33

1 Answer 1


Generally, VaR and ES can been seen from two different points of view:

  1. $R_t$ are portfolio returns. Then VaR is left quantile (e.g. 0.05) and ES is expected return below this quantile.

  2. $L_t = -R_t$ are portfolio losses. Then VaR is right quantile (e.g. 0.95) and ES is expected loss above this quantile.

Whenever analytical formulas exist, difference between them should be up to +- sign.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.