---
title: Bayesian inference with conjugate priors
heading: Estimating a probability from coin tosses, the beta prior and how counts update it, posterior mean, mode and variance, the predictive probability and Laplace's rule of succession, and the Dirichlet prior for several outcomes
eyebrow: S14 · Lesson 7 · Bayesian inference with conjugate priors
xp: 50
---

::: hook
Three tests of a new chip all pass. Maximum likelihood says the pass probability is 1, which no engineer believes after three trials. Bayesian inference fixes this by starting from a prior belief and updating it with the data. For coin-like data there's a prior, the beta distribution, that stays a beta distribution after every update, so learning reduces to adding counts. The same trick, for several outcomes at once, powers language models' smoothing and spam filters.
:::

::: play
- The posterior density for a pass probability μ after m passes in N trials (m set by the fraction slider), starting from a beta(a, b) prior: beta(a + m, b + N − m).
- With a = b = 1 (a uniform prior) and 3 passes in 3, the posterior favors high μ but still allows lower values. More trials sharpen it.
- A stronger prior (larger a + b) needs more data to move.
:::

```sim plot
view: graph
x: u = 0..1
y: p = 0..8
slider: a = 1..10 step 1 = 1
slider: b = 1..10 step 1 = 1
slider: N = 0..30 step 1 = 3
slider: f = 0..1 step 0.05 = 1
plot: u^(a + round(f*N) - 1)*(1 - u)^(b + N - round(f*N) - 1)*(a + b + N - 1)!/((a + round(f*N) - 1)!*(b + N - round(f*N) - 1)!) | samples: 400 | label: posterior density
readout: successes m = round(f*N) | digits: 3
readout: posterior mean = (a + round(f*N))/(a + b + N) | digits: 3
```

::: predict
## A chip passes 3 tests out of 3. Starting from a uniform prior, what probability should you give that it passes the next one?

- [ ] 1
- [x] 0.8
- [ ] 0.75
- [ ] 0.5

::: reveal
A uniform prior is beta(1, 1); three passes make the posterior beta(4, 1), whose mean is 4/5 = 0.8. That mean is the predictive probability of the next pass: Laplace's rule of succession, (m + 1)/(N + 2). It never reaches 1 or 0 from finite data.
:::
:::

::: nameit
## The beta prior

For a probability μ, the **beta** density $\text{Beta}(\mu|a, b) \propto \mu^{a - 1}(1 - \mu)^{b - 1}$ has mean $a/(a + b)$ and variance $\frac{ab}{(a + b)^2(a + b + 1)}$. The parameters act like prior counts of successes and failures.

## Updating

After m successes in N trials (a binomial likelihood $\mu^m(1 - \mu)^{N - m}$), the posterior is

$$\text{Beta}(\mu|a + m, b + N - m)$$

A prior that keeps its form under updating is **conjugate**.

## Summaries

- Posterior mean $\frac{a + m}{a + b + N}$, also the predictive probability of the next success.
- Posterior mode (MAP) $\frac{a + m - 1}{a + b + N - 2}$.
- Maximum likelihood m/N is the limit of weak priors and lots of data.
- **Laplace's rule** (uniform prior): $(m + 1)/(N + 2)$.

## Several outcomes

For K outcomes with probabilities $\mu_k$, the conjugate prior is the **Dirichlet** with parameters $\alpha_k$; counts $m_k$ give posterior $\alpha_k + m_k$ and posterior means $(\alpha_k + m_k)/\sum_j(\alpha_j + m_j)$. Adding 1 to every count before normalizing is "add-one smoothing".
:::

::: example
## With a beta(2, 2) prior and 3 passes in 3, find the posterior mean, MAP estimate and standard deviation.

::: solution
1. **Posterior.**
   $$\text{beta}(5, 2)$$
2. **Mean and MAP.**
   $$\begin{aligned} \text{mean} &= 5/7 \\ &= 0.714 \\ \text{MAP} &= \frac{5 - 1}{7 - 2} \\ &= 0.8 \end{aligned}$$
3. **Spread.**
   $$\begin{aligned} \text{var} &= \frac{5 \times 2}{7^2 \times 8} \\ &= 0.0255 \end{aligned}$$
   a standard deviation of 0.160.
4. **Effect of the prior.** The prior's two pseudo-counts on each side pull the estimate away from the extreme.

**Answer:** the posterior is beta(5, 2), with mean 0.714, MAP estimate 0.8 and standard deviation 0.160.
:::
:::

::: example
## A three-symbol source produced counts 3, 0 and 1. With a uniform Dirichlet prior (α = 1 each), what are the predictive probabilities?

::: solution
1. **Posterior parameters.** They are 4, 1 and 2, a total of 7.
2. **Predictive probabilities.**
   $$\begin{aligned} p &= (4, 1, 2)/7 \\ &= (0.571, 0.143, 0.286) \end{aligned}$$
3. **The unseen symbol.** It keeps probability 1/7 instead of the zero that maximum likelihood would give it, which would make any later occurrence infinitely surprising.

**Answer:** the predictive probabilities are 0.571, 0.143 and 0.286.
:::
:::

::: example
## A failure rate gets a gamma prior with shape 2 and rate 1000 hours (as if 2 failures had been seen in 1000 hours). A fleet then logs 3 failures in 5000 hours. What's the posterior?

::: solution
1. **Update.** The gamma prior is conjugate to Poisson counts: shape and rate add the failures and the hours.
   $$\begin{aligned} &\text{gamma}(2 + 3,\ 1000 + 5000) \\ &= \text{gamma}(5,\ 6000\ \text{h}) \end{aligned}$$
2. **Posterior mean.**
   $$\begin{aligned} E[\lambda] &= 5/6000 \\ &= 8.33 \times 10^{-4}\ \text{per hour} \end{aligned}$$
   between the prior's $2 \times 10^{-3}$ and the data's $6 \times 10^{-4}$.
3. **Standard deviation.**
   $$\begin{aligned} \sigma &= \sqrt5/6000 \\ &= 3.7 \times 10^{-4}\ \text{per hour} \end{aligned}$$
   The estimate is still uncertain by almost half its value after three failures.

**Answer:** the posterior is gamma(5, 6000 h), with mean $8.33 \times 10^{-4}$ per hour and standard deviation $3.7 \times 10^{-4}$ per hour.
:::
:::

::: check
given: a = 1 | 2 | 5; b = 1 | 3; m = 2 | 7; N = 10 | 20
answer: (a + m)/(a + b + N)
tolerance: 0.5%

## A beta({a}, {b}) prior is updated with {m} successes in {N} trials. What's the posterior mean?

::: right
Right. $(a + m)/(a + b + N)$ = {answer}.
:::

::: misses
- [m/N] That's the maximum-likelihood estimate, without the prior.
- [any] Not yet. Add the counts to the prior's parameters.
:::
:::

::: check
given: a = 2 | 5; b = 2 | 3; m = 2 | 7; N = 10 | 20
answer: (a + m - 1)/(a + b + N - 2)
tolerance: 0.5%

## With the same kind of update (beta({a}, {b}) prior, {m} of {N}), what's the MAP estimate?

::: right
Right. The posterior's mode, $(a + m - 1)/(a + b + N - 2)$ = {answer}.
:::

::: misses
- [(a + m)/(a + b + N)] That's the posterior mean; the mode subtracts 1 and 2.
- [any] Not yet. The mode of beta(A, B) is $(A - 1)/(A + B - 2)$.
:::
:::

::: check
given: m = 0 | 3 | 9; N = 10 | 20
answer: (m + 1)/(N + 2)
tolerance: 0.5%

## After {m} successes in {N} trials, what does Laplace's rule of succession predict for the next trial?

::: right
Right. $(m + 1)/(N + 2)$ = {answer}.
:::

::: misses
- [m/N] Laplace's rule uses a uniform prior: add one success and one failure.
- [any] Not yet. Uniform prior, posterior mean.
:::
:::

::: check
given: a = 2 | 5 | 12; b = 3 | 8
answer: a*b/((a + b)^2*(a + b + 1))
tolerance: 0.5%

## What's the variance of a beta({a}, {b}) distribution?

::: right
Right. $ab/((a + b)^2(a + b + 1))$ = {answer}.
:::

::: misses
- [any] Not yet. The beta variance formula.
:::
:::

::: check
given: x = 0 | 1 | 12; y = 1 | 4; z = 3 | 9; c = 0.5 | 1
answer: (x + c)/(x + y + z + 3*c)
tolerance: 0.5%

## A three-outcome source produced counts {x}, {y} and {z}. With a Dirichlet prior of {c} on each outcome, what's the predictive probability of the first?

::: right
Right. $(m_1 + \alpha)/\sum(m_k + \alpha)$ = {answer}.
:::

::: misses
- [x/(x + y + z)] That's maximum likelihood, which gives unseen outcomes zero.
- [any] Not yet. Add α to every count.
:::
:::

::: check
given: a = 1 | 4; b = 1 | 4; N = 5 | 50; m = 2 | 3
answer: (a + m)/(a + b + N) - m/N
tolerance: 0.5%

## How far does the posterior mean (beta({a}, {b}) prior, {m} successes in {N}) sit from the maximum-likelihood estimate?

::: right
Right. $\frac{a + m}{a + b + N} - \frac{m}{N}$ = {answer}.
:::

::: misses
- [any] Not yet. Compute both and subtract.
:::
:::

::: check
kind: parts
given: a = 1 | 2 | 4; b = 500 | 3000; k = 0 | 3 | 12; T = 1000 | 10000

## A failure rate has a gamma({a}, {b} h) prior; then {k} failures occur in {T} hours.

::: part
answer: (a + k)/(b + T)*1e3
tolerance: 0.5%

### What is the posterior mean rate, per thousand hours?

::: misses
- [k/T*1e3] That's the data alone; include the prior.
- [any] $(\alpha + k)/(\beta + T)$.
:::
:::

::: part
answer: sqrt(a + k)/(b + T)*1e3
tolerance: 0.5%

### What is its posterior standard deviation, per thousand hours?

::: misses
- [any] $\sqrt{\alpha'}/\beta'$ for a gamma distribution.
:::
:::
:::

::: check
kind: derivation
vars: a = 0.5..10, b = 0.5..10, m = 0..20, N = 20..40

## A beta(a, b) prior on a success probability is updated with m successes in N trials.

::: step
answer: a + m

### What is the posterior's first parameter?
:::

::: step
answer: (a + m)/(a + b + N)

### What probability does it predict for the next trial's success?
:::

::: step
answer: (a + m)*(a + m + 1)/((a + b + N)*(a + b + N + 1))

### What probability does it predict for the next two trials both succeeding?
:::

::: right
Right. The second success is predicted after updating on the first, so the answer isn't the square of the first.
:::
:::

::: explain
## Why does maximum likelihood give unreasonable estimates from little data, and how does a prior help?

::: model
Maximum likelihood picks whatever parameter makes the observed data most probable, with no regard for how much data there is. Three passes in three trials make "always passes" the most likely explanation, and zero occurrences of a word make its probability zero, so a single later occurrence is treated as impossible. A prior encodes what's believed before the data, such as that a pass probability is unlikely to be exactly 1. With a conjugate beta prior it acts like a few imaginary trials added to the real ones, pulling estimates away from 0 and 1 when data are scarce and fading in influence as real counts accumulate.
:::
:::

::: practice

::: check
given: a = 0.5 | 10; b = 0.5 | 10; m = 0 | 30; N = 40 | 100
answer: (a + m)/(a + b + N)
tolerance: 0.5%

## Posterior mean: beta({a}, {b}), {m} of {N}?

::: misses
- [any] $(a + m)/(a + b + N)$.
:::
:::

::: check
given: m = 0 | 50; N = 60 | 1000
answer: (m + 1)/(N + 2)
tolerance: 0.5%

## Laplace's rule: {m} of {N}?

::: misses
- [any] $(m + 1)/(N + 2)$.
:::
:::

::: check
given: a = 1 | 30; b = 1 | 30
answer: a*b/((a + b)^2*(a + b + 1))
tolerance: 0.5%

## Beta({a}, {b}) variance?

::: misses
- [any] $ab/((a + b)^2(a + b + 1))$.
:::
:::
:::
