コード例 #1
0
def getNBPValue(mean0, var0, mean1, lower=False, log=False):
    """
  Use negative binomial to calculate p-value
  Reference:
  http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.nbinom.html#scipy.stats.nbinom
  """
    from scipy.stats import nbinom
    n = len(mean0)
    nb_p = [mean0[i] / var0[i] for i in range(n)]
    # consisitent with R
    nb_n0 = [mean0[i] * mean0[i] / (var0[i] - mean0[i]) for i in range(n)]
    nb_n = [(lambda t: t if t >= 1 else 1)(x) for x in nb_n0]
    #
    if lower == True:
        if log == False:
            nb_p_low = nbinom.cdf(mean1, nb_n, nb_p)
        else:
            nb_p_low = nbinom.logcdf(mean1, nb_n, nb_p)
        return list(nb_p_low)
    else:
        if log == False:
            nb_p_low = nbinom.sf(mean1, nb_n, nb_p)
        else:
            nb_p_low = nbinom.logsf(mean1, nb_n, nb_p)
        return list(nb_p_low)
コード例 #2
0
    def test_pmf(self):
        n, p = truncatednegbin.convert_params(2, 0.5, 2)
        nb_logpmf = nbinom.pmf(6, n, p) / nbinom.sf(5, n, p)
        tnb_pmf = truncatednegbin.pmf(6, 2, 0.5, 2, 5)
        assert_allclose(nb_logpmf, tnb_pmf, rtol=1e-7)

        tnb_pmf = truncatednegbin.pmf(5, 2, 0.5, 2, 5)
        assert_equal(tnb_pmf, 0)
コード例 #3
0
    def test_logpmf(self):
        n, p = truncatednegbin.convert_params(5, 0.1, 2)
        nb_logpmf = nbinom.logpmf(6, n, p) - np.log(nbinom.sf(5, n, p))
        tnb_logpmf = truncatednegbin.logpmf(6, 5, 0.1, 2, 5)

        assert_allclose(nb_logpmf, tnb_logpmf, rtol=1e-7)

        tnb_logpmf = truncatednegbin.logpmf(5, 5, 0.1, 2, 5)
        assert np.isneginf(tnb_logpmf)
コード例 #4
0
 def beta_on_negbinom_closed_form3(t1=25,t2=25,\
             theta_base=10,m=100.0,deltheta=3):
     """
     This method only works for alpha=0.5.
     """
     if deltheta > theta_base:
         #TODO: Replace this with exception.
         print("deltheta must be smaller than theta.")
         return
     theta_alt = theta_base - deltheta
     neg_binom_ix = 0
     p2 = theta_alt / (t2 + theta_alt)
     p1 = theta_base / (t1 + theta_base)
     mode1 = int(p1 * (m - 1) / (1 - p1))
     beta = 0
     del_beta = 1
     while del_beta > 1e-7 or neg_binom_ix < mode1 or neg_binom_ix < 1000:
         del_beta = nbinom.pmf(neg_binom_ix,m,p2)*\
                     nbinom.sf(neg_binom_ix-1,m,p1)
         beta += del_beta
         neg_binom_ix += 1
     return beta
コード例 #5
0
ファイル: crisprFunction.py プロジェクト: yarker/MAGeCK_Repo
def getNBPValue(mean0,var0,mean1, lower=False,log=False):
  """
  Use negative binomial to calculate p-value
  Reference:
  http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.nbinom.html#scipy.stats.nbinom
  """
  from scipy.stats import nbinom
  n=len(mean0)
  nb_p=[mean0[i]/var0[i] for i in range(n)]; # consisitent with R
  nb_n0=[mean0[i]*mean0[i]/(var0[i]-mean0[i]) for i in range(n)]
  nb_n=[ (lambda t: t if t>=1 else 1)(x) for x in nb_n0]
  #
  if lower==True:
    if log==False:
      nb_p_low=nbinom.cdf(mean1,nb_n,nb_p)
    else:
      nb_p_low=nbinom.logcdf(mean1,nb_n,nb_p)
    return list(nb_p_low)
  else:
    if log==False:
      nb_p_low=nbinom.sf(mean1,nb_n,nb_p)
    else:
      nb_p_low=nbinom.logsf(mean1,nb_n,nb_p)
    return list(nb_p_low)
コード例 #6
0
def test_issue_6682():
    # Reference value from R:
    # options(digits=16)
    # print(pnbinom(250, 50, 32/63, lower.tail=FALSE))
    assert_allclose(nbinom.sf(250, 50, 32. / 63.), 1.460458510976452e-35)
コード例 #7
0
ファイル: __init__.py プロジェクト: ursho2552/albopictus
def nbinom_dist_prob(array, mean, sd):
    p = numpy.float64(mean) / (sd * sd)
    r = mean * p / (1.0 - p)
    return 1.0 - (nbinom.sf(array + 1, r, p)) / (nbinom.sf(array, r, p))
コード例 #8
0
 def test_logpmf_zero(self):
     n, p = truncatednegbin.convert_params(5, 1, 2)
     nb_logpmf = nbinom.logpmf(1, n, p) - np.log(nbinom.sf(0, n, p))
     tnb_logpmf = truncatednegbin.logpmf(1, 5, 1, 2, 0)
     assert_allclose(nb_logpmf, tnb_logpmf, rtol=1e-2, atol=1e-2)
コード例 #9
0
 def test_pmf_zero(self):
     n, p = truncatednegbin.convert_params(5, 0.1, 2)
     nb_pmf = nbinom.pmf(1, n, p) / nbinom.sf(0, n, p)
     tnb_pmf = truncatednegbin.pmf(1, 5, 0.1, 2, 0)
     assert_allclose(nb_pmf, tnb_pmf, rtol=1e-5)
コード例 #10
0
 def _ppf(self, q, n, p):
     return nbinom.ppf(nbinom.sf(0, n, p) * q + nbinom.pmf(0, n, p), n, p)
コード例 #11
0
 def _cdf(self, x, n, p):
     k = floor(x)
     if k == 0:
         return 0.0
     else:
         return (nbinom.cdf(x, n, p) - nbinom.pmf(0, n, p)) / nbinom.sf(0, n, p) 
コード例 #12
0
 def _pmf(self, x, n, p):
     if x == 0:
         return 0.0
     else:
         return nbinom.pmf(x, n, p) / nbinom.sf(0, n, p)