Updating a belief with one dataset
Today let's understand how to turn a belief you already hold into a new one, once a real dataset has had its say.
Here's the running example. A university is trialling a new tutoring program ahead of a qualifying exam. Historically, 55% of students pass this exam without any extra help. This year, 36 students used the new tutoring program, and 25 of them passed, a rate of about 69.4%.
That gap between the two bars, about 14 percentage points, raises the real question: is the tutoring program really better, or did this one batch of 36 students just get lucky?
Bayes' rule: turning a prior belief into a posterior one
Before seeing this year's 36 students, you already held a belief about the true pass rate: the historical rate, 0.55. Statisticians have a name for a belief held before seeing new data, a prior.
Then this year's results came in. The probability of seeing exactly 25 passes out of 36 students, worked out separately for each candidate true rate, is called the likelihood. It is not the probability that a candidate rate is correct. It is the probability the data would come out looking the way it did, if that rate were the true one.
Combine the prior with the likelihood and you get an updated belief, the posterior: what you believe about the true rate after seeing this cohort.
Bayes' rule is the formula for combining them:
\[P(\theta \mid \text{data}) \propto P(\text{data} \mid \theta) \times P(\theta)\]
In words, the posterior is proportional to the likelihood times the prior. Here theta, \(\theta\), stands for the one unknown number this whole lesson is trying to pin down: the true pass rate under the tutoring program.
dbinom(y, n, theta) in R gives the probability of seeing y successes out of n trials, if the true success rate is theta. Try it at a few candidate rates and see which one the 25-of-36 result favors most.
Read the likelihood column as how probable this exact result, 25 of 36, would be under each candidate rate. At theta = 0.694, this cohort's own rate, the likelihood is highest, 0.143105. At theta = 0.3 or theta = 0.9, the likelihood collapses to almost nothing: 25 passes out of 36 would be very hard to explain if the true rate sat that far off.
So the likelihood scores every candidate rate by how well it explains the data you actually saw. The prior brings in what you believed before that data arrived.
The beta distribution: a shape for a belief about a proportion
A prior needs a shape, not a single number, because a belief about a proportion is never "the rate is exactly 0.55". It is a whole range of plausible rates, some more likely than others.
The beta distribution is the standard shape for a belief about a proportion, because it lives on exactly the range a proportion can take, 0 to 1. It has two parameters, written Beta(a, b), and both are read as counts of imagined past data: a pseudo-successes and b pseudo-failures, believed before any real data arrives.
Beta(1, 1) is completely flat, equally plausible everywhere from 0 to 1. It says "no opinion": one imagined success and one imagined failure, so weak together that they carry no real pull. Call this the flat prior.
Beta(11, 9) is centred on 0.55, since 11 divided by (11 + 9) is 0.55, which matches the historical rate. Its two parameters add up to 20, so this prior is worth 20 pseudo-observations, about as much weight as a small pilot study. Call this the mild prior.
The flat curve sits level at a density of 1 everywhere from 0 to 1, no preference anywhere. The mild curve is a hump centred close to 0.55, peaking around theta = 0.56 at a density near 3.54, falling away on both sides. That hump is what "a mild opinion worth 20 pseudo-observations" looks like drawn out.
The conjugate update: add successes and failures to the prior
Here is the one rule that makes a beta prior so convenient. When the data are successes and failures, like passes and fails on an exam, updating a Beta(a, b) prior with y successes and (n - y) failures gives a posterior that is just another beta distribution, Beta(a + y, b + (n - y)). No integration needed, just addition.
Apply that to the mild prior, Beta(11, 9), using this cohort's 25 passes and 11 fails out of 36.
So the mild prior's posterior is Beta(36, 20), mean about 0.643. Look at where 36 and 20 came from: 11 pseudo-successes plus 25 real successes gives 36, and 9 pseudo-failures plus 11 real failures gives 20. The data simply got added onto the prior's own counts.
There is a second curve worth drawing on the same axis: the likelihood itself, rescaled into a proper density over theta. For binomial data like this, that rescaled likelihood takes exactly the shape of Beta(y + 1, (n - y) + 1), which here is Beta(26, 12). It is nobody's prior. It is just the data's own shape, placed on the same axis so you can compare prior, likelihood and posterior directly.
Look at where each curve sits. The posterior, Beta(36, 20), sits between the prior and the rescaled likelihood, pulled toward the likelihood but not all the way to it, because the mild prior still carries its own 20 pseudo-observations of pull on the result.
Quick check: reading the conjugate update
A prior Beta(2, 2) meets a dataset of 8 successes in 10 trials. By the add-counts rule from the last step, what is the posterior?
Three priors meet the same 36 students
Push the add-counts rule further and see what it reveals about priors in general. Take three very different priors, update each one with the same 25 passes and 11 fails from this year's cohort, and watch how far each posterior moves.
The flat prior, Beta(1, 1), carries only 2 pseudo-observations, so it has almost no opinion to defend. Updated with 25 successes and 11 failures, its posterior is Beta(26, 12), mean about 0.684, close to this cohort's own rate of 0.694.
The mild prior, Beta(11, 9), carries 20 pseudo-observations centred on 0.55. Updated the same way, its posterior is Beta(36, 20), mean about 0.643, which sits about 64% of the way from the historical 0.55 toward the cohort's 0.694.
The confident-but-wrong prior, Beta(220, 180), is also centred on 0.55, but carries 400 pseudo-observations, twenty times the mild prior's conviction. Updated with the same 25 passes and 11 fails, its posterior is Beta(245, 191), mean about 0.562, only about 8% of the way from 0.55 to 0.694.
Notice how narrow and tall the confident-but-wrong prior's posterior is next to the other two. All three posteriors saw the exact same 36 students. The only thing that differed between them was how many pseudo-observations each prior brought to the table.
A prior with few pseudo-observations gets swept along by real data. A prior with hundreds of pseudo-observations barely moves, even when the real data disagrees with it. That is the honest warning about priors: being confident is really a claim about how much evidence it should take to move you, and if that confidence is wrong, a prior that strong barely moves even when the data say otherwise.
What a 95% credible interval actually says
A posterior mean is one number. But the posterior is a whole distribution, so it also supports a range: a credible interval.
Take the flat prior's posterior from a moment ago, Beta(26, 12). Its 95% credible interval is the range holding the middle 95% of that distribution, found with qbeta().
So the 95% credible interval runs from 0.530 to 0.820.
Here is the part that is easy to get wrong about an interval like this. Because the posterior IS a probability distribution over the true pass rate theta, you get to say directly:
\[P(0.530 \le \theta \le 0.820 \mid \text{data}) = 0.95\]
There is a 95% probability that the true pass rate lies between 0.530 and 0.820.
A confidence interval of that same width cannot support that sentence. A confidence interval comes from a procedure that, run over and over on new samples, would cover the true rate 95% of the time. It says nothing about where the true rate is likely to sit for this one dataset you actually have. A credible interval makes exactly the statement people wrongly reach for when they talk about a confidence interval, because it comes from an actual probability distribution over the parameter, which a confidence interval never has.
Closing check: what the credible interval says
Recall the flat prior's posterior, Beta(26, 12), the weakest-pulled of the three priors you just compared. Its 95% credible interval runs from 0.530 to 0.820. Which sentence reads that interval correctly?
Your turn: change the prior, read the posterior
The code below computes the posterior mean for a Beta(a, b) prior updated with this cohort's 25 passes and 11 fails out of 36 students. It currently uses the mild prior, Beta(11, 9).
Show answer
# Recompute the posterior mean using the confident-but-wrong prior's alpha and beta
y <- 25; n <- 36
a <- 220
b <- 180
posterior_mean <- (a + y) / (a + y + b + (n - y))
round(posterior_mean, 3)
#> [1] 0.562Change a and b to the confident-but-wrong prior's values, 220 and 180, and run it again. See how far the posterior mean moves this time.
References
- Bayesian Data Analysis, 3rd edition - Gelman, A., Carlin, J.B., Stern, H.S., Dunson, D.B., Vehtari, A. and Rubin, D.B. (2013), CRC Press. The reference text behind the conjugate update and credible intervals used in this lesson.
- Doing Bayesian Data Analysis, 2nd edition - Kruschke, J.K. (2014), Academic Press. A gentler, example-driven path through the same beta-binomial update.
- Statistical Rethinking, 2nd edition - McElreath, R. (2020), CRC Press. Builds the same prior-likelihood-posterior picture from first principles, with worked R code throughout.
- Casella, G. and Berger, R.L. (2002). Statistical Inference, 2nd edition. Duxbury. The standard reference for the confidence interval definition this lesson's credible interval is contrasted against.
What one dataset can and cannot do to a belief
Here's everything in one place. This year's tutoring cohort gave you 25 passes out of 36 students, against a historical pass rate of 0.55. Bayes' rule combined that result with a prior belief by adding real successes and failures onto the prior's own pseudo-successes and pseudo-failures, turning Beta(a, b) into Beta(a + y, b + (n - y)).
How far that update moved depended entirely on how many pseudo-observations the prior carried. The flat prior, worth almost nothing, landed close to the cohort's own 0.694. The mild prior, worth 20 pseudo-observations, moved about 64% of the way there. The confident-but-wrong prior, worth 400, moved only about 8%, because its own conviction outweighed one cohort's worth of real evidence.
And the flat prior's posterior supported a genuine probability statement: a 95% credible interval running from 0.530 to 0.820, inside which the true pass rate sits with 95% probability, a sentence a same-width confidence interval cannot make.
The next lesson in this course covers what to do when a closed-form update like this one does not exist, which is most of the time, by exploring the posterior through sampling instead.