[Scipy-tickets] [SciPy] #1842: wrong value returned by scipy.stats.binom probability mass function when true probability is 1

SciPy Trac scipy-tickets@scipy....
Mon Feb 11 18:17:52 CST 2013


#1842: wrong value returned by scipy.stats.binom probability mass function when
true probability is 1
-------------------------+--------------------------------------------------
 Reporter:  leflage      |       Owner:  rgommers   
     Type:  defect       |      Status:  new        
 Priority:  normal       |   Milestone:  Unscheduled
Component:  scipy.stats  |     Version:  0.11.0     
 Keywords:               |  
-------------------------+--------------------------------------------------
 Below is example code using Python 2.7.2 on Windows with scipy version
 0.11.0:


 {{{
 from scipy import stats

 a = stats.binom(100,1)
 a.pmf(100) #returns nan

 a = stats.binom(100,0)
 a.pmf(0)   #returns nan
 }}}


 The correct answer for both is 1.  Both return nan and give the following
 error:

 ''C:\Python27\lib\site-packages\scipy\stats\distributions.py:6526:
 RuntimeWarning: invalid value encountered in multiply
    return combiln + k*np.log(p) + (n-k)*np.log(1-p)''

 I haven't tested it but I suspect the bug is due to the log conversion of
 the pmf function for binomial. When the true probability is 1, either p or
 1-p become zero, causing a log(0).  One way to fix this might be to make
 two special conditional cases.  For example:

 {{{

 if p == 1 and k == n: return 1.0
 elif p == 0 and k == 0: return 1.0
 else: use_existing_function
 }}}

 Also, there is a similar error in the Poisson pmf function:

 {{{

 a = stats.poisson(0)
 a.pmf(0) ##returns nan
 }}}



 This too should return 1.  I haven't looked at other distributions, but
 there might be more special cases like these.

 It may seem silly to want to calculate pmf's where it's obvious the answer
 is 1, however this does come up frequently when performing summations for
 a mixture distribution.
 (http://en.wikipedia.org/wiki/Mixture_distribution)

 Thanks!
 Lex

-- 
Ticket URL: <http://projects.scipy.org/scipy/ticket/1842>
SciPy <http://www.scipy.org>
SciPy is open-source software for mathematics, science, and engineering.


More information about the Scipy-tickets mailing list