Monte Carlo method (simulation-based estimation)
The Monte Carlo method estimates probabilities and integrals using random sampling.
It is especially useful when exact formulas are hard or high-dimensional.
The hallmark is the convergence rate: the typical error shrinks like \(1/\sqrt{n}\), where \(n\) is the number of samples.
1) Core idea: average of random samples
If \(Z_1,\dots,Z_n\) are i.i.d. samples with finite mean \(E[Z]\), then the sample mean
\(\overline{Z}=\frac{1}{n}\sum_{i=1}^{n} Z_i\) satisfies:
\[
\overline{Z}\xrightarrow[n\to\infty]{}E[Z]\quad \text{(law of large numbers)}.
\]
A standard uncertainty scale is the standard error:
\[
\mathrm{SE}(\overline{Z})=\sqrt{\frac{\mathrm{Var}(Z)}{n}}.
\]
This is why Monte Carlo is “slow”: to reduce error by \(10\times\), you usually need about \(100\times\) more samples.
2) Estimating \(\pi\) with dart throws
Consider the unit square \([0,1]\times[0,1]\) and the quarter circle \(x^2+y^2\le 1\).
The quarter circle area is \(\pi/4\) and the square area is \(1\).
\[
p = P\bigl(X^2+Y^2\le 1\bigr)=\frac{\text{area of quarter circle}}{\text{area of square}}=\frac{\pi/4}{1}=\frac{\pi}{4},
\quad (X,Y)\sim U([0,1]^2).
\]
If we sample \((x_i,y_i)\) uniformly and define the indicator
\(I_i=\mathbf{1}\{x_i^2+y_i^2\le 1\}\), then:
\[
\widehat{p}=\frac{1}{n}\sum_{i=1}^{n} I_i=\frac{\#\text{inside}}{n},
\qquad
\widehat{\pi}=4\widehat{p}.
\]
Because \(I_i\) is Bernoulli, \(\mathrm{Var}(I)=p(1-p)\). A practical plug-in error scale is:
\[
\mathrm{SE}(\widehat{\pi}) \approx 4\sqrt{\frac{\widehat{p}(1-\widehat{p})}{n}}.
\]
3) Estimating an integral \(\int_a^b f(x)\,dx\)
Let \(X\sim U(a,b)\). Then:
\[
E[f(X)] = \frac{1}{b-a}\int_a^b f(x)\,dx
\quad\Longrightarrow\quad
\int_a^b f(x)\,dx = (b-a)E[f(X)].
\]
Using samples \(X_1,\dots,X_n\sim U(a,b)\):
\[
\widehat{I}=(b-a)\cdot \frac{1}{n}\sum_{i=1}^{n} f(X_i).
\]
If \(\sigma_f^2=\mathrm{Var}(f(X))\), then:
\[
\mathrm{SE}(\widehat{I}) \approx |b-a| \sqrt{\frac{\sigma_f^2}{n}}.
\]
In practice, \(\sigma_f^2\) is estimated from the sample:
\[
\widehat{\sigma}_f^2 \approx \overline{f^2} - \overline{f}^2,
\qquad
\overline{f}=\frac{1}{n}\sum_{i=1}^{n}f(X_i),\quad
\overline{f^2}=\frac{1}{n}\sum_{i=1}^{n}f(X_i)^2.
\]
4) Estimating probabilities in a region
Suppose \((X,Y)\) is uniform in a rectangle \(R_0=[x_{\min},x_{\max}]\times[y_{\min},y_{\max}]\),
and you want \(P((X,Y)\in R)\) where \(R\subseteq R_0\) is defined by a test like \(g(x,y)\le 0\).
\[
I_i=\mathbf{1}\{g(X_i,Y_i)\le 0\},\qquad
\widehat{p}=\frac{1}{n}\sum_{i=1}^{n} I_i.
\]
Again \(I_i\) is Bernoulli, so the standard error scale is:
\[
\mathrm{SE}(\widehat{p}) \approx \sqrt{\frac{\widehat{p}(1-\widehat{p})}{n}}.
\]
5) Variance reduction (university extension)
The main cost in Monte Carlo is variance. Methods that reduce variance can dramatically improve accuracy for the same \(n\):
- Antithetic variates: pair samples (e.g., \(U\) and \(1-U\)) to cancel noise.
- Control variates: use a correlated quantity with known mean to correct the estimator.
- Importance sampling: sample more often where the integrand contributes most, then reweight.
- Stratified sampling: divide the domain into subregions and sample each.
Even with variance reduction, the basic Monte Carlo scaling is often close to \(1/\sqrt{n}\); the goal is to make the constant factor much smaller.
6) What this calculator does
- Generates uniform random samples appropriate to the selected estimator.
- Computes the estimate and an uncertainty scale (standard error).
- Displays an animated scatter plot with numeric axes and an in-plot estimate badge.
- Provides step-by-step math/work showing the estimator formula.