26.6 Estimating event probabilities
Event probabilities involving either parameters or predictions or both may be coded in the generated quantities block. For example, to evaluate Pr[λ>5∣y] in the simple Poisson example with only a rate parameter λ, it suffices to define a generated quantity
generated quantities {
int<lower=0, upper=1> lambda_gt_5 = lambda > 5;
// ...
}
The value of the expression lambda > 5
is 1 if the condition is
true and 0 otherwise. The posterior mean of this parameter is the
event probability
Pr[λ>5∣y]=∫I(λ>5)⋅p(λ∣y)dλ≈1MM∑m=1I[λ(m)>5],
where each λ(m)∼p(λ∣y) is distributed
according to the posterior. In Stan, this is recovered as
the posterior mean of the parameter lambda_gt_5
.
In general, event probabilities may be expressed as expectations of indicator functions. For example, Pr[λ>5∣y]=E[I[λ>5]∣y]=∫I(λ>5)⋅p(λ∣y)dλ≈1MM∑m=1I(λ(m)>5). The last line above is the posterior mean of the indicator function as coded in Stan.
Event probabilities involving posterior predictive quantities ˜y work exactly the same way as those for parameters. For example, if ˜yn is the prediction for the n-th unobserved outcome (such as the score of a team in a game or a level of expression of a protein in a cell), then Pr[˜y3>˜y7∣˜x,x,y]=E[I[˜y3>˜y7]∣˜x,x,y]=∫I(˜y3>˜y7)⋅p(˜y∣˜x,x,y)d˜y≈1MM∑m=1I(˜y(m)3>˜y(m)7), where ˜y(m)∼p(˜y∣˜x,x,y).