Bayesian Monte Carlo and MCMCLesson 69 of 76 · 9 min
Update a probability with evidence
Combine a beta prior with a binomial likelihood to obtain a posterior.
Bayesian inference describes an uncertain parameter with a distribution. A prior describes it before the selected data. With observed data fixed, the likelihood is a function of the parameter: how probable those data would be at each candidate value. Prior times likelihood, rescaled to total probability one, is the posterior.
For s successes in n independent yes/no observations, likelihood is proportional to p^s(1−p)^(n−s). A beta(a,b) prior has mean a/(a+b); increasing both shapes at a fixed ratio concentrates it. Beta(2,2) and beta(20,20) both centre at .5, but the latter is narrower. The update gives beta(a+s,b+n−s). With 18/30 and beta(2,2), the posterior is beta(20,14).
The central 95% posterior, or credible, interval leaves 2.5% of posterior probability below and above its endpoints. Conditional on the prior, data and observation model, it contains 95% posterior probability for p. This differs from a confidence procedure's repeated-sampling coverage. The exact beta calculation supplies a target for later Monte Carlo checks; challenge prior concentration and the independent-observation assumption.
a and b are prior shape parameters, s is observed successes and n is observed trials. The posterior mean is (a+s)/(a+b+n); it need not equal the observed success fraction.
- Posterior density
- Prior density
Will 18 successes out of 30 make the posterior mean exactly under this prior?
Write a prediction (optional)
Exact calculation with invented observations
Select Run Python below with the code unchanged.
A saved draft is open. To run the published example, open Tools and select Restore example. Recover draft brings your code back.
Execution error
Technical error details
Your result
Published example output
This result comes from the original Python. Your runs appear above. Build environment ↗
INVENTED observations: 18 successes from 30 Prior mean: 0.5 Posterior shapes: 20 14 Posterior mean: 0.5882352941176471 Central 95% posterior interval for p: [0.4214 0.7452]
Read the first result
The posterior interval concerns the uncertain probability p conditional on this prior, data and likelihood. It is not a range of counts in the next group.
Try an edit
Replace a, b = 2, 2 with a, b = 20, 20. Select Run Python again. Read Prior mean, Posterior mean and Central 95% posterior interval for p. Why do the same observations shift the more concentrated prior less?
Scope. A common fixed p and independent observations are assumed. This toy update does not address clustering, selection or changing probabilities.
Try a fresh case
A stronger prior concentrates near 0.5. What should you examine before using it?
Need a hint?
Hint 1
Compare prior concentration with the amount of observed evidence.
Hint 2
Recalculate under plausible alternative priors.