Solved – Getting Expected Value from Monte Carlo Simulation

expected valuemonte carlo

There are two independent uniform continuous random variables $X$ and $Y$ (such that $0 \leq X \leq 10$, $0 \leq Y \leq 10$). The function $f$ is the difference between the two random variables ($|X-Y|$).

What is the expected value of $f(X,Y)$?

My analytic solution was $10-\sqrt{50}$. I wanted to check my answer with Monte Carlo Simulation.

I randomly chose $1M$ numbers each for $X$ and $Y$ and calculated one million $f(X,Y)$. One intuitive way of getting the expected value is averaging the million values ($\approx$ 3.33). Another way is getting the median(the distributional balance) value ($\approx$ 2.93) among the million values.

The second one was close to my analytic solution.

Maybe I am confusing different concepts. What is the right way of calculating the expected value through the MC simulation? Is my analytic answer correct? If not, how do I calculate it analytically?


updated:

After some research, it seems like the correct analytic solution should be instead(for simplication, the uniform distributions of X, Y are U(0,1) instead of U(0,10):

$E(|X-Y|) = \int_{y=0}^{1}\int_{x=0}^{1}|x-y|2(1-|x-y|)dxdy = \frac{1}{3}$

Because the p.d.f. of Z is $2(1-|x-y|)$, which is derived from the convolution function between $X$ and $-Y$.

Best Answer

The right way of calculating the expected value of a function by Monte Carlo simulation is to calculate the (sample) average of the function value on all n (one million in your write up) replications. You should also look at the sample standard deviation divided by sqrt(n) or sqrt(n-1), to understand the uncertainty in your estimate.

You need to correctly generate the values of X and Y, accounting for dependency, if any, between X and Y. I.e., draw from their joint distribution. if they are independent, this amounts to drawing separately from their own distributions. You have specified Uniform([0,10]) distributions for X and Y, and based on the results of the Monte Carlo simulation, you have assumed they are independent.

Assuming independence of X and Y, then indeed your averaging is the correct thing to do to get expected value of abs(X-Y). Your analytical calculation of 10 - sqrt(5) does indeed correspond to the median of abs(X-Y), not its expected value

Plot the sorted values of abs(X-Y) from the simulation, and you'll see that the median is not equal to the expected value. You may be too used to symmetric distributions, and perhaps in particular the Normal distribution. Median = expected value in such cases, but not for abs(X-Y) in your case.

Related Question