Monte Carlo · an experiment in counting

Count points. Estimate an area.

Sample uniformly inside the rectangle. Count a point as 1 below the curve and 0 above it. The fraction of 1s estimates the fraction of the rectangle under the curve.

50

The slider uses a logarithmic scale from 1 to 100,000 samples. Moving it replays the same sequence of points. New random run generates another sequence.

1. Count the points under f(x)

Uniform points in a rectangle of width 4 and height 1.2. Circles under the curve count as 1; crosses above it count as 0.

Every point has the same chance of landing in any equal-sized patch.

2. Watch the estimate settle

The running area estimate fluctuates around the true area. The horizontal axis shows sample count on a logarithmic scale.

The vertical scale stays fixed. Moving right means more samples, from the same run.

Rectangle area × fraction below = estimated area

4.8×—50=—

True area · reference

≈ 1.66017

Absolute error

—

More samples tend to stabilize the estimate, but a new sample can move it farther from the true area. Try another random run: the path changes; the area we are estimating stays the same.

The curve and the reference value

Here we use a curve whose integral we can calculate, so we can compare our estimate with the answer.

Here, a = 0, b = 4, and c = 1.2, so the rectangle area is 4.8 square units. With t = x / 4, the curve is

f(x) = 0.18 + 0.50 exp(−((t − 0.28) / 0.12)²) + 0.30 exp(−((t − 0.74) / 0.18)²) + 0.07 sin²(6πt).

Each exponential and sin² term is between 0 and 1, so 0.18 ≤ f(x) ≤ 1.05 < c. The reference integral is 1.6601700413794354 square units, evaluated from the Gaussian error function and the sine antiderivative, independently of the sampled points. All displayed decimals are rounded.

Samples come from a seeded pseudorandom generator. Reset returns to one sample and preserves the sequence; New random run changes it. The count and estimate use every sample; the scatter plot displays at most the first 5,000 points to stay readable.