I am new to PyMC but have worked with Bayesian methods in the past. I have often found it useful to plot likelihood chain historioes - i.e., the values computed of the likelihood at each step of the Markov chain, for the separate Markov chains. This can be useful for assessing e.g. convergence or tuning. (they’re maybe better ways, but I thought this one was useful).
After much searching, I haven’t found any example of this. Can anyone tell me how I can plot the likelihood chain histories?
The idata
returned by pm.sample
stores the draws of each chain in the order they were drawn, from 0 to n. After you compute the log likelihood, you can sum over the data dimension then plot with the draw
dimension on the x-axis. I think this will give you what you want.
If you want only the log likelihood contribution, you’ll need to sum the pointwise log likelihood values stored in the log_likelihood
group as @jessegrabowski mentions. If you want both prior and likelihood contributions, there is a lp
variable in the sample_stats
group.
Example code to generate these plots:
import arviz as az
idata = az.load_arviz_data("centered_eight")
az.preview.plot_trace(idata.log_likelihood.sum("school"));
You can also use az.plot_trace
instead of az.preview.plot_trace
in which case you’ll get the following plot:
az.plot_trace(idata.log_likelihood.sum("school"), compact=False);
Using az.preview
give you access to the ongoing refactor effort for ArviZ where are working on more flexible plots and are also rethinking some of the names like plot_trace
which now give you the trace only, if you want the same behaviour as az.plot_trace
you can use az.preview.plot_trace_dist
. Docs for az.preview
are at arviz-plots website
Potentially useful references: Other utils — PyMC dev documentation, if you don’t have a log_likelihood
group, you can use the compute_log_likelihood
function here to get it.