gpt4 book ai didi

python - 来自 PyMC 的 FloatingPointError 从 Dirichlet 分布中抽样

转载 作者:太空狗 更新时间:2023-10-29 17:08:53 27 4
gpt4 key购买 nike

being unsuccessful in using decorators 之后为了定义“指数随机变量的对数”的随机对象,我决定使用 pymc.stochastic_from_dist 为这个新分布手动编写代码。我尝试实现的模型可在此处获得(第一个模型): enter image description here

现在,当我尝试使用 MCMC Metropolis 对 log(alpha) 进行采样并使用正态分布作为建议时(如下图所示的采样方法),我收到以下错误:

  File "/Library/Python/2.7/site-packages/pymc/distributions.py", line 980, in rdirichlet
return (gammas[0]/gammas[0].sum())[:-1]

FloatingPointError: invalid value encountered in divide

虽然抽样没有出现错误,但抽样直方图与本文中的直方图匹配。我的分层模型是:

"""
A Hierarchical Bayesian Model for Bags of Marbles

logalpha ~ logarithm of an exponential distribution with parameter lambd
beta ~ Dirichlet([black and white ball proportions]:vector of 1's)
theta ~ Dirichlet(alpha*beta(vector))

"""

import numpy as np
import pymc
from scipy.stats import expon
lambd=1.
__all__=['alpha','beta','theta','logalpha']
#------------------------------------------------------------
# Set up pyMC model: logExponential
# 1 parameter: (alpha)

def logExp_like(x,explambda):
"""log-likelihood for logExponential"""
return -lambd*np.exp(x)+x
def rlogexp(explambda, size=None):
"""random variable from logExponential"""
sample=np.random.exponential(explambda,size)
logSample=np.log(sample)
return logSample
logExponential=pymc.stochastic_from_dist('logExponential',logp=logExp_like,
random=rlogexp,
dtype=np.float,
mv=False)
#------------------------------------------------------------
#Defining model parameteres alpha and beta.
beta=pymc.Dirichlet('beta',theta=[1,1])
logalpha=logExponential('logalpha',lambd)

@pymc.deterministic(plot=False)
def multipar(a=logalpha,b=beta):
out=np.empty(2)
out[0]=(np.exp(a)*b)
out[1]=(np.exp(a)*(1-b))
return out
theta=pymc.Dirichlet('theta',theta=multipar)

我的测试采样代码是:

from pymc import Metropolis
from pymc import MCMC
from matplotlib import pyplot as plt
import HBM
import numpy as np
import pymc
import scipy
M=MCMC(HBM)
M.use_step_method(Metropolis,HBM.logalpha, proposal_sd=1.,proposal_distribution='Normal')
M.sample(iter=1000,burn=200)

当我在 distributions.py 的第 978 行检查传递给 gamma 分布的 theta 值时,我看到不是零而是小值!所以我不知道如何防止这个浮点错误?

最佳答案

我找到了这个 nugget在他们的文档中:

The stochastic variable cutoff cannot be smaller than the largest element of D, otherwise D’s density would be zero. The standard Metropolis step method can handle this case without problems; it will propose illegal values occasionally, but these will be rejected.

这会让我相信 dtype=np.float(本质上与 float 具有相同的范围)可能不是您想要的方法。文档说它需要是一个 numpy dtype,但它只需要是一个转换为 numpy dtype 对象的 dtype,在 Python2 中(如果我错了请纠正我)数字 dtype 是固定大小的类型,这意味着它们是相同的.也许利用 Decimal模块将是一个选项。通过这种方式,您可以设置精度级别以封装预期值范围,并将其传递给扩展的随机方法,并在其中进行转换。

from decimal import Decimal, getcontext
getcontext().prec = 15
dtype=Decimal

我不知道一旦 numpy 库得到它,它是否仍会被截断,或者它是否会尊重继承的精度级别。我没有对此进行测试的准确方法,但请尝试一下,让我知道它对您的效果如何。

编辑:我测试了精确继承的概念,它似乎成立:

>>> from decimal import Decimal, getcontext
>>> getcontext().prec = 10
>>> Decimal(1) / Decimal(7)
Decimal('0.1428571429')
>>> np.float(Decimal(1) / Decimal(7))
0.1428571429
>>> getcontext().prec = 15
>>> np.float(Decimal(1) / Decimal(7))
0.142857142857143
>>>

关于python - 来自 PyMC 的 FloatingPointError 从 Dirichlet 分布中抽样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19434726/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com