I am confused with numerical integration on Gauss-Hermite quadrature method. Here is an example of what I want to calculate: $$\int_{-\infty}^{+\infty}(x-a)^2e^{-x^2}dx$$ I can directly use Gauss-Hermite quadrature for $f(x)=(x-a)^2$, for the numerical integration, $I=\sum_i f(x_i)w_i=\sum_i(x_i-a)^2w_i$, I can also use this transformation $y=x-a$, then the integration becomes: $$\int_{-\infty}^{+\infty}y^2e^{-(y+a)^2}dy$$ if I use scipy.integrate.quad to calculate these two quadrature, the result are the same, but if I transform the second integration in the following form: $$\int_{-\infty}^{+\infty}(y^2e^{-a^2-2ay})e^{-y^2}dy$$ if I treat $y^2e^{-a^2-2ay}$ as $f(y)$, then the summation should be $\sum_if(y_i)w_i=\sum_i(y_i^2e^{-2ay_i-y_i^2})w_i$, but the result is wrong, so why can't I do this transformation?
here is the python code and what I get from it:
#!/usr/bin/env python
from math import *
import numpy as np
import numpy.polynomial.hermite as nh
import scipy.integrate as si
x,w=nh.hermgauss(28)
print sum(np.power(x-10,2)*w)/sqrt(pi)
print sum(np.power(x,2)*np.exp(-20*x-100)*w)/sqrt(pi)
print si.quad(lambda x:np.power(x,2)*exp(-np.power(x+10,2)),-np.inf,+np.inf)[0]/sqrt(pi)
print si.quad(lambda x:np.power(x-10,2)*exp(-np.power(x,2)),-np.inf,+np.inf)[0]/sqrt(pi)
here is the result
100.5
0.000187032762349
100.5
100.5
Not enough terms. You are only guaranteed equality with infinitely many terms, and I suspect the size of $e^{-100}$ may be a problem. Here is some code (run it after running your code): it works fine for small $a$, choking around $a=6$.
In addition, looking at the quadrature points,
We see that we are only using values of $f$ at points $x_i$ such that $|x_i|<7$. For a function that is nearly 0 on this region, this behaviour is not too surprising.
With the weights,
we have the bound $$\left|\sum_{i=1}^{28} f(x_i) w_i \right| ≤ 28\left(\max_i w_i\right) \sup_{x∈[-6.6,-6.6]} |f(x)| ≤ 10 \sup_{x∈[-6.6,-6.6]} |f(x)| $$