Monte Carlo integration: how many samples?

The question
Choose a sample budget, then inspect the estimates.
- 1 SeeChoose a budget before sampling.
- 2 TryCompare costs as dimension grows.
- 3 ProveDerive the error scale.
- 4 ApplyPlan and interpret an integral estimate.
Before you start · 3 quick checks on averages, uniform sampling, and spread
Two ideas before you start
2 ideasA random location gives a height
A parabola and a vertical probe at the selected horizontal location.
Choose a location to read its height. Random integration samples the horizontal location; the function determines the height.
Exact definition
For a finite interval with \(a\lt b\), a uniform draw \(U\) has density \(1/(b-a)\) on the interval and zero outside. Subintervals of the same length have the same probability. A height \(Y=f(U)\) need not have a uniform distribution.
The buttons are chosen locations, not random draws. The simulator below instead uses independent uniform locations to estimate an integral. In a box in higher dimensions, each coordinate is uniform on its own interval, independently of the other coordinates.
Turn heights into an average
Bars show a short list of heights; a dashed line shows their average.
Switch lists. Multiplying the average height by the domain's width or volume produces an integral estimate.
Exact definition
Let \(D\) be a box of finite positive volume \(V\). For iid uniform points \(U_i\) in \(D\), define
\[\widehat I_n=\frac Vn\sum_{i=1}^n f(U_i).\]Here iid means independent and identically distributed. The target is the fixed integral \(I=\int_D f(u)\,du\). Assume \(f\) is measurable and square-integrable on \(D\); then its uniform-sample mean and variance exist. These illustrative lists are arithmetic examples, separate from the simulator.
Refresher: what varies between runs?
The integral and sampling rule stay fixed. The random locations change, so the estimated integral changes. The standard deviation of those estimates across repeated samples is called their standard error. The absolute error of one estimate is its distance from the true integral. Those quantities describe different things.
Numerical integration constructs deterministic rules from a grid. Here no smoothness is assumed; the function only needs to be square-integrable. We choose an example with a known integral to measure the realized error; in an actual unknown-integral problem that comparison is unavailable.
Choose the sample budget
- Choose the smallest offered multiplier that halves the standard error.
- After checking, select a seeded run and a sample count to inspect its estimate.
- Start over hides the result and clears your choice.
Keep the function and sampling distribution fixed. The target concerns the spread across repeated samples. It does not ask for a promise about the next run's absolute error.
| n | Estimate | Absolute error | SE |
|---|
The shaded band is centered on the known integral and extends two theoretical standard errors in each direction. It shows a reference scale for repeated estimates. It is not an interval computed from the current sample, and it is not a guaranteed error bound. A path may leave it or move away from the integral after more draws.
What the simulator computes
Every larger sample retains the earlier locations and adds new ones. Checkpoints on one path are therefore dependent. Changing the seed selects another reproducible path, not evidence of a new theorem. The mathematical results assume ideal iid uniform points; the browser uses a seeded pseudorandom generator to illustrate that model.
Do not keep trying seeds until an estimate looks favorable. For a reported estimate, choose the sample count in advance. Repeatedly inspecting an ordinary confidence interval and stopping when it looks satisfactory changes its coverage behavior.
Add coordinates
A product grid chooses every combination of coordinate locations. Predict its evaluation cost before opening the comparison. This question counts points; it does not compare the accuracy of the two methods.
| Dimension | Grid points | MC points | MC coordinates |
|---|
Holding the Monte Carlo point count fixed holds only the number of function evaluations fixed. Generating a point requires more coordinates in a larger box, and evaluating the function can become more expensive. The volume and the variance of the sampled heights can also change. The sample-count exponent alone is not a dimension-independent cost guarantee.
A family where the variance can be computed
For a concrete family on the unit cube, take \(f_d(u)=d^{-1}\sum_{j=1}^d u_j^2\). The coordinate contributions are independent under uniform sampling. Their average has the same integral in every dimension, while its variance decreases with dimension. The explorer reports that family's theoretical standard error for its fixed point budget.
This improvement comes from the chosen average of independent coordinates. A different family can have an increasing variance or a rare, narrow region that most samples miss. Grid rules such as Simpson's rule can also gain accuracy from smoothness, which plain Monte Carlo does not use. A fair accuracy comparison must specify the integrand and an error criterion alongside a point count.
Use what you found above: meet a precision goal
Return to the one-dimensional parabola. Select the smallest offered sample count that meets each theoretical SE target. A new goal resets the count. This is a planning exercise with a known population variance.
Derive the scale of the error
Let \(D\) be a box with finite volume \(V\gt0\). Let \(f\) be measurable and square-integrable, and let \(U_i\) be iid uniform points in \(D\). Put \(Y_i=f(U_i)\), \(I=\int_D f\), and \(\sigma^2=\operatorname{Var}(Y_i)\). For the builder assume \(0\lt\sigma^2\lt\infty\) and an integer \(n\ge2\) fixed before sampling. Then
\[\begin{aligned}E[\widehat I_n]&=I,\\\operatorname{Var}(\widehat I_n)&=V^2\sigma^2/n,\\\operatorname{RMSE}(\widehat I_n)&=V\sigma/\sqrt n.\end{aligned}\]RMSE means the square root of the expected squared error relative to the fixed integral. It equals the standard error here because the estimator is unbiased. No normal approximation is needed for these identities.
Use the uniform density to identify the target. Then use independence to add variances and square the multiplier when scaling a random variable. Build the steps or open the read-through.
The same identities hold at a single draw. If the variance is zero, the sampled function is constant almost everywhere and the estimator has zero error almost surely. The builder excludes these cases so its competing formulas remain distinct. A negative integral is allowed; its sign does not change the variance calculation.
From an error scale to an interval
When the population variance is unknown, estimate it from the sampled heights:
\[s_n^2=\frac{1}{n-1}\sum_i(Y_i-\bar Y_n)^2.\]The estimated SE is \(Vs_n/\sqrt n\). Under iid sampling and positive finite variance, the central limit theorem justifies a large-sample normal interval. Using the conventional normal multiplier gives
\[\widehat I_n\ \pm\ 1.96\frac{Vs_n}{\sqrt n}.\]With the sample size fixed in advance, the coverage probability tends to about 95% as \(n\) grows; at any finite \(n\) it is only approximately 95%. This is a repeated-sampling property. A realized interval either contains the fixed integral or misses it. Finite variance does not specify how large the sample must be for a useful approximation, especially when rare large heights dominate.
For a rigorous finite-sample statement with known variance, Chebyshev's inequality gives \(P(|\widehat I_n-I|\ge\varepsilon)\le V^2\sigma^2/(n\varepsilon^2)\) for \(\varepsilon\gt0\); cap the bound at one. It can be much less informative than the normal approximation, but it does not require approximate normality.
Sampling from a nonuniform density without correcting the weights changes the target. If the second moment is infinite, the variance calculation and this RMSE rate are unavailable. For dependent samples, covariance terms remain. More draws cannot repair a wrong sampling distribution or remove a bias introduced by approximating the function itself.
Plan an estimate and report its uncertainty
Use what you found above. Each problem has hints and a worked solution. Enter a decimal without units. Keep extra precision until the final answer and use the tolerance stated in the question.
The same averaging idea appears in Monte Carlo pricing, where a model specifies the sampling distribution and a discounted payoff is the quantity averaged. That lesson adds the financial assumptions needed to interpret the expectation as a price.
Further reading
- A. B. Owen, Monte Carlo Theory, Methods and Examples, chapter 2, “Simple Monte Carlo.”
- C. P. Robert and G. Casella, Monte Carlo Statistical Methods, chapter 3, “Monte Carlo Integration.”