pymc-devs / pymc-examples

Examples of PyMC models, including a library of Jupyter notebooks.
https://www.pymc.io/projects/examples/en/latest/
MIT License
269 stars 234 forks source link

update to cleaner code with `arviz.extract` #449

Closed drbenvincent closed 1 year ago

drbenvincent commented 1 year ago

Lines like this represent a common pattern across many notebooks in the repo:

unpooled_post = unpooled_trace.posterior.stack(chain_draw=("chain", "draw"))

This is basically some xarray stuff to get a 'flat' vector of MCMC samples by collapsing over chain and draws. But it is arguably unclear, opaque, and potentially frustrating to many users.

I propose we use the new(ish) arviz.extract function instead. So we would end up with something like this, which is much much clearer to a wide range of pymc users.

unpooled_post = az.extract(unpooled_trace, group='posterior')

Tagging @twiecki and @OriolAbril for any thoughts

twiecki commented 1 year ago

Yes, way way better.

OriolAbril commented 1 year ago

I would only use group argument when we want to get variables that are not in the posterior group. We set this default because getting variables from the posterior is the most common operation and it is convenient to skip it