def __igamc(a, b):
        # integral from 0 to x (e^(-t) * t^(a-1) dt
        gamma_value = gamma(float(a) / 2.0) - gamma(float(b) / 2.0)

        # integral from 0 to inf (t^(z-1) * e^(-t) dt
        gamma_function = gamma(float(a) / 2.0)

        return 1 - (gamma_value / gamma_function)
def ibeta_cf(d, a, b, x):
    if d == 100:
        return mpfr('0.0')  # end at 100 iterations
    if d == 0:  # First term 1/1+|
        mult = ((x**a) * ((mpfr('1.0') - x)**b)) / a
        mult = mult * gmpy2.gamma(a + b)
        mult = mult / (gmpy2.gamma(a) * gmpy2.gamma(b))
        m = 0
        return mult * mpfr('1.0') / (mpfr('1.0') + ibeta_cf(d + 1, a, b, x))
    elif ((d % 2) == 1):  # Odd terms d_n/1+|
        m = (d - 1) / 2
        return d2mp1(a, b, m, x) / (mpfr('1.0') + ibeta_cf(d + 1, a, b, x))
    else:  # Even terms d_{n+1}+|
        m = d / 2
        return d2m(a, b, m, x) / (mpfr('1.0') + ibeta_cf(d + 1, a, b, x))
Esempio n. 3
0
def fast_etienne_likelihood(mod, params, kda=None, kda_x=None):
    '''
    same as Abundance inner function, but takes advantage of constant
    m value when varying only theta.

    :argument abd: Abundance object
    :argument params: list containing theta and m
    :argument kda_x: precomputed list of exp(kda + ind*immig)
    '''
    theta     = params[0]
    immig     = float (params[1]) / (1 - params[1]) * (mod.community.J - 1)
    log_immig = log (immig)
    theta_s   = theta + mod.community.S
    if not kda_x:
        kda_x = [exp(mod._kda[val] + val * log_immig) for val in \
                 xrange (mod.community.J - mod.community.S)]
    poch1 = exp (mod._factor + log (theta) * mod.community.S - \
                 lpoch (immig, mod.community.J) + \
                 log_immig * mod.community.S + lngamma(theta))
    gam_theta_s = gamma (theta_s)
    lik = mpfr(0.0)
    for val in xrange (mod.community.J - mod.community.S):
        lik += poch1 * kda_x[val] / gam_theta_s
        gam_theta_s *= theta_s + val
    return ((-log (lik)), kda_x)
Esempio n. 4
0
 def likelihood(self, params):
     '''
     log-likelihood function
     
     :argument params: a list of 2 parameters:
     
       * theta = params[0]
       * m     = params[1]
     :returns: log likelihood of given theta and I
     
     '''
     kda       = self._kda
     theta     = params[0]
     immig     = float(params[1]) / (1 - params[1]) * (self.community.J - 1)
     log_immig = log(immig)
     theta_s   = theta + self.community.S
     poch1 = exp(self._factor + log(theta) * self.community.S - \
                 lpoch(immig, self.community.J) + \
                 log_immig * self.community.S + lngamma(theta))
     gam_theta_s = gamma(theta_s)
     lik = mpfr(0.0)
     for abd in xrange(int(self.community.J - self.community.S)):
         lik += poch1 * exp(kda[abd] + abd * log_immig) / gam_theta_s
         gam_theta_s *= theta_s + abd
     return -log(lik)