3 S IMULATION B ASICS
3.4 Simulating Random Behavior
3.4.2 Generating Random Variates
This section introduces common methods for generating observations (random variates) from probability distributions other than the uniform(0,1) distribution.
For example, the time between arrivals of cars to a restaurant’s drive-through window may be exponentially distributed, and the time required for drivers to place orders at the window might follow the lognormal distribution.
Observations from these and other commonly used distributions are obtained by transforming the observations generated by the random number generator to the desired distri- bution. The transformed values are referred to as variates from the specified distribution.
There are several methods for generating random variates from a desired dis- tribution, and the selection of a particular method is somewhat dependent on the characteristics of the desired distribution. Methods include the inverse transfor- mation method, the acceptance/rejection method, the composition method, the convolution method, and the methods employing special properties. The inverse transformation method, which is commonly used to generate variates from both
discrete and continuous distributions, is described starting first with the continu-ous case. For a review of the other methods, see Law and Kelton (2000).
Continuous Distributions
The application of the inverse transformation method to generate random variates from continuous distributions is straightforward and efficient for many continu- ous distributions. For a given probability density function f (x), find the cumula- tive distribution function of X. That is, F(x) = P(X ≤ x). Next, set U = F(x),
where U is uniform(0,1), and solve for x. Solving for x yields x = F−1(U ). The equation x = F−1(U ) transforms U into a value for x that conforms to the given distribution f (x).
As an example, suppose that we need to generate variates from the exponential distribution with mean β. The probability density function f (x) and corresponding cumulative distribution function F(x) are
−x/β
f (x) = β e for x > 0 F(x) =
0 elsewhere
r 1 − e−x/β for x > 0
0 elsewhere
Setting U = F(x) and solving for x yields U = 1 − e−x/β e−x/β = 1 − U
ln (e−x/β) = ln (1 − U) where ln is the natural logarithm
−x/β = ln (1 − U) x = −β ln (1 − U)
The random variate x in the above equation is exponentially distributed with mean β provided U is uniform(0,1).
Suppose three observations of an exponentially distributed random variable with mean β = 2 are desired. The next three numbers generated by the random number generator are U1 = 0.27, U2 = 0.89, and U3 = 0.13. The three numbers are transformed into variates x1, x2, and x3 from the exponential distribution with mean β = 2 as follows:
x1 = −2 ln (1 − U1) = −2 ln (1 − 0.27) = 0.63 x2 = −2 ln (1 − U2) = −2 ln (1 − 0.89) = 4.41 x3 = −2 ln (1 − U3) = −2 ln (1 − 0.13) = 0.28
Figure 3.4 provides a graphical representation of the inverse transformation method in the context of this example. The first step is to generate U, where U is uniform(0,1). Next, locate U on the y axis and draw a horizontal line from that point to the cumulative distribution function [F(x) = 1 − e−x/2]. From this point
1
FIGURE 3.4
Graphical explanation of inverse
transformation method for continuous variates.
F(x) 1.00 U2 = 1 – e–x2/ 2 = 0.89
0.50
U1 = 1 – e–x1/ 2 = 0.27
x1 = –2 ln (1 – 0.27) = 0.63 x2 = –2 ln (1 – 0.89) = 4.41
of intersection with F(x), a vertical line is dropped down to the x axis to obtain the corresponding value of the variate. This process is illustrated in Figure 3.4 for generating variates x1 and x2 given U1 = 0.27 and U2 = 0.89.
Application of the inverse transformation method is straightforward as long as there is a closed-form formula for the cumulative distribution function, which is the case for many continuous distributions. However, the normal distribution is one exception. Thus it is not possible to solve for a simple equation to generate normally distributed variates. For these cases, there are other methods that can be used to generate the random variates. See, for example, Law and Kelton (2000) for a description of additional methods for generating random variates from continuous distributions.
Discrete Distributions
The application of the inverse transformation method to generate variates from discrete distributions is basically the same as for the continuous case. The differ-ence is in how it is implemented. For example, consider the following probability mass function:
0.10 for x = 1 p(x) = P(X = x) = 0.30 for x = 2
0.60 for x = 3
The random variate x has three possible values. The probability that x is equal to 1 is 0.10, P(X = 1) = 0.10; P(X = 2) = 0.30; and P(X = 3) = 0.60. The
cumula-tive distribution function F(x) is shown in Figure 3.5. The random variable x could be used in a simulation to represent the number of defective components on a circuit board or the number of drinks ordered from a drive-through window, for example.
Suppose that an observation from the above discrete distribution is desired.
The first step is to generate U, where U is uniform(0,1). Using Figure 3.5, the
FIGURE 3.5
Graphical explanation of inverse
transformation method for discrete variates.
F(x)
U2 = 0.89
U1 = 0.27 1.00
0.40
U3 = 0.05 0.1
0 1
x3 = 1
2 x1 = 2
3 x2 = 3
value of the random variate is determined by locating U on the y axis, drawing a horizontal line from that point until it intersects with a step in the cumulative function, and then dropping a vertical line from that point to the x axis to read the value of the variate. This process is illustrated in Figure 3.5 for x1, x2, and x3
given
U1 = 0.27, U2 = 0.89, and U3 = 0.05. Equivalently, if 0 ≤ Ui ≤ 0.10, then xi = 1; if 0.10 < Ui ≤ 0.40, then xi = 2; if 0.40 < Ui ≤ 1.00, then xi = 3. Note thatshould we generate 100 variates using the above process, we would expect a value of 3 to be returned 60 times, a value of 2 to be returned 30 times, and a value of 1 to be returned 10 times.
The inverse transformation method can be applied to any discrete distribution by dividing the y axis into subintervals as defined by the cumulative distribution function. In our example case, the subintervals were [0, 0.10], (0.10, 0.40], and (0.40, 1.00]. For each subinterval, record the appropriate value for the random variate. Next, develop an algorithm that calls the random number generator to receive a specific value for U, searches for and locates the subinterval that contains U, and returns the appropriate value for the random variate.
Locating the subinterval that contains a specific U value is relatively straight- forward when there are only a few possible values for the variate.
However, the number of possible values for a variate can be quite large for some discrete distributions. For example, a random variable having 50 possible values could require that 50 subintervals be searched before locating the subinterval that contains U. In such cases, sophisticated search algorithms have been developed that quickly locate the appropriate subinterval by exploiting certain characteristics of the discrete distribution for which the search algorithm was designed. A good source of information on the subject can be found in Law and Kelton (2000).