R and Python Chapter 4. Risk Measures

# Chapter 4. Risk Measures

### R and Python

Copyright 2011 - 2023 Jon Danielsson. This code is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. The GNU General Public License is available at: www.gnu.org/licenses.

##### ES in R
p = c(0.5,0.1,0.05,0.025,0.01,0.001)
VaR = -qnorm(p)
ES = dnorm(qnorm(p))/p
cat("Probabilities:", paste0(p*100,"%"), "\n",
"VaR:", VaR, "\n",
"ES:", ES)

##### ES in Python
from scipy import stats
p = [0.5, 0.1, 0.05, 0.025, 0.01, 0.001]
VaR = -stats.norm.ppf(p)
ES = stats.norm.pdf(stats.norm.ppf(p))/p
for i in range(len(p)):
print("VaR " + str(round(p[i]*100,3)) + "%: " + str(round(VaR[i],3)))
print("ES " + str(round(p[i]*100,3)) + "%: " + str(round(ES[i],3)), "\n")


##### Financial Risk Forecasting
Market risk forecasting with R, Julia, Python and Matlab. Code, lecture slides, implementation notes, seminar assignments and questions.