Apply the methodLesson 36 of 76 · 8 min

Estimate an integral by sampling

Express a finite-domain integral as an expectation with the correct scale factor.

Monte Carlo can estimate numerical quantities that are awkward to integrate analytically. For a function on an interval [a, b], draw X uniformly on that interval. The integral equals the interval width times the expectation of f(X).

The width factor converts an average function value into an area. Forgetting it gives a different quantity. We use f(x)=x squared over [0, 2], where the exact integral is available as a check before considering harder functions.

The numerical standard error must be scaled by the same interval width. This estimator uses independent uniform samples and a finite-variance integrand. Other proposals require weights, and an integrable function can still have an estimator with troublesome or infinite variance.

The integral sign ∫abf(x) dx\int_a^b f(x)\,dx means the signed area under f from a to b; f is the integrand, the function being averaged. X ~ U(0,2) reads ‘draw X uniformly between 0 and 2’.

An average needs its domain scale
  1. Uniform input
    X∼U(0,2)X\sim U(0,2)
  2. Average the integrand
    E[X2]=4/3E[X^2]=4/3
  3. Recover the integral
    ∫02x2 dx=2E[X2]=8/3\int_0^2x^2\,dx=2E[X^2]=8/3
Reference illustration. These exact references use the same finite domain and function as the simulation. Omitting the interval width estimates an average, not the requested integral.

Does the sample mean of X² alone equal its integral over an interval of width two?

Write a prediction (optional)

Seeded teaching simulation

Select Run Python below with the code unchanged.

Tools
About Python ↗

The code is editable. Press Ctrl+Enter or ⌘+Enter to run Python from the editor.

Published example output

This result comes from the original Python. Your runs appear above. Build environment ↗

Integral estimate: 2.64167
Estimated MCSE: 0.01688
Exact integral: 2.66667
Unscaled average: 1.32084

Read the first result

The reference case checks both the integrand and domain-width scaling. It does not imply that Monte Carlo is the most efficient method for every integral.

Try an edit

Replace a, b = 0, 2 with a, b = 1, 3. Select Run Python again. Compare Integral estimate with Exact integral and Unscaled average. The existing formula (b**3-a**3)/3 updates automatically. Which estimate includes the interval width?

Back to the Python editor

Scope. This example has a simple exact answer and bounded integrand. More difficult functions need separate variance and numerical-stability checks.

Try a fresh case

You estimate the integral of a constant 3 over [0, 4]. What is the answer?

Choose an answer
Need a hint?
Hint 1

Draw the constant-height rectangle.

Hint 2

Multiply average height by interval width.

Your notes