8

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?

Amir Dezfouli
  • 199
  • 3
  • 10

1 Answers1

13

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.

Chris Fonnesbeck
  • 4,143
  • 4
  • 29
  • 30
  • and how to define a deterministic function which has a 'self' in the signature because it belongs to a class ? please have a look my related post – Stéphane Feb 15 '17 at 13:52
  • You should not have to do anything for a deterministic function, unless you want to keep their sampled values in a trace, in which case you can wrap it in a `Determinstic` call. See the docs. – Chris Fonnesbeck Feb 16 '17 at 15:21
  • unfortunately this isn't that simple and I didn't find the information in the doc... let say it is 'in construction'. – Stéphane Feb 17 '17 at 17:17
  • The deterministic can be any function of other stochastics in the model. If you are using your own class (I assume this is because its a custom probability function) then it should inherit from a PyMC class. – Chris Fonnesbeck Feb 18 '17 at 19:23
  • no, sorry, it still doesn’t work… I still fail to use pymc3 and I’m stuck with pymc2. I must be missing something obvious. Could you please have a look on http://stackoverflow.com/questions/42205123/how-to-fit-a-method-belonging-to-an-instance-with-pymc3 ? I need to define a theano op but my attemps to combine theano/pymc3 are wrong. – Stéphane Apr 01 '17 at 05:47