问题
I am trying to integrate over a multivariate distribution in python. To test it, I built this toy example with a bivariate normal distribution. I use nquad()
in order to extend it to more than two variables later on. Here is the code:
import numpy as np
from scipy import integrate
from scipy.stats import multivariate_normal
def integrand(x0, x1, mean, cov):
return multivariate_normal.pdf([x0, x1], mean=mean, cov=cov)
mean = np.array([100, 100])
cov = np.array([[20, 0], [0, 20]])
res, err = integrate.nquad(integrand,
[[-np.inf, np.inf], [-np.inf, np.inf]],
args=(mean, cov))
print(res)
The result I get is 9.559199162933625e-10
. Obviously, this is incorrect. It should be (close to) 1.
What is the problem here?
回答1:
scipy's nquad does numerical integration only on bounded rectangular domains. The fact that your integral converges at all is due to the exp(-r^2)
-type weight of the PDF (see here for its explicit form). Hence, you need Hermite quadrature in 2D. Some articles exist on this topic, and quadpy (a project of mine) implements those.
You'll first need to bring your integral into a form that contains the exact weight exp(-r**2)
where r**2
is x[0]**2 + x[1]**2
. Then you cut this weight and feed it into quadpy's e2r2 quadrature:
import numpy
import quadpy
def integrand(x):
return 1 / numpy.pi * numpy.ones(x.shape[1:])
val = quadpy.e2r2.integrate(
integrand,
quadpy.e2r2.RabinowitzRichter(3)
)
print(val)
1.0000000000000004
回答2:
A bit off-topic, but you should use the following routine instead (it is quite fast):
from scipy.stats.mvn import mvnun
import numpy as np
mean = np.array([100, 100])
cov = np.array([[20, 0], [0, 20]])
mvnun(np.array([-np.inf, -np.inf]), np.array([np.inf, np.inf]), mean, cov)
Or use multivariate_normal.cdf
and do the substractions.
来源:https://stackoverflow.com/questions/49433077/integration-of-multivariate-normal-distribution-in-python