Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom likelihood in pymc3

Tags:

pymc

pymc3

How can I define a custom likelihood in PyMC3? In PyMC2, I could use @pymc.potential. I tried to use pymc.Potential in PyMC3, however, it seems that boolean operations cannot be applied to the parameters (I get an error like this when I do that). For example, following code does not work:

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)

It's not possible for me to change all the comparisons inside the likelihood to the Theano syntax (i.e., theano.tensor.{lt,le,eq,neq,gt,ge}). Is there anyway to use define a likelihood function similar to PyMC2?

like image 397
Amir Dezfouli Avatar asked Dec 18 '14 11:12

Amir Dezfouli


1 Answers

You need to use the DensityDist function to wrap your log likelihood. From the examples bundled with the source:

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

You can make arbitrary non-Theano deterministics using the @theano.compile.ops.as_op decorator, but not as easily for Stochastics.

like image 119
Chris Fonnesbeck Avatar answered Nov 18 '22 13:11

Chris Fonnesbeck