Skip to main content

5.5 Monte Carlo integration

Source: Ideas from Computational Physics (Landau, Páez, Bordeianu), §5.5. Rewritten in original wording. Continues 5.4 Gaussian quadrature. Labs: 5.5b Monte Carlo integration labs.

Pond area by stone throwing​

Imagine a pond inside a rectangular fence of known area AboxA_{\mathrm{box}}. Throw NN stones uniformly at random into the box; count how many land in the water (NpondN_{\mathrm{pond}}). Walk the hit-fraction ratio, the pond-area estimator, and π≃4Nhit/N\pi\simeq 4 N_{\mathrm{hit}}/N — pick the matching equation at each step.

No antiderivative, no mesh — only a uniform sampler and a membership test (“inside the pond?”). That is the Monte Carlo idea for integration: replace geometry by counting.

Figure 5.4 — stones in a square; hits inside the inscribed disk estimate the circle’s area.

π\pi from a unit circle in a square​

Put the unit disk x2+y2≤1x^{2}+y^{2}\le 1 inside the square [−1,1]2[-1,1]^{2} of side 22 (area Abox=4A_{\mathrm{box}}=4). The guide’s last two steps are the π/4\pi/4 hit fraction and the factor-of-four estimator. With the chapter LCG and N=104N=10^{4} throws you typically land near 3.143.14 — close enough to see the method, not a replacement for atan(1)\mathrm{atan}(1) in production. The statistical error scales like 1/N1/\sqrt{N}: quadrupling the sample count halves the typical fluctuation.

Why Monte Carlo for integrals?​

In one dimension, trapezoid, Simpson, and Gauss usually beat crude Monte Carlo for smooth ff. Monte Carlo shines when:

  • the domain is awkward (irregular pond, complicated acceptance region);
  • the dimension DD is large — a grid with MM points per axis needs MDM^{D} evaluations, which explodes;
  • you only need a rough answer with a transparent error bar ∼1/N\sim 1/\sqrt{N}.

The next lesson (§5.6) makes the mean-value form precise and shows the DD-independence of the 1/N1/\sqrt{N} law.

Error investigation: trap / Simpson / Gauss / MC​

A standard calibration integral is

I=∫01e−t dt=1−e−1.I=\int_0^{1} e^{-t}\,dt=1-e^{-1}.

Run trapezoid, Simpson, Gauss–Legendre, and a mean-value Monte Carlo estimate at several NN, then plot log⁡10∣Iapprox−I∣\log_{10}|I_{\mathrm{approx}}-I| versus log⁡10N\log_{10} N. On that log–log plane a clean power law

ε∼C Nα\varepsilon\sim C\,N^{\alpha}

appears as a straight segment with slope α\alpha. Deterministic rules for smooth ff often show steep negative slopes (Simpson α≈−4\alpha\approx -4 in hh, Gauss even steeper in NN for analytic integrands) until round-off flattens the floor — algorithms stop improving when floating-point noise dominates (the Fig. 5.5 idea). Monte Carlo’s slope is near α=−1/2\alpha=-1/2; it loses in 1D on this smooth problem, but that shallow slope does not get worse when DD grows.

Use the explorers below (and §5.6’s mean-value chart) to compare regimes rather than memorizing a single “best” rule.

Quiz

Monte Carlo integration

1 / 3

Stone-throwing estimates A_pond by…

Pick an answer to see if you are right.

Next: 5.5b Monte Carlo integration labs.