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?


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$.


1 Answer 1


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.

  • $\begingroup$ Thank you Mark. Then how do I calculate the expected value analytically, if mine was wrong? $\endgroup$
    – Saju
    Jul 4, 2015 at 16:37
  • $\begingroup$ @Saju maybe first tell us how did you calculate it..? $\endgroup$
    – Tim
    Jul 4, 2015 at 16:43
  • $\begingroup$ math.stackexchange.com/questions/48822/… $\endgroup$ Jul 4, 2015 at 16:48
  • 1
    $\begingroup$ @Tim I calculated the geometric probability for 0.5 point(finding a, where |X-Y| is larger than value "a" by the probability of 0.5). (10*10 - (10-a)^2 )/(10*10) = 0.5 However, now it seems it is not the expected value. $\endgroup$
    – Saju
    Jul 4, 2015 at 17:32
  • 1
    $\begingroup$ That is the median. The median does not equal the mean when the distribution is not symmetric. |X-Y| where X and Y are independent Uniform[0,10] is not symmetric.Look at the link I provided in the comment above to see how to calculate E(|X-Y|). If you don't understand that, you need to read a probability book or take a course in introductory calculus-based probability. $\endgroup$ Jul 4, 2015 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.