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
4. Expected utility and Monte Carlo in Python
Camilo A. Garcia Trillos - 2020
In this notebook,
we look at the Monte Carlo method and how to use it to approximate expected utilities or certainty equivalents.
we use Python to plot information using matplotlib, including a histogram and a regression
Let us import some packages: math, numpy, matplotlib and scipy
Expected utility via Monte Carlo
To compute the expected utility of a wealth gamble we can use he law of large numbers. Indeed, if , we have where is a family of independent draws of random variables with for each .
The Monte Carlo method relies on this equality to produce an approximation to the expectation (by choosing a large N and calculating the empirical average).
To see how this works, let us start with being normally distributed, that is , where and is standard normally distributed.
Now, let us suppose first we want to compute expected utility of a CARA utility . We can calculate explicitly
We use this value to compare to the value approximated by Monte Carlo as explained before. Let us build a plot of this function in some given domain.
Plotting the exact solution
There are several libraries allowing us to plot in Python. We will use one of the simplest: Matplotlib.
A simple way to plot in this library is to provide it with vectors of input and output. To try it, let us simply plot the result of the (exact) expected utility when the CARA coefficient changes.
We start by sampling the space of coefficients of risk aversion:
We now implement the exact solution expected CARA utility under normal assumptions. Since it is a simple expression, we can use a lambda function as introduced before.
Note that we use 'np.exp' and not 'math.exp': this is because we want the function to be 'vectorial', that is, to accept vectors as an input
(try changing np.exp for math.exp, run the code and then run the code below... there will be an error).
If for some reason you cannot implement directly a vectorial function, it is possible to use a loop or the function np.vectorize to render the function vector ready.
We are ready to make the plot:
MC implementation
Let us now look at the Monte Carlo approximation of the above function. We start by defining a function that calculates the CARA utility:
We can now generate a sample of wealths, distributed like a .
How can we check that these are Gaussian? We can plot the histogram of the empirical distribution defined. The package matplotlib has a convenient function for this: plt.hist (recall that plt is our alias for pyplot)
It looks like a good Gaussian sample with our parameters (centred in 5 and with standard deviation 2). In later notebooks we will learn some alternative ways for checking Gaussianity.
We can now calculate a Monte Carlo approximation of our expected utility. Examine the code below:
Observe now the following: the estimation is random. To see this, let us run the estimation with another sample
As expected, the two values are close but different. Indeed, this estimator is random, because it depends on the sample, which is itself random. This is something to be taken into account when using Monte Carlo estimators.
In fairness, the Python implementation of the MC estimator can only produce a pseudo-random generation. To see this, we can fix the seed of the pseudo-random generation algorithm and compare the answers
Setting the random states allows us to repeat the same sequence on the pseudo-random generation.
Now, let us remind ourselves of the closed form solution:
We see that the value is very close to the value(s) estimated via MC. Indeed, this error can be explained via the central limit theorem, which give us a control on the L_2 norm and is of the form
Let us verify this empirically, using a plot and a regression. We want to retrieve the rate of convergence, which is the power 1/2 in the above control. To do this we use a log-log plot (think why).
The hardest line of code in the plot above is possibly
Let us look at two parts in particular:
Means make a green dashed line.
while
means: take the value of exp(b), round it to a float with two decimal figures, do the same with m, and write a string that contains exp(b) N^ m with this format. This is saved on a variable label that is used by matplotlib to assign the legends in a plot.
Check that you understand the other lines of code.
Note that the best fit slope is close to -1/2 as expected. This is consistent with the theoretical error given before. Write the equations to be sure you understand why.
Exercise
Compute, via a Monte-Carlo simulation, the expected utility of a CRRA investor for the following gambles.
, where is standard normally distributed and .
where .
You might have to look up online the commands for the corresponding random number generators. (Use the ones in numpy.random).
Write a function that computes the certainty equivalent for a CRRA investor. (Hint: You might have to compute, on a piece of paper, for the different relative risk aversions .)
With and , plot the certainty equivalent of a CRRA investor as a function of relative risk aversion , using gamble .