I am trying to use Monte Carlo method to integrate the following improper integral

$${1 \over \sqrt{2\pi}}\int\limits_{-\infty}^{\infty}x^4e^{(-x^2/2)} \ dx$$

Using change of variables, $ y = x^2/2 $, I can transform the integral into gamma function and get the solution as $3\sqrt{2}\pi$ (as shown here)

However, the integral still remains improper at $0$ to $\infty$ and I am not sure how to get that into a proper one so that I can use Monte-Carlo method to arrive at an approximate solution.

Any help is much appreciated.

Thank you

  • $\begingroup$ The point of MC integration is to find a good random variable $Z$ s.t. $E[Z]$ is the desired integral. You can generate standard normal $X$. Generate $X_1,\dots, X_{10000}$ iid following standard normal. Take the mean of fourth power of the corresponding variable. By LLN, it will converge to the relevant mean $E[Z]$ which is the desired integral. You could do substitution by $y=x^2/2$ and consider generating iid exponential random variable as well. You can find the relevant power here as the normal case. $\endgroup$
    – user45765
    Apr 14, 2023 at 21:34
  • $\begingroup$ Thanks @user45765 for your comments. I have just started learning this approach and I am not sure what you meant by "Take the mean of fourth power of the corresponding variable". Can you please explain a bit more? Thank you $\endgroup$
    – Yoshiro
    Apr 14, 2023 at 21:59
  • $\begingroup$ @Yoshiro The mean of the fourth power is $\langle x^4 \rangle$. If your distribution is the normal distribution that is just your integral. $\endgroup$
    – RicardoMM
    Apr 14, 2023 at 22:36

2 Answers 2


What you have written down is incorrect. $E[X^4]=3$. Thus your answer should be close to 3 instead.

I will denote $p(x)$ as standard normal density $1/\sqrt{2\pi}\exp(-x^2/2)$. Then you want $\int_{-\infty}^{\infty} x^4p(x)dx$ which is really $E[X^4]$ by definition.

Recall weak law of large number for $Z_1,\dots, Z_n$ iid following distribution $f(Z)$, then $\bar{Z}=\frac{Z_1+\dots+Z_n}{n}$ has for any $\epsilon>0$, $P(|\bar{Z}-E[Z]|>\epsilon)\to 0$ as $n\to\infty$. Here you want to choose $Z=X^4$ and by normal MGF, you will see it has $E[Z]<\infty$. The only matter is to compute it.

To simulate $Z_1,\dots, Z_n$, it suffices to simulate $X_1,\dots, X_n$ following $p(x)$ density, where $Z_i=X_i^4$.

Draw $X_1,\dots, X_n$ iid from $p(x)$, then compute $X_i^4=Z_i$. At last take the mean and apply weak law of large number.

Here is the code for simulation in R.

for(i in 1:1000){
x=rnorm(100000,mean = 0,sd=1)

Here is the simulation histogram and you can see it is fairly close to the true answer 3. enter image description here

  • $\begingroup$ It's clear now. Thank you so much for your help. $\endgroup$
    – Yoshiro
    Apr 14, 2023 at 22:45

Explicit Monte Carlo using importance sampling to integrate $\sqrt{\frac{2}{\pi}}\int\limits_0^\infty x^4e^{\frac{-x^2}{2}}dx$ Let $f(x)=e^{-x}dx$ and $h(x)=\sqrt{\frac{2}{\pi}} x^4e^{(x\frac{-x^2}{2})}$.
$f(x)$ is the sampling density with the nice property that $F(x)=\int\limits _0^xf(x)dx$ is easily invertable . In addition, $h(x) $ is bounded.

To get a sample for Monte Carlo. Random number $R$ uniform (0.1) Use $R=F(x)= 1-e^{-x}$ to get $x=-ln(1-R)$. (Note $1-R$ is uniform). Sample value is $h(x)$

Quota sampling can be used to greatly reduce variance.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .