Generating Continuous Random Variables¶
Generating Continuous Random Variables¶
To generate a random variable \(X\), We utilise it's cumulative distribution function \(F\). Note that \(F\) is strictly increasing from \(0\) to \(1\) and the probability density \(f\) and \(F\) have a one to one mapping, i.e., \(F\) has an inverse \(F^{-1}\). So, to get \(X\), we first generate \(U \in (0, 1)\) and then \(X = F^{-1}(U)\).
Exponential Random Variable¶
The distribution function \(F = 1 - exp(-\lambda x)\). Then, the inverse is
and \(X\) will follow the exponential distribution. Further, replacing \(U\) with \(1-U\) will still remain a uniform distribution, meaning \(X\) will still be exponential.
Normal Random Variable¶
Consider \(X, Y \sim \mathcal{N}(0, 1)\). Then,
is the joint distribution. \((X, Y)\) is a point on the cartesian plane and thus, will have an equivalent polar coordinate \((R, \Theta)\) implying \(R^{2} = X^{2} + Y^{2}\) which is a \(\chi_{2}^{2}\) variable. From relation between gamma and \(\chi_{2}^{2}\) variable,
Now, \(R\) and \(\Theta\) are independent variables and can be used to generate \(X\) and \(Y\). \(\Theta\) is uniformly distributed in \([0, 2\pi]\). Taking the inverse of cumulative distribution of \(R^{2}\) (Note that \(f_{R^{2}}(r)\) is a distribution on \(R^{2}\) and not \(R\)),
where \(U_{1}\) and \(U_{2}\) are uniform random variables. Using the transformation back to cartesian coordinates from polar ones,
This approach is called Box-Muller method. To generate standard normals with mean \(\mu\) and variance \(\sigma^{2}\), we simply return \(\mu + \sigma X, \mu+\sigma Y\).
Normal Random Variable from Uniform Distribution¶
Using the central limit theorem, we can add up multiple uniform distributions to get a normal random variable.
because the mean of 12 uniform random variables is \(12 * 0.5 = 6\) and the variance of sum of 12 independent random variables is \(12 \times (1-0)^{2}/12 = 1\). The same equation with 30 uniform distributions will become
and combining more variables will increase the accuracy of the approximation, but also increase computational time.