当前位置: 首页>>代码示例>>Python>>正文


Python special.polygamma方法代码示例

本文整理汇总了Python中scipy.special.polygamma方法的典型用法代码示例。如果您正苦于以下问题:Python special.polygamma方法的具体用法?Python special.polygamma怎么用?Python special.polygamma使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在scipy.special的用法示例。


在下文中一共展示了special.polygamma方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_polygamma

# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import polygamma [as 别名]
def test_polygamma(self):
        poly2 = special.polygamma(2,1)
        poly3 = special.polygamma(3,1)
        assert_almost_equal(poly2,-2.4041138063,10)
        assert_almost_equal(poly3,6.4939394023,10)

        # Test polygamma(0, x) == psi(x)
        x = [2, 3, 1.1e14]
        assert_almost_equal(special.polygamma(0, x), special.psi(x))

        # Test broadcasting
        n = [0, 1, 2]
        x = [0.5, 1.5, 2.5]
        expected = [-1.9635100260214238, 0.93480220054467933,
                    -0.23620405164172739]
        assert_almost_equal(special.polygamma(n, x), expected)
        expected = np.row_stack([expected]*2)
        assert_almost_equal(special.polygamma(n, np.row_stack([x]*2)),
                            expected)
        assert_almost_equal(special.polygamma(np.row_stack([n]*2), x),
                            expected) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:23,代码来源:test_basic.py

示例2: _stats

# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import polygamma [as 别名]
def _stats(self, c):
        # See, for example, "A Statistical Study of Log-Gamma Distribution", by
        # Ping Shing Chan (thesis, McMaster University, 1993).
        mean = sc.digamma(c)
        var = sc.polygamma(1, c)
        skewness = sc.polygamma(2, c) / np.power(var, 1.5)
        excess_kurtosis = sc.polygamma(3, c) / (var*var)
        return mean, var, skewness, excess_kurtosis 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:10,代码来源:_continuous_distns.py

示例3: update_alpha

# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import polygamma [as 别名]
def update_alpha(self, gammat, rho):
        """
        Update parameters for the Dirichlet prior on the per-document
        topic weights `alpha` given the last `gammat`.

        Uses Newton's method: http://www.stanford.edu/~jhuang11/research/dirichlet/dirichlet.pdf

        """
        N = float(len(gammat))
        logphat = sum(dirichlet_expectation(gamma) for gamma in gammat) / N
        dalpha = numpy.copy(self.alpha)
        gradf = N * (psi(numpy.sum(self.alpha)) - psi(self.alpha) + logphat)

        c = N * polygamma(1, numpy.sum(self.alpha))
        q = -N * polygamma(1, self.alpha)

        b = numpy.sum(gradf / q) / ( 1 / c + numpy.sum(1 / q))

        dalpha = -(gradf - b) / q

        if all(rho() * dalpha + self.alpha > 0):
            self.alpha += rho() * dalpha
        else:
            logger.warning("updated alpha not positive")
        logger.info("optimized alpha %s" % list(self.alpha))

        return self.alpha 
开发者ID:largelymfs,项目名称:topical_word_embeddings,代码行数:29,代码来源:ldamodel.py

示例4: update_alpha

# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import polygamma [as 别名]
def update_alpha(self, gammat, rho):
        """
        Update parameters for the Dirichlet prior on the per-document
        topic weights `alpha` given the last `gammat`.

        Uses Newton's method, described in **Huang: Maximum Likelihood Estimation of Dirichlet Distribution Parameters.** (http://www.stanford.edu/~jhuang11/research/dirichlet/dirichlet.pdf)

        """
        N = float(len(gammat))
        logphat = sum(dirichlet_expectation(gamma) for gamma in gammat) / N
        dalpha = numpy.copy(self.alpha)
        gradf = N * (psi(numpy.sum(self.alpha)) - psi(self.alpha) + logphat)

        c = N * polygamma(1, numpy.sum(self.alpha))
        q = -N * polygamma(1, self.alpha)

        b = numpy.sum(gradf / q) / ( 1 / c + numpy.sum(1 / q))

        dalpha = -(gradf - b) / q

        if all(rho() * dalpha + self.alpha > 0):
            self.alpha += rho() * dalpha
        else:
            logger.warning("updated alpha not positive")
        logger.info("optimized alpha %s" % list(self.alpha))

        return self.alpha 
开发者ID:largelymfs,项目名称:topical_word_embeddings,代码行数:29,代码来源:ldamodel.py

示例5: test_polygamma

# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import polygamma [as 别名]
def test_polygamma(self):
        assert_mpmath_equal(sc.polygamma,
                            _time_limited()(_exception_to_nan(mpmath.polygamma)),
                            [IntArg(0, 1000), Arg()]) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:6,代码来源:test_mpmath.py

示例6: label

# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import polygamma [as 别名]
def label(self):
        return 'polygamma' 
开发者ID:chainer,项目名称:chainer,代码行数:4,代码来源:polygamma.py

示例7: forward_cpu

# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import polygamma [as 别名]
def forward_cpu(self, inputs):
        n, x = inputs
        global _polygamma_cpu
        if _polygamma_cpu is None:
            try:
                from scipy import special
                _polygamma_cpu = special.polygamma
            except ImportError:
                raise ImportError('SciPy is not available. Forward computation'
                                  ' of polygamma can not be done.')
        self.retain_inputs((0, 1))
        return utils.force_array(_polygamma_cpu(n, x), dtype=x.dtype), 
开发者ID:chainer,项目名称:chainer,代码行数:14,代码来源:polygamma.py

示例8: forward_gpu

# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import polygamma [as 别名]
def forward_gpu(self, inputs):
        n, x = inputs
        self.retain_inputs((0, 1))
        return utils.force_array(
            cuda.cupyx.scipy.special.polygamma(n, x), dtype=x.dtype), 
开发者ID:chainer,项目名称:chainer,代码行数:7,代码来源:polygamma.py

示例9: backward

# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import polygamma [as 别名]
def backward(self, indexes, gy):
        n, x = self.get_retained_inputs()
        return None, polygamma(n + 1, x) * gy[0], 
开发者ID:chainer,项目名称:chainer,代码行数:5,代码来源:polygamma.py

示例10: polygamma

# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import polygamma [as 别名]
def polygamma(n, x):
    """Polygamma function.

    .. note::
       Forward computation in CPU can not be done if
       `SciPy <https://www.scipy.org/>`_ is not available.

    Args:
        n (:class:`~chainer.Variable` or :ref:`ndarray`): Input variable.
        x (:class:`~chainer.Variable` or :ref:`ndarray`): Input variable.

    Returns:
        ~chainer.Variable: Output variable.
    """
    return PolyGamma().apply((n, x))[0] 
开发者ID:chainer,项目名称:chainer,代码行数:17,代码来源:polygamma.py

示例11: _maximize_alpha

# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import polygamma [as 别名]
def _maximize_alpha(self, max_iters=1000, tol=0.1):
        """
        Optimize alpha using Blei's O(n) Newton-Raphson modification
        for a Hessian with special structure
        """
        D = self.D
        T = self.T

        alpha = self.alpha
        gamma = self.gamma

        for _ in range(max_iters):
            alpha_old = alpha

            #  Calculate gradient
            g = D * (digamma(np.sum(alpha)) - digamma(alpha)) + np.sum(
                digamma(gamma) - np.tile(digamma(np.sum(gamma, axis=1)), (T, 1)).T,
                axis=0,
            )

            #  Calculate Hessian diagonal component
            h = -D * polygamma(1, alpha)

            #  Calculate Hessian constant component
            z = D * polygamma(1, np.sum(alpha))

            #  Calculate constant
            c = np.sum(g / h) / (z ** (-1.0) + np.sum(h ** (-1.0)))

            #  Update alpha
            alpha = alpha - (g - c) / h

            #  Check convergence
            if np.sqrt(np.mean(np.square(alpha - alpha_old))) < tol:
                break

        return alpha 
开发者ID:ddbourgin,项目名称:numpy-ml,代码行数:39,代码来源:lda.py

示例12: test_polygamma

# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import polygamma [as 别名]
def test_polygamma(self):
        assert_mpmath_equal(sc.polygamma,
                            time_limited()(exception_to_nan(mpmath.polygamma)),
                            [IntArg(0, 1000), Arg()]) 
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:6,代码来源:test_mpmath.py

示例13: objectiveGradient

# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import polygamma [as 别名]
def objectiveGradient(lambda_k, nu, tau, Elog_eta_k, nDoc):
  ''' Calculate gradient of objectiveFunc, objective for HDP variational 
      Returns
      -------
        gvec : 2*K length vector,
              where each entry gives partial derivative with respect to
                  the corresponding entry of Cvec
  '''
  # lvec is the derivative of log(lambda_k) via chain rule
  lvec = 1/(lambda_k)
  W = lvec.size
  
  # Derivative of log eta
  digammaAll = digamma(np.sum(lambda_k))
  Elog_lambda_k = digamma(lambda_k) - digammaAll

  # Derivative of Elog_phi_k and E_phi_k
  polygammaAll = polygamma(1,np.sum(lambda_k))
  dElog_phi_k = polygamma(1,lambda_k) - polygammaAll
  lambda_k_sum = np.sum(lambda_k)
  dE_phi_k = (lambda_k_sum - lambda_k) / np.power(lambda_k_sum,2)

  gvec = dElog_phi_k * (N + tau - lambda_k) \
       + dE_phi_k * nu * Elog_eta_k
  gvec = -1 * gvec

  # Apply chain rule!
  gvecC = lvec * gvec
  return gvecC 
开发者ID:daeilkim,项目名称:refinery,代码行数:31,代码来源:BurstyVariationalOptimizer.py

示例14: _hessian_nb2

# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import polygamma [as 别名]
def _hessian_nb2(self, params):
        """
        Hessian of NB2 model.
        """
        if self._transparams: # lnalpha came in during fit
            alpha = np.exp(params[-1])
        else:
            alpha = params[-1]
        a1 = 1/alpha
        params = params[:-1]

        exog = self.exog
        y = self.endog[:,None]
        mu = self.predict(params)[:,None]
        prob = a1 / (a1 + mu)
        dgpart = digamma(a1 + y) - digamma(a1)

        # for dl/dparams dparams
        dim = exog.shape[1]
        hess_arr = np.empty((dim+1,dim+1))
        const_arr = a1*mu*(a1+y)/(mu+a1)**2
        for i in range(dim):
            for j in range(dim):
                if j > i:
                    continue
                hess_arr[i,j] = np.sum(-exog[:,i,None] * exog[:,j,None] *
                                       const_arr, axis=0)
        tri_idx = np.triu_indices(dim, k=1)
        hess_arr[tri_idx] = hess_arr.T[tri_idx]

        # for dl/dparams dalpha
        da1 = -alpha**-2
        dldpda = -np.sum(mu*exog*(y-mu)*a1**2/(mu+a1)**2 , axis=0)
        hess_arr[-1,:-1] = dldpda
        hess_arr[:-1,-1] = dldpda

        # for dl/dalpha dalpha
        #NOTE: polygamma(1,x) is the trigamma function
        da2 = 2*alpha**-3
        dalpha = da1 * (dgpart +
                    np.log(prob) - (y - mu)/(a1+mu))
        dada = (da2 * dalpha/da1 + da1**2 * (special.polygamma(1, a1+y) -
                    special.polygamma(1, a1) + 1/a1 - 1/(a1 + mu) +
                    (y - mu)/(mu + a1)**2)).sum()
        hess_arr[-1,-1] = dada

        return hess_arr

    #TODO: replace this with analytic where is it used? 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:51,代码来源:discrete_model.py

示例15: _calcGradients

# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import polygamma [as 别名]
def _calcGradients(u1, u0, E):
  '''
  '''
  dU1 = dict()
  dU0 = dict()
  K = u1.size
  uboth = u1 + u0
  polygamma1Both = polygamma(1, uboth)
  dU1['Elogv'] = polygamma(1, u1) - polygamma1Both
  dU0['Elogv'] = -polygamma1Both
  dU1['Elog1-v'] = -polygamma1Both
  dU0['Elog1-v'] = polygamma(1,u0) - polygamma1Both

  Q1 = u1 / (uboth * uboth)
  Q0 = u0 / (uboth * uboth)

  dU1_Ebeta = np.tile(E['beta'], (K,1))
  dU1_Ebeta /= E['1-v'][:,np.newaxis]
  diagIDs = np.diag_indices(K)
  dU1_Ebeta[diagIDs] /= -E['v']/E['1-v']  

  # Slow way to force lower-triangle of dU1 to be all zeros
  #lowTriIDs = np.tril_indices(K, -1)
  #dU1_Ebeta[lowTriIDs] = 0

  # Fast way
  lowTriIDs = get_lowTriIDs(K)
  dU1_Ebeta[lowTriIDs] = 0

  # Fastest way
  #lowTriIDs = get_lowTriIDs_flat(K)
  #dU1_Ebeta.ravel()[flatlowTriIDs] = 0

  dU0_Ebeta = dU1_Ebeta * Q1[:,np.newaxis]
  dU1_Ebeta *= -1 * Q0[:,np.newaxis]

  dU1['Ebeta'] = dU1_Ebeta
  dU0['Ebeta'] = dU0_Ebeta
  return dU1, dU0


########################################################### Objective/gradient
########################################################### in terms of c 
开发者ID:daeilkim,项目名称:refinery,代码行数:45,代码来源:OptimizerForHDPFullVarModel.py


注:本文中的scipy.special.polygamma方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。