def z_bi_cephes(n_on, n_off, alpha): tau = 1.0 / alpha aa = n_on bb = n_off + 1 xx = 1.0 / (1 + tau) # Checks to avoid Nan in some cases if aa <= 0.0 or bb <= 0.0: return 0.0 if xx <= 0.0: return 0.0 if xx >= 1.0: return 1.0 # I use the incbet from cephes instead of the scipy.special.betainc function because the latter has numerical # problems in some instances and return Nans, while the incbet from Cephes is more robust P_Bi = incbet(aa, bb, xx) return significance_from_pvalue(P_Bi)
def frequency_test(obs1, tot1, obs2, tot2): """ :param obs1: (int) the count number of an amino acid X in the set of protein 1. :param tot1: (int) the total number of amino acids in the set of protein 1. :param obs2: (int) the count number of an amino acid X in the set of protein 2. :param tot2: (int) the total number of amino acids in the set of protein 2. :return: proportion test p-value """ mean1 = float(obs1) / tot1 mean2 = float(obs2) / tot2 var1 = float(obs1) * (1 - mean1) * (1 - mean1) + (tot1 - obs1) * mean1 * mean1 var2 = float(obs2) * (1 - mean2) * (1 - mean2) + (tot2 - obs2) * mean2 * mean2 df = tot1 + tot2 - 2 svar = (var1 + var2) / df t = (mean1-mean2) / sqrt(svar*(1.0/tot1 + 1.0/tot2)) return cprob.incbet(0.5*df, 0.5, df/(df+t*t))
def _incbet(a, b, x): return incbet(a, b, x)
def test_incbet(): assert_almost_equal(cprob.incbet(1., 3., 0.3), 0.657)