What is Monte Carlo Integration?
Monte Carlo integration is a numerical technique for approximating definite integrals using randomness. It's particularly powerful for integrals that are difficult or impossible to solve analytically, especially those in high dimensions or with complex boundary shapes.
The core idea is surprisingly simple: instead of calculating the integral precisely, we estimate it by taking a large number of random samples within the integration domain and averaging the results. According to the Law of Large Numbers, this average will converge to the true value of the integral as the number of samples increases.
The Underlying Method
Let's say we want to compute the definite integral of a function $f(x)$ over an interval $[a, b]$:
The Monte Carlo method approximates this integral by first relating it to an expected value. The mean value of $f(x)$ over the interval $[a, b]$ is given by:
Rearranging this, we see that the integral $I$ is just the volume of the integration domain $(b-a)$ multiplied by the average value of the function, $I = (b-a) \langle f \rangle$.
We can estimate this average value, $\langle f \rangle$, by generating $N$ random points, $x_1, x_2, \ldots, x_N$, uniformly distributed within the interval $[a, b]$, and then calculating the average of the function evaluated at these points:
Substituting this approximation back into our equation for $I$, we get the Monte Carlo estimator for the integral:
The accuracy of this approximation improves as $N$ (the number of samples) increases. The error typically decreases proportionally to $1/\sqrt{N}$.
Classic Example: Estimating $\pi$
A famous example is estimating $\pi$ by finding the area of a unit circle ($r=1$). This can be framed as a double integral. We define an "indicator function" $f(x, y)$ over a square domain that spans from $[-1, 1]$ on both axes:
The area of the unit circle is the integral of this function over the square domain from $(-1, -1)$ to $(1, 1)$:
The Monte Carlo method approximates this by multiplying the area of the integration domain (the square, which has area $2 \times 2 = 4$) by the average value of the function. We find this average by sampling $N$ random points $(x_i, y_i)$ within the square.
Since $f(x, y)$ is 1 for points inside the circle and 0 for points outside, the sum $\sum f(x_i, y_i)$ simply counts the number of points that fall inside the circle ($N_{inside}$).
This gives us the approximation: $A_{circle} \approx (\text{Area}_{square}) \cdot \frac{N_{inside}}{N_{total}}$. Since $A_{circle} = \pi r^2 = \pi$ for a unit circle, we get:
Interactive 1D Integral Calculator
Use standard JavaScript math syntax. Ex: `Math.sin(x)`.