# Calculating integral by using Monte Carlo method

I am asked to calculate the value of the following integral by using Monte Carlo method. $$I=\int_{\mathbb{R^{10}}}(2\pi)^{-10/2}\exp\left(-\sum_{i=1}^{10}\frac{x_i^2}{2}+\sin\left(\sum_{i=1}^{10}x_i\right)\right)dx_1\dots dx_{10}$$

That seems to be a ten-dimensional integral with an exponential function as outer function and trigonometric function as inner function. My question is how can I calculate a value of this integral because it seems to be an indefinite integral and I thought that Monte Carlo method can be used only for calculating values for definite integrals. I already tried to calculate the integral function by using Maxima, but Maxima refused to calculate it.

• For a good approximation to a definite integral in dimension as high as ten it takes a very large sample. – Michael R. Chernick Apr 27 '18 at 5:37
• it is not indefinite, you have the bounds set to $-\infty$ to $\infty$ – Jan Kukacka Apr 27 '18 at 7:16
• also, note that since the exponential is dominated by the first sum, which quickly grows negative, the highest part of the integral mass will be concentrated around zero, so you don't need uniform sampling of the infinite space – Jan Kukacka Apr 27 '18 at 7:19

Jan Kukacka's comment can be used to construct a Monte Carlo method to estimate this integrate. First note that, since the bounds on the integral are $\mathbb{R}^{10}$, traditional uniform sampling on this space is not possible. However, instead of a uniform sample from this space, we can obtain a sample from a different distribution and adjust the integrand according to the distribution chosen. That is if

$$\int_{\mathbb{R}^{10}}g(x)\,dx$$

is the integral needed, then find a distribution defined on $\mathbb{R}^{10}$ with pdf $f(x)$, so that we have,

\begin{align*} \int_{\mathbb{R}^{10}}g(x)\,dx & = \int_{\mathbb{R}^{10}}g(x) \dfrac{f(x)}{f(x)}\,dx\\ & = \int_{\mathbb{R}^{10}} \dfrac{g(x)}{f(x)} f(x)\,dx \\ & = E_f\left[ \dfrac{g(x)}{f(x)} \right] \,, \end{align*} where the expectation is with respected to the distribution described by $f$. The goal now is to find an appropriate distribution such that most of the mass of the distribution aligns with $g$. As Jan Kukacka's comment pointed out, the highest part of the integral is concentrated near zero, so we can pick a 0 centered distribution. The easiest distribution to work with then is the $N_{10}(0, I_{10})$ distribution.

1. Obtain $N$ samples from $N_{10}(0, I_{10})$
2. Calculate $g(x)/f(x)$ for each sample
3. Calculate sample mean of all $g(x)/f(x)$ values. That is the Monte Carlo estimate.

Here is the R code that does it.

set.seed(10)

eval <- function(x){
integrand <- (2*pi)^(-10/2) * exp(-sum(x^2)/2 + sin(sum(x))  )/ prod(dnorm(x))
return(integrand)
}

N <- 100000
est <- numeric(length = N)
for(i in 1:N)
{
samp <- rnorm(10)
est[i] <- eval(samp)
}
mean(est) #1.263585

• +1. For (much) faster convergence you might observe that this reduces to a one-dimensional integral over the variable $y=\sum x_i.$ Obviously the other nine orthogonal directions integrate out to unity. – whuber Apr 27 '18 at 13:40
• Thank you very much Greenparker! By using R I was trying to make a program that counts random dots inside of a 10-dimensional cube where the function lies, but I could not make it work. – Student982 Apr 27 '18 at 14:01
• @whuber: I think that, as written, the R code is almost completely equivalent to simulate directly$$S=\sum_{i=1}^{10}X_i\sim{\cal N}(0,10)$$and then average$$\exp\{\sin(S)\}$$except for the rnorm(10) instead of rnorm(1,sd=sqrt(10)). – Xi'an Apr 28 '18 at 12:17
• May I point out that, given the importance function chosen in the end, the importance sampling justification is not absolutely needed since the integral is directly expressible as$$\int_{\mathbb{R}^{10}}\exp\left(\sin\left(\sum_{i=1}^{10}x_i\right)\right) (2\pi)^{-10/2}\exp\left(-\sum_{i=1}^{10}\frac{x_i^2}{2}\right)\text{d}x_1\dots \text{d}x_{10}$$i.e. as the expectation of the function$$\exp\left(\sin\left(\sum_{i=1}^{10}x_i\right)\right)$$under the ${\cal N}_{10}(0,\mathbf{I}_{10})$ distribution. – Xi'an Apr 28 '18 at 12:29
• @Xi'an ah yes, of course. I missed that detail. – Greenparker Apr 28 '18 at 13:08