-1

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!!!

Dashi
  • 821
  • 6
  • 12
ExamFML
  • 11

1 Answers1

1

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).

Finally,

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}} $

Dashi
  • 821
  • 6
  • 12
  • 1
    but what about the CI, how would I get the variance? – ExamFML Mar 29 '18 at 23:58
  • 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 – Dashi Mar 30 '18 at 00:23
  • 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). – BruceET Mar 30 '18 at 07:56