Académique Documents
Professionnel Documents
Culture Documents
Discrete RVs
Continuous RVs
Rejection method
Illusions are art, for the feeling person, and it is by art that we
live, if we do
Elizabeth Bowen
1/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
2/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
3/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
Congruential generators
Congruential generators were the first reasonable class of
pseudo-random number generators. R uses a pseudo-random
number generator called the Mersenne-Twister, which has similar
properties to congruential generators.
Given an initial number X0 {0, 1, . . . , m 1} and two big
numbers A and B we define a sequence of numbers
Xn {0, 1, . . . , m 1}, n = 0, 1, . . ., by
Xn+1 = (AXn + B )
mod m.
4/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
5/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
6/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
Seeding
The number X0 is called the seed. If you know the seed (as well as
m, A, and B ), then you can reproduce the whole sequence exactly.
This is a very good idea from a scientific point of view; being able
to repeat an experiment means that your results are verifiable.
To generate n pseudo-random numbers in R, use runif(n). R
does not use a congruential generator, but is still needs a seed to
generate pseudo-random numbers. For a given value of seed
(assumed integer), the command set.seed(seed) always puts
you at the same point on the cycle of pseudo-random numbers.
7/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
8/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
> set.seed(42)
> runif(2)
[1] 0.9148060 0.9370754
> RNG.state <- .Random.seed
> runif(2)
[1] 0.2861395 0.8304476
> set.seed(42)
> runif(4)
[1] 0.9148060 0.9370754 0.2861395 0.8304476
> .Random.seed <- RNG.state
> runif(2)
[1] 0.2861395 0.8304476
2. Simulation of Random Variables
9/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
10/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
1.0
0.8
(0.875,1) mapped to 3
0.6
0.4
U ~ U(0,1)
(0.5,0.875) mapped to 2
0.2
(0.125,0.5) mapped to 1
0.0
(0,0.125) mapped to 0
X ~ binom(3, 0.5)
11/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
12/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
13/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
0.8
1.0
0.0
0.2
0.4
0.6
If X U (1, 3) then
FX (x ) = (x 1)/2 for
x (1, 3) and thus
FX1 (y) = 2y + 1 for
y (0, 1).
14/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
0.6
0.8
1.0
0.0
0.2
0.4
If X exp() then
FX (x ) = 1 e x for
x 0 and thus
FX1 (y) = 1 log (1 y).
15/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
R command
rbinom
rpoisson
rgeom
rnbinom
runif
rexp
rnorm
rgamma
rbeta
rt
rf
rchisq
rweibull
16/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
17/26
Discrete RVs
Continuous RVs
Rejection method
0.3
0.0
0.1
0.2
0.4
0.5
0.6
iid uniforms
a
1
b
2
18/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
19/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
Rejection method (uniform envelope) Suppose that fX is nonzero only on [a, b], and fX k .
1. Generate X U (a, b) and Y U (0, k ) independent of X
(so P = (X , Y ) is uniformly distributed over the rectangle
[a, b] [0, k ]).
2. If Y < fX (X ) then return X , otherwise go back to step 1.
Example: consider the triangular pdf fX defined as
if 0 < x < 1;
x
(2 x ) if 1 x < 2;
fX (x ) =
0
otherwise.
We apply the rejection method as follows:
source(rejecttriangle.r)
2. Simulation of Random Variables
20/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
21/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
fX (x )
.
h(x )
22/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
Efficiency
23/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
Example: gamma
24/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
f (x )
m x m1 e ()x
= sup
.
h(x ) x >0
(m)
25/26
iid uniforms
Discrete RVs
Continuous RVs
Rejection method
26/26