• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python math.lgamma函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中math.lgamma函数的典型用法代码示例。如果您正苦于以下问题:Python lgamma函数的具体用法?Python lgamma怎么用?Python lgamma使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



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

示例1: compute_likelihood

def compute_likelihood(document, model, phi, var_gamma):
    likelihood = 0
    digsum = 0
    var_gamma_sum = 0
    dig = [0 for x in range(model.num_topics)]

    for k in range(0, model.num_topics):
        dig[k] = digamma(var_gamma[k])
        var_gamma_sum = var_gamma[k] + var_gamma_sum

    digsum = digamma(var_gamma_sum)

    likelihood = math.lgamma(model.alpha * model.num_topics) \
                 - model.num_topics * math.lgamma(model.alpha) \
                 - (math.lgamma(var_gamma_sum))

    for k in range(0, model.num_topics):
        likelihood += ((model.alpha - 1) * (dig[k] - digsum)
                       + math.lgamma(var_gamma[k]) - (var_gamma[k] - 1)
                       * (dig[k] - digsum))

        for n in range(0, document.unique_word_count):
            if phi[n][k] > 0:
                likelihood += document.word_counts[n] * \
                              (phi[n][k] * ((dig[k] - digsum)
                                            - math.log(phi[n][k])
                                            + model.log_prob_w[k][document.words[n]]))

    return likelihood
开发者ID:schomper,项目名称:Thesis,代码行数:29,代码来源:util_functions.py


示例2: log_likelihood

 def log_likelihood(self, full=False):
     ll = (math.lgamma(self.alpha) - math.lgamma(self.alpha + self.total_customers)
             + sum(math.lgamma(c) for tables in self.tables.itervalues() for c in tables)
             + self.ntables * math.log(self.alpha))
     if full:
         ll += self.base.log_likelihood(full=True) + self.prior.log_likelihood()
     return ll
开发者ID:pearsonhenry,项目名称:vpyp,代码行数:7,代码来源:pyp.py


示例3: LogCombinations

def LogCombinations(x,y):
    u"""Calculates the logarithm of a binomial coefficient.
    This avoids overflows. Implemented with gamma functions for efficiency"""
    result=lgamma(x+1)
    result-=lgamma(y+1)
    result-=lgamma(x-y+1)
    return result
开发者ID:PeteBleackley,项目名称:nltk,代码行数:7,代码来源:EntropyCalculator.py


示例4: log_likelihood

 def log_likelihood(self, full=False):
     ll = (math.lgamma(self.K * self.alpha) - math.lgamma(self.K * self.alpha + self.N)
             + sum(math.lgamma(self.alpha + self.count[k]) for k in xrange(self.K))
             - self.K * math.lgamma(self.alpha))
     if full:
         ll += self.prior.log_likelihood()
     return ll
开发者ID:Peratham,项目名称:vpyp,代码行数:7,代码来源:prob.py


示例5: UpdateKappaSigmaSq

    def UpdateKappaSigmaSq(self,it):
        for ii in xrange(self.T-1):
            new_kappa_sigma_sq = self.kappa_sigma_sq[ii]+(2*np.ceil(2*np.random.random())-3)*(np.random.geometric(1.0/(1+np.exp(self.log_kappa_sigma_sqq[ii])))-1)

            if new_kappa_sigma_sq <0:
                accept = 0
            else:
                lam1 = 1.0*self.lambda_sigma + self.kappa_sigma_sq[ii]
                gam1 = 1.0*self.lambda_sigma/self.mu_sigma + 1.0*self.rho_sigma/(1-self.rho_sigma)*self.lambda_sigma/self.mu_sigma
                loglike = lam1*np.log(gam1)-math.lgamma(lam1)+(lam1-1)*np.log(self.sigma_sq[ii+1])
                pnmean = self.sigma_sq[ii]*self.rho_sigma/(1-self.rho_sigma)*self.lambda_sigma/self.mu_sigma
                loglike = loglike + self.kappa_sigma_sq[ii]*np.log(pnmean)- math.lgamma(1.0*self.kappa_sigma_sq[ii]+1)

                lam1 = 1.0*self.lambda_sigma + new_kappa_sigma_sq
                gam1 = 1.0*self.lambda_sigma/self.mu_sigma + self.rho_sigma/(1-self.rho_sigma)*self.lambda_sigma/self.mu_sigma
                new_loglike = lam1*np.log(gam1)-math.lgamma(lam1)+(lam1-1)*np.log(self.sigma_sq[ii+1])
                pnmean = self.sigma_sq[ii]*self.rho_sigma/(1-self.rho_sigma)*self.lambda_sigma/self.mu_sigma
                new_loglike = new_loglike + new_kappa_sigma_sq*np.log(pnmean)-math.lgamma(1.0*new_kappa_sigma_sq+1)
                log_accept = new_loglike - loglike
                accept =1
                if np.isnan(log_accept) or np.isinf(log_accept):
                    accept = 0
                elif log_accept <0:
                    accept = np.exp(log_accept)

            self.kappa_lambda_sigma_accept = self.kappa_lambda_sigma_accept + accept
            self.kappa_sigma_sq_count = self.kappa_sigma_sq_count +1
            if np.random.random()<accept :
                self.kappa_sigma_sq[ii] = new_kappa_sigma_sq
            self.log_kappa_sigma_sqq[ii] = self.log_kappa_sigma_sqq[ii]+1.0/it**0.55*(accept-0.3)
开发者ID:KaneFu,项目名称:ngar,代码行数:30,代码来源:ngar_5years.py


示例6: sample_document

 def sample_document(self, m):
     z = self.corpus[m]["state"]         # Step1: カウントを減らす
     if z > 0:
         self.topic_document_freq[z] -= 1
         self.topic_document_sum -= 1
         for v in self.corpus[m]["bag_of_words"]:
             self.topic_word_freq[z][v] -= 1
             self.topic_word_sum[z] -= 1
     n_d_v = defaultdict(float)          # Step2: 事後分布の計算
     n_d = 0.0
     for v in self.corpus[m]["bag_of_words"]:
         n_d_v[v] += 1.0
         n_d += 1.0
     p_z = defaultdict(lambda: 0.0)
     for z in xrange(1, self.K + 1):
         p_z[z] = math.log((self.topic_document_freq[z] + self.alpha) / (self.topic_document_sum + self.alpha*self.K))
         p_z[z] += (math.lgamma(self.topic_word_sum[z] + self.beta*self.V) - math.lgamma(self.topic_word_sum[z] + n_d + self.beta*self.V))
         for v in n_d_v.iterkeys():
             p_z[z] += (math.lgamma(self.topic_word_freq[z][v] + n_d_v[v] + self.beta) - math.lgamma(self.topic_word_freq[z][v] + self.beta))
     max_log = max(p_z.values())     # オーバーフロー対策
     for z in p_z:
         p_z[z] = math.exp(p_z[z] - max_log)
     new_z = self.sample_one(p_z)        # Step3: サンプル
     self.corpus[m]["state"] = new_z     # Step4: カウントを増やす
     self.topic_document_freq[new_z] += 1
     self.topic_document_sum += 1
     for v in self.corpus[m]["bag_of_words"]:
         self.topic_word_freq[new_z][v] += 1
         self.topic_word_sum[new_z] += 1
开发者ID:kenchin110100,项目名称:topic_model,代码行数:29,代码来源:mixture_of_unigram_model.py


示例7: incomplete_gamma

def incomplete_gamma(x, s):
    r"""
    This function computes the incomplete lower gamma function
    using the series expansion:

    .. math::

       \gamma(x, s) = x^s \Gamma(s)e^{-x}\sum^\infty_{k=0}
                    \frac{x^k}{\Gamma(s + k + 1)}

    This series will converge strongly because the Gamma
    function grows factorially.

    Because the Gamma function does grow so quickly, we can
    run into numerical stability issues. To solve this we carry
    out as much math as possible in the log domain to reduce
    numerical error. This function matches the results from
    scipy to numerical precision.
    """
    if x < 0:
        return 1
    if x > 1e3:
        return math.gamma(s)
    log_gamma_s = math.lgamma(s)
    log_x = log(x)
    value = 0
    for k in range(100):
        log_num = (k + s)*log_x + (-x) + log_gamma_s
        log_denom = math.lgamma(k + s + 1)
        value += math.exp(log_num - log_denom)
    return value
开发者ID:jfinkels,项目名称:goftests,代码行数:31,代码来源:utils.py


示例8: calc_full

def calc_full(n, alphas):
    """ Calculate the log likelihood under DirMult distribution with alphas=avec, given data counts of nvec."""
    lg_sum_alphas = math.lgamma(alphas.sum())
    sum_lg_alphas = np.sum(scipy.special.gammaln(alphas))
    lg_sum_alphas_n = math.lgamma(alphas.sum() + n.sum())
    sum_lg_alphas_n = np.sum(scipy.special.gammaln(n+alphas))
    return lg_sum_alphas - sum_lg_alphas - lg_sum_alphas_n + sum_lg_alphas_n 
开发者ID:garibaldu,项目名称:radioblobs,代码行数:7,代码来源:score_DirMult.py


示例9: tdens

 def tdens(self, n, X):
     C = (1.0 + (X * X) / (n * 1.0))
     h = math.lgamma((n + 1.0) / 2.0) - math.lgamma(n / 2.0) 
     h = math.exp(h)
     h = h / math.sqrt(math.pi) / math.sqrt(n)
     Result = h * (C ** (-((n / 2.0) + (1.0 / 2.0))))
     return Result
开发者ID:duhadler,项目名称:mpFormulaCPython,代码行数:7,代码来源:Distributions.py


示例10: incompleteBetaFunction

def incompleteBetaFunction(x,a,b):
    lbeta = math.lgamma(a + b) - math.lgamma(a) - math.lgamma(b) \
            + a * math.log(x) + b * math.log(1.0 - x)
    if (x < (a + 1)/(a + b + 2)):
        return math.exp(lbeta) * contFractionBeta(a,b,x)/a
    else:
        return 1 - math.exp(lbeta) * contFractionBeta(b,a,1.-x)/b
开发者ID:bwengals,项目名称:hadrian,代码行数:7,代码来源:spec.py


示例11: log_likelihood

 def log_likelihood(self, full=False):
     ll = (math.lgamma(self.K * self.alpha) - math.lgamma(self.K * self.alpha + self.N)
             + sum(math.lgamma(self.alpha + self.count[k]) for k in self.count)
             - len(self.count) * math.lgamma(self.alpha)) # zero counts
     if full:
         ll += self.prior.log_likelihood()
     return ll
开发者ID:pearsonhenry,项目名称:vpyp,代码行数:7,代码来源:prob.py


示例12: UpdateKappa

    def UpdateKappa(self, it):
        for ii in xrange(self.T-1):
            for jj in xrange(self.p):

                new_kappa = self.kappa[ii][jj]+(2*np.ceil(2*np.random.random())-3)*(np.random.geometric(1.0/(1+np.exp(self.log_kappa_q[ii][jj])))-1)

                if new_kappa < 0:
                    accept = 0
                else:
                    lam1 = self.lambda_[jj] + 1.0*self.kappa[ii][jj]
                    gam1 = self.lambda_[jj]/self.mu[jj] + self.delta[jj]
                    loglike = lam1*np.log(gam1) - math.lgamma(lam1)+(lam1-1)*np.log(self.psi[ii+1][jj])
                    pnmean = self.psi[ii][jj] * self.delta[jj]
                    loglike = loglike + 1.0*self.kappa[ii][jj]*np.log(pnmean) - math.lgamma(1.0*self.kappa[ii][jj]+1)

                    lam1 = self.lambda_[jj] + 1.0*new_kappa
                    gam1 = self.lambda_[jj]/self.mu[jj] + self.delta[jj]
                    new_loglike = lam1*np.log(gam1) - math.lgamma(lam1)+(lam1-1)*np.log(self.psi[ii+1][jj])
                    pnmean = self.psi[ii][jj]*self.delta[jj]
                    new_loglike = new_loglike + new_kappa*np.log(pnmean)-math.lgamma(1.0*new_kappa+1)
                    log_accept = new_loglike - loglike
                    accept =1
                    if np.isnan(log_accept) or np.isinf(log_accept):
                        accept =0
                    elif log_accept <0:
                        accept = np.exp(log_accept)

                self.kappa_accept = self.kappa_accept + accept
                self.kappa_count = self.kappa_count +1

                if np.random.random() < accept:
                    self.kappa[ii][jj] = new_kappa
                self.log_kappa_q[ii][jj] = self.log_kappa_q[ii][jj] + 1.0/it**0.55*(accept-0.3)
开发者ID:KaneFu,项目名称:ngar,代码行数:33,代码来源:ngar_5years.py


示例13: theta_likelihood

def theta_likelihood(theta, S, J):
    S += prior_s
    J += prior_j
    #If any of the values are 0 or negative return likelihood that will get rejected
    if theta <= 0 or S <= 0 or J <= 0:
        return 10000000
    else:
        return -(S * math.log(theta) + math.lgamma(theta) - math.lgamma(theta + J))
开发者ID:DrewWham,项目名称:The-Clonalescent,代码行数:8,代码来源:clonalescent.py


示例14: __compute_factor

 def __compute_factor(self):
     self._factor = lgamma (self.community.J + 1)
     phi = table(self.community.abund)
     phi += [0] * int (max (self.community.abund) - len (phi))
     for spe in xrange (self.community.S):
         self._factor -= log (max (1, self.community.abund[spe]))
     for spe in xrange (int(max(self.community.abund))):
         self._factor -= lgamma (phi[spe] + 1)
开发者ID:fransua,项目名称:ecolopy,代码行数:8,代码来源:untb_model.py


示例15: logchoose

 def logchoose(ni, ki):
     try:
         lgn1 = lgamma(ni + 1)
         lgk1 = lgamma(ki + 1)
         lgnk1 = lgamma(ni - ki + 1)
     except ValueError:
         raise ValueError
     return lgn1 - (lgnk1 + lgk1)
开发者ID:gigascience,项目名称:galaxy-genome-diversity,代码行数:8,代码来源:rank_terms.py


示例16: multiTLogPDF

def multiTLogPDF(x,mu,Sigma,nu,p):

    part1 =   math.lgamma( 0.5 * (p + nu) )
    part2 = - math.lgamma( 0.5 * nu ) - 0.5 * p * np.log( nu ) - 0.5 * p * np.log( np.pi )
    part3 = - 0.5 * np.log( np.linalg.det(Sigma) )
    part4 = - 0.5 * ( nu + p ) * np.log( 1.0 + nu**(-1) * np.dot( np.dot( (x - mu), np.linalg.inv(Sigma) ), (x - mu) ) )

    return part1 + part2 + part3 + part4
开发者ID:can-cs,项目名称:pmmh-correlated2015,代码行数:8,代码来源:models_dists.py


示例17: _ewens_theta_likelihood

 def _ewens_theta_likelihood (self, theta):
     '''
     returns the likelihood of theta for a given dataset
     
     '''
     if theta < 0:
         return float ('-inf')
     return self.community.S * log(theta) + lgamma(theta) - lgamma(theta + self.community.J)
开发者ID:fransua,项目名称:ecolopy,代码行数:8,代码来源:ewens_model.py


示例18: p

def p(n,m,p):
    """Probability of m success out of n events, where an individual event succeeds with probability P... Useful for calculating <H^hat(j|w)> in semantic_information fun\
ction below"""
    try:
        return exp(lgamma(n+1)  - lgamma(n-m+1) - lgamma(m+1) +  m*log(p) + (n-m)*log(1.0-p))
    except:
        print "WARNING: domain range errer...returning 0"
        return 0
开发者ID:flashman,项目名称:trend-analysis,代码行数:8,代码来源:utils.py


示例19: log_Beta

def log_Beta(alphas):
    """
    the beta function is a product-of-gammas over a gamma-of-sum
    the gamma function generalizes factorials
    tested against wolfram alpha
    """
    #return product(map(gamma,alphas)) / gamma(sum(alphas))
    return sum(lgamma(alpha) for alpha in alphas) - lgamma(sum(alphas))
开发者ID:sboosali,项目名称:PGM,代码行数:8,代码来源:util.py


示例20: Bernstein

def Bernstein(n, k):
    """Bernstein polynomial.

    """
    # binom
    coeff = exp(lgamma(1+n)-lgamma(1+k)-lgamma(1+n-k))

    return lambda x: coeff*x**k*(1-x)**(n-k)
开发者ID:michael-hartmann,项目名称:pyxgradients,代码行数:8,代码来源:bezier.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python math.ln函数代码示例发布时间:2022-05-27
下一篇:
Python math.ldexp函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap