Sei sulla pagina 1di 7

DISCRETE RV GENERATION

Inverse Transform Method: assume a pmf is given


P {X = xj } = pj , j = 0, 1, 2, . . . .
RV Generation: first generate U U ni(0, 1) and then set

x0 if
U < p0

p 0 U < p0 + p 1
x1 if
..
X=
Pj1
Pj

x if

i=0 pi U <
i=0 pi

.j
.
Notes:
Pj
a) P {X = xj } = P {Fj1 U < Fj } = pj , Fj = i=0 pi.
b) This method uses table inversion for xj s and Fj s.
c) If table has n entries, simple (linear) search time is O(n);
binary search can reduce this to O(log(n));
d) Using probability, average # searches is 1 + j(E[X]);
if xj = j, # searches reduced by starting at j = bE[X]c.
e) For large finite n, precompute Fj s.

MATLAB EXAMPLE
Unfair Die with p = [.1 .3 .1 .05 .25 .2]
>> p = [ .1 .3 .1 .05 .25 .2 ];
>> js = [1:6]; F = cumsum(p)
F =
0.1
0.4
0.5
0.55
0.8
1
>> K=10000; for k=1:K, X(k) = min(js(F>=rand));end
>> hist(X,6), title(Unfair Die Histogram)
>> for i = 1 : 6, P(i) = sum(X==i)/K; end, disp(P)
0.0968 0.2997 0.1003 0.0497 0.252 0.2015
Unfair Die Histogram
3000

2500

2000

1500

1000

500

1.5

2.5

3.5

4.5

5.5

DISCRETE RV GENERATION CONTINUED


Explicit Inverse Transform Method Examples:
Discrete Uniform RVs: P {X = j} = pj = 1/n, j = 1, . . . , n,
use X = bnU c + 1 (no search needed).
P
Application 1: calculation of large N mean A = N1 N
j=1 aj ;
PK
1
use A K j=1 aXj , with K << N .
Application 2: generate random permutation of 1, 2, . . . , n.
Algorithm: initialize Pi = i, i = 1, 2 . . . , n
for k = n : -1 : 2, generate U U ni(0, 1)
set j = bkU c + 1 and interchange Pj , Pk values
end
output P1, P2, . . . , Pn.
Geometric RVs: let rj = P {X = j} = pq j1, with q = 1 p,
Pj1
notice i=1 ri = 1 q j1; so given a U U ni(0, 1),
)
1 q j1 U < 1 q j implies X = b log(1U
log(q) c + 1.
Bernoulli RVs: simple method generates U U ni(0, 1),

1 if U p
X=
0 if U > p.
If desired number of Xs is large, use geometric RVs N ,
where N is trial # for first success:
given N1, set X1 = X2 = . . . = XN11 = 0, XN1 = 1,
given N2, set XN1+1 = . . . = XN1+N21 = 0, XN1+N2 = 1,
etc.

MATLAB EXAMPLE
Geometric RVs with p = .3
>> q=.7; K=10000; X=1+floor(log(rand(1,K))/log(q));
>> hist(X), title(Geometric RVs with p = .3)
>> for i=1:max(X), P(i)=sum(X==i)/K; end, disp(P)
Columns 1 through 6
0.3019
0.2107
0.1444
0.0993
0.0741
0.0479
Columns 7 through 12
0.0334
0.0263
0.0189
0.0122
0.0093
0.0068
Columns 13 through 18
0.0054
0.0032
0.0023
0.001
0.0009
0.0008
Columns 19 through 24
0.0006
0
0.0001
0.0001
0
0.0001
Geometric RVs with p = .3
7000

6000

5000

4000

3000

2000

1000

10

15

20

25

30

DISCRETE RV GENERATION CONTINUED


Generation of Poisson RVs: pmf is
j
P {X = j} = e j! = pj , j = 0, 1, 2, . . ..
Pj i

Use Fj = e
i=0 i! ; simple method:
check successive js until Fj1 U < Fj , and set X = j;
expected number of steps is 1 + E[X] = 1 + ;
for large , start search at j = bc, and
then the ave. # of searches 1 + 0.798 .
Generation of Binomial
RVs: pmf for Bin(n, p) is

P {X = j} = nj pj q nj = rj , j = 0, 1, . . . , n, q = 1 p.
(nj)p
Use rj+1 = (j+1)(1p)
rj with linear search,
then the ave. # of searches 1 + np;
for p > 1/2, generate Y Bin(n, 1 p) and set X =
nY;
for large n, could start search at j = bnpc;
alternate method: generate X1, X
P2,n...Xn with
Xi Bin(1, p), and set X = i=1 Xi.

MATLAB EXAMPLE
Binomial RVs with n = 9, p = .65

>> n=9; p=.65; q = 1-p; r(1) = q^n; js=[1:n+1];


>> for j=1:n, r(j+1) = r(j)*p*(n-j+1)/(j*q); end
>> F = cumsum(r); disp(F)
Columns 1 through 5
7.8816e-05 0.0013962 0.011182 0.053588 0.17172
Columns 6 through 10
0.39111 0.66273 0.87891
0.97929
1
>> K=10000; for k=1:K, X(k)=min(js(F>=rand))-1; en
>> hist(X,n), title(Binomial RVs with n=9, p=.65
>> for i = 1:n+1, P(i) = sum(X==i-1)/K; end, disp(
Columns 1 through 5
0
0.0014
0.0084
0.0403
0.1189
Columns 6 through 10
0.2172
0.2654
0.2245
0.1041
0.0198
>> disp(r)
Columns 1 through 5
7.8816e-05 0.0013173 0.009786 0.042406 0.11813
Columns 6 through 10
0.21939 0.27162
0.21619
0.10037 0.020712

Binomial RVs with n=9, p=.65


3000

2500

2000

1500

1000

500

10

Potrebbero piacerti anche