[Scipy-tickets] [SciPy] #1568: One-sided fisher_exact test returns p-value < 1 for 0 successful attempts

SciPy Trac scipy-tickets@scipy....
Thu Nov 24 15:39:35 CST 2011


#1568: One-sided fisher_exact test returns p-value < 1 for 0 successful attempts
-------------------------+--------------------------------------------------
 Reporter:  biocs        |       Owner:  somebody   
     Type:  defect       |      Status:  new        
 Priority:  normal       |   Milestone:  Unscheduled
Component:  scipy.stats  |     Version:  devel      
 Keywords:               |  
-------------------------+--------------------------------------------------

Comment(by warren.weckesser):

 Here's the snippet of relevant code from the function fisher_exact():
 {{{
     if alternative == 'less':
         pvalue = hypergeom.cdf(c[0,0], n1 + n2, n1, n)
     elif alternative == 'greater':
         if c[0, 0]:
             x = c[0, 0] - 1
         else:
             x = c[0, 0]
         pvalue = hypergeom.sf(x, n1 + n2, n1, n)
 }}}
 n1 and n2 are the row sums, and n is the first column sum.
 There is an explicit check for c[0,0] being nonzero, in which case 1 is
 subtracted before calling hypergeom.sf().  I don't know why that check
 is there, and it appears to be wrong; 1 should also be subtracted when
 c[0,0] is 0.  For the example [[0,1],[2,3]], we have c[0,0]=0, n1=1, n2=5,
 n=2.  Without the correction, we get the expected answer:
 {{{
 In [138]: hypergeom.sf(-1, 6, 1, 2)
 Out[138]: 0.99999999999999933
 }}}
 but the mysterious correction in the code results in:
 {{{
 In [139]: hypergeom.sf(0, 6, 1, 2)
 Out[139]: 0.33333333333333309
 }}}

 A consistency check for this calculation is that switching the rows and
 columns of
 the table should not change the result.  For example,
 {{{
 In [169]: fisher_exact([[2,4],[1,6]], alternative='greater')
 Out[169]: (3.0, 0.43706293706293786)

 In [170]: fisher_exact([[6,1],[4,2]], alternative='greater')
 Out[170]: (3.0, 0.43706293706293742)
 }}}
 But this check fails when the upper left value is 0:
 {{{
 In [171]: fisher_exact([[0,1],[2,3]], alternative='greater')
 Out[171]: (0.0, 0.33333333333333309)

 In [172]: fisher_exact([[3,2],[1,0]], alternative='greater')
 Out[172]: (0.0, 1.0)
 }}}

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


More information about the Scipy-tickets mailing list