Obtain logp values for each RV at starting point

I am running a hierarchical regression model and due to bad input variables it sometimes fail at the starting point. Following is an example of such error:

SamplingError: Initial evaluation of model at starting point failed!
Starting values:
{'ctrl': array([[ 0.27 , -0.138,  0.108,  0.439]]), 'pos_truncnormal_audit_interval__': array([[ 0.,  0.,  0.,  0., nan,  0.,  0.,  0.,  0.]])

Now from this message I know the troublesome variable is the 5th element of the RV pos_truncnormal_audit_interval__ and in the next iteration would like to exclude it from my model. But I’m not being able to return this information as any object.

When I’m trying to get the information by running

for RV in model.basic_RVs:
            print(RV.name, pm.logp(RV, model.test_point))

I’m receiving the following error:

NotImplementedError: Cannot convert {'ctrl': array([[0.00605671, 0.05232037, 0.07515219]]), 'pos_truncnormal_audit_interval__': array([[0., 0., 0., 0., 0., 0., 0., 0.]])} to a tensor variable.

Is there any way to extract this information as an object so that it can be used for further diagnosis.

Many thanks in advance.

Are you perhaps looking for model.point_logps


1 Like

model.point_logps provides logp for each RV as a single value e.g for ‘pos_truncnormal_audit’: nan, whereas I’m looking for the array
‘pos_truncnormal_audit_interval__’: array([[ 0., 0., 0., 0., nan, 0., 0., 0., 0.]])

I mean that if you look at the source code you’ll see how we obtain it. You can just skip that inner sum step:


You can also just do model.compile_logp(sum=False)(model.initial_point) but you won’t have the nice dictionary output.

Actually, that was the first approach I’ve tried. But when I tried to run it it gives an error:

AttributeError: 'Model' object has no attribute 'logp'

I found a similar issue reported here: 'Model' object has no attribute 'logp_elemwiset' · Issue #3172 · pymc-devs/pymc · GitHub
So Itried to implement it by RV which gives the following error:

AttributeError: The `rv.logp(x)` method was removed. Instead use `pm.logp(rv, x)`.`

So when I tried to use pm.logp I received the following error:

TypeError: logp() got an unexpected keyword argument 'sum'

And without the sum argument it actually provides the same output as the default model.point_logps() when I try get the information from pm.logp(RV, x) for each RV
So a bit stuck here and not sure what am I missing here as I can’t access the model.logp method.
Any help on this would be greatly helpful!

You might be using an old version of pymc. Model.logp is definitely available for a while. Can you update?

I was using version 4.0.0. Updating to 4.4.0 resolved the issue. Thanks a lot Ricardo for your help on this.

Best Regards