Professional Documents
Culture Documents
Basics of Monte Carlo Simulation: He Shuangchi
Basics of Monte Carlo Simulation: He Shuangchi
He Shuangchi
Example
You are on a game show. You are given the choice of three doors: Behind
one door is a car; behind the others, goats. You pick a door, say door 1.
Then, the host, who knows what are behind the doors, opens another
door, say door 3. It has a goat. He then says to you, “Do you want to
pick door 2?” Is it to your advantage to switch your choice?
1 2
? ?
1
http://en.wikipedia.org/wiki/Monty_Hall_problem
IE5004 Basics of Monte Carlo Simulation 2 / 36
Two opposite intuitions
“After the host reveals a goat, you now have a one-in-two chance of
being correct. Whether you change your selection or not, the odds are
the same.”—No need to switch
“The car has a 1/3 chance of being behind your pick and a 2/3
chance of being behind one of the other two doors. The host opens a
door, the odds for the two sets don’t change but the odds move to 0
for the open door and 2/3 closed door.”—You should switch
Question
Which argument is correct?
Before we proceed, let us make the following assumptions about the host.
The host must open a door that was not picked by you.
The host must open a door to reveal a goat and never the car.
The host must offer the chance to switch between the originally
chosen door and the remaining closed door.
1/3 1
Door 2 Door 3 1/3 Goat Car
1/3 1
Door 3 Door 2 1/3 Goat Car
2
http://en.wikipedia.org/wiki/Monte_Carlo_method
IE5004 Basics of Monte Carlo Simulation 8 / 36
(−1, 1) (1, 1)
Example: Estimating π
Let X and Y be independent uniform random variables over (−1, 1). The
joint probability density function is given by
(
1/4 if − 1 < x < 1 and − 1 < y < 1,
f (x, y ) = fx (x)fY (y(−1,) =
−1) (1, −1)
0 otherwise.
= (0, 0)
(−1, 1) (1, 1)
0.5
−0.5
−1
−1 −0.5 0 0.5 1
Example
Let g (x) be a function. Compute
Z 1
θ= g (x) dx
0
If U1 , . . . , Un are iid uniformly distributed over (0, 1), g (U1 ), . . . , g (Un ) are
iid all having expected value θ. By the strong law of large numbers,
n
1X
g (Uk ) → E[g (U)] = θ as n → ∞
n
k=1
Exercise
Use the Monte Carlo method to evaluate
Z 1
θ= x 4 dx
0
where U1 , . . . , Un are iid random variables uniformly distributed over (0, 1).
Example
Let g (x) be a function. Compute
Z b
θ= g (x) dx for a < b
a
Example
Let g (x) be a function. Compute
Z ∞
θ= g (x) dx
0
Exercise
Use the Monte Carlo method to evaluate
Z ∞
θ= e −x dx
0
Let
e 1−1/y
h(y ) =
y2
In the simulation, we use
n
1X
θ≈ h(Uk )
n
k=1
where U1 , . . . , Un are iid random variables uniformly distributed over (0, 1).
Because a computer can follow only very specific steps, the generated
numbers are not truly random.
They are deterministically generated.
They have all the appearances of being independent uniform (0,1)
random variables.
These pseudorandom numbers must satisfy the following conditions.
All generated numbers are within (0, 1).
For 0 ≤ a < b ≤ 1, the relative frequency that generated numbers lie
in (a, b) is b − a.
The sequence of numbers can pass a statistical test of independence.
where
x0 , a, c, and m are given nonnegative integers
the initial value x0 is called the seed
a is called the multiplier
c is called the increment
m is called the modulus
Then, pseudorandom numbers are generated by
xn
un = for n = 0, 1, . . .
m
Example
Consider a linear congruential generator given by
xn = (2xn−1 + 1) mod 7
x1 = (2 × 3 + 1) mod 7 = 0, u1 = 0/7 = 0,
x2 = (2 × 0 + 1) mod 7 = 1, u2 = 1/7 = 0.1429,
x3 = (2 × 1 + 1) mod 7 = 3, u3 = 3/7 = 0.4286,
x4 = (2 × 3 + 1) mod 7 = 0, u4 = 0/7 = 0,
x5 = (2 × 0 + 1) mod 7 = 1, u5 = 1/7 = 0.1429,
x6 = (2 × 1 + 1) mod 7 = 3, u6 = 3/7 = 0.4286,
x7 = (2 × 3 + 1) mod 7 = 0, u7 = 0/7 = 0,
IE5004 Basics of Monte Carlo Simulation 19 / 36
Period of a random number generator
Definition
The period of a random number generator is the number of values that
can be produced before the exact sequence of values repeats.
Question
What is the period of the linear congruential generator in the previous
example?
Example
A linear congruential generator with
Question
Let X be a Bernoulli random variable with P[X = 1] = p. How can we use
the values of U to generate the values of X ?
Let (
1 if 0 < U ≤ p,
X =
0 if p < U < 1.
Then,
P[X = xj ] = pj for j = 0, 1, . . .
P
where x0 < x1 < · · · and j pj = 1.
Question
How can we generate the values of X using the values of U?
Let
X = xk if F (xk−1 ) ≤ U < F (xk )
Then,
Example
Let X be a discrete uniform random variable that is equally likely to take
each value of 1, . . . , n, i.e., P[X = k] = 1/n. Generate the values of X .
Example
Let X be a geometric random variable with P[X = k] = q k−1 p for
q = 1 − p and k = 1, 2, . . .. Generate the values of X .
F (k) = P[X ≤ k]
= 1 − P[X > k]
= 1 − P[the first k trials are all failures]
= 1 − qk
X =k if 1 − q k−1 ≤ U < 1 − q k
q k < 1 − U ≤ q k−1
Then,
k log q < log(1 − U) ≤ (k − 1) log q
Because log q < 0,
log(1 − U)
k −1≤ <k
log q
Because U has the same distribution as 1 − U, we can use
log U
X = +1
log q
The inverse transform method can also be used for generating the values
of a continuous random variable.
Proposition
Let U be a uniform (0, 1) random variable. Let F be the cumulative
distribution function of a continuous random variable. Then,
X = F −1 (U)
Example
Let X be an exponential random variable with rate 1. Generate the values
of X using the values of U.
F (x) = 1 − e −x for x ≥ 0
Let u = 1 − e −x . Then,
x = − log(1 − u)
Therefore, F −1 (u) = − log(1 − u). We may generate the desired random
variable by
X = F −1 (U) = − log(1 − U)
Because 1 − U has the same distribution as U, we can use
X = − log U
IE5004 Basics of Monte Carlo Simulation 30 / 36
The rejection method
Start
Generate a Is Yes
Generate Set X = Y
random number f (Y )
Y~g U ≤ cg (Y ) ?
U
No
Figure 5.1. The rejection method for simulating a random variable X having density
function1 f .Generate Y that has density g
Step
Step 2 Generate a uniform (0, 1) number U
5.2 3The
Step Rejection
Set X = Y Method
if
f (Y )
Suppose we have a method for generatingU ≤ a random variable having density
c · g (Y ) from the continuous
function g(x). We can use this as the basis for generating
distribution having density
Otherwise, function
return of f (x)
to Step 1. by generating Y from g and then
accepting this generated value with a probability proportional to f (Y )/g(Y ).
Specifically, let c be a constant such that
Proposition
f (y)
The random variable X generated c the
by for rejection
all y method has density f .
g(y)
IE5004 Basics of Monte Carlo Simulation 32 / 36
Generating a standard normal random variable
Example
Let Z be a standard normal random variable. Generate the values of Z .
The procedure is as follows.
Generate X = |Z | using the rejection method.
Set Z = X with probability 0.5 and Z = −X with probability 0.5.
Note that X = |Z | has density
2 2
f (x) = √ e −x /2 for x > 0
2π
Let Y be an exponential random variable with rate 1, whose density is
We have r
f (x) 2 x−x 2 /2
= e
g (x) π
Because
x2 1 1
x− = − (x − 1)2 +
2 2 2
It follows that x − x 2 /2 ≤ 1/2 for all x. Then,
r
f (x) 2e
≤ for all x > 0
g (x) π
p
We take c = 2e/π and then
f (x) 2
= e −(x−1) /2
c · g (x)
Reading assignment
Read Chapters 3, 4.1, and 5.1–5.2 in Simulation by Ross
Exercise problems
3.2, 3.3, 3.5, 3.7, 4.1, 5.1, and 5.5 in Simulation by Ross