# compute Expected Shortfall / Conditional VaR from distribution

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")
plt.legend()
plt.show()
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}")


outputs

VAR: 4.354602809080357
ES: 3.9394240486966927
solES: -3.9372871924925747


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

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.