CS(2) Basic Monte Carlo Integration

Sample Mean Method

Carla Martins
3 min readNov 11, 2024

Integral Calculation as Sample Mean

The sample mean method is a fundamental approach in Monte Carlo integration. It allows us to approximate the value of an integral by using random sampling and calculating the average of the function’s values at those points.

Given a function h(x) defined over the interval [a, b], we want to estimate the integral:

Using the Monte Carlo Integration

  1. Generate a large number n of random values x1, x2, x3, …, xn in the interval [a,b];
  2. Calculate the values of h(x) at these random points;
  3. Take the average of these values, then multiply by the length of the interval (b-a), to estimate the integral.

The Monte Carlo estimate is given by:

As n (the sample size) increases, this estimate becomes more accurate.

Practical example with R:
Let’s estimate the integral of cos(x) over the interval [0, pi/2] using the sample mean method. Mathematically this is represented by:

--

--

Carla Martins
Carla Martins

Written by Carla Martins

Compulsive learner. Passionate about technology. Speaks C, R, Python, SQL, Haskell, Java and LaTeX. Interested in creating solutions.