Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: results of "scipy.dblquad" are confused! #17554

Closed
Leisures2 opened this issue Dec 7, 2022 · 1 comment
Closed

BUG: results of "scipy.dblquad" are confused! #17554

Leisures2 opened this issue Dec 7, 2022 · 1 comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.integrate

Comments

@Leisures2
Copy link

Describe your issue.

I want to calculate integration of the pdf of bivariate_normal distribution (should be 1).
But I got an extremely small result using "dblquad" and don't know the reason.
屏幕截图 2022-12-07 190628

Reproducing Code Example

import numpy as np
import scipy as sp
from scipy.integrate import dblquad  
Cov_BossGal1 = np.array(
    [[2.860520e-02, -4.939281e-02],
     [-4.939281e-02, 5.307187e-01],
     ])
x_BossGal1 = np.array([10.23, 25.00])
D_try = sp.stats.multivariate_normal(x_BossGal1,Cov_BossGal1)
print(D_try.cdf(np.array([np.infty,np.infty]))) # cdf = 1(There is no problem)
print(dblquad(lambda y, x: D_try.pdf(np.array([x, y])), -np.infty, np.infty, -np.infty, np.infty)) #this one is so small!

Error message

scipy.integrate.dblquad

SciPy/NumPy/Python version information

scipy.integrate

@Leisures2 Leisures2 added the defect A clear bug or issue that prevents SciPy from being installed or used as expected label Dec 7, 2022
@tupui
Copy link
Member

tupui commented Dec 7, 2022

Hi @Leisures2, thank you for reporting. The issue is that the PDF mode is very localised, so if you integrate numerically over a very large domain it is very easy to miss a spike. Just changing the bounds solves it:

dblquad(lambda x, y: D_try.pdf(np.array([x, y])), 0, 100, 0, 100)
# (0.9969079885378376, 1.4899985581153049e-08)

If that's not an option, then you need to play with the tolerances, e.g.

dblquad(lambda x, y: D_try.pdf(np.array([x, y])), -np.infty, np.infty, -np.infty, np.infty, epsabs=1e-50)
# (0.999999999999939, 5.4931195671689236e-09)

As you discretise the problem further, it takes more time.

I am closing now as I don't think there is any issue here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.integrate
Projects
None yet
Development

No branches or pull requests

3 participants