【问题标题】:creating stochastic decorator in PyMC 2.3在 PyMC 2.3 中创建随机装饰器
【发布时间】:2014-06-12 16:51:49
【问题描述】:

我正在尝试为 PyMC 2.3 复制documentations 中给出的示例:

@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)

当试图分配这个时:

test = switchpoint()

我收到了错误信息(完整的错误信息在本文末尾):

TypeError: 'numpy.ndarray' 对象不可调用

我猜这是兼容性问题;我正在使用 Enthought Canopy Python 2.7.6(64 位)发行版。 Numpy 版本 1.8.0 和 Scipy 0.13.3。

谁能帮我找出问题所在?

注意:我在 google 群组上发现了一个相对较小的 old thread,显然存在同样的问题。但是该线程没有关于问题状态的更新。

这是完整的错误信息:

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

【问题讨论】:

    标签: python numpy pymc


    【解决方案1】:

    PyMC 实际上将switchpoint 变成了一个常规变量。所以你只需要这样做

    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)
    

    【讨论】:

    • 谢谢!那时我没有掌握这个概念。您的回复解决了问题,非常有帮助。
    猜你喜欢
    • 1970-01-01
    • 2014-07-23
    • 1970-01-01
    • 2014-02-13
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 2017-11-16
    相关资源
    最近更新 更多