Posterior draws, transformed quantities, and equal-tail vs HPD intervals
Once you can draw from a posterior, the posterior of any function of the parameters comes for free, and you can summarise it with an equal-tail or a highest-posterior-density interval. The two only agree when the posterior is symmetric.
University lab From my coursework in the MSc in Statistics and Machine Learning at Linköping University, rewritten as a note to review later.
The first Bayesian Learning lab was mostly about one habit: once you can draw random numbers from a posterior, you can answer almost any question about it by just working with the draws. Probabilities, means, the distribution of some messy function of the parameter, intervals. This note collects that habit and the one place where it gets subtle: which interval to report. (The lab was done in a pair.)
Draws are the posterior
Say the posterior of a parameter is something we can sample from, like . With draws :
- a posterior probability is a fraction: ,
- a posterior mean is an average: ,
- the Monte Carlo error of these shrinks like , so going from 100 to 10,000 draws buys one extra decimal.
In the lab, 10,000 draws gave against an exact value of from pbeta. Close, and as close as says it should be.
The nice part is transformations. If you want the posterior of , you don’t need a change-of-variables formula. You just compute for every draw. The log-odds is one line of code.
A less trivial example: the Gini coefficient
Incomes are often modelled as log-normal: . With known and the prior , the posterior is a scaled inverse chi-squared,
where is the number of observations and is the average squared deviation of the log-incomes from . You sample it as with .
A mistake I made here. In my draft I checked my draws against rinvchisq(N, df = n) without a scale argument. That’s the unscaled inverse chi-squared, so it’s a different distribution, and the comparison didn’t mean anything. The theoretical density has to include :
For log-normal incomes the Gini coefficient (0 = everyone earns the same, 1 = one person earns everything) is
where is the standard normal CDF. So the posterior of is just 2 * pnorm(sqrt(sigma2) / sqrt(2)) - 1 applied to each draw of . No algebra needed.
Two kinds of 90% interval
A 90% credible interval is any interval holding 90% of the posterior mass. There are many of those, and two common choices:
- Equal-tail: cut 5% off each end. It’s just the 5% and 95% quantiles of the draws.
- Highest posterior density (HPD): the region where the density is above some level , with chosen so the region holds 90%. Every point inside is more plausible than every point outside.
For a symmetric, single-peaked posterior they’re the same. For a skewed one they’re not. The posterior of is right-skewed, and in the lab the equal-tail interval was while the HPD was : shifted towards the peak and a bit shorter. The HPD is always the shortest interval for a given level, which is its selling point.
The way I think about the trade-off:
- Equal-tail intervals behave well under transformation. If is a 90% equal-tail interval for , then is one for , because is increasing and quantiles map through it. HPD intervals don’t have this property.
- HPD intervals can be a union of pieces if the posterior has two peaks. That’s honest, but harder to report.
Computing an HPD interval yourself
In the lab I first used HDInterval::hdi, but computing it by hand from a kernel density estimate is short and shows what it is. Sort the density grid from highest to lowest and keep adding points until they hold 90% of the mass:
set.seed(1)
draws <- rgamma(10000, shape = 3, rate = 20) # any skewed posterior sample
equal_tail <- quantile(draws, c(0.05, 0.95))
d <- density(draws) # kernel density on an even grid
ord <- order(d$y, decreasing = TRUE) # highest density first
mass <- cumsum(d$y[ord]) / sum(d$y) # even spacing cancels out
inside <- ord[mass <= 0.90]
hpd <- range(d$x[inside]) # OK for a single-peaked posterior
round(equal_tail, 3) # 0.042 0.317
round(hpd, 3) # 0.023 0.279
The HPD is pulled towards zero where the gamma density is highest, and it’s narrower. If the posterior had two peaks, range() would be wrong and you’d need to find the separate runs in inside.
Key takeaways
- With posterior draws, probabilities are fractions, expectations are averages, and the posterior of is applied to each draw.
- Monte Carlo error falls like .
- A scaled inverse chi-squared needs its scale ; the unscaled version is a different distribution.
- Equal-tail and HPD intervals agree only for symmetric posteriors. HPD is shortest; equal-tail is invariant under monotone transformations.
Resources
- Andrew Gelman et al., Bayesian Data Analysis, 3rd ed., Chapter 2 (single-parameter models, posterior intervals) and Chapter 3 (normal model with unknown variance): http://www.stat.columbia.edu/~gelman/book/
- Rob J. Hyndman, “Computing and graphing highest density regions”, The American Statistician 50(2) (1996): https://doi.org/10.1080/00031305.1996.10474359
- Wikipedia, “Gini coefficient”, log-normal case: https://en.wikipedia.org/wiki/Gini_coefficient
- 732A91 Bayesian Learning, Linköping University (course lab, spring 2020).