Run "generated quantities" code bloc only once (after convergence)

I’ve got a quite heavy “generated quantities” code bloc in my model. Heavy in both computionnal cost & memory usage.

I know that the “generated quantities” code bloc is executed at every MCMC step, this slows down my model sampling.

I would like to first sample my model without running the “generated quantities” code bloc, and then running it only once, after the sampling procedure ends (using the parameter posterior found during the sampling).

I’d like to do a “Predictive Posterior Check”, but I only need to do it once, not at every sampling step.

Any idea how to do this with pystan ?

Any help appreciated.

The easiest way to accomplish what you’re talking about is to do your posterior predictions in Python. Use extract to pull a posterior sample from your model and only make one prediction (https://pystan.readthedocs.io/en/latest/api.html#pystan.StanFit4model.extract)

However, any predictions you make from a Bayesian model are probabilistic themselves. The predictions are just functions of your posterior samples, so you usually want to compute your predictions for every posterior sample you draw and then look at distributions of your predictions. I don’t know your application, but it would be unusual to just look at one prediction instead of looking at a collection of them.

4 Likes

Thank you very much for this answer.

I had the same idea (but one day after I did the post…) your message confirms that it’s a solution to my problem.

It’s bit sad that Stan doesn’t provide direct solution to that problem (Pymc does ;) ),
since this makes me create duplicate code (which is not a good practice).
But I have a solution, so it’s ok.