Lesson 16 of 19 · Statistics for AI, GenAI & LLMs

Bayesian Inference: Updating Beliefs About a Model, Not Just a Message

Lesson 3 used Bayes' theorem to update the probability of one event. The same move works on a number you can never observe directly, like a model's true accuracy.

In Lesson 3 the hypothesis was a yes/no event: "this response is a hallucination." You had a base rate, a detector's flag, and one update. This lesson changes the hypothesis. It becomes a parameter: the true accuracy θ of a model, the true win rate of a prompt, the weight of a network. A parameter can take any value in a range, so your belief about it is a whole distribution, not a single probability. Lesson 13 introduced the Beta distribution for exactly this kind of quantity, a number between 0 and 1. Here you put it to work.

1. From events to parameters

The theorem has not changed. Only the thing on the left has:

P(θ | data) ∝ P(data | θ) × P(θ)
posterior likelihood prior

∝ means "proportional to." The denominator from Lesson 3, P(evidence), only rescales the curve to total 1, so you can drop it while finding the shape.

PieceLesson 3 (an event)This lesson (a parameter)
Prior2% of responses hallucinateA distribution over θ before the eval: "accuracy is probably around 0.8"
LikelihoodDetector flags 90% of real hallucinationsHow probable "6 correct out of 10" is for each possible θ
Posterior27% that a flagged response is realA new distribution over θ after the eval

Lesson 5 returned one number (the maximum likelihood estimate, the observed proportion) plus a confidence interval. The Bayesian answer is a full curve of how plausible each θ is, given the prior and the data.

2. The Beta prior is a count of imagined prior evaluations

A team is evaluating a new model version on a yes/no task: each prompt is answered correctly or not. Call the unknown true probability of a correct answer θ. Before running anything, the team has a view. The previous version scored around 80%, and they expect the new one to be similar. They encode that as a Beta(8, 2) prior (Lesson 13: the mean of Beta(α, β) is α/(α+β)).

prior mean = α / (α + β) = 8 / (8 + 2) = 0.80
prior 95% credible interval = 0.518 to 0.972

The useful way to read α and β is as pseudo-counts: Beta(8, 2) behaves as if the team had already seen 8 correct and 2 wrong answers, 10 imagined prompts in total. That is a real opinion, but still wide: the prior alone allows anything from about 52% to 97%.

A prior is a modelling choice, not a fact: state where it came from, and check how far the answer moves if you change it.

3. The conjugate update, worked in full

The team runs a small eval: n = 10 prompts, k = 6 answered correctly. Given a value θ, the probability of that result is proportional to θk(1−θ)n−k. Multiply by the prior, which has the same form with exponents α−1 and β−1, and exponents on the same base add:

likelihood ∝ θ^k (1−θ)^(n−k)
prior ∝ θ^(α−1) (1−θ)^(β−1)
posterior ∝ θ^(α+k−1) (1−θ)^(β+n−k−1) = Beta(α+k, β+n−k)

The posterior is another Beta. A prior that stays in its own family after the update is called conjugate. The update is just addition: add the correct answers to α and the wrong answers to β.

prior Beta(8, 2)
data k = 6 correct, n − k = 4 wrong
posterior Beta(8 + 6, 2 + 4) = Beta(14, 6)
prior mean 8 / 10 = 0.800
MLE (Lesson 5) 6 / 10 = 0.600
posterior mean 14 / 20 = 0.700
95% credible interval 0.488 to 0.874

The posterior mean 0.70 sits exactly halfway between the prior mean 0.80 and the observed 0.60. The next section shows why.

How the interval was computed The interval comes from the Beta distribution directly, computed three ways. (1) Integrate the density numerically (Simpson's rule) and invert the cumulative distribution by bisection at 0.025 and 0.975. (2) For whole-number α and β, the Beta cumulative distribution equals a binomial tail sum, which is exact. (3) Draw 400,000 random Beta samples and read the 2.5th and 97.5th percentiles. All three agree to three decimals (0.488 to 0.874); the simulation lands within about 0.001 of the other two, as expected from sampling noise.

4. Small eval sets stay near the prior, large ones overwhelm it

Rewrite the posterior mean as a weighted average of the prior mean and the observed proportion. With a prior worth α+β = 10 pseudo-prompts and n real ones:

posterior mean = (α+k) / (α+β+n)
= w × prior mean + (1 − w) × k/n, w = (α+β) / (α+β+n)
n = 10: w = 10 / 20 = 0.50 → 0.50 × 0.80 + 0.50 × 0.60 = 0.700
n = 1000: w = 10 / 1010 = 0.0099 → 0.0099 × 0.80 + 0.9901 × 0.60 = 0.602

Now run a much larger eval with the same 60% hit rate: n = 1000, k = 600. The posterior is Beta(8 + 600, 2 + 400) = Beta(608, 402).

EvalPosteriorMean95% credible intervalP(θ > 0.8)
No data (prior)Beta(8, 2)0.8000.518 to 0.9720.564
6 of 10Beta(14, 6)0.7000.488 to 0.8740.163
600 of 1000Beta(608, 402)0.6020.572 to 0.632about 0

The last column is something a Bayesian posterior hands you for free: a direct probability for a claim. After 10 prompts, there is still a 16% chance the new model is above 80%, because the prior keeps that door open. After 1000 prompts, the claim is dead. (The interval width falls from 0.386 to 0.060, the square-root tax from Lesson 5.)

00.5100.51 n = 10, k = 6: the prior still matters n = 1000, k = 600: the data decides 95% credible interval 0.488 to 0.874 95% credible interval 0.572 to 0.632 prior Beta(8,2) posteriorBeta(14,6) posteriorBeta(608,402) prior Beta(8,2) true accuracy θtrue accuracy θ (note the taller vertical scale)

Dashed purple is the Beta(8,2) prior. Left: 10 prompts drag it only part of the way toward 0.60, and it stays wide. Right: 1000 prompts leave a narrow spike on 0.60 (vertical scale about six times taller).

The prior is worth α+β imagined observations and the eval is worth n real ones; whichever is bigger wins. Ten prompts really do carry little information, so the posterior stays cautious.

5. Sequential updating: yesterday's posterior is today's prior

Evals rarely arrive in one batch. Because the update is addition, you can do it in pieces. Start with the Beta(14, 6) posterior from the first 10 prompts and treat it as the prior for the next batch: 40 new prompts, 30 correct.

day 1 posterior (becomes day 2 prior) Beta(14, 6)
day 2 data k = 30, n − k = 10
day 2 posterior Beta(14 + 30, 6 + 10) = Beta(44, 16)
pooled in one go: 36 correct of 50, from Beta(8, 2)
Beta(8 + 36, 2 + 14) = Beta(44, 16) same answer
mean = 44 / 60 = 0.733, 95% credible interval 0.616 to 0.836

Batch by batch or all at once, the result is identical, so you never need to keep raw data around to keep a running belief: two numbers, α and β, summarise everything seen so far.

6. MAP vs. MLE, and why L2 regularization is a prior

Lesson 5's maximum likelihood estimate picks the θ that makes the data most probable. The Bayesian counterpart picks the θ at the peak of the posterior, called the maximum a posteriori estimate, or MAP.

MLE = argmax P(data | θ) = k / n = 6/10 = 0.600
MAP = argmax P(data | θ) × P(θ) = (α+k−1)/(α+β+n−2)
= 13 / 18 = 0.722

The MAP is the MLE pulled toward the prior. Two limits are worth noticing. With a flat prior, Beta(1, 1), the MAP formula gives k/n, so MAP equals MLE. With lots of data, the prior's pull vanishes, as in the table above. MAP keeps only the peak, not the curve's width, so it says nothing about how sure to be.

The Lesson 8 connection, in one line

Lesson 8 introduced weight penalties (L2) as a way to hold a model back from overfitting. They are exactly a Gaussian prior on the weights. Take logs of the posterior, then negate to get a loss you minimise:

−log P(w | data) = −log P(data | w) − log P(w) + constant
= (loss on the data) + −log P(w) + constant
prior w ~ Normal(0, τ²): −log P(w) = ||w||² / (2τ²) + constant
MAP loss = data loss + λ · ||w||², with λ = 1 / (2τ²)

The first term is the data's negative log-likelihood, summed over examples: the cross-entropy loss from Lesson 7, or, for regression with Gaussian noise of variance σ², the squared-error loss from Lesson 6 divided by 2σ². The second is the L2 penalty. The formula λ = 1/(2τ²) holds against that summed negative log-likelihood; against plain squared error it becomes σ²/τ², and if the loss is averaged over n examples, divide λ by n.The prior's width sets its strength: τ = 1 gives λ = 0.5, and a tighter prior, τ = 0.5, gives λ = 1/(2 × 0.25) = 2. "Weights should be small" is the belief "weights are probably near zero," and regularization strength is how firmly you hold it. So regularized training is MAP estimation; a fully Bayesian treatment keeps the whole posterior over the weights.

7. Credible intervals vs. confidence intervals

Compare the Lesson 5 rule of thumb (observed ± 2 standard errors) with the Beta credible interval on the same evals.

n = 10, k = 6: se = √(0.6 × 0.4 / 10) = 0.155
confidence (Lesson 5): 0.60 ± 2 × 0.155 = 0.29 to 0.91 (width 0.62)
credible (Beta(14,6)): 0.488 to 0.874 (width 0.39)
n = 1000, k = 600: se = √(0.6 × 0.4 / 1000) = 0.0155
confidence: 0.60 ± 0.031 = 0.569 to 0.631
credible (Beta(608,402)): 0.572 to 0.632

At n = 1000 they agree to within a few thousandths. At n = 10 the credible interval is far narrower, and the reason is the prior: it contributed the equivalent of 10 more prompts. That narrowing is earned only if the prior was reasonable. A wrong confident prior gives a tight interval around the wrong place.

What each interval does and does not say A credible interval of 0.488 to 0.874 says: given this prior and this data, there is a 95% probability that the true accuracy lies in that range. A confidence interval says nothing about this particular range. It describes the procedure: if you repeated the whole eval many times and built an interval each time, about 95% of those intervals would contain the true accuracy. Any single confidence interval either contains it or does not. Reading a confidence interval as "95% probability the truth is in here" is the most common slip, and it is only correct for the Bayesian interval.
Frequentist (Lesson 5)Bayesian (this lesson)
What θ isA fixed unknown constantUncertain, so described by a distribution
What is randomThe data, across repeated evalsYour belief; the observed data is fixed
Point estimateMLE: 0.600Posterior mean 0.700 or MAP 0.722
IntervalConfidence: 0.29 to 0.91, a statement about the procedureCredible: 0.488 to 0.874, a statement about θ
Prior knowledgeNot part of the formal methodRequired, and stated openly
Behaviour with lots of dataBoth converge to nearly the same answer

8. The posterior predictive: probability the next prompt is answered correctly

What you often need is not θ itself but a forecast: will the next prompt be answered correctly? Without uncertainty you would plug in a single estimate. A Bayesian averages the prediction over every θ, weighted by the posterior. That is the posterior predictive.

For one yes/no outcome, "correct" has probability θ, so averaging θ over the posterior is just the posterior mean:

P(next prompt correct | data) = E[θ] = 14 / (14 + 6) = 0.70
plug-in with the MLE: 0.60

The two forecasts differ because the Bayesian one admits the 10-prompt eval is thin and leans toward the prior. The difference grows for events involving more than one prompt. Take the probability that the next two prompts are both correct. The Beta gives a closed form, α(α+1) / ((α+β)(α+β+1)):

P(both correct) = 14 × 15 / (20 × 21) = 210 / 420 = 0.500
plug in MLE: 0.60² = 0.360
plug in posterior mean: 0.70² = 0.490
gap between 0.500 and 0.490 = posterior variance = 0.1² = 0.010

An independent numerical integration of θ² against the Beta(14, 6) density also gives 0.500. The leftover 0.010 is the posterior variance: two answers are positively linked through the shared unknown θ even though they are independent once θ is known.

9. Why this matters for building AI systems

Why This Matters Almost every number an AI team acts on is an estimate from a thin sample. Bayesian inference keeps the thinness visible instead of hiding it in a single score.

Test a new prompt on 10 cases and a Beta posterior lets you say "there is a 16% chance this is above 80%" instead of quoting a bare score. Per-prompt or per-route success rates in production work the same way: add one to α or β per call, and the estimate starts at the prior and sharpens as traffic arrives. Bandit-style routing between models keeps one Beta per option.

Weight penalties are priors, so tuning weight decay means choosing how firmly to believe weights are small.

Recap

Leads into Lesson 17

Every posterior here, like every p-value in Lesson 15, assumes the eval data was a fair sample. If the prompts were cherry-picked, the groups were assigned by something related to the outcome, or test items leaked into training, no prior and no test can fix the number. Lesson 17 covers study design: random sampling, randomized comparisons, confounding, and data leakage.

✅ Check Yourself

1. A team's prior on a model's accuracy is Beta(8, 2). They run 5 prompts and 3 are answered correctly. What is the posterior?

Beta(3, 2)
Beta(11, 7)
Beta(11, 4)

2. With a Beta(8, 2) prior, why does a 10-prompt eval leave the posterior near the prior, while a 1000-prompt eval does not?

The prior counts as about 10 imagined prompts, so it is outweighed once the real sample is much larger than that
The Bayesian method ignores data until n passes 100
Large eval sets change the prior itself

3. Adding an L2 weight penalty to a training loss is equivalent to which Bayesian choice?

Using the posterior mean instead of the MAP
MAP estimation with a zero-mean Gaussian prior on the weights
Choosing a flat prior, so the estimate equals the MLE

4. A report gives a 95% credible interval of 0.49 to 0.87 for accuracy. What does it say?

95% of future prompts will score between 0.49 and 0.87
If the eval were repeated many times, 95% of such intervals would contain the truth, but this one may not
Given the prior and the data, there is a 95% probability the true accuracy is in that range

5. A team updates a Beta posterior after batch 1, then uses it as the prior for batch 2. Compared with pooling both batches into one update from the original prior, the result is:

Different, because batch 1 gets counted twice
Identical, because the update just adds the correct and wrong counts to α and β
Different, because later batches always get more weight