Bayes' Rule#
Bayes' rule tells us how to reverse a conditional probability — to go from to :
The denominator is the total probability of the data: 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 | |---|---|---| | | Prior | Belief about before seeing data | | | Likelihood | How probable the observed data is, assuming is true | | | Posterior | Updated belief about after seeing data | | | Evidence (marginal likelihood) | Overall probability of the data, averaging over all possible |
Bayes' rule is the optimal way to update beliefs when you receive new information. It follows directly from the definition of conditional probability:
The proportional form is all you need when comparing hypotheses — the denominator is the same normalising constant for every .
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 were true, how often would we see data like ?
- Posterior = the prior re-weighted by the likelihood. When the likelihood strongly favours a particular , the posterior concentrates there, even if the prior was diffuse.
Worked example — coin bias estimation. You have a coin that may be fair () with prior or biased toward heads () with prior . You flip it three times and observe H, H, T (two heads, one tail).
Likelihood of this sequence under each hypothesis:
Evidence: .
Posterior:
The data slightly favours , but the prior was strong for , 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 for , ignoring the base rate. Let us apply Bayes:
- Prior: .
- Likelihood: (true positive rate).
- False positive: (1% of healthy people test positive).
- Evidence: .
Now Bayes:
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 and then uses Bayes' rule to compute for prediction:
Since does not depend on , the classifier that maximises the posterior is:
This is the MAPMaximum A Posteriori (estimate) (Maximum A Posteriori) decision rule — pick the class that makes the observed features 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 () | Class B () | |---|---|---| | | 0.6 | 0.4 | | | 0.9 | 0.3 | | | 0.1 | 0.7 |
Given , which class does MAP predict?
(Class B). Even though Class A is more common (prior 0.6), the likelihood strongly favours B when , so the posterior shifts.
Given :
- Class A:
- Class B:
. Now the prior and likelihood both favour A.
Naive Bayes Mention#
The generative classification framework requires — the probability of the entire feature vector given the class. For high-dimensional , estimating this joint directly is infeasible. Naive Bayes makes the conditional independence assumption from Week 4:
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.
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#
In Bayes' rule P(θ | D) = P(D | θ) P(θ) / P(D), which term is the prior?
Browser lab#
Bar charts for a discrete Bayesian update: prior → unnormalised posterior → normalised posterior for three hypotheses.
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)]}")