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

**URL:** <https://discourse.mc-stan.org/t/run-generated-quantities-code-bloc-only-once-after-convergence/7037>\
**Category:** PyStan\
**Created:** [December 20, 2018, 4:20pm UTC](https://discourse.mc-stan.org/t/run-generated-quantities-code-bloc-only-once-after-convergence/7037 "2018-12-20T16:20:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hubert\_Wassner](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@Hubert\_Wassner](https://discourse.mc-stan.org/u/Hubert_Wassner)\
**Post date:** [December 20, 2018, 4:20pm UTC](https://discourse.mc-stan.org/t/run-generated-quantities-code-bloc-only-once-after-convergence/7037/1 "2018-12-20T16:20:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [December 22, 2018, 9:43pm UTC](https://discourse.mc-stan.org/t/run-generated-quantities-code-bloc-only-once-after-convergence/7037/2 "2018-12-22T21:43:25Z")

</div>

> [@Hubert\_Wassner](#):
>
> but I only need to do it once, not at every sampling step.

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](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.

---

<div class="post-metadata">

**Author:** ![Hubert\_Wassner](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@Hubert\_Wassner](https://discourse.mc-stan.org/u/Hubert_Wassner)\
**Post date:** [December 26, 2018, 4:33pm UTC](https://discourse.mc-stan.org/t/run-generated-quantities-code-bloc-only-once-after-convergence/7037/3 "2018-12-26T16:33:25Z")

</div>

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.
