Integral of $\int_{-\infty}^\infty x^2e^{-\frac {(x-\mu)^2}{2\sigma^2}}dx$ Calculate this integral
$\int_{-\infty}^\infty x^2e^{-\frac {(x-\mu)^2}{2\sigma^2}}dx$
I know the trick can be using $E[x^2]=var[x]+E^2[x]$, but how to solve it in an analytic way?
 A: 
Remember that : $ e^{x} = \sum_{n=0}^{\infty} \frac{x^{n}}{n!}  $
So, we may have : $ e^{-\frac{(x-\mu)^{2}}{a}} = \sum_{n=0}^{\infty} \frac{(x-\mu)^{2n}}{(-a)^{n}n!}  $. Here, I nicknamed $a = -2\sigma^{2}$.

Now your integral can be rewritten as
$$ \int_{-\infty}^{\infty} \sum_{n=0}^{\infty}\frac{x^{2}(x-\mu)^{2n}}{(-a)^{n}n!} \:\:dx = \sum_{n=0}^{\infty} \frac{1}{(-a)^{n}n!}\int_{-\infty}^{\infty} x^{2}(x-\mu)^{2n} \:\:dx $$
From here you can calculate each of the $ \int_{-\infty}^{\infty} x^{2}(x-\mu)^{2n} \:\:dx $ using partial integration. 
I may do half-way here:


*

*$$ \int x^{2}(x-\mu)^{2n} \:\:dx = x^{2} \frac{(x-\mu)^{2n+1}}{2n+1} - \frac{2}{2n+1}\int x (x-\mu)^{2n+1} dx  $$


*

*$$ \int x(x-\mu)^{2n+1}dx = x \frac{(x-\mu)^{2n+2}}{2n+2} - \int \frac{(x-\mu)^{2n+2}}{2n+2} dx $$
$$=  x \frac{(x-\mu)^{2n+2}}{2n+2}-\frac{(x-\mu)^{2n+3}}{(2n+2)(2n+3)}  $$... 
Is this okay? hope this helps.


A: The substitution $z=\frac{x-\mu}{\sigma}$ converts your integral to $\sigma\sqrt{2\pi}\int_\mathbb{R}(\mu^2+2\mu\sigma z + \sigma^2 z^2)e^{-z^2/2}dz$. We now have to evaluate three integrals. The first is famous; the second vanishes because of an odd integrand; the third can be obtained by differentitating under the integral. Explicitly we have $\int_\mathbb{R}e^{-az^2}dz=\sqrt{\dfrac{\pi}{a}},\,\int_\mathbb{R}z^2e^{-az^2}dz=\sqrt{\dfrac{\pi}{4a^3}}.$
A: Write
$$\int_{-\infty}^\infty x^2e^{-(x-\mu)/2\sigma^2}dx=\int_{-\infty}^\infty x(x-\mu+\mu)e^{-(x-\mu)/2\sigma^2}dx=\int_{-\infty}^\infty x(x-\mu)e^{-(x-\mu)/2\sigma^2}dx.$$
The second identity holds because of the parity of the term $x\mu$.
Then by parts,
$$\int_{-\infty}^\infty x(x-\mu)e^{-(x-\mu)/2\sigma^2}dx=-\sigma^2xe^{-(x-\mu)/2\sigma^2}+\sigma^2\int_{-\infty}^\infty e^{-(x-\mu)/2\sigma^2}dx.$$
I guess that you know the rest.
A: Another way to do it is by expanding the function into a linear combination of Hermite functions. Define: $H_{n+1} = \frac{d}{dx}H_{n-1}$, and let $H_0$ be the gaussian in question, i.e.:


*

*$H_0(x) = e^{-\frac {(x-\mu)^2}{2\sigma^2}}$

*$H_1(x) = -\frac{x-\mu}{\sigma^2}H_0(x)$

*$H_2(x) = \big(\frac{x^2 - 2\mu x +\mu^2-\sigma^2}{\sigma^4}\big)H_0(x)$


Thus $x^2 H_0(x)=\sigma^4H_2(x) - 2\mu\sigma^2H_1(x) + (\mu^2+\sigma^2)H_0(x)$. When integrating from $-\infty$ to $+\infty$ only the $H_0$ term matters, since the others are derivatives of it and they all decay exponentially. So:
$$ \int_{-\infty}^\infty x^2e^{-\frac {(x-\mu)^2}{2\sigma^2}}dx = (\mu^2+\sigma^2)\int_{-\infty}^\infty e^{-\frac {(x-\mu)^2}{2\sigma^2}}dx
= (\mu^2 + \sigma^2)\sqrt{2\pi \sigma^2}$$
