Generating Discrete Random Variates
Generating Discrete Random Variates
1/ 24
The inverse distribution function (idf) of X is the function F : (0, 1) X for all u (0, 1) as F (u ) = min{x : u < F (x )}
x
F () is the cdf of X That is, if F (u ) = x , x is the smallest possible value of X for which F (x ) is greater than u
2/ 24
Example 6.2.1
Two common ways of plotting a cdf with X = {a, a + 1, . . . , b }:
1.0 F (u) = x
1.0 F (u) = x F () u
F ()
0.0
0.0
Theorem (6.2.1) Let X = {a, a + 1, . . . , b } where b may be and F () be the cdf of X For any u (0, 1), if u < F (a), F (u ) = a else F (u ) = x where x X is the unique possible value of X for which F (x 1) u < F (x )
Discrete-Event Simulation: A First Course Section 6.2: Generating Discrete Random Variates 3/ 24
Algorithm 6.2.1
For X = {a, a + 1, . . . , b }, the following linear search algorithm denes F (u ) Algorithm 6.2.1
x = a; while (F(x) <= u ) x++; return x; /*x is F (u )*/
4/ 24
Algorithm 6.2.2
Idea: start at a more likely point For X = {a, a + 1, . . . , b }, a more ecient linear search algorithm denes F (u ) Algorithm 6.2.2
x = mode; /*initialize with the mode of X */ if (F(x) <= u) while (F(x) <= u) x++; else if (F(a) <= u) while (F(x-1) > u) x--; else x = a; return x; /* x is F (u )*/
Idf Examples
In some cases F (u ) can be determined explicitly If X is Bernoulli(p ) and F (x ) = u , then x = 0 i 0 < u < 1 p: F (u ) = 0 0 < u < 1p 1 1p u <1
6/ 24
F (u ) = a
F (x 1) u < F (x )
7/ 24
F (u ) = 0
ln(1 u ) ln(p )
8/ 24
Inversion Examples
Example 6.2.5 Consider X with pdf 0.1 x=2 f (x ) = 0.3 x=3 0.6 x=6
1.0 u F () F ()
2 3 4 5 6 x
2 3 4 5 6 x
11/ 24
returns 2 with probability 0.1, 3 with probability 0.3 and 6 with probability 0.6 which corresponds to the pdf of X This example can be made more ecient: check the ranges for u associated with x = 6 rst (the mode), then x = 3, then x =2 Problems may arise when |X | is large or innite
12/ 24
13/ 24
Example 6.2.9
X is a Binomial(n, p ) random variate
x
F (x ) =
t =0
n x
p x (1 p )nx
x = 0, 1, 2, . . . , n
Except for special cases, an incomplete beta function cannot be inverted to form a closed form expression for the idf Inversion is not easily applied to generation of Binomial(n, p ) random variates
14/ 24
15/ 24
Example 6.2.10
To generate Binomial(10, 0.4), the pdf is (to 0.ddd precision)
x: 0 1 2 3 4 5 6 7 8 9 10 f (x ) : 0.006 0.040 0.121 0.215 0.251 0.201 0.111 0.042 0.011 0.002 0.000
Random variates can be generated by lling a 1000-element integer-valued array a[] with 6 0s, 40 1s, 121 2s, etc. Binomial(10, 0.4) Random Variate
j = Equilikely(0,999); return a[j];
This algorithm is portable, robust, clear, synchronized and monotone, with small marginal execution time The algorithm is not exact: f (10) = 1/9765625 Set-up time and memory eciency could be problematic: for 0.ddddd precision, need 100 000-element array
Discrete-Event Simulation: A First Course Section 6.2: Generating Discrete Random Variates 16/ 24
The library rvms can be used to compute the cdf array by calling cdfBinomial(n,p,x) for x = 0, 1, . . . , n Only drawback is some ineciency (setup time and memory)
17/ 24
Example 6.2.12
The cdf array from Example 6.2.11 can be eliminated
cdf values computed as needed by Alg. 6.2.2 Reduces set-up time and memory Increases marginal execution time
Function idfBinomial(n,p,u) in library rvms does this Binomial(n, p ) random variates can be generated by inversion Generating a Binomial Random Variate
u = Random(); return idfBinomial(n, p, u); /* in library rvms*/
The algorithm is: portable, exact, robust, clear The algorithm is not: synchronized or monotone Marginal execution: O(n) complexity
19/ 24
An incomplete gamma function cannot be inverted to form an idf Inversion to generate a Poisson() requires searching the cdf as in Examples 6.2.11 and 6.2.12
Discrete-Event Simulation: A First Course Section 6.2: Generating Discrete Random Variates 20/ 24
Example 6.2.14
Generating a Poisson Random Variate
a = 0.0; x = 0; while (a < ) { a += Exponential(1.0); x++; } return x - 1;
The algorithm does not rely on inversion or the large n version of Binomial(n, p ) The algorithm is: portable, exact, robust; not synchronized or monotone; marginal execution time can be long for large It is obscure. Clarity will be provided in Section 7.3
21/ 24
The algorithm is: portable, exact, robust, clear; not synchronized or monotone; marginal execution complexity is O(n)
Discrete-Event Simulation: A First Course Section 6.2: Generating Discrete Random Variates 22/ 24
Library rvgs
Includes 6 discrete random variate generators (as below) and 7 continuous random variate generators
long long long long long long Bernoulli(double p ) Binomial(long n, double p ) Equilikely(long a, long b ) Geometric(double p ) Pascal(long n, double p ) Poisson(double )
Functions Bernoulli, Equilikely, Geometric use inversion; essentially ideal Functions Binomial, Pascal, Poisson do not use inversion
23/ 24
Library rvms
Provides accurate pdf, cdf, idf functions for many random variates Idfs can be used to generate random variates by inversion Functions idfBinomial, idfPascal, idfPoisson may have high marginal execution times Not recommended when many observations are needed due to time ineciency Array of cdf values with inversion may be preferred
24/ 24