8.8 Program block: generated quantities
The generated quantities
program block is rather different than
the other blocks. Nothing in the generated quantities block affects
the sampled parameter values. The block is executed only after a
sample has been generated.
Among the applications of posterior inference that can be coded in the generated quantities block are
- forward sampling to generate simulated data for model testing,
- generating predictions for new data,
- calculating posterior event probabilities, including multiple comparisons, sign tests, etc.,
- calculating posterior expectations,
- transforming parameters for reporting,
- applying full Bayesian decision theory,
- calculating log likelihoods, deviances, etc. for model comparison.
Parameter estimates, predictions, statistics, and event probabilities calculated directly using plug-in estimates. Stan automatically provides full Bayesian inference by producing draws from the posterior distribution of any calculated event probabilities, predictions, or statistics.
Within the generated quantities block, the values of all other variables declared in earlier program blocks (other than local variables) are available for use in the generated quantities block.
It is more efficient to define a variable in the generated quantities block instead of the transformed parameters block. Therefore, if a quantity does not play a role in the model, it should be defined in the generated quantities block.
After the generated quantities statements are executed, the constraints on the declared generated quantity variables are validated.
All variables declared as generated quantities are printed as part of the output. Variables declared in nested blocks are local variables, not generated quantities, and thus won’t be printed. For example:
generated quantities {
int a; // added to the output
{int b; // not added to the output
} }