Solved – Approximating integrals using Monte Carlo simulation in R

monte carlorself-study

How do I approximate the following integral using MC simulation?

$$
\int_{-1}^{1} \int_{-1}^{1} |x-y| \,\mathrm{d}x \,\mathrm{d}y
$$

Thanks!

Edit (Some context): I am trying to learn how to use simulation to approximate integrals, and am getting some practice done when I ran into some difficulties.

Edit 2+3: Somehow I got confused and thought I needed to split the integral into separate parts. So, I actually figured it out:

n <- 15000
x <- runif(n, min=-1, max=1)
y <- runif(n, min=-1, max=1)
mean(4*abs(x-y))

Best Answer

Just for reference, a low dimensional integral like that is usually more efficiently done via deterministic quadrature instead of Monte Carlo. Monte Carlo comes into its own at about 4 to 6 dimensions. Got to learn it in low dimensions first, of course...