Monte Carlo integration

Solving integrals is an important mathematical concept related to areas and other quantities modeled by a function. Informally, a definite integral is defined as the signed area of the region outlined by the function f of a real variable x between a and b. Let's consider a simple function that is integrable between the interval [a, b] defined by the integral:

Monte Carlo integration

The Monte Carlo integration method relies on the law of large numbers to approximate the integral using the Mean Value theorem for integrals. If f(x) is continuous on the [a, b] interval, then there must exist a value u in [a, b] such that we can apply the following formula:

Monte Carlo integration

This formula can be rewritten as follows:

Monte Carlo integration

Since the fundamental theorem of calculus implies F'(x) = f(x), we can rewrite the equation as follows:

Monte Carlo integration

The preceding formula is the first mean value theorem, where f(c) is the average value of f(x) on the [a, b] interval. Since we can use Monte Carlo simulation to obtain the average value of f(x) on the [a, b] interval, we can write the sample mean as follows:

Monte Carlo integration

In the preceding formula, Monte Carlo integration is the sample mean of the simulated distribution of independent uniform random variables U1, U2, …, Un on the interval [a, b]. This formula can be rewritten as follows:

Monte Carlo integration

Therefore, the integer can be approximated by b – a times the sample mean of our simulated distribution on the [a, b] interval. Now, let's apply this method to approximate the integral for Monte Carlo integration as follows:

> u <- runif(100000, min=0, max=4)
> mean(u^2)*(4-0)
[1] 21.34383

Let's check the solution using the integrate() function in R. The integrate function allows us to calculate the integral by adaptive quadrature of functions of one variable over a finite or infinite interval. To use this function, we must first define f(x) and save it in the integrand object as follows:

> integrand <- function(x) {x^2}

Then, we use the integrate() function with our integrand function and set the interval [a, b] and lower and upper limit with the lower and upper arguments, respectively, as follows:

> integrate(integrand, lower = 0, upper = 4)
21.33333 with absolute error < 2.4e-13

When we solve the integral, the exact answer is as follows:

Monte Carlo integration
Monte Carlo integration
Monte Carlo integration

Now, let's solve the integral Monte Carlo integration using both methods.

Using the Monte Carlo method, we can write the following code:

> u <- runif(100000, min=3, max=6)
> mean(cos(u))*(6-3)
[1] -0.4214223

Using the integrate() function, we can write the following code:

> integrand <- function(x) {cos(x)}
> integrate(integrand, lower=3, upper=6)
-0.4205355 with absolute error < 2.1e-14

The exact solution is given in the following formula:

Monte Carlo integration
Monte Carlo integration
Monte Carlo integration

As you can see, the solution we obtained using the Monte Carlo method (-0.4214223) is quite close to those obtained using the integrate() function and by solving the integral by hand (-0.4205355).

Multiple integration

We can also use the Monte Carlo method to find the approximate solution to multiple integrals. Multiple integrals are the generalization of definite integrals of two or more real variables. For example, we could approximate the value of a double integral as follows:

Multiple integration

Using Monte Carlo simulation, we can create a large set of independent uniform random variables, with x defined as U1, U2, …, Un and y defined as V1, V2, …, Vn, and then take the average of the two sample means; since the law of large numbers implies the following formula:

Multiple integration

For example, let's approximate the integral for Multiple integration:

#By default min=0 and max=1 in the runif()
> U <- runif(100000) 
> V <- runif(100000)
> mean(sin(U + V))
[1] 0.7723399

Now let's adapt this approach to approximate the integral of Multiple integration as follows:

> U <- runif(100000, min=3, max=7)
> V <- runif(100000, min=1, max=5)
> UV.mean <- mean(sin(U+V))

Next, we need to compensate for the joint density of U and V by multiplying with (7 – 3)(5 – 1) or 16, as follows:

> UV.mean*16
[1] 3.235

More generally, we could say for Multiple integration, we need to multiply the average of the sample means by (b – a)(d – c).

Let's test our solution using the adaptIntegrate() function from the cubature package, which is an extension of the basic integrate() function in R. You can install the package as follows:

> install.packages("cubature")

Load the library using the following function:

> library("cubature")

To define the separate variables such as x and y, the adaptIntegrate() function requires that you use a vector, in this case x, and specify a separate index for each variable as follows:

> f <- function(x) { sin(x[1]+ x[2]) } 
> adaptIntegrate(f, lowerLimit = c(3, 1), upperLimit = c(7, 5)) 
$integral
[1] 3.272092

$error
[1] 3.16107e-05

# full output truncated

As you can see, the estimate we got by applying the Monte Carlo method (3.235) was very close to the value we obtained using the adaptIntegrate() function (3.272092).

Other density functions

So far, we have showed you how to estimate integrals by simulating independent random variables from the uniform distribution. We used the uniform density defined as f(u) = 1/(b – a) to estimate our integrals. To approximate Other density functions, we run the following lines of code:

> u <- runif(100000, min=0, max=4)
> mean(u^2)*(4-0)
[1] 21.34182

However, if the density of X is h(x), then we can also write the sample mean as follows:

Other density functions

Therefore, we can approximate the integral by the sample averages of Other density functions. So we can also estimate the integral as follows:

> mean(u^2/dunif(u, min=0, max=4))
[1] 21.34182

Now let's apply this method to estimate the integral Other density functions:

> u <- runif(100000, min=1, max=pi)
> mean(exp(u)/dunif(u, min=1, max=pi))
[1] 20.42738

Instead of using the uniform distribution for Monte Carlo integration, we can also use other distributions. For example, let's estimate the integral Other density functions using the rexp() function to simulate random variables from the exponential distribution.

First, we need to rewrite the integral in a form so that we can use the default settings for the rexp() function as follows:

Other density functions

Let's take a look at this in the following lines of code:

> u <- rexp(10000) 
> mean(exp(-(u+1))/dexp(u))
[1] 0.3678794

Now let's check the solution with the integrate() function as follows:

> f <- function(x) { exp(-x) } 
> integrate(f, lower=1, upper=Inf)
0.3678794 with absolute error < 2.1e-05

Monte Carlo integration is only successful when the ratio of Other density functions converges. So try to pick h(x), which will keep the ratio roughly constant and avoid situations where Other density functions can be arbitrarily large.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.250.203