You are on page 1of 1

Lab 10, Monte Carlo Integration

Familiarise yourself with the following MatLab Functions:


exp(); rand(); mean(); std(); var(); median(); quantile();
histogram(); plot(); if-then-else; while;

Topics
The Gamma distribution, Gamma(k, ), is a generalisation of the exponential distribution (this is because
Gamma(1, ) = E()). The pdf of Gamma(k, ) is given by
( k

xk1 ex : if x 0
f (x) = (k)
0 : otherwise.

If X Gamma(k, ), then the expectation and variance of X are given by E[X] = k and V[X] = k2
respectively. Note that, for positive integers k, (k) = (k 1)!. The function (k) can be evaluated with
the MatLab command: gamma(k).

Tasks
Consider X Gamma(2, 1). We will use simulation methods to see what the median of X is.

1. Create a MatLab function for evaluating f (x). Use your function to plot the pdf of X.
2. We are going to perform rejection sampling on Gamma(2, 1). Let the proposal distribution be the
uniform distribution, U(a, b), with a pdf of g(x). The lower bound of this interval should be a = 0.
Select a reasonable upper bound, b, for this interval so that P(X > b) is neglectably small (choose b so
that f (x) < 0.0005 for all x > b). Write down the pdf function g(x).

3. For your interval, [0, b], select the smallest possible value m such that f (x) < mg(x) for all 0 x b.
4. Perform rejection sampling to generate 1000 observations from the distribution Gamma(2, 1). How
many times did your algorithm sample from U(a, b)?
5. Plot a histogram of your 1000 observations. Plot the pdf of Gamma(2, 1) on the same set of axes and
compare it to the histogram.
6. Calculate the sample mean and sample variance of your 1000 observations. Are they close to the
population mean and population variance?
, for the median of X.
7. Use your sample to compute an estimate, M
]) to estimate the area under
8. Perform MC integration (using 5000 new samples on the interval [0, M

the Gamma(2, 1)-curve between 0 and M . How close is your estimate to 0.5?

You might also like