# Why is the distribution of rand()^2 different than of rand()*rand()?

In Libre Office Calc, the rand() function is available, which chooses a random value between 0 and 1 from a uniform distribution. I'm a bit rusty on my probability, so when I saw the following behaviour, I was puzzled:

A = 200x1 column of rand()^2

B = 200x1 column of rand()*rand()

mean(A) = 1/3

mean(B) = 1/4

Why is mean(A) != 1/4?

• Because the expectation of the square of a random variable does not equal the square of its expectation. – Michael M Nov 20 '13 at 19:10
• If rand() operates like other similar operators then A is the same random number squared and B is two random numbers, multiplied. – Peter Flom Nov 20 '13 at 19:26
• I understand. Would be very helpful though if I could see the math spelled out, or linked to a resource that does this. – Jefftopia Nov 20 '13 at 19:30
• Simplifying the situation might help you see the point. Suppose Rand() were replaced by Int(2*Rand()): this takes on the values $0$ and $1$ with equal probabilities. There are two possibilities for its square and four possibilities for the product of two (independent) values: what happens when you work out their expectations? – whuber Nov 20 '13 at 20:00

It may be helpful to think of rectangles. Imagine you have the chance to get land for free. The size of the land will be determined by (a) one realization of the random variable or (b) two realizations of the same random variable. In the first case (a), the area will be a square with the side length being equal to the sampled value. In the second case (b), the two sampled values will represent width and length of a rectangle. Which alternative do you choose?

Let $\mathbf{U}$ be a realization of a positive random variable.

a) The expected value of one realization $\mathbf{U}$ determines the area of the square which is equal to $\mathbf{U}^2$. On average, the size of the area will be $$\mathop{\mathbb{E}}[\mathbf{U}^2]$$

b) If there are two independent realizations $\mathbf{U}_1$ and $\mathbf{U}_2$, the area will be $\mathbf{U}_1 \cdot \mathbf{U}_2$. On average, the size equals $$\mathop{\mathbb{E}}[\mathbf{U}_1 \cdot \mathbf{U}_2] = \mathop{\mathbb{E}^2}[\mathbf{U}]$$ since both realizations are from the same distribution and independent.

When we calculate the difference between the size of the areas a) and b), we obtain

$$\mathop{\mathbb{E}}[\mathbf{U}^2] - \mathop{\mathbb{E}^2}[\mathbf{U}]$$

The above term is identical to $\mathop{\mathbb{Var}}[\mathbf{U}]$ which is inherently greater or equal to $0$.

This holds for the general case.

In your example, you sampled from the uniform distribution $\mathcal{U}(0,1)$. Hence,

$$\mathop{\mathbb{E}}[\mathbf{U}] = \frac{1}{2}$$ $$\mathop{\mathbb{E}^2}[\mathbf{U}] = \frac{1}{4}$$ $$\mathop{\mathbb{Var}}[\mathbf{U}] = \frac{1}{12}$$

With $\mathop{\mathbb{E}}[\mathbf{U}^2] = \mathop{\mathbb{Var}}[\mathbf{U}] + \mathop{\mathbb{E}^2}[\mathbf{U}]$ we obtain $$\mathop{\mathbb{E}}[\mathbf{U}^2] = \frac{1}{12} + \frac{1}{4} = \frac{1}{3}$$

These values were derived analytically but they match the ones you obtained with the random number generator.

• For an arbitrary $a$ and $b$, I get $\frac{a^{2}+ab+b^{2}}{3}$ – John Nov 20 '13 at 21:20
• That's a clever usage of variance. And here I was about to bang out the math directly. – Affine Nov 20 '13 at 21:21
• This makes sense to me. It all hinges on variance being non-negative. I'm also curious as to how John got his answer. – Jefftopia Nov 20 '13 at 21:27
• Basically just followed what Sven did, but replaced them with the formulas for a more general uniform distribution. – John Nov 20 '13 at 21:47
• Shouldn't $\mathop{\mathbb{E}}[\mathbf{U}^2] - \mathop{\mathbb{E}}[\mathbf{U}^2]$ read $\mathop{\mathbb{E}}[\mathbf{U}^2] - \mathop{\mathbb{E}^2}[\mathbf{U}]$? – BoppreH Nov 21 '13 at 0:47

Not to suggest that there's anything lacking from Sven's excellent answer, but I wanted to present a relatively elementary take on the question.

Consider plotting the two components of each product in order to see that the joint distribution is very different.

Note that the product tends only to be large (near 1) when both components are large, which happens much more easily when the two components are perfectly correlated rather than independent.

So for example, the probability that the product exceeds $1-\epsilon$ (for small $\epsilon$) is about $\epsilon/2$ for the $U^2$ ('A') version, but for the $U_1\times U_2$ ('B') version it's about $\epsilon^2/2$.

Quite a difference!

It may help to draw iso-product contours on graphs like those above - that is, curves where xy=constant for values like 0.5, 0.6, 0.7, 0.8, 0.9. As you go to larger and larger values, the proportion of points above and to the right of the contour goes down much more quickly for the independent case.