Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integration of Multivariate Normal Distribution in Python

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?

like image 245
user192859 Avatar asked May 10 '26 15:05

user192859


2 Answers

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.

like image 64
Tzoiker Avatar answered May 12 '26 05:05

Tzoiker


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
like image 33
Nico Schlömer Avatar answered May 12 '26 06:05

Nico Schlömer