Exemplo n.º 1
0
def beta_fisher(x1, x2, params={'alpha': .5, 'beta': .5}):
    score1 = beta_score(x1, params)
    score2 = beta_score(x2, params)
    temp = polygamma(1, params['alpha'] + params['beta'])
    fisher_info_mat = np.array([[polygamma(1, params['alpha']) - temp, -temp],
                                [-temp,
                                 polygamma(1, params['beta']) - temp]])
    return np.dot(np.dot(score1, inv(fisher_info_mat)), score2.T)[0, 0]
Exemplo n.º 2
0
def gamma_diffusion(x1, x2, params={'t': .09}):
    try:
        n = len(x1)
    except:
        n = 1
    geo = polygamma(1, x1) - polygamma(1, x2)
    squ_geo = np.sum(geo * geo)
    return np.exp((-.25 / params['t']) * squ_geo)
Exemplo n.º 3
0
    def metric_det(param_a, param_b):
        """Compute the determinant of the metric.

        Parameters
        ----------
        param_a : array-like, shape=[n_samples,]
        param_b : array-like, shape=[n_samples,]

        Returns
        -------
        metric_det : array-like, shape=[n_samples,]
        """
        metric_det = polygamma(1, param_a) * polygamma(1, param_b) - \
            polygamma(1, param_a + param_b) * (polygamma(1, param_a) +
                                               polygamma(1, param_b))
        return metric_det
Exemplo n.º 4
0
def gamma_fisher(x1, x2, params={'alpha': 1., 'theta': 1.}):
    score1 = gamma_score(x1, params)
    score2 = gamma_score(x2, params)
    temp = 1. / params['theta']
    fisher_info_mat = np.array([[polygamma(1, params['alpha']), temp],
                                [temp, params['alpha'] / params['theta']**2]])
    return np.dot(np.dot(score1, inv(fisher_info_mat)), score2.T)[0, 0]
Exemplo n.º 5
0
    def inner_product_matrix(self, base_point=None):
        """Compute inner product matrix at the tangent space at base point.

        Parameters
        ----------
        base_point : array-like, shape=[n_samples, 2]

        Returns
        -------
        base_point : array-like, shape=[n_samples, 2, 2]
        """
        if base_point is not None:
            raise ValueError('The metric depends on the base point.')
        base_point = gs.to_ndarray(base_point, to_ndim=2)
        matrices = []
        for point in base_point:
            param_a, param_b = point
            g0 = gs.array(
                [polygamma(1, param_a) - polygamma(1, param_a + param_b),
                 - polygamma(1, param_a + param_b)])
            g1 = gs.array(
                [- polygamma(1, param_a + param_b), polygamma(1, param_b)
                 - polygamma(1, param_a + param_b)])
            matrices.append(gs.stack([g0, g1]))
        return gs.stack(matrices)
def grep_gradient(alpha, m, x, K, alphaz):
    gradient = np.zeros((alpha.shape[0], 2))
    lmbda = npr.gamma(alpha, 1.)
    lmbda[lmbda < 1e-5] = 1e-5
    Tinv_val = fun_Tinv(lmbda, alpha)
    h_val = fun_H(Tinv_val, alpha)
    u_val = fun_U(Tinv_val, alpha)

    zw = m * lmbda / alpha
    zw[zw < 1e-5] = 1e-5
    logp_der = grad_logp(zw, K, x, alphaz)
    logp_val = logp(zw, K, x, alphaz)
    logq_der = grad_logQ_Z(zw, alpha)

    gradient[:, 0] = logp_der * (h_val - lmbda / alpha) * m / alpha
    gradient[:, 1] = logp_der * lmbda / alpha
    gradient[:, 0] += logp_val * (
        np.log(lmbda) + (alpha / lmbda - 1.) * h_val - sp.digamma(alpha) +
        sp.polygamma(2, alpha) / 2. / sp.polygamma(1, alpha))
    gradient += grad_entropy(alpha, m)

    return gradient
Exemplo n.º 7
0
def grad_entropy(alpha, m):
    gradient = np.zeros((alpha.shape[0], 2))
    gradient[:, 0] = 1. - 1. / alpha + (1. - alpha) * sp.polygamma(1, alpha)
    gradient[:, 1] = 1. / m

    return gradient
Exemplo n.º 8
0
 def coefficients(param_a, param_b):
     metric_det = self.metric_det(param_a, param_b)
     c1 = (polygamma(2, param_a) * polygamma(1, param_b) -
           polygamma(2, param_a) * polygamma(1, param_a + param_b) -
           polygamma(1, param_b) * polygamma(2, param_a + param_b)) / (
         2 * metric_det)
     c2 = - polygamma(1, param_b) * polygamma(2, param_a + param_b) / (
         2 * metric_det)
     c3 = (polygamma(2, param_b) * polygamma(1, param_a + param_b) -
           polygamma(1, param_b) * polygamma(2, param_a + param_b)) / (
         2 * metric_det)
     return c1, c2, c3
def fun_Tinv(z, alpha):
    return (np.log(z) - sp.digamma(alpha)) / np.sqrt(sp.polygamma(1, alpha))
Exemplo n.º 10
0
def fun_U(eps, alpha):
    poly1 = sp.polygamma(1, alpha)
    poly2 = sp.polygamma(2, alpha)

    return 0.5 * poly2 / poly1 + 0.5 * poly2 * eps / np.sqrt(poly1) + poly1
Exemplo n.º 11
0
def fun_H(eps, alpha):
    poly1 = sp.polygamma(1, alpha)
    poly2 = sp.polygamma(2, alpha)
    T_val = fun_T(eps, alpha)

    return T_val * (0.5 * eps * poly2 / np.sqrt(poly1) + poly1)
Exemplo n.º 12
0
def fun_T(eps, alpha):
    return np.exp(eps * np.sqrt(sp.polygamma(1, alpha)) + sp.digamma(alpha))
Exemplo n.º 13
0
from __future__ import absolute_import

import autograd.numpy as np
import autograd.scipy.special as sp

### Gamma functions ###
sp.polygamma.defjvp(lambda g, ans, gvs, vs, n, x: g * sp.polygamma(n + 1, x),
                    argnum=1)
sp.psi.defjvp(lambda g, ans, gvs, vs, x: g * sp.polygamma(1, x))
sp.digamma.defjvp(lambda g, ans, gvs, vs, x: g * sp.polygamma(1, x))
sp.gamma.defjvp(lambda g, ans, gvs, vs, x: g * ans * sp.psi(x))
sp.gammaln.defjvp(lambda g, ans, gvs, vs, x: g * sp.psi(x))
sp.rgamma.defjvp(lambda g, ans, gvs, vs, x: g * sp.psi(x) / -sp.gamma(x))
sp.multigammaln.defjvp(lambda g, ans, gvs, vs, a, d: g * np.sum(
    sp.digamma(np.expand_dims(a, -1) - np.arange(d) / 2.), -1))

### Bessel functions ###
sp.j0.defjvp(lambda g, ans, gvs, vs, x: -g * sp.j1(x))
sp.y0.defjvp(lambda g, ans, gvs, vs, x: -g * sp.y1(x))
sp.j1.defjvp(lambda g, ans, gvs, vs, x: g * (sp.j0(x) - sp.jn(2, x)) / 2.0)
sp.y1.defjvp(lambda g, ans, gvs, vs, x: g * (sp.y0(x) - sp.yn(2, x)) / 2.0)
sp.jn.defjvp(lambda g, ans, gvs, vs, n, x: g *
             (sp.jn(n - 1, x) - sp.jn(n + 1, x)) / 2.0,
             argnum=1)
sp.yn.defjvp(lambda g, ans, gvs, vs, n, x: g *
             (sp.yn(n - 1, x) - sp.yn(n + 1, x)) / 2.0,
             argnum=1)

### Error Function ###
sp.erf.defjvp(
    lambda g, ans, gvs, vs, x: 2. * g * sp.inv_root_pi * np.exp(-x**2))
Exemplo n.º 14
0
def beta_score(x, params):
    v1 = np.log(x) - polygamma(0, params['alpha']) + polygamma(
        0, params['alpha'] + params['beta'])
    v2 = np.log(1 - x) - polygamma(0, params['beta']) + polygamma(
        0, params['alpha'] + params['beta'])
    return np.array([[v1, v2]])
Exemplo n.º 15
0
def gamma_kld(a1, a2, theta1=10., theta2=10.):
    return np.sum((a1 - a2) * polygamma(0, a1) - np.log(gammaFn(a1)) +
                  np.log(gammaFn(a2)) + a2 *
                  (np.log(theta2) - np.log(theta1)) + a1 *
                  ((theta1 - theta2) / theta2))
Exemplo n.º 16
0
def gamma_score(x, params):
    v1 = np.log(x) - np.log(params['theta']) - polygamma(0, params['alpha'])
    v2 = x / params['theta']**2 - params['alpha'] / params['theta']
    return np.array([[v1, v2]])