Generating Random Numbers: Some Examples Example 1: The following 10 numbers are realizations from a Standard Uniform distribution: .54463 .15389 .85961 .61149 .05219 .41417 .28357 .17783 .40950 .82995 Explain how to use these numbers to generate 10 iid Bernoulli random numbers with a success probability p = .4. Generate a sample of size 10 from that distribution. Solution: Let ( 1 if 0 < Ui ≤ .4, Yi = 0 if .4 < Ui < 1, for i = 1, . . . , 10. Thus by inverse cdf method Y1 , . . . , Y10 are iid Bernoulli(.4) Here the sample generated is 0, 1, 0, 0, 1, 0, 1, 1, 0, 0 Example 2: Generate a random sample of size 10 from the Bin(n = 10, p = .4) using the above values from U (0, 1) Solution: X ∼ Bin(n, p) means that X is the number of successes in n iid Bernoulli trials with a success probability p. Thus X = Y1 + Y2 , . . . + Yn where Y1 , . . . , Yn are iid Bernoulli(p). In Problem 1, we generated Y1 , . . . , Y10 ∼ iid Bernoulli(.4). Thus set X = Y1 + Y2 , . . . + Y10 to obtain X ∼ Bin(10, .4). Here, a value from Bin(10, .4) is generated as X = 0 + 1 + 0 + 0 + 1 + 0 + 1 + 1 + 0 + 0 = 4. To generate another value from Bin(10, .4), one needs to repeat the whole procedure, beginning with obtaining a new sample from Standard Uniform. Thus this method is the least efficient method for generating random samples from the Binomial distribution, partcularly if n is even moderately large. An efficient method is the table look-up method. We need the cdf for the Bin(10, .4) which is available on the Tables page. We reproduce it below: x P (X ≤ x) 0 0.006046618 5 0.833761382 1 0.046357402 6 0.945238118 2 0.167289754 7 0.987705446 3 0.382280602 8 0.998322278 4 0.633103258 9 0.999895142 Using this table, it is possible to produce 10 values using the 10 standard uniforms from Problem 1 and applying the inverse cdf method. For example, the first value to exceed U1 = .54463 in the above table is 0.633103258; thus the value of the Binomial r.v. returned is 4. Repeating this procedure to transform the 10 values from U (0, 1), the 10 values from Bin(10, .4) obtained are 4, 2, 6, 4, 2, 4, 3, 3, 4, 5, respectively, obviously a more efficient method than summing Bernoulli’s. 1 Example 3: A continuous random variable Y has pdf ( 2 2 y , −1 ≤ y ≤ 1, f (y) = 3 0, otherwise Use the first random number u1 = .54463 to generate a value y from this distribution. Solution: We can use the inverse method for continuous random variables, but first we need to find the cdf of Y . The cdf of Y is FY (y) = P (Y ≤ y) For y < −1, FY (y) = 0 since P (Y ≤ 0) = 0. For −1 ≤ y ≤ 1 Z y 1 3 2 1 y 1 FY (y) = t dt = y 3 = y 3 + 2 −1 2 2 −1 2 For y > 1, FY (y) = 1 Thus the cdf of Y is given by FY (y) = 0 1 3 y 2 + 1 2 1 To use the inverse cdf method set y < −1 −1 ≤ y ≤ 1, y>1 1 1 u = y3 + 2 2 and solve for y 1 3 1 y + 2 2 3 y = 2u − 1 1 y = (2u − 1) 3 u = So to obtain a realization y1 from a distribution with cdf FY , substitute u1 = .54463 in the 1 above; so we have y1 = (2 × .54463 − 1) 3 = .44691 Example 4: Using the first two random numbers u1 and u2 explain how to generate values of a random variable X where X ∼ Erlang(k = 2, λ = 3). Generate a value from this distribution using u1 = .54463, u2 = .15389. Solution: By definition of the Erlang distribution, X ∼ Erlang(k, λ) if X = Y1 + . . . + Yk where Y1 , . . . , Yk are iid Exp(λ). Thus, first generate two random variables from Exp(3) using the inverse cdf method: 1 Yi = − log(1 − Ui ), i = 1, 2 λ Then set X = Y1 + Y2 to obtain an Erlang(2, 3) random variable. For this example, y1 = − 13 log(1 − .54463) = .26221 and y2 = − 13 log(1 − .15389) = .05570. Thus a value from Erlang(2, 3) is x = .26221 + .05570 = .32191 2