Esempio n. 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)
Esempio n. 2
0
def test_nbinom_11465(mu, q, expected):
    # test nbinom.logcdf at extreme tails
    size = 20
    n, p = size, size / (size + mu)
    # In R:
    # options(digits=16)
    # pnbinom(mu=10, size=20, q=120, log.p=TRUE)
    assert_allclose(nbinom.logcdf(q, n, p), expected)
Esempio n. 3
0
def no_admixes(p, admixes, hard_cutoff=20, r=0):
    if admixes > hard_cutoff:
        return -float('inf')
    if r > 1:
        if hard_cutoff is None:
            return nbinom.logpmf(admixes, n=r, p=1.0 - p)
        else:
            return nbinom.logpmf(admixes, n=r, p=1.0 - p) - nbinom.logcdf(
                hard_cutoff, n=r, p=1.0 - p)
    else:
        if hard_cutoff is None:
            return geom.logpmf(admixes + 1, 1.0 - p)

        return geom.logpmf(admixes + 1, 1.0 - p) - geom.logcdf(
            hard_cutoff + 1, 1.0 - p)
Esempio n. 4
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)