How to interact with `pm.Model().logp()`?

Trying to get the log probability for a PyMC model evaluated at a particular set of parameter values. I am attempting to follow this documentation webpage: behind_the_scenes_of_the_logp_function .

The following code chuck is from the above webpage:

with pm.Model() as model:
    z = pm.Normal('z', mu=0., sigma=5.)             # ==> aesara.tensor.var.TensorVariable
    x = pm.Normal('x', mu=z, sigma=1., observed=5.) # ==> aesara.tensor.var.TensorVariable
# The log-prior of z=2.5
pm.logp(z, 2.5).eval()                              # ==> -2.65337645
# ???????
x.logp({'z': 2.5})                                  # ==> -4.0439386
# ???????
model.logp({'z': 2.5})                              # ==> -6.6973152

The line model.logp({'z': 2.5}) returns the following error: TypeError: unhashable type: 'dict'.

Any help would be appreciated; thanks!

1 Like

I think you need the compile_logp() method of pm.Model:

model.compile_logp()({'z': 2.5}) 
1 Like

Hi Christian,

compile.logp() works great! Maybe I can help update the current docs when I am a little more knowledgeable about what is going on under the hood!

Absolutely! Documentation-related PRs are always appreciated, but particularly now that v4 is out and the documentation is lagging a bit behind.

I think this is covered in this notebook: PyMC and Aesara — PyMC 4.1.3 documentation. There are still challenges though like pm.logp not being in the API docs (tracked in Review and update API docs · Issue #5282 · pymc-devs/pymc · GitHub); if it were, it could have a “See Also” section linking to compile_logp method (also same could be done in model.logp method too)

2 Likes