Skip to main content
© 2026 ePowerAI. All rights reserved.
CoursesContact
eAI
CoursesContact
Week 5: Bayes' Rule and Generative Classification
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 05· Prob & Stats· Foundations22 min read

Week 5: Bayes' Rule and Generative Classification

✦Learning Outcomes
  • State and apply Bayes' rule in discrete settings
  • Name prior, likelihood, posterior, evidence
  • Compute a MAPMaximum A Posteriori (estimate) class label for a tiny feature table
  • Interpret base-rate neglect with a medical-test example
◆Prerequisites

Required: Weeks 1–4 (probability axioms, distributions, expectation, conditioning, independence).

Key: This week is the gateway to Week 9 (Bayesian estimation) and the probabilistic modeling approach used across RL and Generative Models.

Bayes' Rule#

Bayes' rule tells us how to reverse a conditional probability — to go from P(data∣hypothesis)P(\text{data} \mid \text{hypothesis})P(data∣hypothesis) to P(hypothesis∣data)P(\text{hypothesis} \mid \text{data})P(hypothesis∣data):

P(θ∣D)=P(D∣θ) P(θ)P(D).P(\theta \mid D) = \frac{P(D \mid \theta) \, P(\theta)}{P(D)}.P(θ∣D)=P(D)P(D∣θ)P(θ)​.

The denominator is the total probability of the data: P(D)=∑θ′P(D∣θ′)P(θ′)P(D) = \sum_{\theta'} P(D \mid \theta') P(\theta')P(D)=∑θ′​P(D∣θ′)P(θ′) for discrete hypotheses. In the continuous case the sum becomes an integral, but the structure is identical.

The four ingredients have standard names:

| Symbol | Name | Meaning | |---|---|---| | P(θ)P(\theta)P(θ) | Prior | Belief about θ\thetaθ before seeing data | | P(D∣θ)P(D \mid \theta)P(D∣θ) | Likelihood | How probable the observed data is, assuming θ\thetaθ is true | | P(θ∣D)P(\theta \mid D)P(θ∣D) | Posterior | Updated belief about θ\thetaθ after seeing data | | P(D)P(D)P(D) | Evidence (marginal likelihood) | Overall probability of the data, averaging over all possible θ\thetaθ |

Bayes' rule is the optimal way to update beliefs when you receive new information. It follows directly from the definition of conditional probability:

P(θ∣D)=P(θ,D)P(D)=P(D∣θ)P(θ)P(D).P(\theta \mid D) = \frac{P(\theta, D)}{P(D)} = \frac{P(D \mid \theta) P(\theta)}{P(D)}.P(θ∣D)=P(D)P(θ,D)​=P(D)P(D∣θ)P(θ)​.

The proportional form P(θ∣D)∝P(D∣θ) P(θ)P(\theta \mid D) \propto P(D \mid \theta) \, P(\theta)P(θ∣D)∝P(D∣θ)P(θ) is all you need when comparing hypotheses — the denominator P(D)P(D)P(D) is the same normalising constant for every θ\thetaθ.

Prior, Likelihood, Posterior#

Discipline with vocabulary. Bayes' rule is simple algebraically, but decades of confusion stem from sloppy naming. Be precise:

  • Prior = your model of the world before this experiment. It can be uniform (maximal uncertainty), informed by previous experiments, or subjective.
  • Likelihood = the forward model — if θ\thetaθ were true, how often would we see data like DDD?
  • Posterior = the prior re-weighted by the likelihood. When the likelihood strongly favours a particular θ\thetaθ, the posterior concentrates there, even if the prior was diffuse.

Worked example — coin bias estimation. You have a coin that may be fair (θ=0.5\theta = 0.5θ=0.5) with prior P(θ=0.5)=0.7P(\theta = 0.5) = 0.7P(θ=0.5)=0.7 or biased toward heads (θ=0.8\theta = 0.8θ=0.8) with prior P(θ=0.8)=0.3P(\theta = 0.8) = 0.3P(θ=0.8)=0.3. You flip it three times and observe H, H, T (two heads, one tail).

Likelihood of this sequence under each hypothesis:

P(D∣θ=0.5)=(0.5)2(0.5)1=0.125,P(D∣θ=0.8)=(0.8)2(0.2)1=0.128.P(D \mid \theta = 0.5) = (0.5)^2 (0.5)^1 = 0.125, \quad P(D \mid \theta = 0.8) = (0.8)^2 (0.2)^1 = 0.128.P(D∣θ=0.5)=(0.5)2(0.5)1=0.125,P(D∣θ=0.8)=(0.8)2(0.2)1=0.128.

Evidence: P(D)=0.125⋅0.7+0.128⋅0.3=0.0875+0.0384=0.1259P(D) = 0.125 \cdot 0.7 + 0.128 \cdot 0.3 = 0.0875 + 0.0384 = 0.1259P(D)=0.125⋅0.7+0.128⋅0.3=0.0875+0.0384=0.1259.

Posterior:

P(θ=0.5∣D)=0.125⋅0.70.1259≈0.695,P(θ=0.8∣D)=0.128⋅0.30.1259≈0.305.P(\theta = 0.5 \mid D) = \frac{0.125 \cdot 0.7}{0.1259} \approx 0.695, \quad P(\theta = 0.8 \mid D) = \frac{0.128 \cdot 0.3}{0.1259} \approx 0.305.P(θ=0.5∣D)=0.12590.125⋅0.7​≈0.695,P(θ=0.8∣D)=0.12590.128⋅0.3​≈0.305.

The data slightly favours θ=0.8\theta = 0.8θ=0.8, but the prior was strong for θ=0.5\theta = 0.5θ=0.5, so the posterior remains tilted toward the fair coin. With more flips, the likelihood eventually dominates.

Worked Medical Test — Base-Rate Neglect#

A disease affects 1 in 1000 people (prior = 0.001). A test is 99% accurate: it returns positive for 99% of diseased people and negative for 99% of healthy people.

A patient tests positive. What is the probability they actually have the disease?

Confusion arises because people substitute P(positive∣disease)=0.99P(\text{positive} \mid \text{disease}) = 0.99P(positive∣disease)=0.99 for P(disease∣positive)P(\text{disease} \mid \text{positive})P(disease∣positive), ignoring the base rate. Let us apply Bayes:

  • Prior: P(D)=0.001P(D) = 0.001P(D)=0.001.
  • Likelihood: P(+∣D)=0.99P(+ \mid D) = 0.99P(+∣D)=0.99 (true positive rate).
  • False positive: P(+∣¬D)=0.01P(+ \mid \neg D) = 0.01P(+∣¬D)=0.01 (1% of healthy people test positive).
  • Evidence: P(+)=P(+∣D)P(D)+P(+∣¬D)P(¬D)=0.99⋅0.001+0.01⋅0.999=0.00099+0.00999=0.01098P(+) = P(+ \mid D)P(D) + P(+ \mid \neg D)P(\neg D) = 0.99 \cdot 0.001 + 0.01 \cdot 0.999 = 0.00099 + 0.00999 = 0.01098P(+)=P(+∣D)P(D)+P(+∣¬D)P(¬D)=0.99⋅0.001+0.01⋅0.999=0.00099+0.00999=0.01098.

Now Bayes:

P(D∣+)=0.99⋅0.0010.01098=0.000990.01098≈0.090.P(D \mid +) = \frac{0.99 \cdot 0.001}{0.01098} = \frac{0.00099}{0.01098} \approx 0.090.P(D∣+)=0.010980.99⋅0.001​=0.010980.00099​≈0.090.

Despite a "99% accurate" test, a positive result implies only about a 9% chance of actually having the disease. Why? Because the disease is so rare that even the small false-positive rate (1%) swamps the true positives. This is base-rate neglect — a pervasive reasoning error in medicine, law, and data science. Always ask: how rare is the thing I am testing for?

Generative Classification Sketch#

A generative classifier models the joint distribution P(x,y)P(x, y)P(x,y) and then uses Bayes' rule to compute P(y∣x)P(y \mid x)P(y∣x) for prediction:

P(y∣x)=P(x∣y) P(y)P(x).P(y \mid x) = \frac{P(x \mid y) \, P(y)}{P(x)}.P(y∣x)=P(x)P(x∣y)P(y)​.

Since P(x)P(x)P(x) does not depend on yyy, the classifier that maximises the posterior is:

y^=arg⁡max⁡yP(x∣y) P(y).\hat y = \arg\max_y P(x \mid y) \, P(y).y^​=argymax​P(x∣y)P(y).

This is the MAPMaximum A Posteriori (estimate) (Maximum A Posteriori) decision rule — pick the class yyy that makes the observed features xxx most probable, weighted by how common each class is a priori.

Worked example — tiny classifier. Suppose we have two classes and one binary feature:

| | Class A (y=0y = 0y=0) | Class B (y=1y = 1y=1) | |---|---|---| | P(y)P(y)P(y) | 0.6 | 0.4 | | P(x=0∣y)P(x=0 \mid y)P(x=0∣y) | 0.9 | 0.3 | | P(x=1∣y)P(x=1 \mid y)P(x=1∣y) | 0.1 | 0.7 |

Given x=1x = 1x=1, which class does MAP predict?

  • P(x=1∣y=0)⋅P(y=0)=0.1⋅0.6=0.06P(x=1 \mid y=0) \cdot P(y=0) = 0.1 \cdot 0.6 = 0.06P(x=1∣y=0)⋅P(y=0)=0.1⋅0.6=0.06
  • P(x=1∣y=1)⋅P(y=1)=0.7⋅0.4=0.28P(x=1 \mid y=1) \cdot P(y=1) = 0.7 \cdot 0.4 = 0.28P(x=1∣y=1)⋅P(y=1)=0.7⋅0.4=0.28

y^=1\hat y = 1y^​=1 (Class B). Even though Class A is more common (prior 0.6), the likelihood strongly favours B when x=1x = 1x=1, so the posterior shifts.

Given x=0x = 0x=0:

  • Class A: 0.9⋅0.6=0.540.9 \cdot 0.6 = 0.540.9⋅0.6=0.54
  • Class B: 0.3⋅0.4=0.120.3 \cdot 0.4 = 0.120.3⋅0.4=0.12

y^=0\hat y = 0y^​=0. Now the prior and likelihood both favour A.

Naive Bayes Mention#

The generative classification framework requires P(x∣y)P(x \mid y)P(x∣y) — the probability of the entire feature vector given the class. For high-dimensional xxx, estimating this joint directly is infeasible. Naive Bayes makes the conditional independence assumption from Week 4:

P(x∣y)=∏j=1dP(xj∣y).P(x \mid y) = \prod_{j=1}^d P(x_j \mid y).P(x∣y)=j=1∏d​P(xj​∣y).

Each feature is modelled independently given the class. This is almost always false, yet it works well: even with wrong probability estimates, the ranking of classes often remains correct. Naive Bayes is the simplest generative classifier and is widely used for text classification (spam detection, sentiment analysis) as a fast, interpretable baseline.

Exercise · Fill in the blank

Prior P(H)=0.2, likelihood P(D | H)=0.6, P(D | not H)=0.1. Using Bayes, compute P(H | D) rounded to 3 decimal places.


Knowledge check#

Question 1 of 4

In Bayes' rule P(θ | D) = P(D | θ) P(θ) / P(D), which term is the prior?

P(θ)
P(D | θ)
P(θ | D)
P(D)

Browser lab#

Bar charts for a discrete Bayesian update: prior → unnormalised posterior → normalised posterior for three hypotheses.

python · runs in browser
import numpy as np
import matplotlib.pyplot as plt

hypotheses = ["θ=0.3", "θ=0.5", "θ=0.7"]
prior = np.array([0.3, 0.5, 0.2])
theta_vals = np.array([0.3, 0.5, 0.7])

# Observe 7 heads in 10 flips
n, k = 10, 7
likelihood = theta_vals**k * (1 - theta_vals)**(n - k)

unnorm_posterior = prior * likelihood
evidence = unnorm_posterior.sum()
posterior = unnorm_posterior / evidence

fig, axes = plt.subplots(1, 3, figsize=(12, 4))
colors = ["tab:blue", "tab:orange", "tab:green"]
for ax, vals, title in zip(axes,
    [prior, unnorm_posterior, posterior],
    ["Prior", f"Prior × Likelihood\n(unnormalised)", "Posterior\n(normalised)"]):
    ax.bar(hypotheses, vals, color=colors, alpha=0.8)
    for i, v in enumerate(vals):
        ax.text(i, v + max(vals)*0.02, f"{v:.3f}", ha="center", fontsize=9)
    ax.set_ylim(0, max(vals) * 1.15)
    ax.set_title(title)

fig.suptitle(f"Bayesian Update — {k} heads in {n} flips", fontsize=14)
plt.tight_layout()
plt.show()

print(f"Evidence P(D) = {evidence:.4f}")
print("Posterior:", dict(zip(hypotheses, np.round(posterior, 4))))
print(f"MAP estimate: θ = {theta_vals[np.argmax(posterior)]}")
← Previous
Week 4: Conditioning and Independence
Next →
Week 6: Multivariate Gaussians
On this page
  • Bayes' Rule
  • Prior, Likelihood, Posterior
  • Worked Medical Test — Base-Rate Neglect
  • Generative Classification Sketch
  • Naive Bayes Mention
  • Knowledge check
  • Browser lab