Monte Carlo Integration

Zach Wolpe
3 min readNov 18, 2019

--

Monte Carlo Integration is a sampling technique of approximating the area under a complex curve — often required in calculus — that is not easily integrated.

This article provides a simple introduction to MC integration — implemented in Python

One may find an equation of interest to non-integrable (that is we cannot integrate the function), but as the area under the curve is often of interesting, sampling techniques can be used to approximate the true value. Conducted as follows.

Algorithm

  1. Define the domain space to search
  2. Randomly sample from the domain space
  3. Assess the sample to be above or below the true function by using its values as inputs — solving for f(x) at the specific sampled value of X & comparing this value to the sampled Y
  4. Repeat steps 2 and 3 many comes
  5. Compute the total area of domain space
  6. Compute the proportion of samples below the curve: a converging approximation of the true area under the curve
  7. Multiply 5 and 6 to get the estimate for the area under the curve

That’s it!

Implementation

Consider the following example, using an integrable function such that the true values can be confirmed.

(Taken from a Statistical Learning Module at the University of Pretoria).

Import the required packages

Define the equation to integrate f(x)

Define the MCMC sampling function

Results

Compute the Monte Carlo Integration results for question 1.1, 1.2, 1.3.

Question 1.1

Question 1.2

Question 1.3

As this technique is readily extended to multivariable functions. In fact, numerically integrating functions of many variables almost always require Monte Carlo integration or some variation.

Monte Carlo techniques allow us to leverage computation to solve mathematical problems in new ways — however, as the function of interest grows in dimensionality the sparsity of the domain space requires exponentially increasing computational power.

All code, as well as a SAS implementation, available here.

--

--

Zach Wolpe
Zach Wolpe

Written by Zach Wolpe

Machine Learning Engineer. Writing for fun.

No responses yet