I want to integrate let's say $f(x)=\sin(x)$ over the interval $(0,2\pi)$ using Monte Carlo Integration. I did so by using R.

I generated a random set of uniform numbers $(x)$ and took the mean of $f(x)$ then multiplied it by the interval length (2pi). Now I don't understand why we have to multiply the mean by the interval length to get the area.

If I want to create a confidence interval of this area obtained, I would need to find the standard deviation, do I just get the standard deviation of $f(x)$ and multiply it by $2\pi$, or multiply $f(x)$ by $2 \pi $ then get the standard deviation of that or what? I really don't understand why am multiplying by $2\pi$ to begin with so if you can explain that to me and let me know how to obtain a CI, that would be great!!!

  • $\begingroup$ Reserve Monte Carlo for integration in large dimensions. $\endgroup$ – GNUSupporter 8964民主女神 地下教會 Mar 29 '18 at 21:04
  • 1
    $\begingroup$ Disagree. Understanding Monte Carlo on small cases is valuable. $\endgroup$ – Did Mar 29 '18 at 21:48

For Monte Carlo integration you need to generate random points $(x,y)$, and then check if the random point $y\leq f(x)$.

The steps need to be like the following:

  1. Get the domain for the $x$ variable $\rightarrow$ $[0,2\pi]$ in your case.
  2. Construct the min/max for the $y$ variable.

$y_{min} =f(x_{min}) = \sin(x_{min})$

In your case you know that the $\max(\sin(x))=1$, but if you didn't know, you would do the following:

  1. Generate a random $x$ in your domain.
  2. Calculate $f(x)$ at every step. Keep track of the max result.

When you have both $y_{min}$ and $y_{max}$ then you have a limiting rectangle to fit your function in (geometrically).


Generate random $(x,y)$ points that are within the domains you fixed above. At every step, check if $y \leq f(x)$ and count the number of occurrences, let's call it ct_pts_in.

After all your Monte-Carlo steps are done, the area under the curve is:

$ A = A_{rectangle} \cdot \frac{\text{ct_pts_in}}{\text{total_random_points}} $

  • 1
    $\begingroup$ but what about the CI, how would I get the variance? $\endgroup$ – ExamFML Mar 29 '18 at 23:58
  • $\begingroup$ Try to vary the sample size from $1000$ - $10^6$. with incremental steps of $1000$. Plot the values of the integral and see how that varies with sample size. This is a numerical approximation $\endgroup$ – Dashi Mar 30 '18 at 0:23
  • $\begingroup$ I think you may be doing the simplest kind of Monte Carlo integration, which imitates approximating the area under a curve by a number of rectangles (as in the first figure on this page). $\endgroup$ – BruceET Mar 30 '18 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.