Monte Carlo Integration
Monte Carlo Integration
Monte Carlo integration uses random sampling to estimate integrals — particularly powerful in high dimensions where deterministic quadrature becomes infeasible.
Basic Idea
To compute , draw uniform random samples and estimate:
The error scales as regardless of dimension — a huge advantage over grid-based methods in dimensions (which scale as ).
Importance Sampling
When is sharply peaked, plain sampling is inefficient. Instead, sample from a distribution that is large where is large:
Optimal choice: (reduces variance to zero for positive ).
Estimating π
A classic application: generate random points . The fraction landing inside the unit quarter-circle () converges to :
Reproducible Randomness: LCG
For reproducibility, we use a Linear Congruential Generator (LCG):
with parameters , , (Numerical Recipes constants). Starting from a seed , this generates a deterministic sequence of uniform pseudo-random numbers in .
Implementation
lcg_random(seed, n)— returns list of uniform values via LCGmonte_carlo_integrate(f_func, a, b, N=10000, seed=42)— plain Monte Carlo estimatemonte_carlo_pi(N=10000, seed=42)— estimate using the unit circle method
import math
def lcg_random(seed, n):
a = 1664525
c = 1013904223
m = 2**32
state = seed
result = []
for _ in range(n):
state = (a * state + c) % m
result.append(state / m)
return result