Building random variable with given variance and expectation

34 Views Asked by At

I'm given variance V, expectation E and number n - amount of values random variable takes. Is there any general algorithm to construct discrete random variable with such variance and expectation? (probabilies of each outcome is a rational number)

Code implementation is allowed too :)

1

There are 1 best solutions below

2
On

Take any discrete random variable $X$ which takes $n$ different values with mean $\mu$ and variance $\sigma^2$. For instance you can take $X$ uniformly distributed on $\{0,1,\cdots,n-1\}$ which has mean $\mu=\frac{n-1}{2}$ and variance $\sigma^2=\frac{n^2-1}{12}$.

Then $Y=E+\sqrt V\frac{X-\mu}{\sigma}$ has mean $E$, variance $V$ and takes $n$ different values.