[Math] Simple Monte Carlo Integration

integrationmonte carlo

I am trying to use Monte Carlo Integration, which is nicely described in the answer here (Confusion about Monte Carlo integration).

I am using Monte Carlo Integration to evaluate $\int_0^1x^2\,dx$.

I set $w(x) = f(x)/g(x) = x^2/2x = x/2$

Then, I solved for $(1/n)\sum_i^nw(x_n) = (1/n)\sum_i^nx/2$

However, I do not know if this is a good solution. I would like to know of other ideas, and why they are better.

The reason I do not think this is a good solution is if I actually solve for:

$\int_0^1x^2\,dx$ = $x^3/3 = 1/3-0 = 1/3$

But when I look at my answer, the mean will be 1/4 (not 1/3). Had I used a different equation, such as $(1/n)\sum_i^nx/1.5$, then I will get a mean of 1/3, which is very close to what the true integration is. Thank you!

Best Answer

The algorithm for the Monte-Carlo approach is the following.

  • Generate $n$ uniform random points $x_n \in (0, 1)$
  • Set $\displaystyle I = \dfrac{1}{n} \sum_{i = 1}^n x_n^2$

We can then compare the error from the exact value versus the result from the Monte Carlo approach. Depending on how we coded up the Monte-Carlo Method, we can also estimate the error.

Example 1: Using the method above with $n = 1000$, we get the plot:

enter image description here

This produces $I = 0.3323369730904328$.

We can also use other improved Monte Carlo Methods.

Example 2: Using a Quasi-Monte-Carlo method with $n = 1000$, we get the plot:

enter image description here

This produces $I = 0.3332527188916689$.

Example 3: Using an Adaptive-Monte-Carlo method with $n = 1000$, we get the plot:

enter image description here

This produces $I = 0.333727841861132$.

Example 4: Using an Adaptive-Quasi-Monte-Carlo method with $n = 1000$, we get the plot:

enter image description here

This produces $I = 0.3333327629439999$.

In all cases we can compare that to the actual result of:

$$\displaystyle \int_0^1 x^2~ dx = \dfrac{1}{3}$$

Related Question