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))
integrate(integrate(abs(x-y), y, -1, 1), x, -1, 1);and get the answer 8/3. – Karl Oct 24 '11 at 0:02integrate(Vectorize(function(y) integrate(function(x) abs(x-y), -1, 1)$value), -1, 1)and get a numeric approximation. Using the cubature packageadaptIntegrate(function(x) abs(x[1] - x[2]), c(-1, -1), c(1, 1))can be used. This is just to give a couple of ideas for numeric evaluation of integrals that could come in handy, for instance when testing if a simulation works correctly. – NRH Oct 24 '11 at 13:14