Beta prior and posterior for the defect rate
Set the mode of the prior Beta distribution and how many of the 20 sampled items are defective. The posterior follows immediately.
Most plausible defect rate before the data (the script uses 0.2, i.e. α = 5, β = 17)
Observed in the sample of n = 20
Examples:
Prior
Posterior mode
Posterior mean
95 % HDI of the posterior
Highest density interval
Prior
Posterior
95 % HDI
Observed share X / n
Likelihood (rescaled)
The prior is Beta(α, β) with α = 1 + 20 · mode and β = 1 + 20 · (1 − mode). For mode 0.2 this gives α = 5 and β = 17, as in the script.
Bayes' theorem: posterior ∝ likelihood × prior. With a Beta prior and binomial data the posterior is again a Beta distribution, Beta(α + X, β + n − X). The script computes the same curve by numerical integration.
Watch the posterior move from the prior mode towards X / n. It always lies between the two.