Integration of Multivariate Normal Distribution in Python

你离开我真会死。 提交于 2019-12-04 13:44:44

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!