Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does integrating scipy.multivariate_normal give incorrect probability?

I’m trying to integrate an independent bivariate normal distribution over a square region. The numerical integration does not match a Monte Carlo simulation. What’s going wrong here?

import numpy as np
from scipy import integrate
from scipy.stats import multivariate_normal

sigmaX = 0.5
sigmaY = 0.8
region = 1.0  # Region of interest is a unit square centered at the mean (0,0)

# Numerical integration for answer:
def pdf(x,y):
    return multivariate_normal.pdf([x,y], mean=[0,0], cov=[[sigmaX, 0], [0, sigmaY]])
probability, err = integrate.nquad(pdf, [[-region/2.0, region/2.0], [-region/2.0, region/2.0]])

# Monte Carlo simulation for answer:
simulations = 1_000_000
X = np.random.normal(scale=sigmaX, size=simulations)
Y = np.random.normal(scale=sigmaY, size=simulations)
hits = sum(1 for s in range(simulations) if ((abs(X[s]) < region/2.0) and (abs(Y[s]) < region/2.0)))/simulations

print(f'Numerical integration gives probability {probability:.1%}\n'
      f'Monte Carlo gives probability {hits:.1%}')

Output:

Numerical integration gives probability 22.1%

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Monte Carlo gives probability 31.9%

>Solution :

The diagonals of the covariance matrix are the variances, while the scale argument of np.random.normal is the standard deviation. One way to fix your calculation is to change the cov argument of the pdf function to

cov=[[sigmaX**2, 0], [0, sigmaY**2]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading