Lensa ML
Lensa ML

How Monte Carlo Works

Step 1 of 6

Random Sampling

Drawing observations from a distribution

Number of samples (N)30
0.00.10.20.30.40.50.60.70.80.91.0ValueCountmean=0.556
N SAMPLES
30
MEAN
0.5561
STD DEV
0.2672

As we draw more samples, the histogram starts to resemble a uniform distribution. The mean is settling closer to 0.5.

Step 1 of 6: Random Sampling

Drawing observations from a distribution

Number of samples (N)30
0.00.10.20.30.40.50.60.70.80.91.0ValueCountmean=0.556
N SAMPLES
30
MEAN
0.5561
STD DEV
0.2672

As we draw more samples, the histogram starts to resemble a uniform distribution. The mean is settling closer to 0.5.

Step 2 of 6: Law of Large Numbers

Sample means converge to the true mean

Number of samples (N)50
0.500.250.50.751Sample numberRunning mean+0.05-0.05
N
50
RUNNING MEAN
0.4594
TRUE MEAN
0.5000
ERROR
0.0406

The running mean is settling down and approaching 0.5. Fluctuations are smaller, but the estimate still wanders around the true value.

Step 3 of 6: Estimating π

Throw darts to approximate a circle's area

Number of darts (N)50
011xyinsideoutside
N DARTS
50
INSIDE
40
π ESTIMATE (click)
3.2000
ERROR
0.0584

As we throw more darts, the estimate improves. The area of a quarter-circle with radius 1 is π/4, so multiplying the ratio by 4 gives us π. More darts = better estimate.

Step 4 of 6: Monte Carlo Integration

Approximate integrals by random sampling

Number of samples (N)30
Estimate = (1/N) × Σ f(xᵢ) where xᵢ ~ Uniform[0,1]est2/πxf(x) = sin(πx)00.5100.250.50.751
N
30
ESTIMATE (click)
0.6256
TRUE VALUE (click)
0.6366
ERROR
0.0110

With few samples, each random x-value contributes a noisy estimate of the integral. The average of f(xᵢ) values is a rough approximation of the area under sin(πx).

Step 5 of 6: Importance Sampling

Sample smarter, not harder

Number of samples (N)40
Proposal center0.70
targetproposal00.250.50.751uniformx
N
40
UNIFORM EST
0.920
IMPORTANCE EST
1.065
VAR RATIO
0.10

The proposal distribution closely matches the target peak. Importance sampling concentrates samples where the target is large, reducing variance compared to uniform sampling.

Step 6 of 6: MCMC — Metropolis-Hastings

A random walk that samples from any distribution

Step size0.80
Number of steps (N)100
-1-100112233startacceptedrejected
N STEPS
100
ACCEPTED
74
ACCEPT RATE
74%
CHAIN MEAN
(1.4, 0.9)

Good balance! The chain accepts enough proposals to move efficiently while rejecting enough to avoid wandering too far. In 2D, acceptance rates around 40–70% give efficient exploration.