def _computeBinomialPval(cls, x, size, prob, tail):
     from proto.RSetup import r
     x, size, prob = int(x), int(size), float(prob)
     if prob * size >= cls.MIN_SUCCESSES_FOR_NORM_APPROXIMATION <= (
             1 - prob) * size:
         mean = size * prob
         sd = (size * prob * (1 - prob))**0.5
         lessPval = r.pnorm(x, mean, sd)
         if tail == 'less':
             pval = lessPval
         elif tail == 'more':
             pval = 1 - lessPval
         elif tail == 'different':
             pval = min(1, 2 * min(lessPval, 1 - lessPval))
         else:
             from gold.application.LogSetup import logMessage, logging
             logMessage(
                 'Unsupported tail (%s) encountered in _computeBinomialTail.'
                 % tail,
                 level=logging.WARN)
     elif x > cls.MAX_SUCCESSES_FOR_BINOMIAL_RUNTIME:
         return None
         #raise NotImplementedError()
     else:
         if tail == 'less':
             pval = r.pbinom(x, size, prob)
         elif tail == 'more':
             pval = 1 - r.pbinom(x - 1, size, prob)
         elif tail == 'different':
             pval = min(
                 1, 2 * min(r.pbinom(x, size, prob),
                            1 - r.pbinom(x - 1, size, prob)))
     return pval
Esempio n. 2
0
 def _calcBinomialPVals(self, countMatrix, colSums, rowSums, N):
     from proto.RSetup import r
     pValMatrix = numpy.zeros(countMatrix.shape)
     for i in xrange(pValMatrix.shape[0]):
         for j in xrange(pValMatrix.shape[1]):
             q = x = int(countMatrix[i, j])
             size = int(colSums[j])
             prob = float(rowSums[i]) / N
             #                print q,size,prob
             rawPval = min(
                 r.pbinom(q, size, prob, True),
                 r.pbinom(q, size, prob, False) + r.dbinom(q, size, prob))
             #                rawPval = min( r.pbinom(q, size, prob, True) - 0.5 * r.dbinom(q, size, prob), \
             #                               r.pbinom(q, size, prob, False) + 0.5 * r.dbinom(q, size, prob) )
             pValMatrix[i, j] = min(rawPval * 2, 1)  #correct for two-tail..
     return pValMatrix
 def _compute(self):
     Ois = self._countOfSegmentsInBin.getResult()
     globalOis = self._countOfSegmentsAcrossBins.getResult()
     binCoverageProportion = self._proportionOfAnalysisRegionInBin.getResult(
     )
     #print self._region
     #print "<p>**** %d, %d, %f</p>" % (Ois, globalOis, binCoverageProportion)
     #Eis =float(globalOis)*binCoverageProportion
     from proto.RSetup import r
     return 1.0 - r.pbinom(
         Ois, globalOis, binCoverageProportion
     )  #handle tails.. Do some trick to send in lower.tail..
    def _compute(self):
        from proto.RSetup import r
        #r("require(Defaults)")
        #r('setDefaults(q, save="no")')
        #r("useDefaults(q)")

        x = self._numPointsInside.getResult()
        size = self._numPointsTotal.getResult()
        prob = self._segmentCoverProportion.getResult()
        se = math.sqrt(1.0 * (prob) * (1 - prob) / size)
        if size < 1 or prob in [0, 1]:
            return None
        if self._tail == 'less':
            pval = r.pbinom(x, size, prob)
        elif self._tail == 'more':
            pval = 1 - r.pbinom(x - 1, size, prob)
        elif self._tail == 'different':
            pval = min(
                1, 2 *
                min(r.pbinom(x, size, prob), 1 - r.pbinom(x - 1, size, prob)))

        #return {'P-value':pval, 'SegCover':prob, 'PointsInside':x, 'PointsTotal':size}
        return OrderedDict([ ('P-value', float(pval)), ('Test statistic: PointsInside', x), ('E(Test statistic): ExpPointsInside', prob*size), \
                             ('DiffFromExpected', x-prob*size), ('PointsTotal', size), ('SegCoverage', prob) ])