Section 7.2: Generating Continuous Random Variates: Discrete-Event Simulation: A First Course
Section 7.2: Generating Continuous Random Variates: Discrete-Event Simulation: A First Course
1/ 29
The inverse distribution function (idf) of X is the function F 1 : (0, 1) X for all u (0, 1) as F 1 (u ) = x where x X is the unique possible value for F (x ) = u
Assumes the cdf is strictly monotone increasing True if f (x ) > 0 for all x X
There is a one-to-one correspondence between possible values x X and cdf values u = F (x ) (0, 1)
2/ 29
0.0
3/ 29
Examples
If X is a continuous variable with possible value 0 < x < b and pdf f (x ) = 2x /b 2 , the cdf is F (x ) = (x /b )2 0<u<1 x = F 1 (u ) = b u
4/ 29
5/ 29
Inversion Examples
Example 7.2.4: Generating a Uniform(a, b ) Random Variate
u = Random(); return a + (b - a) * u;
Because this algorithm has reverse monotonicity, the algorithm in Example 7.2.5 is preferred.
Discrete-Event Simulation: A First Course Section 7.2: Generating Continuous Random Variates 6/ 29
Algorithms in Example 7.2.4 and 7.2.5 are ideal Both are portable, exact, robust, ecient, clear, synchronized and monotone It is not always possible to solve for a continuous random variable idf explicitly by algebraic techniques Two other options may be available
Use a function that accurately approximates F 1 () Determine the idf by solving u = F (x ) numerically
7/ 29
Approximate Inversion
If Z is a Normal(0, 1), the cdf is the special function () The idf can be approximated as the ratio of two fourth degree polynomials (Odeh and Evans, 1974) The approximation is ecient and essentially has negligible error The idf 1 () cannot be evaluated in closed form
8/ 29
Approximation of ()
For any u (0, 1), a Normal(0, 1) idf approximation is 1 1 (u ) a (u ) where
1 a (u ) =
t + p (t )/q (t ) 0.0 < u < 0.5 t p (t )/q (t ) 0.5 u < 1.0 2 ln(u ) 0.0 < u < 0.5 2 ln(1 u ) 0.5 u < 1.0
and t= and q (t ) = b0 + b1 t + + b4 t 4 The ten coecients can be chosen to produce an absolute error less than 109 for all 0.0 < u < 1.0
Discrete-Event Simulation: A First Course Section 7.2: Generating Continuous Random Variates 9/ 29
p (t ) = a0 + a1 t + + a4 t 4
Example 7.2.6
Inversion can be used to generate Normal(0, 1) variates: Example: 7.2.6: Generating a Normal(0, 1) Random Variate
u = Random(); 1 return a (u );
This algorithm is portable, essentially exact, robust, reasonably ecient, synchronized and monotone Clarity?
10/ 29
Alternative Method 1
11/ 29
Alternative Method 2
If U1 and U2 are independent Uniform(0, 1) RVs then Z1 = and Z2 = 2 ln(U1 ) sin(2 U2 ) will be independent Normal(0, 1) RVs (Box and Muller, 1958) This algorithm is: portable, exact, robust and relatively ecient; This algorithm is not: clear or monotone The algorithm is synchronized only in pair-wise fashion 2 ln(U1 ) cos(2 U2 )
12/ 29
Numerical Inversion
Numerical inversion provides another way to generate continuous random variates; that is, u = F (x ) can be solved for x iteratively Newtons method provides a good compromise between rate of convergence and robustness Given u (0, 1), let t be close to the value of x for which u = F (x ) If F () is expanded in a Taylors series about the point t F (x ) = F (t ) + F (t )(x t ) + Recall F (t ) = f (t ) For small |x t |, ignore (x t )2 and higher order terms
Discrete-Event Simulation: A First Course Section 7.2: Generating Continuous Random Variates 14/ 29
1 F (t )(x t )2 + 2!
Newtons Method
Set u = F (x ) F (t ) + f (t )(x t ) and solve for x to obtain x t+ u F (t ) f (t )
u F (ti ) f (ti )
i = 0, 1, 2,
F ()
u F (ti )
0.0 ti
Discrete-Event Simulation: A First Course
x ti+1
15/ 29
16/ 29
Algorithm 7.2.2
Algorithm 7.2.2
Given u (0, 1), the pdf f (), the cdf F () and a convergence parameter > 0, this algorithm will solve for x = F 1 (u ) x = ; /* is E[X]*/ do { t = x; x = t + (u - F(t)) / f(t); } while (|x-t| > ); return x; /* x is F 1 (u )*/
If u is small and X is non-negative, a negative value of x may occur early in the iterative process. Negative t will cause F (t ) and f (t ) to be undened for positive RVs
Discrete-Event Simulation: A First Course Section 7.2: Generating Continuous Random Variates 17/ 29
Algorithms 7.2.1 and 7.2.2 together provide a general purpose inversion approach to continuous random variate generation E.g., the Erlang(n, b ) idf function in rvms is based on Alg.7.2.2 and can be used with Algorithm 7.2.1
Discrete-Event Simulation: A First Course Section 7.2: Generating Continuous Random Variates 18/ 29
The algorithm is: portable, exact, robust, and clear The algorithm is not ecient (it is O(n)), synchronized or monotone
19/ 29
This algorithm requires only one log() evaluation, rather than n Can further improve eciency by using t *= Random(); The algorithm remains O(n), so is not ecient if n is large
20/ 29
The algorithm is: portable, exact, robust, clear The algorithm is not: ecient(it is O(n)), synchronized or monotone
Discrete-Event Simulation: A First Course Section 7.2: Generating Continuous Random Variates 21/ 29
The algorithm is: portable, exact, robust, clear The algorithm is not synchronized or monotone Eciency depends on algs. used for Normal and Chisquare
22/ 29
In practice, using a large but nite value of n and a small but (x ) and f (x ) non-zero value of , perfect agreement between f will not be achieved
In the discrete case, it is due to natural sampling variability In the continuous case, the quantization error associated with binning the sample is an additional factor
23/ 29
Quantization Error
Let B = [m /2, m + /2] be a small histogram bin Use the Taylor expansion of f (x ) at x = m
f (x ) = f (m)+f (m)(x m)+ 1 1 f (m)(x m)2 + f (m)(x m)3 + 2! 3!
The probability of falling within the bin is Pr(x B ) = f (x )dx = = f (m) + 1 f (m)3 + 24
24/ 29
For all x B , the histogram density is (x ) = 1 Pr(X B ) f (m) + 1 f (m)2 f 24 Unless f (m) = 0, there is a positive or negative bias between
(x ), the experimental density of the histogram bin and f f (m), the theoretical pdf evaluated at the bin midpoint
This bias may be signicant if the curvature of the pdf is large at the bin midpoint
25/ 29
Example 7.2.9
X is a continuous random variable with pdf f (x ) = The cdf X is
x
2 (x + 1)3
x >0
F (x ) =
0
f (t )dt = 1
1 (x + 1)2
x >0
Note the pdf curvature is very large close to x = 0; therefore, the histogram will not match the pdf well for the bins close to x =0
Discrete-Event Simulation: A First Course Section 7.2: Generating Continuous Random Variates 26/ 29
27/ 29
Compare the empirical cdf (section 4.3) with the population cdf F (x ) Eliminates binning quantization error (x ) F (x ) For large samples (as n ), F
28/ 29
Library rvgs
29/ 29