LessonThis is a lesson restated in new words. Any figure in it is an example from the source, not this desk's record.

Monte Carlo integration estimates a definite integral by sampling the input at random and averaging the rectangle areas those samples suggest. On an interval from a to b, draw inputs uniformly, evaluate the function, multiply by the width b minus a, and divide by the number of draws. The lesson tests the idea on the integral from 0 to 2 of 4 x cubed, whose exact value is 16. With 10,000 uniform draws the script prints about 15.985.

Why it matters

Some integrals have an antiderivative and a short exact answer. Some do not. The method needs only the ability to evaluate the function, which is why the lesson bothers with an integral it can also solve by hand. The exact 16 is a scoreboard. A method that cannot get near 16 on a cubic is not ready for an integral with no scoreboard. The same averaging habit is the one used when a price is an expectation and the expectation has no closed form.

The idea in plain words

Draw the curve and mark two ends, a and b. The integral is the area under the curve between them. Scatter points along the horizontal axis by drawing each one uniformly between a and b, so every spot in the interval is equally likely. Evaluate the function at each point. Those heights are the vertical sides of rectangles. The lesson gives every rectangle the same width, the full length b minus a, and then averages the areas. Averaging repairs the oversized width. One rectangle of width b minus a and height f(x) is too big to be a slice. The mean of many such rectangles is the estimate of the area.

In symbols, add (b minus a) times f at each sample, from the first sample to the last, and multiply by one over the sample count. That is (b minus a) times the average function value. The lesson states that this average is approximately the integral from a to b.

The coding test uses a from 0 and b from 2, and f(x) = 4 x cubed. A plot with 100 evenly spaced inputs, each cubed and multiplied by four, draws the curve. The estimate itself uses 10,000 uniform draws on that same interval, not the 100 plotting points. The width is 2. The average of the heights, times 2, prints 15.985.

How to use it

Define a function that returns four times x cubed. Draw 10,000 uniforms between 0 and 2 with NumPy. Evaluate the function on the whole array. Multiply by 2, sum, and divide by 10,000. Compare the print with 16. A second run moves, because the draws move. More draws pull the print toward 16. The lesson's single reported approximation is 15.985.

Any other integrand replaces the function body. Any other limits replace the uniform bounds and the width. The width has to be the distance between the same bounds you sampled. Sampling from 0 to 2 and multiplying by a width of 1 estimates a different area.

The plot is optional. It is how the lesson shows the curve before the average is trusted. The estimate does not read the picture. It reads the random sample.

What usually goes wrong

Dividing by the wrong count, or forgetting the width, is the arithmetic error. The lesson's 15.985 is the average height times 2, with 10,000 in the denominator. Using 100 because that was the plotting grid understates the sample and, if those 100 points are evenly spaced, answers a different method than the uniform Monte Carlo the lesson defined.

Sampling from a normal distribution instead of a uniform on [a, b] changes the estimator. The lesson's points are uniform so that the plain average, times the width, lines up with the area. A different sampling distribution needs a different weight.

The exact 16 is available only because this cubic has an antiderivative. Treating 15.985 as more authoritative than 16 inverts the lesson. The gap of a few hundredths is the sampling gap on this run. On an integral with no closed form, that same gap is invisible, which is why the lesson rehearses on a known answer.

The rectangle story uses a width of b minus a for every sample and then averages. Building true Riemann slices with a small delta x, and also dividing by the sample count, double-counts the scaling. Follow one construction. The lesson's construction is the wide rectangle, averaged.

Questions people ask

What integral is the test?

From 0 to 2 of 4 x cubed d x. The analytical value used as the baseline is 16.

What did 10,000 samples return?

About 15.985, from uniform draws on [0, 2], averaged and multiplied by the width 2.

When is the method the one to use?

When the integral has no analytical antiderivative and the function can still be evaluated at a random input. The cubic is the lesson's check, not the only target.