24.4 Posterior predictive simulation in Stan
Posterior predictive quantities can be coded in Stan using the generated quantities block.
24.4.1 Simple Poisson model
For example, consider a simple Poisson model for count data with a rate parameter λ>0 following a gamma-distributed prior, λ∼gamma(1,1). The likelihood for N observations y1,…,yN is modeled as Poisson, yn∼poisson(λ).
24.4.2 Stan code
The following Stan program defines a variable for ˜y by random number generation in the generated quantities block.
data {
int<lower = 0> N;
int<lower = 0> y[N];
}
parameters {
real<lower = 0> lambda;
}
model {
lambda ~ gamma(1, 1);
y ~ poisson(lambda);
}
generated quantities {
int<lower = 0> y_tilde = poisson_rng(lambda);
}
The random draw from the sampling distribution for ˜y is coded using Stan’s Poisson random number generator in the generated quantities block. This accounts for the sampling component of the uncertainty; Stan’s posterior sampler will account for the estimation uncertainty, generating a new ˜y(m)∼p(y∣λ(m)) for each posterior draw λ(m)∼p(θ∣y).
The posterior draws ˜y(m) may be used to estimate the expected value of ˜y or any of its quantiles or posterior intervals, as well as event probabilities involving ˜y. In general, E[f(˜y,θ)∣y] may be evaluated as E[f(˜y,θ)∣y]≈1MM∑m=1f(˜y(m),θ(m)), which is just the posterior mean of f(˜y,θ). This quantity is computed by Stan if the value of f(˜y,θ) is assigned to a variable in the generated quantities block. That is, if we have
generated quantities {
real f_val = f(y_tilde, theta);
...
where the value of f(˜y,θ) is assigned to variable f_val
,
then the posterior mean of f_val
will be
the expectation E[f(˜y,θ)∣y].
24.4.3 Analytic posterior and posterior predictive
The gamma distribution is the conjugate prior distribution for the Poisson distribution, so the posterior density p(λ∣y) will also follow a gamma distribution.
Because the posterior follows a gamma distribution and the sampling distribution is Poisson, the posterior predictive p(˜y∣y) will follow a negative binomial distribution, because the negative binomial is defined as a compound gamma-Poisson. That is, y∼negative-binomial(α,β) if λ∼gamma(α,β) and y∼poisson(λ). Rather than marginalizing out the rate parameter λ analytically as can be done to define the negative binomial probability mass function, the rate λ(m)∼p(λ∣y) is sampled from the posterior and then used to generate a draw of ˜y(m)∼p(y∣λ(m)).