Changeset 1890:043f2b1864cd in orange-bioinformatics for orangecontrib/bio/obiProb.py


Ignore:
Timestamp:
10/15/13 13:07:59 (6 months ago)
Author:
markotoplak
Branch:
default
Message:

Numberically stable p-values for obiProb.Binomial.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orangecontrib/bio/obiProb.py

    r1889 r1890  
    7171 
    7272    def p_value(self, k, N, m, n): 
    73         subtract = n - k + 1 > k 
    74         result = sum([self.__call__(i, N, m, n) for i in (range(k) if subtract else range(k, n+1))]) 
    75         return max(1.0 - result if subtract else result, 0.0) 
     73        if n - k + 1 <= k: 
     74            #starting from k gives the shorter list of values 
     75            return sum(self.__call__(i, N, m, n) for i in range(k, n+1)) 
     76        else: 
     77            value = 1.0 - sum(self.__call__(i, N, m, n) for i in range(k)) 
     78            #if the value is small it is probably inexact due to the limited 
     79            #precision of floats, as for example  (1-(1-1e-20)) -> 0 
     80            #if so, compute the result without substraction 
     81            if value < 1e-3: #arbitary threshold 
     82                #print "INEXACT", value, sum(self.__call__(i, N, m, n) for i in range(k, n+1)) 
     83                return sum(self.__call__(i, N, m, n) for i in range(k, n+1)) 
     84            else: 
     85                return value 
    7686 
    7787class Hypergeometric(LogBin): 
     
    97107            value = 1.0 - sum(self.__call__(i, N, m, n) for i in (range(k))) 
    98108            #if the value is small it is probably inexact due to the limited 
    99             #precision of floats, as for example  (1-(1-10e-20)) -> 0 
     109            #precision of floats, as for example  (1-(1-1e-20)) -> 0 
    100110            #if so, compute the result without substraction 
    101111            if value < 1e-3: #arbitary threshold 
Note: See TracChangeset for help on using the changeset viewer.