A Beta(α, β) prior for a proportion p and y successes in n binomial trials give the posterior Beta(α + y, β + n − y). Its mean is a weighted average of the prior mean and the observed proportion y/n, with weight w = (α + β)/(α + β + n) on the prior: the prior acts like α + β extra observations. The left panel shows prior (gold, dashed), likelihood (blue, scaled to a density) and posterior (teal) with its 95% credible interval; the right panels show the marginal (beta-binomial) distribution of Y and the posterior obtained by simulation: draw p from the prior, then Y, and keep the draws with Y = y.
code/ch02_statistical_modeling.R (dbetabinom, hpd_beta, bayes_sim).