Generating Random Variables For Simulation: 13.1 Inverse Transform Method
Generating Random Variables For Simulation: 13.1 Inverse Transform Method
1 Actually, most operating systems provide a random integer between 1 and 𝑁 = 232 − 1. This is
easy to convert into a Uniform(0, 1) by just dividing by 𝑁 .
2 One cannot always trust the random number generator provided by one’s operating system. It is
worth reading the literature on what guarantees different random number generators provide and
on how to “seed” the random number generator [10].
Idea: Let 𝑢 be our random instance from 𝑈 (0, 1). We want to map 𝑢 to 𝑥, where
𝑥 is an instance of the r.v. 𝑋. The key point is that the 𝑥 that we output needs to
be consistent with the distribution of 𝑋.
Let’s suppose there is some mapping which takes each 𝑢 and assigns it a unique
𝑥. Such a mapping is illustrated by 𝑔 −1 (·) in Figure 13.1. Here, the y-axis shows
𝑢, between 0 and 1, being mapped to an 𝑥 on the x-axis between 0 and ∞.
U(0,1)
g
1
X
0 x
Question: Can you figure out what the mapping, 𝑔 −1 (·), should be?
Hint: Think about what property we want for our output. What should be the
probability of outputting a value between 0 and 𝑥?
Question: What is the actual probability that 𝑔 −1 (·) outputs a value in (0, 𝑥)?
Answer: Because 𝑔 −1 (·) only maps values in (0, 𝑢) to values in (0, 𝑥), the
probability of outputting a value in (0, 𝑥) is the probability that the uniform
instance is in (0, 𝑢).
Question: And what is the probability that the uniform instance is in (0, 𝑢)?
Answer: 𝑢.
So we want that
𝑢 = P {0 < 𝑈 < 𝑢} = P {0 < 𝑋 < 𝑥} = 𝐹𝑋 (𝑥).
The discrete case follows the same basic idea as the continuous case (see Fig-
ure 13.2). This time, we want to generate a discrete r.v., 𝑋, with the following
probability mass function (p.m.f.):
𝑥0 w/prob 𝑝 0
w/prob 𝑝 1
𝑥1
𝑋= .
...
w/prob 𝑝 𝑘
𝑥𝑘
U(0,1)
1
p3
p2
p1
Êp0
x0 x1 x2 x3 X
0
Notice that again our 𝑔(·) function, shown in blue in Figure 13.2, is 𝐹𝑋 (·), the
c.d.f.
This sounds easy enough, but it is not always practical. If 𝑋 can take on many
Í
values, then we have to compute many partial sums: ℓ𝑖=0 𝑝 𝑖 for all 0 ≤ ℓ ≤ 𝑘.
For this method to be practical, we therefore need closed-form expressions for
Íℓ
𝑖=0 𝑝 𝑖 for all ℓ. Equivalently, we need a closed form for 𝐹𝑋 (𝑥) = P {𝑋 ≤ 𝑥} for
any 𝑥. Then we could do the same thing as in the continuous case, as in (13.1):
generate 𝑢 ∈ 𝑈 (0, 1), and set 𝑥 = 𝐹𝑋−1 (𝑢), where a ceiling may be necessary
since 𝑥 is discrete. Thus, as in the continuous case, we need to both have a
closed-form expression for the c.d.f. and also know how to invert this function.
The Inverse Transform method required both knowing the c.d.f., 𝐹𝑋 (𝑥), of the
r.v. 𝑋 that we’re trying to generate, and also being able to invert 𝐹𝑋 (𝑥). However,
there are many cases where we aren’t able to satisfy both of these requirements.
1. We need the p.d.f., 𝑓 𝑋 (𝑡) (or p.m.f.) of the r.v. 𝑋 that we’re trying to generate.
2. We need to know how to generate some other r.v. 𝑌 , where 𝑌 and 𝑋 take on
the same set of values, that is,
𝑓 𝑋 (𝑡) > 0 ⇐⇒ 𝑓𝑌 (𝑡) > 0.
Here’s the algorithm for a discrete r.v. 𝑋, with p.m.f. 𝑝 𝑋 (𝑖) = P {𝑋 = 𝑖}.
Question: On average, how many values of 𝑌 are generated before one is ac-
cepted?
The Accept–Reject method works the same way for continuous random variables,
except that we now use the p.d.f. instead of the p.m.f.
Similarly to the Accept–Reject algorithm for the discrete case, we can show that:
Density of returning 𝑡 on an iteration = Density of generating 𝑡 · P {accept 𝑡}
𝑓 𝑋 (𝑡)
= 𝑓𝑌 (𝑡) ·
𝑐 · 𝑓𝑌 (𝑡)
1
= 𝑓 𝑋 (𝑡) · .
𝑐
Hence,
∫
1 1
P {Return some value on a given iteration} = 𝑓 𝑋 (𝑡) · 𝑑𝑡 = ,
𝑡 𝑐 𝑐
so the expected number of iterations needed to get an instance of 𝑋 is 𝑐.
Example 13.6 Generate r.v. 𝑋 with p.d.f. 𝑓 𝑋 (𝑡) = 20𝑡 (1 − 𝑡) 3 , 0 < 𝑡 < 1.
If you plot 𝑓 𝑋 (𝑡), it looks like Figure 13.3. Observe that 𝑋 has positive p.d.f.
only in the interval (0, 1). Thus we want to choose a 𝑌 that is easy to generate
and also has positive p.d.f. only in (0, 1).
Question: Suppose we now apply the Accept–Reject method. What will 𝑐 be?
Answer: Based on the plot, 𝑐 should not be too bad – just over 2. To determine
𝑐 precisely, we want to determine
𝑓 𝑋 (𝑡)
= max 20𝑡 (1 − 𝑡) 3 .
max
𝑡 𝑓𝑌 (𝑡) 𝑡
f X (t)
2.5
2.0
1.5
1.0
0.5
t
0 0.2 0.4 0.6 0.8 1
Taking the derivative with respect to 𝑡, and setting it equal to zero, we have
𝑑 1
(20𝑡 (1 − 𝑡) 3 ) = 0 ⇐⇒ 𝑡 = .
𝑑𝑡 4
Observe how easy it was to make a good guess for 𝑓𝑌 (𝑡) just by looking at the
plot of 𝑓 𝑋 (𝑡).
Answer: No. While it is easy to get 𝐹𝑋 (𝑥), unfortunately 𝐹𝑋 (𝑥) is not easy to
invert. Thus we won’t be able to solve 𝑢 = 𝐹𝑋 (𝑥) for 𝑥.
Question: What is a good choice for a r.v. 𝑌 that we know how to generate, such
that 𝑓𝑌 (𝑡) fits 𝑓 𝑋 (𝑡) reasonably well?
0.8
f X (t)
0.6
f Y (t)
0.4
0.2
t
0 2 4 6 8 10
Figure 13.4 Solid line shows 𝑓 𝑋 (𝑡). Dashed line shows proposed 𝑓𝑌 (𝑡).
2
So, the maximum value occurs when 𝑡 − 𝑡2 is maximized.
𝑡2
𝑑
0= 𝑡− = 1 − 𝑡 =⇒ 𝑡 = 1.
𝑑𝑡 2
So,
√︂
𝑓 𝑋 (1) 2𝑒
𝑐= = ≈ 1.3.
𝑓𝑌 (1) 𝜋
𝑒 −𝜆 𝜆𝑖
Consider 𝑋 ∼ Poisson(𝜆), with 𝑝 𝑋 (𝑖) = 𝑖! .
Answer: It looks like we should be able to apply the Accept–Reject method, but
it is hard to find the right 𝑌 distribution to match up to (see [48, p. 503]).
13.3 Readings
A lot more is known about generating random variables than we have described
in this chapter. Some particularly well-written texts are [63] and [48].
13.4 Exercises