Simple Algorithm
Machine Learning · Convergence · 26 min

Conjugate Priors & Exact Posteriors

Beta–binomial, normal–normal and Dirichlet–multinomial.

For a handful of models, Bayesian inference needs no integrals and no sampling: the posterior has the same form as the prior, and updating means adding counts. This lesson works out the four conjugate pairs that matter most, their predictive distributions and what they teach about shrinkage, and then asks the deeper question of why a prior should exist at all, which de Finetti's theorem answers.

What you’ll learn

  • Define conjugacy, construct the conjugate prior of an exponential family, and read hyperparameters as pseudo-observations.
  • Derive the Beta–binomial posterior and predictive distributions, and quantify shrinkage toward the prior mean.
  • Derive the normal–normal posterior by completing the square, and state the normal–inverse-gamma update with its Student-tt marginals.
  • Use the Dirichlet–multinomial and Gamma–Poisson models for counts, connecting them to additive smoothing and overdispersion.
  • State de Finetti's theorem for exchangeable sequences and explain why it fails for finite sequences.

Conjugacy, precisely

LikelihoodConjugate priorPosterior after nn observationsPredictive for a new observation
Bernoulli / binomial, ppBeta(a,b)\mathrm{Beta}(a, b)Beta(a+h, b+t)\mathrm{Beta}(a + h,\ b + t)Bernoulli with mean a+ha+b+n\frac{a+h}{a+b+n}; beta-binomial for mm trials
N(μ,σ2)\mathcal N(\mu, \sigma^2), σ2\sigma^2 knownN(μ0,τ02)\mathcal N(\mu_0, \tau_0^2)N(μn,τn2)\mathcal N(\mu_n, \tau_n^2), precisions addN(μn, σ2+τn2)\mathcal N(\mu_n,\ \sigma^2 + \tau_n^2)
N(μ,σ2)\mathcal N(\mu, \sigma^2), both unknownnormal–inverse-gammanormal–inverse-gammaStudent tt
categorical / multinomial, π\boldsymbol\piDir(α)\mathrm{Dir}(\boldsymbol\alpha)Dir(α+n)\mathrm{Dir}(\boldsymbol\alpha + \mathbf n)categorical with αk+nkα0+n\frac{\alpha_k + n_k}{\alpha_0 + n}; Dirichlet-multinomial for several draws
Poisson, rate λ\lambdaGamma(α,β)\mathrm{Gamma}(\alpha, \beta) (rate β\beta)Gamma(α+∑ixi, β+n)\mathrm{Gamma}(\alpha + \sum_i x_i,\ \beta + n)negative binomial
N(μ,Σ)\mathcal N(\boldsymbol\mu, \Sigma), both unknownnormal–inverse-Wishartnormal–inverse-Wishartmultivariate Student tt
In every row the hyperparameters are pseudo-data: aa and bb prior heads and tails, αk\alpha_k prior counts per category, α\alpha prior events in β\beta prior time units. Here α0=∑kαk\alpha_0 = \sum_k\alpha_k.

Conjugacy is a convenience, not a law of nature: it buys closed forms and instant updates at the price of a restricted prior shape. When the prior shape genuinely matters, or the model is not an exponential family, the posterior must be computed numerically (Approximate Inference). Conjugate models remain the workhorses of streaming and online problems, such as the Beta posteriors behind Thompson sampling in bandits and the Gaussian updates inside the Kalman filter.

Beta–binomial: shrinkage and prediction

With prior Beta(a,b)\mathrm{Beta}(a, b) and hh heads in nn flips, the posterior mean is a+ha+b+n\frac{a+h}{a+b+n}. Subtracting the MLE gives the exact size of the pull toward the prior mean m0=a/(a+b)m_0 = a/(a+b):

E[p∣D]−p^=a+ba+b+n (m0−p^).\E[p \mid \data] - \hat p = \frac{a+b}{a+b+n}\,\big(m_0 - \hat p\big).

The prior's strength a+ba + b acts as a sample size: when n=a+bn = a + b, the posterior mean sits exactly halfway between the prior mean and the MLE.

Predict before you look

Prior Beta(100,100)\mathrm{Beta}(100, 100), a coin whose MLE settles near 0.70.7. About how many flips does it take before the posterior mean is within 0.050.05 of the MLE?

Show the answer

600 flips

Solve 200200+n⋅(0.7−0.5)≤0.05\frac{200}{200+n}\cdot(0.7 - 0.5) \le 0.05: 40≤10+0.05n40 \le 10 + 0.05n, so n≥600n \ge 600. A prior worth 200 imaginary flips needs three times that many real flips before the data win by this criterion.

DerivationThe beta-binomial predictive
  1. P(k∣D)=∫01(mk)pk(1−p)m−k pα−1(1−p)β−1B(α,β) dp=(mk)B(α+k, β+m−k)B(α,β)P(k \mid \data) = \int_0^1 \binom{m}{k}p^k(1-p)^{m-k}\,\frac{p^{\alpha-1}(1-p)^{\beta-1}}{B(\alpha, \beta)}\,dp = \binom{m}{k}\frac{B(\alpha+k,\ \beta+m-k)}{B(\alpha, \beta)}

    The probability of kk heads in mm future flips averages the binomial over the posterior Beta(α,β)\mathrm{Beta}(\alpha, \beta), with α=a+h\alpha = a + h and β=b+t\beta = b + t:

  2. E[k]=m αα+β,Var⁡[k]=m αβ(α+β)2⋅α+β+mα+β+1\E[k] = m\,\frac{\alpha}{\alpha+\beta},\qquad \Var[k] = m\,\frac{\alpha\beta}{(\alpha+\beta)^2}\cdot\frac{\alpha+\beta+m}{\alpha+\beta+1}

    Its mean is that of a binomial with p=α/(α+β)p = \alpha/(\alpha+\beta), but its variance is inflated by uncertainty about pp:

  3. P(HH∣D)=E[p2∣D]=α(α+1)(α+β)(α+β+1)=110156≈0.705P(\text{HH} \mid \data) = \E\big[p^2 \mid \data\big] = \frac{\alpha(\alpha+1)}{(\alpha+\beta)(\alpha+\beta+1)} = \frac{110}{156} \approx 0.705

    Example: a flat prior and 9 heads in 10 flips give Beta(10,2)\mathrm{Beta}(10, 2). The chance that the next two flips are both heads is B(12,2)/B(10,2)=10⋅1112⋅13≈0.705B(12, 2)/B(10, 2) = \frac{10\cdot11}{12\cdot13} \approx 0.705, between the squared posterior mean 0.6940.694 and the plug-in 0.92=0.810.9^2 = 0.81.

Gaussian models: precisions add

DerivationNormal–normal with known variance
  1. log⁡p(μ∣D)=−(μ−μ0)22τ02−∑i=1n(xi−μ)22σ2+const=−12(1τ02+nσ2)μ2+(μ0τ02+nxˉσ2)μ+const\log p(\mu \mid \data) = -\frac{(\mu - \mu_0)^2}{2\tau_0^2} - \sum_{i=1}^n\frac{(x_i - \mu)^2}{2\sigma^2} + \text{const} = -\frac12\Big(\frac{1}{\tau_0^2} + \frac{n}{\sigma^2}\Big)\mu^2 + \Big(\frac{\mu_0}{\tau_0^2} + \frac{n\bar x}{\sigma^2}\Big)\mu + \text{const}

    Data x1,…,xn∼N(μ,σ2)x_1, \dots, x_n \sim \mathcal N(\mu, \sigma^2) with σ2\sigma^2 known, and prior μ∼N(μ0,τ02)\mu \sim \mathcal N(\mu_0, \tau_0^2). The log-posterior is a quadratic in μ\mu:

  2. 1τn2=1τ02+nσ2,μn=τn2(μ0τ02+nxˉσ2)\frac{1}{\tau_n^2} = \frac{1}{\tau_0^2} + \frac{n}{\sigma^2},\qquad \mu_n = \tau_n^2\Big(\frac{\mu_0}{\tau_0^2} + \frac{n\bar x}{\sigma^2}\Big)

    Complete the square. Precisions add, and the posterior mean is the precision-weighted average of the prior mean and the sample mean:

  3. x~∣D∼N(μn, σ2+τn2)\tilde x \mid \data \sim \mathcal N\big(\mu_n,\ \sigma^2 + \tau_n^2\big)

    A new observation is x~=μ+ε\tilde x = \mu + \varepsilon with independent ε∼N(0,σ2)\varepsilon \sim \mathcal N(0, \sigma^2), so its predictive variance adds noise and parameter uncertainty, and only the second shrinks with nn:

Predict before you look

Prior μ∼N(0,1)\mu \sim \mathcal N(0, 1), noise variance σ2=4\sigma^2 = 4, and n=4n = 4 observations with mean xˉ=3\bar x = 3. What is the posterior mean of μ\mu?

Show the answer

1.5

The data's precision is n/σ2=1n/\sigma^2 = 1, equal to the prior's precision 1/τ02=11/\tau_0^2 = 1, so the posterior mean is halfway: μn=0⋅1+3⋅12=1.5\mu_n = \frac{0\cdot1 + 3\cdot1}{2} = 1.5, with posterior variance τn2=12\tau_n^2 = \frac12. The predictive variance of a new observation is 4+0.5=4.54 + 0.5 = 4.5.

QuantityPriorPosterior
varianceσ2∼IG(α0,β0)\sigma^2 \sim \mathrm{IG}(\alpha_0, \beta_0)αn=α0+n2\alpha_n = \alpha_0 + \frac n2,  βn=β0+12∑i(xi−xˉ)2+κ0n(xˉ−μ0)22κn\ \beta_n = \beta_0 + \frac12\sum_i(x_i - \bar x)^2 + \frac{\kappa_0 n(\bar x - \mu_0)^2}{2\kappa_n}
mean given varianceμ∣σ2∼N(μ0,σ2/κ0)\mu \mid \sigma^2 \sim \mathcal N(\mu_0, \sigma^2/\kappa_0)κn=κ0+n\kappa_n = \kappa_0 + n,  μn=κ0μ0+nxˉκn\ \mu_n = \frac{\kappa_0\mu_0 + n\bar x}{\kappa_n}
mean, variance integrated outStudent ttμ∣D∼t2αn(μn, βnαnκn)\mu \mid \data \sim t_{2\alpha_n}\big(\mu_n,\ \frac{\beta_n}{\alpha_n\kappa_n}\big)
new observationStudent ttx~∣D∼t2αn(μn, βn(κn+1)αnκn)\tilde x \mid \data \sim t_{2\alpha_n}\big(\mu_n,\ \frac{\beta_n(\kappa_n + 1)}{\alpha_n\kappa_n}\big)
The normal–inverse-gamma prior for unknown mean and variance, with IG(α,β)\mathrm{IG}(\alpha, \beta) the density ∝(σ2)−α−1e−β/σ2\propto (\sigma^2)^{-\alpha-1}e^{-\beta/\sigma^2} and tν(m,s2)t_\nu(m, s^2) a Student-tt with ν\nu degrees of freedom, location mm and squared scale s2s^2. The prior mean counts as κ0\kappa_0 observations. Integrating out an unknown variance turns Gaussian predictions into heavier-tailed tt predictions, which is honest when nn is small; as nn grows, 2αn→∞2\alpha_n \to \infty and the tt becomes Gaussian.

Counts: Dirichlet–multinomial and Gamma–Poisson

Categories. With a Dirichlet prior π∼Dir(α1,…,αK)\boldsymbol\pi \sim \mathrm{Dir}(\alpha_1, \dots, \alpha_K), density ∝∏kπkαk−1\propto \prod_k \pi_k^{\alpha_k - 1}, and counts n1,…,nKn_1, \dots, n_K, the posterior is Dir(α1+n1,…,αK+nK)\mathrm{Dir}(\alpha_1 + n_1, \dots, \alpha_K + n_K). The predictive probability of category kk is the posterior mean (αk+nk)/(α0+n)(\alpha_k + n_k)/(\alpha_0 + n), where α0=∑kαk\alpha_0 = \sum_k \alpha_k: additive smoothing, with αk=1\alpha_k = 1 (Laplace) or αk=12\alpha_k = \frac12 (Jeffreys' prior for the multinomial) as common defaults. Predicting a whole sequence this way is a Pólya urn: draw a ball, put it back with one more of the same color, and the urn's composition is the current posterior.

Rates. With a Gamma prior λ∼Gamma(α,β)\lambda \sim \mathrm{Gamma}(\alpha, \beta), density ∝λα−1e−βλ\propto \lambda^{\alpha-1}e^{-\beta\lambda}, and Poisson counts x1,…,xnx_1, \dots, x_n, the posterior is Gamma(α+∑ixi, β+n)\mathrm{Gamma}(\alpha + \sum_i x_i,\ \beta + n): α\alpha prior events observed over β\beta prior time units. The predictive distribution of a new count is negative binomial, with mean αn/βn\alpha_n/\beta_n and variance αnβn(1+1βn)\frac{\alpha_n}{\beta_n}\big(1 + \frac{1}{\beta_n}\big), larger than its mean. A Gamma mixture of Poissons is overdispersed, which is why the negative binomial is the standard model for counts that vary more than a Poisson allows.

Check your understanding

A three-sided die has a Dir(1,1,1)\mathrm{Dir}(1, 1, 1) prior and shows counts (5,0,3)(5, 0, 3) in 8 rolls. What predictive probability does the unseen face get?

  1. 00
  2. 1/11≈0.0911/11 \approx 0.091
  3. 1/8=0.1251/8 = 0.125
  4. 1/31/3
Show the answer

B. 1/11≈0.0911/11 \approx 0.091

The posterior is Dir(6,1,4)\mathrm{Dir}(6, 1, 4), and the predictive probability of face 2 is 1/(6+1+4)=1/111/(6 + 1 + 4) = 1/11. With Jeffreys' Dir(12,12,12)\mathrm{Dir}(\frac12, \frac12, \frac12) it would be 0.5/9.5≈0.0530.5/9.5 \approx 0.053: the prior's strength sets how much probability unseen outcomes keep.

Think first Prior λ∼Gamma(2,1)\lambda \sim \mathrm{Gamma}(2, 1) for a daily event rate; you then observe 3, 5 and 4 events on three days. What are the posterior mean and the predictive variance of tomorrow's count?

The posterior is Gamma(2+12, 1+3)=Gamma(14,4)\mathrm{Gamma}(2 + 12,\ 1 + 3) = \mathrm{Gamma}(14, 4), with mean 3.53.5: between the prior mean 2 and the sample mean 4, with weight 3/43/4 on the data. Tomorrow's count is negative binomial with mean 3.53.5 and variance 3.5 (1+14)=4.3753.5\,(1 + \tfrac14) = 4.375. The extra 0.8750.875 over a Poisson's variance is your remaining uncertainty about λ\lambda.

Exchangeability and de Finetti

Where does the prior come from? A sequence X1,X2,…X_1, X_2, \dots is exchangeable if the joint law of every finite prefix is unchanged by permuting its terms: the order of the observations carries no information. That is weaker than independence, and it is often all you are willing to assume.

Read it as a justification of the Bayesian model. Exchangeability alone implies that the data behave as if a parameter pp were drawn from a prior μ\mu and the observations were then i.i.d. given pp. The parameter is not a mystical quantity: it is the long-run frequency, and the prior is your belief about that frequency.

The theorem needs an infinite sequence. Draw both balls, without replacement, from an urn holding one red and one black: (X1,X2)(X_1, X_2) is (1,0)(1, 0) or (0,1)(0, 1) with probability 12\frac12 each, which is exchangeable, yet X2=1−X1X_2 = 1 - X_1. Any mixture of i.i.d. sequences has Cov⁡(X1,X2)=E[p2]−(Ep)2=Var⁡μ(p)≥0\Cov(X_1, X_2) = \E[p^2] - (\E p)^2 = \Var_\mu(p) \ge 0, while here the covariance is −14-\frac14. Diaconis & Freedman (1980) quantified the gap: the first kk terms of an exchangeable sequence of length nn over a finite set are within total variation distance of order k/nk/n of a mixture of i.i.d. sequences.

Key takeaways

  • A conjugate prior keeps the posterior in the prior's family; for exponential families it is ∝exp⁡(η⊤χ−νA(η))\propto \exp(\boldsymbol\eta^\top\boldsymbol\chi - \nu A(\boldsymbol\eta)) and the hyperparameters are pseudo-data.
  • Beta–binomial: the posterior mean shrinks the MLE toward the prior mean by the factor (a+b)/(a+b+n)(a+b)/(a+b+n), and the beta-binomial predictive has the binomial's mean with inflated variance.
  • Normal–normal: precisions add and the posterior mean is precision-weighted; with an unknown variance (normal–inverse-gamma) the marginal posterior and the predictive are Student tt.
  • Dirichlet–multinomial gives additive smoothing and the Pólya urn; Gamma–Poisson gives a negative-binomial predictive, overdispersed relative to the Poisson.
  • de Finetti: an infinite exchangeable binary sequence is a mixture of i.i.d. Bernoulli sequences, so the prior is a belief about a long-run frequency. Finite exchangeable vectors need not be mixtures, but nearly are when k≪nk \ll n.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

A Beta(a,b)\mathrm{Beta}(a, b) prior with a+b=30a + b = 30 meets data with MLE p^\hat p. After how many observations is the posterior mean exactly halfway between the prior mean and p^\hat p?

  1. 15
  2. 30
  3. 60
  4. It is never exactly halfway
Show the answer

B. 30

The weight on the data is n/(a+b+n)n/(a+b+n), which equals 12\frac12 when n=a+b=30n = a + b = 30. The prior's strength is literally a sample size.

Question 2

Prior μ∼N(10,4)\mu \sim \mathcal N(10, 4); one observation x=16x = 16 with known noise variance 44. What is the posterior?

  1. N(16,4)\mathcal N(16, 4)
  2. N(13,4)\mathcal N(13, 4)
  3. N(13,2)\mathcal N(13, 2)
  4. N(13,8)\mathcal N(13, 8)
Show the answer

C. N(13,2)\mathcal N(13, 2)

The two precisions are equal (14\frac14 each), so the posterior mean is the plain average, 13, and the precisions add to 12\frac12: posterior variance 2. Combining information always reduces the variance in this model.

Question 3

(X1,X2)(X_1, X_2) is exchangeable with Cov⁡(X1,X2)<0\Cov(X_1, X_2) < 0. What can you conclude?

  1. It cannot be the beginning of an infinite exchangeable sequence: mixtures of i.i.d. sequences have nonnegative covariance
  2. It is not exchangeable after all, because exchangeable variables must be independent
  3. It must be i.i.d., because exchangeability implies independence for pairs
  4. Nothing: de Finetti's theorem covers every exchangeable vector
Show the answer

A. It cannot be the beginning of an infinite exchangeable sequence: mixtures of i.i.d. sequences have nonnegative covariance

If it extended to an infinite exchangeable sequence, de Finetti would make it a mixture of i.i.d. sequences, whose covariance is Var⁡μ(p)≥0\Var_\mu(p) \ge 0. Sampling without replacement produces exactly such negatively correlated, non-extendable exchangeable vectors.

Further reading

  • A. Gelman, J. B. Carlin, H. S. Stern, D. B. Dunson, A. Vehtari & D. B. Rubin, Bayesian Data Analysis, 3rd ed. (CRC Press, 2013), chapters 2–3.
  • P. Diaconis & D. Ylvisaker, "Conjugate priors for exponential families," Annals of Statistics 7(2), 269–281 (1979).
  • B. de Finetti, "La prévision : ses lois logiques, ses sources subjectives," Annales de l'Institut Henri Poincaré 7(1), 1–68 (1937).
  • P. Diaconis & D. Freedman, "Finite exchangeable sequences," Annals of Probability 8(4), 745–764 (1980).
  • B. Efron & C. Morris, "Data analysis using Stein's estimator and its generalizations," Journal of the American Statistical Association 70(350), 311–319 (1975).