Skip to main content
© 2026 ePowerAI. All rights reserved.
CoursesContact
eAI
CoursesContact
Week 9: Bayesian Estimation and MAP
Prob & Stats
01Week 1: Probability Spaces and Events
02Week 2: Random Variables and Distributions
03Week 3: Expectation, Variance, and Covariance
04Week 4: Conditioning and Independence
05Week 5: Bayes' Rule and Generative Classification
06Week 6: Multivariate Gaussians
07Week 7: Laws of Large Numbers and the CLT
08Week 8: Point Estimation — Maximum Likelihood
09Week 9: Bayesian Estimation and MAP
10Week 10: Bias, Variance, and Uncertainty
11Week 11: Information Theory for ML
12Week 12: Exponential Families (lite)
13Week 13: Graphical Models (lite)
14Week 14: Capstone — Probabilistic Language of ML
Week 09· Prob & Stats· Inference22 min read

Week 9: Bayesian Estimation and MAP

✦Learning Outcomes
  • Write posterior ∝\propto∝ likelihood ×\times× prior
  • Compute MAPMaximum A Posteriori (estimate) for a discrete or Beta–Binomial example
  • Contrast MAP and MLE (prior influence)
  • State conjugate prior intuition (Beta–Binomial; Normal mean with known variance)
◆Prerequisites

Required: Week 5 (Bayes' rule), Week 8 (MLE). You should be comfortable computing posteriors from priors and likelihoods.

Later use: Bayesian methods directly inform modern ML — from uncertainty quantification in neural networks to the ELBO in variational inference (mentioned in Week 11 and Generative Models).

Parameters as Random#

Classical (frequentist) statistics treats the unknown parameter θ\thetaθ as a fixed but unknown constant. An estimator θ^\hat \thetaθ^ is judged by its behaviour over repeated sampling.

Bayesian statistics treats θ\thetaθ as a random variable. Before seeing data, you encode your uncertainty about θ\thetaθ in a prior distribution p(θ)p(\theta)p(θ). After observing data DDD, you update to the posterior:

p(θ∣D)=p(D∣θ) p(θ)p(D)∝p(D∣θ) p(θ).p(\theta \mid D) = \frac{p(D \mid \theta) \, p(\theta)}{p(D)} \propto p(D \mid \theta) \, p(\theta).p(θ∣D)=p(D)p(D∣θ)p(θ)​∝p(D∣θ)p(θ).

The Bayesian perspective is appealing in ML:

  • Uncertainty is explicit: you get a full distribution over parameters, not a point estimate.
  • Prior knowledge can be encoded: regularisation in optimisation is often equivalent to a Bayesian prior (e.g., L2 weight decay ↔\leftrightarrow↔ Gaussian prior).
  • Sequential updates are natural: today's posterior becomes tomorrow's prior.

The cost: you must specify a prior (where does it come from?), and computing the posterior often requires high-dimensional integration (MCMC, variational inference — beyond this course).

MAP Estimation#

The Maximum A Posteriori (MAPMaximum A Posteriori (estimate)) estimate is the mode of the posterior:

θ^MAP=arg⁡max⁡θp(θ∣D)=arg⁡max⁡θp(D∣θ) p(θ).\hat \theta_{\text{MAP}} = \arg\max_\theta p(\theta \mid D) = \arg\max_\theta p(D \mid \theta) \, p(\theta).θ^MAP​=argθmax​p(θ∣D)=argθmax​p(D∣θ)p(θ).

Since p(D)p(D)p(D) is constant in θ\thetaθ, MAP maximises likelihood ×\times× prior. Compared to MLE:

  • MAP = MLE when the prior is uniform (all θ\thetaθ equally plausible a priori).
  • MAP shrinks toward the prior when data are scarce; the influence of the prior diminishes as nnn grows.
  • MAP is a point estimate — it gives you one θ\thetaθ, not a full distribution. The posterior may be multimodal (multiple plausible parameter values); MAP picks one mode and ignores the rest.

MAP is widely used in practice because it adds a regularisation interpretation while retaining the optimisation framework (just add log⁡p(θ)\log p(\theta)logp(θ) to the objective).

Beta–Binomial#

The classic conjugate pair for a probability parameter. Suppose we model a coin's bias p∈[0,1]p \in [0, 1]p∈[0,1]:

  • Prior: p∼Beta(α,β)p \sim \text{Beta}(\alpha, \beta)p∼Beta(α,β) with density f(p)∝pα−1(1−p)β−1f(p) \propto p^{\alpha-1} (1-p)^{\beta-1}f(p)∝pα−1(1−p)β−1 for p∈[0,1]p \in [0, 1]p∈[0,1].
  • Likelihood: Observe kkk heads in nnn flips: L(p)∝pk(1−p)n−kL(p) \propto p^k (1-p)^{n-k}L(p)∝pk(1−p)n−k.
  • Posterior: p∣data∼Beta(α+k,  β+n−k)p \mid \text{data} \sim \text{Beta}(\alpha + k,\; \beta + n - k)p∣data∼Beta(α+k,β+n−k).

The Beta prior is conjugate to the Bernoulli/Binomial likelihood — the posterior is also Beta. The hyperparameters act as pseudo-counts: α−1\alpha - 1α−1 is like having seen α−1\alpha - 1α−1 imaginary heads, and β−1\beta - 1β−1 imaginary tails, before the experiment.

Worked example. Prior: Beta(2,2)\text{Beta}(2, 2)Beta(2,2) (mild belief that the coin is roughly fair — equivalent to having seen 1 head and 1 tail). Observe k=7k = 7k=7 heads in n=10n = 10n=10 flips.

Posterior: Beta(2+7,  2+3)=Beta(9,5)\text{Beta}(2 + 7,\; 2 + 3) = \text{Beta}(9, 5)Beta(2+7,2+3)=Beta(9,5).

The posterior mean is α+kα+β+n=914≈0.643\frac{\alpha + k}{\alpha + \beta + n} = \frac{9}{14} \approx 0.643α+β+nα+k​=149​≈0.643, and the MAP is α+k−1α+β+n−2=812≈0.667\frac{\alpha + k - 1}{\alpha + \beta + n - 2} = \frac{8}{12} \approx 0.667α+β+n−2α+k−1​=128​≈0.667.

Compare to MLE: p^MLE=7/10=0.7\hat p_{\text{MLE}} = 7/10 = 0.7p^​MLE​=7/10=0.7. The Beta(2,2)(2,2)(2,2) prior pulls the estimate slightly toward 0.5. With n=10n = 10n=10 the prior is still visible; with n=1000n = 1000n=1000 heads and 500500500 tails, the prior would be negligible.

Beta distribution shapes:

  • Beta(1,1)\text{Beta}(1, 1)Beta(1,1) = Uniform(0,1)(0, 1)(0,1) — no prior information.
  • Beta(0.5,0.5)\text{Beta}(0.5, 0.5)Beta(0.5,0.5): U-shaped, favours extremes (Jeffreys prior).
  • Beta(α>1,β>1)\text{Beta}(\alpha > 1, \beta > 1)Beta(α>1,β>1): unimodal, centred near α/(α+β)\alpha/(\alpha+\beta)α/(α+β).

Normal Mean with Known Variance#

The second classic conjugate pair. Assume Xi∼N(μ,σ2)X_i \sim \mathcal{N}(\mu, \sigma^2)Xi​∼N(μ,σ2) with σ2\sigma^2σ2 known.

  • Prior: μ∼N(μ0,τ02)\mu \sim \mathcal{N}(\mu_0, \tau_0^2)μ∼N(μ0​,τ02​).
  • Likelihood: xˉ∼N(μ,σ2/n)\bar x \sim \mathcal{N}(\mu, \sigma^2 / n)xˉ∼N(μ,σ2/n) (sufficient statistic).
  • Posterior: μ∣data∼N(μn,τn2)\mu \mid \text{data} \sim \mathcal{N}(\mu_n, \tau_n^2)μ∣data∼N(μn​,τn2​), where
μn=1τ02μ0+nσ2xˉ1τ02+nσ2,1τn2=1τ02+nσ2.\mu_n = \frac{\frac{1}{\tau_0^2}\mu_0 + \frac{n}{\sigma^2}\bar x}{\frac{1}{\tau_0^2} + \frac{n}{\sigma^2}}, \qquad \frac{1}{\tau_n^2} = \frac{1}{\tau_0^2} + \frac{n}{\sigma^2}.μn​=τ02​1​+σ2n​τ02​1​μ0​+σ2n​xˉ​,τn2​1​=τ02​1​+σ2n​.

The posterior mean is a precision-weighted average of the prior mean and the sample mean. Precisions (1/variance1/\text{variance}1/variance) add: posterior precision = prior precision + data precision.

  • If the prior is very tight (τ02\tau_0^2τ02​ small), the posterior mean stays near μ0\mu_0μ0​ unless the data are overwhelming.
  • If the prior is diffuse (τ02\tau_0^2τ02​ large), the posterior mean approaches xˉ\bar xxˉ and the posterior variance approaches σ2/n\sigma^2 / nσ2/n.

Posterior Predictive (lite)#

The posterior predictive distribution for a new observation x~\tilde xx~ averages the sampling distribution over the posterior:

p(x~∣D)=∫p(x~∣θ) p(θ∣D) dθ.p(\tilde x \mid D) = \int p(\tilde x \mid \theta) \, p(\theta \mid D) \, d\theta.p(x~∣D)=∫p(x~∣θ)p(θ∣D)dθ.

This is not p(x~∣θ^)p(\tilde x \mid \hat \theta)p(x~∣θ^) — plugging in a point estimate ignores parameter uncertainty. For the Beta–Binomial, the posterior predictive probability of heads on the next flip is the posterior mean:

P(X~=1∣D)=α+kα+β+n.P(\tilde X = 1 \mid D) = \frac{\alpha + k}{\alpha + \beta + n}.P(X~=1∣D)=α+β+nα+k​.

This is a natural "smoothed" estimate that accounts for both the data and prior uncertainty.

Exercise · Multiple choice

When does MAP equal MLE?

When the prior is uniform
When there is no data
When the likelihood is Gaussian
MAP never equals MLE

Knowledge check#

Question 1 of 4

Beta(2, 2) prior + 7 heads in 10 flips. What is the posterior distribution?

Beta(9, 5)
Beta(7, 3)
Beta(2, 2)
Beta(5, 5)

Browser lab#

Beta–Binomial posterior density for different priors given the same dataset.

python · runs in browser
import numpy as np
import matplotlib.pyplot as plt
from math import gamma as math_gamma

def beta_pdf(x, a, b):
    """Beta(a, b) density at points x (scalar or array)."""
    x = np.asarray(x, dtype=float)
    log_pdf = (a - 1) * np.log(x) + (b - 1) * np.log(1 - x)
    log_pdf -= np.log(math_gamma(a)) + np.log(math_gamma(b)) - np.log(math_gamma(a + b))
    valid = (x > 0) & (x < 1)
    result = np.zeros_like(x)
    result[valid] = np.exp(log_pdf[valid])
    return result

n, k = 10, 7

prior_configs = [
    ("Beta(1,1) — Uniform", 1, 1, "tab:blue"),
    ("Beta(2,2) — mild fairness", 2, 2, "tab:orange"),
    ("Beta(5,2) — favour heads", 5, 2, "tab:green"),
    ("Beta(0.5,0.5) — Jeffreys", 0.5, 0.5, "tab:red"),
]

p_grid = np.linspace(0.001, 0.999, 300)

fig, axes = plt.subplots(1, 2, figsize=(12, 5))

for label, a, b, color in prior_configs:
    prior_pdf = beta_pdf(p_grid, a, b)
    axes[0].plot(p_grid, prior_pdf, color=color, linewidth=2, label=label)

axes[0].set_title("Priors")
axes[0].set_xlabel("p"); axes[0].set_ylabel("Density")
axes[0].legend(fontsize=8)

for label, a, b, color in prior_configs:
    post_a, post_b = a + k, b + (n - k)
    post_pdf = beta_pdf(p_grid, post_a, post_b)
    axes[1].plot(p_grid, post_pdf, color=color, linewidth=2, label=label)
    mle = k / n
    map_est = (post_a - 1) / (post_a + post_b - 2) if post_a > 1 and post_b > 1 else np.nan
    axes[1].axvline(mle, color="black", linestyle=":", alpha=0.5)
    if not np.isnan(map_est):
        axes[1].scatter([map_est], [beta_pdf(map_est, post_a, post_b)],
                        color=color, s=50, zorder=5, marker="D")

axes[1].axvline(mle, color="black", linestyle=":", alpha=0.5, label=f"MLE={mle:.2f}")
axes[1].set_title(f"Posteriors — {k} heads / {n} flips")
axes[1].set_xlabel("p"); axes[1].set_ylabel("Density")
axes[1].legend(fontsize=8)

plt.tight_layout()
plt.show()

for label, a, b, _ in prior_configs:
    post_a, post_b = a + k, b + n - k
    post_mean = post_a / (post_a + post_b)
    print(f"{label:30s} posterior: Beta({post_a},{post_b})  mean={post_mean:.3f}")
← Previous
Week 8: Point Estimation — Maximum Likelihood
Next →
Week 10: Bias, Variance, and Uncertainty
On this page
  • Parameters as Random
  • MAP Estimation
  • Beta–Binomial
  • Normal Mean with Known Variance
  • Posterior Predictive (lite)
  • Knowledge check
  • Browser lab