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 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’.
- Uniform input
- Average the integrand
- Recover the 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.
A saved draft is open. To run the published example, open Tools and select Restore example. Recover draft brings your code back.
Execution error
Technical error details
Your result
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?
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?
Need a hint?
Hint 1
Draw the constant-height rectangle.
Hint 2
Multiply average height by interval width.