Python 在PyMC 2.3中创建随机装饰器
我试图复制PyMC 2.3中给出的示例:Python 在PyMC 2.3中创建随机装饰器,python,numpy,pymc,Python,Numpy,Pymc,我试图复制PyMC 2.3中给出的示例: @pm2.stochastic(dtype=int) def switchpoint(value=1900, t_l=1851, t_h=1962): """The switchpoint for the rate of disaster occurrence.""" if value > t_h or value < t_l: # Invalid values return -np.i
@pm2.stochastic(dtype=int)
def switchpoint(value=1900, t_l=1851, t_h=1962):
"""The switchpoint for the rate of disaster occurrence."""
if value > t_h or value < t_l:
# Invalid values
return -np.inf
else:
# Uniform log-likelihood
return -np.log(t_h - t_l + 1)
我收到了错误消息(在本文末尾填写错误消息):
TypeError:“numpy.ndarray”对象不可调用
我猜这是一个兼容性问题;我使用的是Python2.7.6(64位)发行版。Numpy版本1.8.0和Scipy版本0.13.3
有人能帮我找到问题所在吗
注:我在谷歌上发现了一个相对较小的群体,显然也有同样的问题。但是,线程没有问题状态的更新
以下是完整的错误消息:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/arash/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pymc/CommonDeterministics.py", line 975, in __call__
plot=False)
File "/Users/arash/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pymc/PyMCObjects.py", line 435, in __init__
verbose=verbose)
File "/Users/arash/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pymc/Node.py", line 216, in __init__
Node.__init__(self, doc, name, parents, cache_depth, verbose=verbose)
File "/Users/arash/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pymc/Node.py", line 127, in __init__
self.parents = parents
File "/Users/arash/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pymc/Node.py", line 150, in _set_parents
self.gen_lazy_function()
File "/Users/arash/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pymc/PyMCObjects.py", line 446, in gen_lazy_function
self._value.force_compute()
File "LazyFunction.pyx", line 257, in pymc.LazyFunction.LazyFunction.force_compute (pymc/LazyFunction.c:2409)
File "/Users/arash/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pymc/CommonDeterministics.py", line 967, in eval_fun
return self(*args, **kwargs)
TypeError: 'numpy.ndarray' object is not callable
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/Users/arash/Library/enthund/Canopy_64bit/User/lib/python2.7/site packages/pymc/CommonDeterministics.py”,第975行,在调用中__
plot=False)
文件“/Users/arash/Library/enthund/Canopy_64bit/User/lib/python2.7/site packages/pymc/PyMCObjects.py”,第435行,在__
详细的
文件“/Users/arash/Library/enthund/Canopy_64bit/User/lib/python2.7/site packages/pymc/Node.py”,第216行,在__
节点.\uuuuu init\uuuuuuuuuuuu(self,doc,name,parent,cache\u depth,verbose=verbose)
文件“/Users/arash/Library/enthund/Canopy_64bit/User/lib/python2.7/site packages/pymc/Node.py”,第127行,在_init中__
self.parents=父母
文件“/Users/arash/Library/enthund/Canopy_64bit/User/lib/python2.7/site packages/pymc/Node.py”,第150行,在父集合中
self.gen_lazy_函数()
gen_lazy_函数中的文件“/Users/arash/Library/enthund/Canopy_64bit/User/lib/python2.7/site packages/pymc/PyMCObjects.py”,第446行
self.\u value.force\u compute()
pymc.LazyFunction.LazyFunction.force_compute(pymc/LazyFunction.c:2409)中的文件“LazyFunction.pyx”,第257行
文件“/Users/arash/Library/enthund/Canopy_64bit/User/lib/python2.7/site packages/pymc/CommonDeterministics.py”,第967行,在eval_-fun中
返回自我(*args,**kwargs)
TypeError:“numpy.ndarray”对象不可调用
PyMC实际上将开关点
转换为正则变量。所以你只需要做
test = switchpoint
这看起来很奇怪,因为您将它定义为一个修饰函数,但实际上您不应该这样使用它。如果你看一下定义随机变量的其他方法,它会更有意义,如下所示:
switchpoint = DiscreteUniform('switchpoint', lower=0, upper=110, doc='Switchpoint[year]')
这是:
def switchpoint_logp(value, t_l, t_h):
if value > t_h or value < t_l:
return -np.inf
else:
return -np.log(t_h - t_l + 1)
def switchpoint_rand(t_l, t_h):
from numpy.random import random
return np.round( (t_l - t_h) * random() ) + t_l
switchpoint = Stochastic( logp = switchpoint_logp,
doc = 'The switchpoint for the rate of disaster occurrence.',
name = 'switchpoint',
parents = {'t_l': 1851, 't_h': 1962},
random = switchpoint_rand,
trace = True,
value = 1900,
dtype=int,
rseed = 1.,
observed = False,
cache_depth = 2,
plot=True,
verbose = 0)
def开关点日志(值、t\u l、t\u h):
如果值>t_h或值
谢谢!当时我没有掌握这个概念。你的回答解决了问题,真的很有帮助。
def switchpoint_logp(value, t_l, t_h):
if value > t_h or value < t_l:
return -np.inf
else:
return -np.log(t_h - t_l + 1)
def switchpoint_rand(t_l, t_h):
from numpy.random import random
return np.round( (t_l - t_h) * random() ) + t_l
switchpoint = Stochastic( logp = switchpoint_logp,
doc = 'The switchpoint for the rate of disaster occurrence.',
name = 'switchpoint',
parents = {'t_l': 1851, 't_h': 1962},
random = switchpoint_rand,
trace = True,
value = 1900,
dtype=int,
rseed = 1.,
observed = False,
cache_depth = 2,
plot=True,
verbose = 0)