Lecture 011

Generating Random Variable for Simulation

Using c.d.f.

We would like to generate any random variable given a distribution from a uniform random variable (u = \text{Uniform}(0, 1)). We also make following assumptions:

  1. we know the c.d.f. F_X(x) of X.
  2. we can easily invert F_X(x) so that we can get x from F_X(x)
x = \left(F_X^{-1}\right)(u) \text{ where } u \text{ is Uniform input}

Continuous

To generate random variable:

  1. Generate u \in \text{U}(0, 1)
  2. Run x = F_X^{-1}(u)

Example: generate X \sim \text{Exp}(\lambda)

\begin{align*} F(x) = u =& 1 - e^{-\lambda x}\\ 1 - e^{-\lambda x} =& u\\ -\lambda x =& \ln(1 - u)\\ x =& - \frac{1}{\lambda} \ln(1 - u) \text{ where } u \text{ is Uniform input}\\ \end{align*}

Discrete

To generate

X = \begin{cases} x_0 & \text{ with probability } p_0\\ x_1 & \text{ with probability } p_1\\ x_2 & \text{ with probability } p_2\\ x_3 & \text{ with probability } p_3\\ ... \end{cases}

We could have do:

  1. Arrange x_0, ..., x_k such that x_0 < x_1 < ... < x_k
  2. Generate x \in \text{U}(0, 1)
  3. If 0 \leq u \leq p_0, then output x_0
  4. If p_0 \leq u \leq p_0 + p_1, then output x_1
  5. If p_0 + p_1 \leq u \leq p_0 + p_1 + p_2, then output x_2
  6. If \sum_{i = 0}^{l - 1} p_i \leq u \sum_{i = 0}^l p_i, then output x_l

But computing partial sum \sum_{i = 0}^l p_i for all l is inefficient, so we still need F_X(u) and invertible and use the same method as continuous case.

Accept / Reject Method

Motivation: generate random variables for distribution in which F_X is unknown (like X \sim \text{Normal}(\mu, \sigma^2)) Idea: generate numbers and throw away some of them.

Discrete

Suppose we have uniform Q = \begin{cases} 1 & \text{with probability } q_1 = 0.33\\ 2 & \text{with probability } q_2 = 0.33\\ 3 & \text{with probability } q_3 = 0.33\\ \end{cases}, and want to generate P = \begin{cases} 1 & \text{with probability } p_1 = 0.20\\ 2 & \text{with probability } p_2 = 0.30\\ 3 & \text{with probability } p_3 = 0.50\\ \end{cases}.

Requirement:

Note that Q is uniform. But this requirement is not necessary.

Proposal 1:

  1. Generate j \in Q.
  2. We accept with probability p_j, go to step 1 otherwise.

Concerns with Proposal 1:

Proposal 2:

  1. Generate j \in Q.
  2. We accept with probability \frac{p_j}{q_j}, go to step 1 otherwise.

Concerns with Proposal 2:

Proposal 3:

  1. Generate j \in Q.
  2. Choose c \geq \max_j(\frac{p_j}{q_j}) (usually equal for efficiency)
  3. We accept with probability \frac{p_j}{c \cdot q_j}, go to step 1 otherwise.

Proof Correctness:

\begin{align*} Pr\{\text{Generate } j\} =& p_j\\ =& \frac{Pr\{j \text{ Generated and Accepted}\}}{Pr\{\text{Any Number Generated and Accepted}\}}\\ =& \frac{Pr\{j \text{ Generated}\} \cdot Pr\{j \text{ Accepted} | j \text{ Generated}\}}{\sum_j Pr\{j \text{ Generated and Accepted}\}}\\ =& \frac{q_j \cdot \frac{p_j}{c \cdot q_j}}{\sum_j \frac{p_j}{c}}\\ =& \frac{\frac{p_j}{c}}{\frac{1}{c}}\\ =& p_j\\ \end{align*}

Note that the probability of any value accepted is \frac{1}{c}. Therefore c = \max_j(\frac{p_j}{q_j}) is the expected number of iteration we need to generate one random value.

Continuous

Method: to generate X given Y

  1. Find Y s.t. f_Y(t) > 0 \iff f_X(t) > 0 (this requires finding the bound a \leq t \leq b when 0 < f_Y \leq 1) and generate t \in Y. For uniform, f_Y(t) = \frac{1}{b - a}
  2. Choose c = \max_t(\frac{f_X(t)}{f_Y(t)}). For uniform, c = \max_t((b - a)f_X(t)). Finding the max involved taking the first (and maybe second) derivative.
  3. We accept with probability \frac{f_X(t)}{c \cdot f_Y(t)}, go to step 1 otherwise.
Generating Normal Distribution

Idea:

Note that for distributions involve infinity, we can no longer use Y \sim \text{Uniform}(a, b) because uniform distribution can't extend to infinity with non-zero probability.

\begin{align*} f_Y(t) =& e^{-t} \tag{0 < t < \infty}\\ f_X(t) =& \frac{2}{\sqrt{2\pi}}e^{-\frac{1}{2}t^2} \tag{0 < t < \infty}\\ \end{align*}

We find c:

\begin{align*} c =& \max_t(\frac{f_X(t)}{f_Y(t)})\\ =& \max_t(\sqrt{\frac{2}{\pi}}e^{t - \frac{t^2}{2}}) \end{align*}

Taking the derivative to find maximum.

0 = \frac{d}{dt}\left(t - \frac{t^2}{2}\right) = 1 - t \implies t = 1

Which gives c = \frac{f_X(1)}{f_Y(1)} = 1.3

Generating Poisson Distribution

Challenges:

Table of Content