Registry Synced

Monte Carlo Methods

306 words
2 min read

Reading compass

Now ยท ๐ŸŽฏ Learning Objectives

Monte Carlo Methods

๐ŸŽฏ Learning Objectives

  • Understand the law of large numbers and its role in Monte Carlo
  • Estimate integrals using Monte Carlo simulation
  • Compute confidence intervals for Monte Carlo estimates
  • Implement variance reduction techniques

1.1 Intuition: Estimating by Random Sampling

Monte Carlo methods use random sampling to estimate quantities that are difficult to compute analytically. The idea: generate many random samples, compute the statistic, and average.
๐Ÿ”‘ Key Insight: The law of large numbers guarantees that Monte Carlo estimates converge to the true value as sample size increases.

1.2 Monte Carlo Integration

Estimate ฮธ=โˆซabf(x)dx\theta = \int_a^b f(x) dx:
ฮธ^=bโˆ’anโˆ‘i=1nf(Ui)\hat{\theta} = \frac{b-a}{n} \sum_{i=1}^n f(U_i)
Where UiโˆผUniform(a,b)U_i \sim \text{Uniform}(a,b).
python
import numpy as np
# estimate integral of sin(x) from 0 to pi
n = 10000
x = np.random.uniform(0, np.pi, n)
estimate = np.pi * np.mean(np.sin(x))
print(f"MC estimate: {estimate:.4f}, true: 2.0")

1.3 Variance Reduction

Antithetic Variates

Use negatively correlated pairs: UU and 1โˆ’U1-U.
python
u = np.random.uniform(0, 1, n//2)
estimates = (f(u) + f(1-u)) / 2

Control Variates

Leverage known expectation of a correlated variable.

1.4 Confidence Intervals

ฮธ^ยฑzฮฑ/2sn\hat{\theta} \pm z_{\alpha/2} \frac{s}{\sqrt{n}}
Where ss is the sample standard deviation of the Monte Carlo estimates.

โœ… Practice Questions

Q1: Estimate โˆซ01exdx\int_0^1 e^x dx using MC with 10000 samples.
Solution
python
import numpy as np
x = np.random.uniform(0, 1, 10000)
estimate = np.mean(np.exp(x))  # since (b-a) = 1
print(estimate)  # approx 1.718 (true: e-1 = 1.71828)
Q2: How does the MC error decrease with nn?
Solution
MC error โˆฯƒ/n\propto \sigma / \sqrt{n}. To halve the error, quadruple nn (this is the curse of MC โ€” slow convergence). Join Discord NextImportance Sampling
Document outline

Keep your place and jump directly to a heading.

Table of contents

No note selected

Open a chapter to see its outline, backlinks, and sources here.