def binProbGt(k, size, prob): """ binomial probability that x is > k (up to n). The corresponding R code for this is: pbinom(k, size = n, prob = p, lower=F) """ # -- manually, not exact enough #sum = 0.0 #for i in range(0, k): #sum+=binProb(n, p, i) #return sum # -- using scipy, not exact enough: # 1.0 - cdf is not as exact as sf #return 1.0 - scipy.stats.distributions.binom.cdf(k-1, n, p) # scipy is too complicated to compile on the cluster #return scipy.stats.distributions.binom.sf(k, size, prob) return dist.pbinom(k, size,prob)
def binProbGt(k, size, prob): """ binomial probability that x is > k (up to n). The corresponding R code for this is: pbinom(k, size = n, prob = p, lower=F) """ # -- manually, not exact enough #sum = 0.0 #for i in range(0, k): #sum+=binProb(n, p, i) #return sum # -- using scipy, not exact enough: # 1.0 - cdf is not as exact as sf #return 1.0 - scipy.stats.distributions.binom.cdf(k-1, n, p) # scipy is too complicated to compile on the cluster #return scipy.stats.distributions.binom.sf(k, size, prob) return dist.pbinom(k, size, prob)
def __next(self, a, b, obsOcc): width = b - a + 1 if width < self.MIN_WIDTH or width > self.MAX_WIDTH: return if obsOcc < self.MIN_OCC or obsOcc > self.MAX_WIDTH: return n = sum(self.N[len(self.w)][a - self.location[0]:b - self.location[0] + 1]) try: obsFreq = obsOcc / float(n) except: cli.warning('n error') expFreq = self.bg.freq(self.w, (a, b)) expOcc = expFreq * n #pv = ppois(obsOcc, expOcc) #pv = ppois_cached(obsOcc, expOcc) #pv = pbinom_right_left_cached(obsOcc, n, expFreq) if self.params['under']: pv = pbinom_left(obsOcc, n, expFreq) else: pv = pbinom(obsOcc, n, expFreq) ev = 1.0 label = '%s|%s' % (self.w, reverse_complement(self.w)) w = self.w spaces = self.w.count('N') if spaces >= 1: label = label.replace('N' * spaces, 'n{%d}' % spaces) w = self.w.replace('N' * spaces, 'n{%d}' % spaces) self.R.append([ w, label, obsFreq, expFreq, obsOcc, expOcc, pv, ev, -log10(ev), a, b, b - a + 1, 0, n, 0, 0 ])
def calcBinomScore(background, foreground, genes, backgroundProb): TP = len(genes.intersection(foreground)) binomProb = dist.pbinom(TP, len(genes), backgroundProb) binomScore = -math.log10(binomProb) return binomScore