(来自https://stats.stackexchange.com/的交叉发布,因为问题被标记为该网站的主题)
尝试在pymc中做http://greenteapress.com/thinkbayes/html/thinkbayes004.html#toc23(基本上只是从瓮中拉出骰子并滚动几次)
import pymc as mc
die_faces = [4,6,8,12,20]
die_tosses = [x-1 for x in [6,8,7,7,5,4]] # 3 is a 4 on 0th indexed dice
which_die = mc.Categorical('which_die', [1./len(die_faces) for _ in range(len(die_faces))])
#imp = 0 # impossible
imp = 0.00000000000001 # impossible
die_probs = [[(1./face_count if i<face_count else imp) for i in range(max(die_faces))]
for face_count in die_faces]
@mc.deterministic
def die_results(which_die=which_die):
return die_probs[which_die]
observation = mc.Categorical('obs', die_results, value=die_tosses, observed=True, size=len(die_tosses))
model = mc.Model([which_die, die_results, observation])
map_ = mc.MAP( model )
map_.fit() #stores the fitted variables' values in foo.value
mcmc = mc.MCMC( model )
mcmc.sample( 50000, 25000 )
die_data = mcmc.trace('which_die')[:]
for i in range(len(die_faces)):
print 1.*list(die_data).count(i)/len(die_data)
这段代码似乎基本上得到了预期的答案(从已知的确切答案中略微提出)。
问题在于,将值0设为不可能似乎更合乎逻辑/更正确。当我使用它时,我得到以下错误(间歇性地):
Traceback (most recent call last):
File "manydice.py", line 19, in <module>
observation = mc.Categorical('obs', die_results, value=die_tosses, observed=True, size=len(die_tosses))
File "/usr/local/lib/python2.7/dist-packages/pymc/distributions.py", line 3179, in __init__
verbose=verbose, **kwds)
File "/usr/local/lib/python2.7/dist-packages/pymc/PyMCObjects.py", line 772, in __init__
if not isinstance(self.logp, float):
File "/usr/local/lib/python2.7/dist-packages/pymc/PyMCObjects.py", line 929, in get_logp
raise ZeroProbability(self.errmsg)
pymc.Node.ZeroProbability: Stochastic obs's value is outside its support, or it forbids its parents' current values.
是否有更惯用和/或更正确的方式来表示此问题?
答案 0 :(得分:2)
发布了一个类似的问题here,其中解决方案是使用多项式而不是分类变量。
还有一个similar thread on Google Groups解决方案就是你所做的:使概率非常接近,但等于零。
最后,在处理概率时很常见,你不能拥有零概率的东西,所以我认为没有比将概率设置得非常接近0更好的方法来解决问题。记住概率实际上,那个小的“不可能”。
然而,这个问题没有明确的答案。如果你有兴趣,就数学网站zero probability or impossibilities的含义有一些冗长的对话和哲学辩论。 (另一个类似的问题:0的值除以0无穷大还是零?)