Home Academia Contact Archive

PaC Exercise 3.1

Posted on February 18, 2021
import numpy as np

Exercise 3.1

Let X be a number chosen uniformly at random from [1, n]. Find Var(X).

Solution: We have


$$ E[X] = \sum_{i=1}^n i P(X=i) = \frac 1 n \sum_{i=1}^n i = \frac 1 n \frac{n(n+1)}{2} = \frac{n+1}{2}, $$

and


$$ E[X^2] = \sum_{i=1}^n i^2 P(X^2=i) = \frac 1 n \sum_{i=1}^n i^2 = \frac 1 n \frac{n(n+1)(2n+1)}{6} = \frac{(n+1)(2n+1)}{6}. $$


$$ \mathrm{Var}(X) = E[X^2] - E[X]^2 = \frac{n^2-1}{12}. $$

For instance, Var(X) = 60.66… for n = 27.

We verify this result with simple sampling.

num_samples = 10**7
n = 27

samples = np.random.randint(low=1, high=n+1, size=num_samples)

np.mean((samples - samples.mean())**2)
60.66757909209802