6.2 Non-Uniform Random Numbers
Uniform random numbers are useful, but usually we want to generate random numbers from some non-uniform distribution. There are a few ways to do this depending on the distribution.
6.2.1 Inverse CDF Transformation
The most generic method (but not necessarily the simplest) uses the inverse of the cumulative distribution function of the distribution.
Suppose we wnat to draw samples from a distribution with density \(f\) and cumulative distribution function \(F(x) = \int_{-\infty}^x f(t)\,dt\). Then we can do the following:
Draw \(U\sim \text{Unif}(0, 1)\) using any suitable PRNG.
Let \(X = F^{-1}(U)\). Then \(X\) is distributed according to \(f\).
Of course this method requires the inversion of the CDF, which is usually not possible. However, it works well for the Exponential(\(\lambda\)) distribution. Here, we have that \[ f(x) = \frac{1}{\lambda}e^{-x/lambda} \] and \[ F(x) = 1-e^{-x/\lambda}. \] Therefore, the inverse of the CDF is \[ F^{-1}(u) = -\lambda\log(1-u). \]
First we can draw our uniform random variables.
set.seed(2017-12-4)
<- runif(100)
u hist(u)
rug(u)
Then we can apply the inverse CDF.
<- 2 ## Exponential with mean 2
lambda <- -lambda * log(1 - u)
x hist(x)
rug(x)
The problem with this method is that inverting the CDF is usually a difficult process and so other methods will be needed to generate other random variables.
6.2.2 Other Transformations
The inverse of the CDF is not the only function that we can use to transform uniform random variables into random variables with other distributions. Here are some common transformations.
To generate Normal random variables, we can
Generate \(U_1, U_2\sim\text{Unif}(0, 1)\) using a standard PRNG.
Let \[\begin{eqnarray*} Z_1 & = & \sqrt{-2\log U_1}\cos(2\pi\, U_2)\\ Z_2 & = & \sqrt{-2\log U_1}\sin(2\pi\, U_2) \end{eqnarray*}\] Then \(Z_1\) and \(Z_2\) are distributed independent \(N(0, 1)\).
What about multivariate Normal random variables with arbitrary covariance structure? This can be done by applying an affine transformation to independent Normals.
If we want to generate \(X\sim\mathcal{N}(\mu, \Sigma)\), we can
Generate \(Z\sim\mathcal{N}(0, I)\) where \(I\) is the identity matrix;
Let \(\Sigma = LL^\prime\) be the Cholesky decomposition of \(\Sigma\).
Let \(X = \mu + Lz\). Then \(X\sim\mathcal{N}(\mu, \Sigma)\).
In reality, you will not need to apply any of the transformations described above because almost any worthwhile analytical software system will have these generators built in, if not carved in stone. However, once in a while, it’s still nice to know how things work.