Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Image: ubuntu2004
Risk measures
Camilo A. Garcia Trillos - 2020
In this notebook
we learn some probability distributions available in scipy.stats.
we use the associated functions to calculate V@R and ES
we introduce a Monte Carlo estimator for both V@R and ES
Some distributions do not have easy formulas for their value at risk or expeted shortfall. We introduce here some functions and procedures in Python to approximate their value.
We start by importing the modules we use in this notebook. The package scipy.stats contains some statistical distributions and tests. We will use some of the functions on it.
PDF, CDF and Quantile functions
It is possible to calculate directly the PDF, CDF and quantile function for several distributions in Python, thanks to the scipy.stats library. This is achieved with the following general structure:
PDF: st.[name distribution].pdf(probability, parameters)
CDF: st.[name distribution].cdf(probability, parameters)
Quantile function: st.[name distribution].ppf(probability, parameters)
Let us illustrate with some examples.
Standard Gaussian
We recover the familiar bell-shaped pdf.
The plot illustrates the properties of the cdf: non-decreasing, with limits and and equal to 0 and 1 respectively.
Note how this is the reflection of the CDF plot around the x=y line.
Some numbers that appear commonly are associated to the quantile at levels 0.95 and 0.99 for the Gaussian distribution:
Other examples of continuous distributions
There are many more distributions than the ones above. Have a look at the help to know more.
Examples of discrete distributions
In the case of discrete distributions, there is no pdf function. Instead, we have a probability function () which is calculated in the scipy.stats package using the following structure:
st.[name distribution].pmf(probability, parameters)
The CDF and quantile are calculated as for the continuous case.
Let us see one example with the Poisson distribution. Recall that if is distributed Poisson with parameter ,
Calculating V@R, ES using quantiles
Let us implement two functions that receive a (static) instance of a probability distribution from scipy.stats, and return V@R and ES.
Value at risk
Taking to be a random variable denoting, for example, profit and losses, we can easily calculate Value at Risk whenever is distributed following one of the distributions in scipy.stats, by using the quantile function.
We can show that (see lecture notes that) for some . Note that in the case of a continuous distribution we get
Let us see some examples:
We got the value we were expecting. Let us now check the case of a bernoulli random variable with .
Expected shortfall
Recall (see the lecture notes) that
where .
We are going to use the method 'expect' associated to a given distribution in scipy.stats. Look at the help of this function to understand more, but here are two examples of use:
We can use this available fucntion to define our own function to calculate expected shortfall.
As can be seen, there is a small difference due to the approximation error in the function "expect", but the approximation is very good.
Let us compare both value at risk and expected shortfall for different values
We can verify visually several properties:
Expected shortfall is always larger or equal than value at risk
Expected shortfall tends to when .
Let us now check the case of a Bernoulli random variable with .
This is an example that shows that expected shortfall is more regular with respect to the level. Here it is continuous in alpha while value at risk is not.
Monte Carlo approximations
A Monte Carlo approximation might be useful either to replace the calculation of integrals for expected shortfall as above, or to calculate both value at risk and expected shortfall in cases where we do not have an explicit expression for the pdf.
The idea is simply replace the calculation of expected shortfall or value at risk on the distribution bu that of the empirical measure coming from a large sample of the distribution.
Suppose that are all i.i.d. samples of the same distribution in . Let us also denote by this sample after ordering (i.e. if and only if ). Then we have that
where .
Remark: Naturally, this is not the only estimator, There are other choices of estimator that will be presented later in the lecture notes.
Let us define a function that calculate the approximation of value at risk and expected shortfall for any sample
We can test by comparing in cases as the ones we had before. Remember the use of the random number generator.
We start with the standard normal case.
Comparing with the values before (3.6526957480816815, 4.330428440691612), this is not a bad approximation. Note, though that we required a large sample (here, 1000000). A larger sample might be required for higher levels in VAR and ES.
Also, remember that this estimator is random (this is why we had to fix the seed of the generator). You can check this by removing the seed.
Let us see also the case of the Bernoulli(0.75).
Again, we get close enough values to the ones defined before.
Exercises
Plot the values of value at risk for value at risk for a t-distribution as a function of the number of degrees of freedom. Repeat the exercise with expected shortfall. What do you observe?
Assume follows a standard Gaussian. Write a function such that . Test the function. What is the value associated to 0.975?
Assume that you have invested £100, equally, in two investments with P&L given respectively by a) A Gaussian random variable with mean 60 and sd 100; and b) An exponential random variable with mean 75. Assume further that both are independent.
Calculate the value at risk and expected shortfall of your total P&L.