简体   繁体   中英

stats.scipy.chi2 returns value of 0.0

I am trying to calculate some p-values with scipy.stats.chi2.cdf, as below. But the function is returning 0.

from scipy import stats
1-stats.chi2.cdf(332.185545938, 18.8967858326)
0.0

Can anything tell me if there is another module to use, or if there is a parameter that I am missing that will allow it to calculate p-values this small, can I somehow set the code to return the minimum non-zero P-value?

Cheers

You could use the survival function instead (1-cdf):

>>> from scipy import stats
>>> 1 - stats.chi2.cdf(332.185545938, 18.8967858326)
0.0
>>> stats.chi2.sf(332.185545938, 18.8967858326)
4.1718344487607692e-59

And we can check this result using the analytic cdf:

>>> import mpmath
>>> mpmath.mp.dps = 100
>>> def chi2_cdf(k,x): return mpmath.gammainc(x/2.0,0,k/2.0)/mpmath.gamma(x/2.0)
>>> chi2_cdf(332.185545938, 18.8967858326)
mpf('0.9999999999999999999999999999999999999999999999999999999999582816555123918223004416138082464542159504946')
>>> float(1-_)
4.171834448760817e-59

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM