Extracting likelihood from pymc3 model

I ran a mixed effect model using bambi with pymc3 as the backend. For my edification, I wanted to see the likelihood function that bambi specified.

import bambi as bb
model_bb = bb.Model(data)
results_bb = model_bb.fit('y ~ time + x1 + x2 + x3', random='time|index', backend='pymc3', samples=1000, chains=1)

pm_model = model_bb.backend.model

Where pm_model is the pymc3 model.

Is there a way to see how the likelihood was specified?

That is a little tough – if you are in a notebook, pm_model.vars will list all the variables, and each of them has a LaTeX representation, so something like

pretty_model = '\n'.join([var._latex_repr_() for var in pm_model.vars])

will give you an idea. It looks like bambi wraps each parameter in an array that we do not introspect for that pretty printing (PR’s welcome!), so more manual work might be needed.

I’ve been meaning to give pymc3.Model instances a nice latex representation…

I am using a notebook. Thank you - this gives me an idea of how the variables are defined.

I was trying to understand the offset in hierarchical models. This blog post is very helpful


I always want to rename _latex_repr_ to __latex__ that is a more pythonic style
CC @fonnesbeck

1 Like

@sthomas522 The pr implementing this should land later today (https://github.com/pymc-devs/pymc3/pull/2450) – I tried this on a few bambi models, and there’s something funny so the print out does not look great (lots of things are just named array).

It was actually a bit more difficult than I expected. I have some ideas for how to do this right, but would love to hear your thoughts in the mean time. Please install from master whenever this lands and let us know if it helps!

1 Like

Wow - thanks so much! I will take a look tomorrow and let you know how it goes. I noticed the array names as well - very strange.

This looks great, and is very helpful! The array’s seem to correspond with the multiple variables in a hierarchical model. I don’t know if it is feasible to represent the array’s as their priors, but I can figure this out by looking at my model specification.

When I try a similar model with bambi, I don’t get the same LaTeX output

I like the formula representation and prior selection in bambi, but I may stick to fully specifying in pymc3 because it seems more transparent to me.

Bambi image from above (since I am limited to 1 image per post)

Uh oh – do you think you could post a toy example of the data to recreate that? it looks like it is a bug on our side: the output is LaTeX, but must be malformed…

I’m also still not sure what to do about python using underscores in variable names, which turn into subscripts in tex! Probably can escape that somehow…

Here’s a toy example. I can send you the notebook too if that helps.

Thanks for this! The Flat distribution had some malformed LaTeX that is now fixed – I also just opened a PR to escape special LaTeX characters so this will keep looking better (https://github.com/pymc-devs/pymc3/pull/2485)

On the other hand, it looks like Bambi nests some of the functions pretty deeply, so the representation of them isn’t so beautiful :smiley:

I think we should limit number of functions displayed

Yes – I wonder it it would be better to change that last line to vote ~ Bernoulli(p=f(array, Intercept, party_id), f(array, party_id : age)), or try to be clever about it – for example, numpy.mean.__name__ is 'mean', so there might be a reasonable human readable way of doing this…