After thining my posterior trace , sample_posterior_predictive gives an index error

I want to thin my traces before feeding them into sample_posterior_predictive, which I do with this:

thin = tr.posterior.sel(draw = slice(0,None,100))

This works, but the coordinates have gaps, which sample_posterior_predictive doesn’t like (IndexError).
I can fix this with something like this:

thin = thin.assign_coords(coords={'draw':np.array(range(0,thin['draw'].shape[0]))})

Is there some obvious canonical way to do this that I am missing?

What pymc version are you using? This is indeed the recommended way to generate posterior predictive samples for only a subset of the posterior samples if using >=4. You might have taken the example from the v4 docs but not be using it?

IIRC it is not supported as is in v3, where you have two options (I haven’t tested the exact code below though, it might need some minor tweaks):

  • Workaround 1: reset the coordinates. thin = tr.posterior.sel(draw = slice(0,None,100)).reset_coords("draw", drop=True)
  • Workaround 2: draw from the # first draws instead of a thinned version, if the model has converged there should be no noticeable differences. You can do that with the samples argument of sample_posterior_predictive and providing the full posterior.

I am using 3.11.4.
reset_coords gave an error (ValueError: cannot remove index coordinates with reset_coords: {'draw'}) but I just tried reset_index and that worked just fine! Thank you very much!

I think I am going to have to invest some time into learning Arviz / xdata data structures !

2 Likes