Sum Rule, Product Rule and Bayes' Theorem
Two rules generate all of probabilistic machine learning. The sum rule removes a variable you do not care about; the product rule factors a joint distribution into simpler pieces. Combined, they give Bayes' theorem, which turns "how likely is the data under each hypothesis" into "how likely is each hypothesis given the data".
The two rules
The product rule extends to a chain: . Language models are built on exactly this factorization: the probability of a sentence is a product of next-token probabilities.
Bayes' theorem
Equate the two forms of the product rule and divide:
The evidence is a normalizing constant computed with the sum rule. It is often the hard part, so many methods work with the unnormalized product .
The base-rate trap
A test for a rare condition (1% prevalence) has 95% sensitivity and a 5% false-positive rate. Given a positive test, what is the chance of the condition?
Only about 16%. The prior matters: most positives come from the large healthy group. The same logic explains why a 99%-accurate fraud model can still produce mostly false alarms.
import numpy as np
prior, sens, fpr = 0.01, 0.95, 0.05
post = sens * prior / (sens * prior + fpr * (1 - prior))
print(f"P(condition | positive) = {post:.4f}")
# check by simulation
rng = np.random.default_rng(0)
n = 1_000_000
sick = rng.uniform(size=n) < prior
positive = np.where(sick, rng.uniform(size=n) < sens, rng.uniform(size=n) < fpr)
print(f"simulated: {sick[positive].mean():.4f}")
# a second positive test (independent given the condition) updates again
post2 = sens * post / (sens * post + fpr * (1 - post))
print(f"after two positive tests: {post2:.4f}")P(condition | positive) = 0.1610
simulated: 0.1612
after two positive tests: 0.7848
Yesterday's posterior is today's prior: Bayes' theorem can be applied sequentially as data arrives.
A Bayesian update over a grid
The same computation works for a continuous parameter. Estimate a coin's bias from 7 heads in 10 flips, with a uniform prior, by evaluating prior times likelihood on a grid and normalizing:
import numpy as np
mu = np.linspace(0, 1, 1001)
prior = np.ones_like(mu)
heads, flips = 7, 10
likelihood = mu**heads * (1 - mu)**(flips - heads)
unnorm = likelihood * prior
dmu = mu[1] - mu[0]
posterior = unnorm / (unnorm.sum() * dmu) # sum rule gives the evidence
print("posterior mode:", round(float(mu[np.argmax(posterior)]), 4))
print("posterior mean:", round(float((mu * posterior).sum() * dmu), 4), " (exact 8/12 =", round(8 / 12, 4), ")")
print("P(mu > 0.5 | data):", round(float(posterior[mu > 0.5].sum() * dmu), 4))posterior mode: 0.7
posterior mean: 0.6667 (exact 8/12 = 0.6667 )
P(mu > 0.5 | data): 0.8861
In software
- Naive Bayes classifiers apply Bayes' theorem with a product of per-feature likelihoods (Naive Bayes).
- Spam filters, A/B-test dashboards and medical decision tools report posteriors, not likelihoods.
- Probabilistic programming libraries (PyMC, NumPyro, Stan) approximate posteriors when the evidence integral is intractable.
Exercises
Exercise 1: Marginalize
for is , , , . Find and .
Answer
; .
Solution
Sum rule: . Product rule: .
Exercise 2: Two boxes
Box A holds 3 red and 1 blue ball; box B holds 1 red and 3 blue. A box is chosen by a fair coin and a red ball is drawn. What is the probability it came from box A?
Answer
.
Solution
Exercise 3: Prevalence matters (code)
Repeat the test calculation with prevalence 10% instead of 1%.
Answer
About 0.679.
Solution
prior, sens, fpr = 0.10, 0.95, 0.05
print(round(sens * prior / (sens * prior + fpr * (1 - prior)), 4))0.6786
: with a less rare condition, a positive test is much more convincing.
Test yourself
In Bayes' theorem, is called…
- the prior
- the likelihood
- the posterior
- the evidence
The evidence is computed with…
- the product rule only
- the sum rule: integrating likelihood times prior over
- the maximum of the likelihood
- the derivative of the prior
A language model computes the probability of a sentence as a product of next-token probabilities. Which rule justifies this?
- The sum rule
- The chain (product) rule
- The law of large numbers
- Independence of all tokens