7

如何在 PyMC3 中定义自定义可能性?在 PyMC2 中,我可以使用@pymc.potential. 我尝试pymc.Potential在 PyMC3 中使用,但是,布尔运算似乎无法应用于参数(当我这样做时出现这样的错误)例如,以下代码不起作用:

from pymc import *

with Model() as model:
    x = Normal('x', 1, 1)

    def z(u):
        if u > 0: #comparisons like this are not supported
        # if theano.tensor.lt(0,u): this is how comparison should be done
            return u ** 2
        return -u**3

    x2 = Potential('x2', z(x))

    start = model.test_point
    h = find_hessian(start)
    step = Metropolis(model.vars, h)
    sample(100, step, start)

我不可能将可能性内的所有比较都更改为 Theano 语法(即 theano.tensor.{lt,le,eq,neq,gt,ge})。无论如何要使用定义类似于 PyMC2 的似然函数吗?

4

1 回答 1

13

您需要使用该DensityDist函数来包装您的日志可能性。从与源捆绑的示例中:

with Model() as model:
    lam = Exponential('lam', 1)

    failure = np.array([0, 1])
    value = np.array([1, 0])

    def logp(failure, value):
        return sum(failure * log(lam) - lam * value)

    x = DensityDist('x', logp, observed=(failure, value))

您可以使用装饰器进行任意的非 Theano 确定性@theano.compile.ops.as_op,但对于随机指标来说并不容易。

于 2014-12-31T17:59:30.447 回答