Search code examples
probability-densitypymc3

How to get the log-denisty from pymc3 model?


I have drawn samples from a simple model using pymc3:

import pymc3 as pm

with pm.Model() as model:
    var_x = pm.Normal(name='var_x', mu = 0, sd = 1)
    trace = pm.sample(10)

print(trace['var_x'])

I wonder if trace contains the values of the log-density (pm.Normal) for each value in trace['var_x'] and how to extract it.

If trace does not retain the log-density, is there any other possibility to get the values by using pymc3?

Thanks


Solution

  • In your case you can recompute it by doing

    [var_x.logp(i) for i in trace]
    

    or more general

    [[free.logp(i) for i in trace] for free in model.free_RVs]]
    

    You may also want to check how similar expressions are used in PyMC3 to compute information criteria stats