Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
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
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
2500
2000
1500
1000
500
10