AI Knowledge Tree

MAP estimation and conjugate priors

Add a prior, compute the posterior, and use conjugacy to keep it in closed form.

Maximum likelihood trusts the data alone. When data are scarce or you hold prior belief about a parameter, Bayesian inference combines both: it treats the parameter as random, places a prior on it, and updates to a posterior[1]. Conjugate priors keep that update in closed form, and MAP estimation reads off its peak.

The posterior over parameters

Treat the parameter θ\theta as a random variable with prior density p(θ)p(\theta). Bayes' rule gives the posterior after observing data D\mathcal{D}:

p(θ∣D)=p(D∣θ) p(θ)p(D)∝p(D∣θ)⏟likelihood p(θ)⏟prior.p(\theta \mid \mathcal{D}) = \frac{p(\mathcal{D} \mid \theta)\, p(\theta)}{p(\mathcal{D})} \propto \underbrace{p(\mathcal{D} \mid \theta)}_{\text{likelihood}}\,\underbrace{p(\theta)}_{\text{prior}}.

The evidence p(D)=∫p(D∣θ)p(θ) dθp(\mathcal{D}) = \int p(\mathcal{D} \mid \theta)p(\theta)\,d\theta is a constant in θ\theta, so for finding the shape (and the peak) of the posterior it can be ignored: posterior ∝\propto likelihood ×\times prior.

MAP estimation

The maximum a posteriori (MAP) estimate is the posterior mode:

θ^MAP=arg⁡max⁡θ  [log⁡p(D∣θ)+log⁡p(θ)].\hat\theta_{\text{MAP}} = \arg\max_\theta\; \big[\log p(\mathcal{D} \mid \theta) + \log p(\theta)\big].

It is maximum likelihood plus an additive prior term. With a Gaussian prior on weights, log⁡p(θ)=−12τ2∥θ∥2+const\log p(\theta) = -\frac{1}{2\tau^2}\lVert\theta\rVert^2 + \text{const}, and MAP becomes likelihood maximization with an L2 (ridge) penalty. A Laplace prior gives an L1 penalty. Regularization is a prior in disguise.

As data accumulate, the likelihood term grows with nn while the prior term stays fixed, so the MAP estimate converges to the MLE: the prior washes out.

Conjugate priors

A prior is conjugate to a likelihood if the posterior belongs to the same family as the prior[2]. Then updating only changes the family's parameters, with no integration required.

Beta-Bernoulli. For nn Bernoulli(qq) trials with kk successes, the likelihood is qk(1−q)n−kq^k(1-q)^{n-k}. The conjugate prior is the Beta distribution p(q)∝qα−1(1−q)β−1p(q) \propto q^{\alpha-1}(1-q)^{\beta-1}. Multiplying,

p(q∣D)∝qk(1−q)n−k qα−1(1−q)β−1=q(α+k)−1(1−q)(β+n−k)−1,p(q \mid \mathcal{D}) \propto q^{k}(1-q)^{n-k}\, q^{\alpha-1}(1-q)^{\beta-1} = q^{(\alpha + k) - 1}(1-q)^{(\beta + n - k) - 1},

which is Beta(α+k, β+n−k)\text{Beta}(\alpha + k,\ \beta + n - k). The update just adds successes to α\alpha and failures to β\beta. The prior parameters act as pseudo-counts: prior belief stated as "as if I had already seen α\alpha successes and β\beta failures".

The posterior mean and mode are

E[q∣D]=α+kα+β+n,q^MAP=α+k−1α+β+n−2.\mathbb{E}[q \mid \mathcal{D}] = \frac{\alpha + k}{\alpha + \beta + n}, \qquad \hat q_{\text{MAP}} = \frac{\alpha + k - 1}{\alpha + \beta + n - 2}.

The MAP mode reduces to the MLE k/nk/n when α=β=1\alpha = \beta = 1 (a flat prior).

MAP estimation returns the posterior mode, which sits between the data's MLE and the prior mean; the prior pulls the estimate toward 0.5.
MAP estimation returns the posterior mode, which sits between the data's MLE and the prior mean; the prior pulls the estimate toward 0.5.

Check it on arrays

python · numpy
import numpy as np

rng = np.random.default_rng(12)

# True coin bias, small sample so the prior matters
true_q = 0.7
n = 20
data = (rng.random(n) < true_q).astype(int)
k = data.sum()
print(f"observed {k} heads in {n} flips; MLE = {k/n:.3f}")

# Beta(2,2) prior (mild belief near 0.5), conjugate update
alpha0, beta0 = 2.0, 2.0
alpha_post, beta_post = alpha0 + k, beta0 + (n - k)
post_mean = alpha_post / (alpha_post + beta_post)
post_mode = (alpha_post - 1) / (alpha_post + beta_post - 2)
print(f"posterior Beta({alpha_post}, {beta_post})")
print(f"posterior mean {post_mean:.3f}, MAP mode {post_mode:.3f}")

# Verify conjugacy numerically: posterior ∝ likelihood * prior on a grid
q = np.linspace(1e-3, 1 - 1e-3, 5000)
dq = q[1] - q[0]
prior = q**(alpha0 - 1) * (1 - q)**(beta0 - 1)
like = q**k * (1 - q)**(n - k)
post = prior * like
post /= post.sum() * dq                          # normalize (Riemann sum)
beta_pdf = q**(alpha_post - 1) * (1 - q)**(beta_post - 1)
beta_pdf /= beta_pdf.sum() * dq
print("max |grid posterior - Beta pdf|:", round(np.max(np.abs(post - beta_pdf)), 6))
print("grid MAP:", round(q[np.argmax(post)], 3))

The conjugate update gives a Beta posterior whose density matches the likelihood-times-prior computed on a grid, and the MAP mode pulls the estimate toward the prior on this small sample.

Exercises

Exercise 1. Start from a Beta(2,2)\text{Beta}(2, 2) prior on a coin's bias qq. You observe 88 heads in 1010 flips. Give the posterior distribution and compute its mean and MAP mode.

Solution

Step 1. The Beta-Bernoulli update adds successes to α\alpha and failures to β\beta. With α=2\alpha = 2, β=2\beta = 2, k=8k = 8, n−k=2n - k = 2:

p(q∣D)=Beta(α+k, β+n−k)=Beta(10, 4).p(q \mid \mathcal{D}) = \text{Beta}(\alpha + k,\ \beta + n - k) = \text{Beta}(10,\ 4).

Step 2. Posterior mean α+kα+β+n=1014=57≈0.714\frac{\alpha + k}{\alpha + \beta + n} = \frac{10}{14} = \frac{5}{7} \approx 0.714.

Step 3. MAP mode α+k−1α+β+n−2=912=0.75\frac{\alpha + k - 1}{\alpha + \beta + n - 2} = \frac{9}{12} = 0.75. Both sit below the MLE k/n=0.8k/n = 0.8, pulled toward the prior mean 0.50.5.

Exercise 2. Show that with a flat Beta(1,1)\text{Beta}(1, 1) prior, the MAP mode equals the MLE k/nk/n, and explain why this is the prior that adds no information.

Solution

Step 1. The MAP mode formula is q^MAP=α+k−1α+β+n−2\hat q_{\text{MAP}} = \frac{\alpha + k - 1}{\alpha + \beta + n - 2}. Substitute α=β=1\alpha = \beta = 1:

q^MAP=1+k−11+1+n−2=kn,\hat q_{\text{MAP}} = \frac{1 + k - 1}{1 + 1 + n - 2} = \frac{k}{n},

which is the MLE.

Step 2. The prior parameters act as pseudo-counts: Beta(1,1)\text{Beta}(1, 1) adds α−1=0\alpha - 1 = 0 prior successes and β−1=0\beta - 1 = 0 prior failures. With zero pseudo-counts the prior contributes nothing to the mode, and Beta(1,1)\text{Beta}(1, 1) is the uniform density on [0,1][0, 1], flat over all biases.

Exercise 3 (code). Confirm the conjugate update numerically. Compute the normalized likelihood-times-prior on a grid for the Exercise 1 setting, and check that its mode matches the closed-form MAP 0.750.75 and its mean matches 5/75/7.

Solution
python · numpy
import numpy as np

alpha0, beta0 = 2.0, 2.0
k, n = 8, 10

q = np.linspace(1e-4, 1 - 1e-4, 200_000)
dq = q[1] - q[0]
prior = q**(alpha0 - 1) * (1 - q)**(beta0 - 1)
like = q**k * (1 - q)**(n - k)
post = prior * like
post /= post.sum() * dq                    # normalize (Riemann sum)

grid_mode = q[np.argmax(post)]
grid_mean = (q * post).sum() * dq

assert np.isclose(grid_mode, 0.75, atol=1e-3)
assert np.isclose(grid_mean, 5 / 7, atol=1e-3)
print("grid MAP:", round(grid_mode, 4), " grid mean:", round(grid_mean, 4))

The grid posterior peaks at 0.750.75 and averages to 5/7≈0.71435/7 \approx 0.7143, matching the closed-form Beta(1010, 44) mode and mean.

The next lesson shifts from estimating distributions to quantifying their information content, starting with entropy.


References

  1. [1]Bishop, C. M. (2006). Pattern Recognition and Machine Learning. Springer. §1.2.5-1.2.6, MAP estimation and Bayesian curve fitting.↩
  2. [2]Murphy, K. P. (2022). Probabilistic Machine Learning: An Introduction. MIT Press. §4.6, conjugate priors and the Beta-Bernoulli model.↩