def rho(beta, mk, K, N):
    """
    rho(beta, data) is the Dirichlet multinomial likelihood.

    rho(beta, data) together with the d_xi(beta) make up
    the posterior for the nsb estimator
    """

    return np.prod([mp.power(mp.rf(beta, np.double(n)), mk[n])
                    for n in mk]) / mp.rf(K * beta, np.double(N))
def compute_g(n):
    """g_k from DLMF 5.11.3/5.11.5"""
    a = compute_a(2*n)
    g = []
    for k in range(n):
        g.append(mp.sqrt(2)*mp.rf(0.5, k)*a[2*k])
    return g
Exemplo n.º 3
0
 def _get_value(self, x, mu, v):
     try:
         return rf(v, x) / gamma(x + 1) * (v / float(v + mu))**v * (
             mu / float(v + mu))**x
     except ValueError:
         print(x, mu, v, file=sys.stderr)
         return 1
Exemplo n.º 4
0
def o_function(l, k, r):
    res = mpmath.hyp1f1(-1j / k + l, 2 * l + 2, -2 * 1j * k * r)
    res *= (1. / (2 * l + 1)) * (k * r)**l
    res *= mpmath.gamma(1 + 1j / k)
    res *= mpmath.rf(-1j / k, l)
    res *= (-2j)**l / mpmath.fac(2 * l)
    res *= mpmath.exp(mpmath.pi / (2 * k))
    return res
Exemplo n.º 5
0
def revert(coeffs, taylor=True):
    """
    Series reversion.

    Given `coeffs`, the Taylor coefficients of a function, find the
    Taylor coefficients of the compositional inverse of the function.

    The calculation here is based on the formulas for the coefficients
    of the inverse in terms of Bell polynomials:
    https://en.wikipedia.org/wiki/Bell_polynomials#Reversion_of_series

    If the argument `taylor` is True, the coefficients must be the
    Taylor series coefficients.  If it is not True, the coefficients
    are the derivatives.  That is, they are the Taylor coefficients
    multiplied by the factorial terms.
    """
    c0 = coeffs[0]
    if coeffs[1] == 0:
        raise ValueError('coeffs[1] must be nonzero.')

    if taylor:
        # Rescale the coefficients to the derivative values.
        f = [c * mpmath.factorial(i) for i, c in enumerate(coeffs)]
    else:
        f = coeffs

    m = len(f)
    f_hat = [f[k + 1] / ((k + 1) * f[1]) for k in range(1, m - 1)]

    # inv_derivs will be the series of derivatives of the inverse.
    g = [0] * m
    g[1] = 1 / f[1]

    for n in range(2, m):
        s = sum((-1)**k * mpmath.rf(n, k) *
                _bell_incomplete_poly(n - 1, k, f_hat[:n - k])
                for k in range(1, n))
        g[n] = s / f[1]**n

    if taylor:
        # Convert the derivatives to the Taylor coefficients
        inv_coeffs = [c / mpmath.factorial(k) for k, c in enumerate(g)]
    else:
        inv_coeffs = g

    return inv_coeffs, c0
Exemplo n.º 6
0
def _rhoi(x, nxkx, beta):
  return power(rf(beta, np.double(x)), nxkx[x])
def log_likelihood_DP_alpha(a, K1, N):
    """
    Alpha-dependent terms of the log-likelihood of a Dirichlet Process.
    """

    return (K1 - 1.) * mp.log(a) - mp.log(mp.rf(a + 1., N - 1.))
Exemplo n.º 8
0
def _logvarrho_DP(a, rnsum, K1, N):
    return rnsum + _logprod_DP(a, K1) - mp.log(mp.rf(a + 1., N - 1.))
Exemplo n.º 9
0
def _logvarrhoi_DP(n, mk):
    return mp.log(mp.rf(1., n - 1.)) * mk[n]
Exemplo n.º 10
0
def _rho(beta, mk, K,
         N):  # Computes the Dirichlet-multinomial likelihood from eq. (68)
    rn = np.array([_rhoi(n, beta, mk, K, N) for n in mk])
    return rn.prod() / mp.rf(K * beta, np.double(N))
Exemplo n.º 11
0
def _rho(beta, nxkx, N, K):
    kappa = beta * K
    rx = np.array([_rhoi(x, nxkx, beta) for x in nxkx])
    return rx.prod() / rf(kappa, np.double(N))
Exemplo n.º 12
0
def _rhoi(x, nxkx, beta):
    return power(rf(beta, np.double(x)), nxkx[x])
Exemplo n.º 13
0
def _rhoi(x, nxkx, beta):
    return power(rf(beta, float(x)), float(nxkx[x]))
Exemplo n.º 14
0
def _rho(beta, nxkx, N, K):  # this is p(B|c)*log(m)/p(B)
    kappa = beta * K
    rx = np.array([_rhoi(x, nxkx, beta) for x in nxkx])
    return rx.prod() / rf(kappa, np.double(N))
Exemplo n.º 15
0
def _rho(beta, nxkx, N, K):# this is p(B|c)*log(m)/p(B)
  kappa = beta*K
  rx = np.array([_rhoi(x, nxkx, beta) for x in nxkx])
  return rx.prod()/rf(kappa, np.double(N))
Exemplo n.º 16
0
 def _get_value(self, x, mu, v):
     try:
         return rf(v, x) / gamma(x+1) * ( v/float(v+mu) ) ** v * ( mu/float(v+mu) ) ** x
     except ValueError:
         print(x, mu, v, file=sys.stderr)
         return 1
Exemplo n.º 17
0
def _rhoi(n, beta, mk, K,
          N):  # Computes the ratio of gamma functions in the product
    return mp.power(mp.rf(beta, np.double(n)), mk[n])