You are on page 1of 80

Monte Carlo Algorithms

Shusen Wang
Application 1: Calculating Pi
Calculating Pi

• We already know 𝜋 ≈ 3.141592653589 …


• Pretend we do not know the value of 𝜋.
• Can we find it out (approximately) using a random number generator?
Calculating Pi

𝑦
• Assume 𝑥, 𝑦 is a point sampled from
1
the square uniformly at random.

−1
1
𝑥

−1
Calculating Pi

𝑦
• Assume 𝑥, 𝑦 is a point sampled from
1
the square uniformly at random.
• What is the probability that 𝑥, 𝑦 is in
−1 the circle?
1
𝑥

−1
Calculating Pi

𝑦
• Assume 𝑥, 𝑦 is a point sampled from
1
the square uniformly at random.
• What is the probability that 𝑥, 𝑦 is in
−1 the circle?
𝑥
1 • Area of the square is 𝐴2 = 24 = 4.
• Area of the circle is 𝐴4 = 𝜋𝑟 4 = 𝜋.
78 :
−1 • Probability: 𝑃 = = .
79 ;
Calculating Pi
• Suppose 𝑛 points are uniformly sampled from −1, 1 × −1, 1 .
:>
• Then, in expectation, 𝑃𝑛 = points are in the circle.
;
𝑦
• Assume 𝑥, 𝑦 is a point sampled from
1
the square uniformly at random.
• What is the probability that 𝑥, 𝑦 is in
−1 the circle?
𝑥
1 • Area of the square is 𝐴2 = 24 = 4.
• Area of the circle is 𝐴4 = 𝜋𝑟 4 = 𝜋.
78 :
−1 • Probability: 𝑃 = = .
79 ;
Calculating Pi
• Suppose 𝑛 points are uniformly sampled from −1, 1 × −1, 1 .
:>
• Then, in expectation, 𝑃𝑛 = points are in the circle.
;
𝑦
1
• Given a point 𝑥, 𝑦 , how do you know
whether 𝑥, 𝑦 is in the circle?
−1 • If 𝑥 4 + 𝑦 4 ≤ 1, then it is in the circle.
1
𝑥

−1
Calculating Pi
• Suppose 𝑛 points are uniformly sampled from −1, 1 × −1, 1 .
:>
• Then, in expectation, 𝑃𝑛 = points are in the circle.
;
𝑦
1
• We found 𝑚 points in the circle.
:>
• If 𝑛 is big, then 𝑚 ≈ .
−1 ;

1
𝑥 • Thus, 𝜋 ≈
;B
.
>

−1
Calculating Pi
• Suppose 𝑛 points are uniformly sampled from −1, 1 × −1, 1 .
:>
• Then, in expectation, 𝑃𝑛 = points are in the circle.
;
𝑦
1 • Law of large numbers:
;B
→ 𝜋, as 𝑛 → ∞.
>
−1
1
𝑥

−1
Calculating Pi
• Suppose 𝑛 points are uniformly sampled from −1, 1 × −1, 1 .
:>
• Then, in expectation, 𝑃𝑛 = points are in the circle.
;
𝑦
1 • Law of large numbers:
;B
→ 𝜋, as 𝑛 → ∞.
>
−1
𝑥
1 • Concentration bound:
;B 2
−𝜋 =𝑂 .
> >
−1
Calculating Pi
;B
𝑛 points are sampled from the square; 𝑚 are in the circle. Then 𝜋 ≈ .
>

𝑦
1

−1
1
𝑥

−1
Calculating Pi
;B
𝑛 points are sampled from the square; 𝑚 are in the circle. Then 𝜋 ≈ .
>

𝑦 Algorithm
1
1. User specifies a big 𝑛; reset
counter 𝑚 = 0.
−1
1
𝑥

−1
Calculating Pi
;B
𝑛 points are sampled from the square; 𝑚 are in the circle. Then 𝜋 ≈ .
>

𝑦 Algorithm
1
1. User specifies a big 𝑛; reset
counter 𝑚 = 0.
−1 2. For 𝑖 = 1 to 𝑛:
1
𝑥
a) Randomly generate 𝑥 ∈ −1, 1 .
b) Randomly generate 𝑦 ∈ −1, 1 .
c) If 𝑥 4 + 𝑦 4 ≤ 1, then 𝑚 ← 𝑚 + 1.
−1 ;B
3. Return 𝜋 ≈ .
>
Application 2: Buffon's Needle Problem
Buffon's Needle Problem

Buffon, 1707 – 1788 Buffon's Needle Problem


French scientist
Buffon's Needle Problem

• The parallel lines have distance 𝑑.


• Needles have length 𝑙.

𝑑
Buffon's Needle Problem

• The parallel lines have distance 𝑑.


𝑙 • Needles have length 𝑙.
needle
• Randomly throw a needle; the needle
may or may not lie across a line.

𝑑
Buffon's Needle Problem

• The parallel lines have distance 𝑑.


𝑙 • Needles have length 𝑙.
needle
• Randomly throw a needle; the needle
may or may not lie across a line.

𝑑
needle
Buffon's Needle Problem

• The parallel lines have distance 𝑑.


𝑙 • Needles have length 𝑙.
needle
• Randomly throw a needle; the needle
may or may not lie across a line.

4M
With probability 𝑃 = , they are across.
:N
𝑑
needle
• It can be proved using integral.
Buffon's Needle Problem

𝑙
needle

4M
With probability 𝑃 = , they are across.
:N
𝑑
needle
Buffon's Needle Problem
4M
With probability 𝑃 = , they are across.
:N

𝑙 • Randomly throw a total of 𝑛 needles.


needle
4M>
• In expectation, 𝑃𝑛 = needles lie
:N
across the lines.

𝑑
needle
Buffon's Needle Problem
4M
With probability 𝑃 = , they are across.
:N

𝑙 • Randomly throw a total of 𝑛 needles.


needle
4M>
• In expectation, 𝑃𝑛 = needles lie
:N
across the lines.
• Actually observe 𝑚 needles across the
lines.
𝑑
4 M >
needle • If 𝑛 is big, 𝑚 ≈ .
: N
Buffon's Needle Problem
4M
With probability 𝑃 = , they are across.
:N

𝑙 • Randomly throw a total of 𝑛 needles.


needle
4M>
• In expectation, 𝑃𝑛 = needles lie
:N
across the lines.
• Actually observe 𝑚 needles across the
lines.
𝑑
4 M >
needle • If 𝑛 is big, 𝑚 ≈ .
: N
4 M >
• Thus, 𝜋 ≈ .
N B
Buffon's Needle Problem

Researcher Year 𝑛= 𝑚= Estimate of 𝛑


Wolf 1850 5000 2532 3.1596
Smith 1855 3204 1218 3.1554
De Morgan 1860 600 382 3.137
Fox 1884 1030 489 3.1595
Lazzerini 1901 3408 1808 3.1415929
Reina 1925 2520 859 3.1795
Buffon's Needle Problem
Application 3: Area of A Region
What is the area of the grey region?
𝑦
2

2
𝑥
1
What is the area of the grey region?
𝑦
2

2
𝑥
1
What is the area of the grey region?
𝑦
• If a point 𝑥, 𝑦 is in the circle, it
2 must satisfy
4 4
𝑥−1 + 𝑦−1 ≤ 1.

1
𝑟=1

2
𝑥
1
What is the area of the grey region?
𝑦
• If a point 𝑥, 𝑦 is in the circle, it
2 must satisfy
4 4
𝑥−1 + 𝑦−1 ≤ 1.

1
• If a point 𝑥, 𝑦 is in the quarter
circle, it must satisfy
𝑥 4 + 𝑦 4 ≤ 24 .

2
𝑥
1
What is the area of the grey region?
𝑦
2 • A point 𝑥, 𝑦 in the grey region
satisfies both of
1. 𝑥−1 4 + 𝑦−1 4 ≤ 1,
2. 𝑥 4 + 𝑦 4 > 24.
1

2
𝑥
1
What is the area of the grey region?
𝑦
2 • A point 𝑥, 𝑦 in the grey region
satisfies both of
1. 𝑥−1 4 + 𝑦−1 4 ≤ 1,
2. 𝑥 4 + 𝑦 4 > 24.
1

2
𝑥
1
What is the area of the grey region?
𝑦
2 • Area of the square: 𝐴2 = 24 = 4.

2
𝑥
1
What is the area of the grey region?
𝑦
2 • Area of the square: 𝐴2 = 24 = 4.
• Area of the grey region: 𝐴4 .
• A point uniformly sampled from the
square falls in the grey region w.p.
1
78 78
𝑃= = .
79 ;

2
𝑥
1
What is the area of the grey region?
𝑦 • Uniformly sample 𝑛 points from the
square 0, 2 × 0, 2 .
2 > 78
• In expectation, 𝑛𝑃 = points fall
;
in the grey region.

2
𝑥
1
What is the area of the grey region?
𝑦 • Uniformly sample 𝑛 points from the
square 0, 2 × 0, 2 .
2 > 78
• In expectation, 𝑛𝑃 = points fall
;
in the grey region.
• We actually observe 𝑚 points in the
1 grey region.
> 78
• If 𝑛 is big, then 𝑚 ≈ .
;
;B
• Thus, 𝐴4 ≈ .
>

2
𝑥
1
What is the area of the grey region?
𝑦 Algorithm
2 1. User specify a big 𝑛; reset
counter: 𝑚 = 0.
2. For 𝑖 = 1 to 𝑛:
a) Randomly generate 𝑥 ∈ 0, 2 .
b) Randomly generate 𝑦 ∈ 0, 2 .
1 c) If both of the following conditions
are satisfied, then 𝑚 ← 𝑚 + 1:
i. 𝑥−1 4+ 𝑦−1 4 ≤ 1.
ii. 𝑥 4 + 𝑦 4 > 24 .
;B
3. Return area = .
>

2
𝑥
1
What is the area of the grey region?
𝑦 Algorithm
2 1. User specify a big 𝑛; reset
counter: 𝑚 = 0.
2. For 𝑖 = 1 to 𝑛:
a) Randomly generate 𝑥 ∈ 0, 2 .
b) Randomly generate 𝑦 ∈ 0, 2 .
1 c) If both of the following conditions
are satisfied, then 𝑚 ← 𝑚 + 1:
i. 𝑥−1 4+ 𝑦−1 4 ≤ 1.
ii. 𝑥 4 + 𝑦 4 > 24 .
;B
3. Return area = .
>

2
𝑥
1
What is the area of the grey region?
𝑦 Algorithm
2 1. User specify a big 𝑛; reset
counter: 𝑚 = 0.
2. For 𝑖 = 1 to 𝑛:
a) Randomly generate 𝑥 ∈ 0, 2 .
b) Randomly generate 𝑦 ∈ 0, 2 .
1 c) If both of the following conditions
are satisfied, then 𝑚 ← 𝑚 + 1:
i. 𝑥−1 4+ 𝑦−1 4 ≤ 1.
ii. 𝑥 4 + 𝑦 4 > 24 .
;B
3. Return area ≈ .
>

2
𝑥
1
Application 4: Integration
Integration
2
• We are given a function, e.g., 𝑓 𝑥 = .
2WXYZ [ ⋅ ]^_` [ 8
c
• Calculate the integral: 𝐼 = ∫d.e 𝑓 𝑥 𝑑𝑥.
Integration
2
• We are given a function, e.g., 𝑓 𝑥 = .
2WXYZ [ ⋅ ]^_` [ 8
c
• Calculate the integral: 𝐼 = ∫d.e 𝑓 𝑥 𝑑𝑥.
• If 𝑓 𝑥 is very involved, there is no way to analytically calculate the
integral.
• Using Monte Carlo to approximate the integral.
Monte Carlo Integration (Univariate)

f
Task: Given a univariate function 𝑓 𝑥 , calculate 𝐼 = ∫g 𝑓 𝑥 𝑑𝑥.
Monte Carlo Integration (Univariate)

f
Task: Given a univariate function 𝑓 𝑥 , calculate 𝐼 = ∫g 𝑓 𝑥 𝑑𝑥.

1. Draw 𝑛 samples from 𝑎, 𝑏 uniformly at random; denote them by


𝑥2 , ⋯ , 𝑥> .
2
2. Calculate 𝑄> = 𝑏 − 𝑎 ⋅ ∑>mn2 𝑓 𝑥m .
>
f
3. Return 𝑄> as an approximation to the integral 𝐼 = ∫g 𝑓 𝑥 𝑑𝑥.
Monte Carlo Integration (Univariate)

f
Task: Given a univariate function 𝑓 𝑥 , calculate 𝐼 = ∫g 𝑓 𝑥 𝑑𝑥.

1. Draw 𝑛 samples from 𝑎, 𝑏 uniformly at random; denote them by


𝑥2 , ⋯ , 𝑥> .
2
2. Calculate 𝑄> = 𝑏 − 𝑎 ⋅ ∑>mn2 𝑓 𝑥m .
>
f
3. Return 𝑄> as an approximation to the integral 𝐼 = ∫g 𝑓 𝑥 𝑑𝑥.
Monte Carlo Integration (Univariate)

f
Task: Given a univariate function 𝑓 𝑥 , calculate 𝐼 = ∫g 𝑓 𝑥 𝑑𝑥.

1. Draw 𝑛 samples from 𝑎, 𝑏 uniformly at random; denote them by


𝑥2 , ⋯ , 𝑥> .
2
2. Calculate 𝑄> = 𝑏 − 𝑎 ⋅ ∑>mn2 𝑓 𝑥m .
>
f
3. Return 𝑄> as an approximation to the integral 𝐼 = ∫g 𝑓 𝑥 𝑑𝑥.
Monte Carlo Integration (Univariate)

f
Task: Given a univariate function 𝑓 𝑥 , calculate 𝐼 = ∫g 𝑓 𝑥 𝑑𝑥.

1. Draw 𝑛 samples from 𝑎, 𝑏 uniformly at random; denote them by


𝑥2 , ⋯ , 𝑥> .
2
2. Calculate 𝑄> = 𝑏 − 𝑎 ⋅ ∑>mn2 𝑓 𝑥m .
>
f
3. Return 𝑄> as an approximation to the integral 𝐼 = ∫g 𝑓 𝑥 𝑑𝑥.

Theory: Law of large numbers guarantees 𝑄> → 𝐼, as 𝑛 → ∞.


Monte Carlo Integration (Univariate): Example

2 c
Task: Given function 𝑓 𝑥 = , calculate 𝐼 = ∫d.e 𝑓 𝑥 𝑑𝑥.
2WXYZ [ ⋅ ]^_` [ 8
Monte Carlo Integration (Univariate): Example

2 c
Task: Given function 𝑓 𝑥 = , calculate 𝐼 = ∫d.e 𝑓 𝑥 𝑑𝑥.
2WXYZ [ ⋅ ]^_` [ 8

1. Draw 𝑛 samples from 0.8, 3 uniformly at random; denote them by


𝑥2 , ⋯ , 𝑥> .
Monte Carlo Integration (Univariate): Example

2 c
Task: Given function 𝑓 𝑥 = , calculate 𝐼 = ∫d.e 𝑓 𝑥 𝑑𝑥.
2WXYZ [ ⋅ ]^_` [ 8

1. Draw 𝑛 samples from 0.8, 3 uniformly at random; denote them by


𝑥2 , ⋯ , 𝑥> .
2
2. Calculate 𝑄> = 2.2 ⋅ ∑>mn2 𝑓 𝑥m .
>
Monte Carlo Integration (Univariate): Example

2 c
Task: Given function 𝑓 𝑥 = , calculate 𝐼 = ∫d.e 𝑓 𝑥 𝑑𝑥.
2WXYZ [ ⋅ ]^_` [ 8

1. Draw 𝑛 samples from 0.8, 3 uniformly at random; denote them by


𝑥2 , ⋯ , 𝑥> .
2
2. Calculate 𝑄> = 2.2 ⋅ ∑>mn2 𝑓 𝑥m .
>
c
3. Return 𝑄> as an approximation to the integral 𝐼 = ∫d.e 𝑓 𝑥 𝑑𝑥.
Monte Carlo Integration (Multivariate)

Task: Given a multivariate function 𝑓 𝐱 , calculate 𝐼 = ∫p 𝑓 𝐱 𝑑𝐱.

𝐱 ∈ ℝN is a vector Ω is a subset of ℝN
Monte Carlo Integration (Multivariate)

Task: Given a multivariate function 𝑓 𝐱 , calculate 𝐼 = ∫p 𝑓 𝐱 𝑑𝐱.

1. Draw 𝑛 samples from set Ω uniformly at random; denote them by


𝐱2 , ⋯ , 𝐱 > .
2. Calculate 𝑉 = ∫p 𝑑𝐱.
Monte Carlo Integration (Multivariate)

Task: Given a multivariate function 𝑓 𝐱 , calculate 𝐼 = ∫p 𝑓 𝐱 𝑑𝐱.

1. Draw 𝑛 samples from set Ω uniformly at random; denote them by


𝐱2 , ⋯ , 𝐱 > .
2. Calculate 𝑉 = ∫p 𝑑𝐱.
2
3. Calculate 𝑄> = 𝑉 ⋅ ∑>mn2 𝑓 𝐱 m .
>
4. Return 𝑄> as an approximation to the integral 𝐼 = ∫p 𝑓 𝐱 𝑑𝐱.
Monte Carlo Integration: Bivariate Example

𝑦
1, if 𝑥 4 + 𝑦 4 ≤ 1;
1 • Let 𝑓 𝑥, 𝑦 = t
0, otherwise.

−1
1
𝑥

−1
Monte Carlo Integration: Bivariate Example

𝑦
1, if 𝑥 4 + 𝑦 4 ≤ 1;
1 • Let 𝑓 𝑥, 𝑦 = t
0, otherwise.
• Let Ω = −1, 1 × −1, 1 .
−1
1
𝑥 • What is 𝐼 = ∫p 𝑓 𝑥, 𝑦 𝑑𝑥 𝑑𝑦?

−1
Monte Carlo Integration: Bivariate Example

𝑦
1, if 𝑥 4 + 𝑦 4 ≤ 1;
1 • Let 𝑓 𝑥, 𝑦 = t
0, otherwise.
• Let Ω = −1, 1 × −1, 1 .
−1
1
𝑥 • What is 𝐼 = ∫p 𝑓 𝑥, 𝑦 𝑑𝑥 𝑑𝑦?
• 𝐼 is the area of the circle:
𝐼 = 𝜋𝑟 4 = 𝜋.
−1
Monte Carlo Integration: Bivariate Example

1. Draw 𝑛 samples from set Ω uniformly


𝑦 at random; denote them by
1
𝑥2 , 𝑦2 , ⋯ , 𝑥> , 𝑦> .

−1
1
𝑥

−1
Monte Carlo Integration: Bivariate Example

1. Draw 𝑛 samples from set Ω uniformly


𝑦 at random; denote them by
1
𝑥2 , 𝑦2 , ⋯ , 𝑥> , 𝑦> .
2. Calculate 𝑉 = ∫p 𝑑𝑥 𝑑𝑦 = 4. (It is the
−1 area of set Ω.)
1
𝑥

−1
Monte Carlo Integration: Bivariate Example

1. Draw 𝑛 samples from set Ω uniformly


𝑦 at random; denote them by
1
𝑥2 , 𝑦2 , ⋯ , 𝑥> , 𝑦> .
2. Calculate 𝑉 = ∫p 𝑑𝑥 𝑑𝑦 = 4. (It is the
−1 area of set Ω.)
1
𝑥
2
3. Calculate 𝑄> = 𝑉 ⋅ ∑>mn2 𝑓 𝑥m , 𝑦m .
>

−1
Monte Carlo Integration: Bivariate Example

1. Draw 𝑛 samples from set Ω uniformly


𝑦 at random; denote them by
1
𝑥2 , 𝑦2 , ⋯ , 𝑥> , 𝑦> .
2. Calculate 𝑉 = ∫p 𝑑𝑥 𝑑𝑦 = 4. (It is the
−1 area of set Ω.)
1
𝑥
2
3. Calculate 𝑄> = 𝑉 ⋅ ∑>mn2 𝑓 𝑥m , 𝑦m .
>
4. Return 𝑄> as an approximation to the
−1
integral 𝜋 = ∫p 𝑓 𝑥, 𝑦 𝑑𝑥 𝑑𝑦.
Application 5: Estimate of Expectation
Expectation

• Let 𝑋 be a 𝑑-dimensional random vector.


• Let 𝑝 𝐱 be a probability density function (PDF).
Expectation

• Let 𝑋 be a 𝑑-dimensional random vector.


• Let 𝑝 𝐱 be a probability density function (PDF).
• Property: ∫ℝ~ 𝑝 𝐱 𝑑𝐱 = 1.
Expectation

• Let 𝑋 be a 𝑑-dimensional random vector.


• Let 𝑝 𝐱 be a probability density function (PDF).
• Property: ∫ℝ~ 𝑝 𝐱 𝑑𝐱 = 1.
2
• E.g., PDF of uniform distribution is 𝑝 𝑥 = , for 𝑥 ∈ 0, 𝑡 .

Expectation

• Let 𝑋 be a 𝑑-dimensional random vector.


• Let 𝑝 𝐱 be a probability density function (PDF).
• Property: ∫ℝ~ 𝑝 𝐱 𝑑𝐱 = 1.
2
• E.g., PDF of uniform distribution is 𝑝 𝑥 = , for 𝑥 ∈ 0, 𝑡 .

2 [„… 8
• E.g., PDF of univariate Gaussian is 𝑝 𝑥 = exp − .
• 4: 4• 8
Expectation

• Let 𝑋 be a 𝑑-dimensional random vector.


• Let 𝑝 𝐱 be a probability density function (PDF).
• Let 𝑓 𝐱 be any function of vector variable.
• Expectation: 𝔼‡~‰ 𝑓 𝑋 = ∫ℝ~ 𝑓 𝐱 ⋅ 𝑝 𝐱 𝑑𝐱.
Monte Carlo Estimate of Expectation

Task: Estimate the expectation 𝔼‡~‰ 𝑓 𝑋 = ∫ℝ~ 𝑓 𝐱 ⋅ 𝑝 𝐱 𝑑𝐱.


Monte Carlo Estimate of Expectation

Task: Estimate the expectation 𝔼‡~‰ 𝑓 𝑋 = ∫ℝ~ 𝑓 𝐱 ⋅ 𝑝 𝐱 𝑑𝐱.

1. Draw 𝑛 random samples from the probability distribution 𝑝(𝐱);


denote them by 𝐱2 , ⋯ , 𝐱 > .
Monte Carlo Estimate of Expectation

Task: Estimate the expectation 𝔼‡~‰ 𝑓 𝑋 = ∫ℝ~ 𝑓 𝐱 ⋅ 𝑝 𝐱 𝑑𝐱.

1. Draw 𝑛 random samples from the probability distribution 𝑝(𝐱);


denote them by 𝐱2 , ⋯ , 𝐱 > .
2
2. Calculate 𝑄> = ∑>mn2 𝑓 𝐱 m .
>
Monte Carlo Estimate of Expectation

Task: Estimate the expectation 𝔼‡~‰ 𝑓 𝑋 = ∫ℝ~ 𝑓 𝐱 ⋅ 𝑝 𝐱 𝑑𝐱.

1. Draw 𝑛 random samples from the probability distribution 𝑝(𝐱);


denote them by 𝐱2 , ⋯ , 𝐱 > .
2
2. Calculate 𝑄> = ∑>mn2 𝑓 𝐱 m .
>
3. Return 𝑄> as an approximation to 𝔼‡~‰ 𝑓 𝑋 .
Monte Carlo and Beyond
Monte Carlo

• The term “Monte Carlo method"


was firstly introduced in 1947 by
Nicholas Metropolis.

Reference

• Metropolis. The beginning of the Monte


Carlo method. Los Alamos Science, 125–130,
Casino de Monte-Carlo, Monaco 1987.
Monte Carlo Algorithms

• Monte Carlo refers to algorithms that rely on repeated random


sampling to obtain numerical results.
• The output of Monte Carlo algorithms can be incorrect.
• In all of our examples, the algorithms’ outputs are incorrect.
• But they are close to the correct solution.
Monte Carlo Algorithms

• Monte Carlo refers to algorithms that rely on repeated random


sampling to obtain numerical results.
• The output of Monte Carlo algorithms can be incorrect.
• Las Vegas algorithms are those always produce the correct answers.
• E.g., random quicksort.
• Atlantic City algorithms are polynomial-time randomized algorithms
that answer correctly w.p. greater than 75%.
Question
What is the area of the grey region?
𝑦
2

2
𝑥
0 1
What are the areas of the red and blue regions?
𝑦

0
𝑥
1 2
Thank you!

You might also like