Skip to content

Sum Rule, Product Rule, and Bayes' Theorem

All of probability reduces to two rules: the sum rule (to remove a variable) and the product rule (to split a joint into pieces). From these follows Bayes’ theorem — the formula that inverts a likelihood into a posterior belief. Bayes is the engine of spam filters, medical diagnosis, and every Bayesian machine-learning method.

A real-life example: a scary-looking positive test

A test for a rare disease is “99% accurate,” and you test positive. Should you panic? Surprisingly, no — if the disease is rare, most positives are false positives. Bayes’ theorem combines the test result (the likelihood) with the disease’s rarity (the prior) to give the real chance you’re sick (the posterior), which can be far below the “99%” your intuition screams. This base-rate reasoning is Bayes in action, and it’s where human intuition famously fails.

The two rules

For random variables x,y\mathbf{x}, \mathbf{y} with joint p(x,y)p(\mathbf{x}, \mathbf{y}):

Sum rule (marginalization) — remove a variable by summing/integrating it out:

p(x)=yp(x,y)(discrete),p(x)=p(x,y)dy(continuous).p(\mathbf{x}) = \sum_{\mathbf{y}} p(\mathbf{x}, \mathbf{y}) \quad\text{(discrete)}, \qquad p(\mathbf{x}) = \int p(\mathbf{x}, \mathbf{y})\,d\mathbf{y} \quad\text{(continuous)}.

Product rule — factor a joint into a conditional times a marginal:

p(x,y)=p(yx)p(x).p(\mathbf{x}, \mathbf{y}) = p(\mathbf{y} \mid \mathbf{x})\,p(\mathbf{x}).

That’s it. Every other identity in probability is built from these two.

Bayes’ theorem

Since the product rule can factor either way (p(x,y)=p(yx)p(x)=p(xy)p(y)p(\mathbf{x},\mathbf{y}) = p(\mathbf{y}\mid\mathbf{x})p(\mathbf{x}) = p(\mathbf{x}\mid\mathbf{y})p(\mathbf{y})), rearranging gives Bayes’ theorem:

p(xy)posterior=p(yx)likelihood  p(x)priorp(y)evidence.\underbrace{p(\mathbf{x} \mid \mathbf{y})}_{\text{posterior}} = \frac{\overbrace{p(\mathbf{y} \mid \mathbf{x})}^{\text{likelihood}}\;\overbrace{p(\mathbf{x})}^{\text{prior}}}{\underbrace{p(\mathbf{y})}_{\text{evidence}}}.

The four pieces:

  • Prior p(x)p(\mathbf{x}) — what you believed before seeing data.
  • Likelihood p(yx)p(\mathbf{y} \mid \mathbf{x}) — how probable the data is under each hypothesis.
  • Posterior p(xy)p(\mathbf{x} \mid \mathbf{y}) — your updated belief after the data.
  • Evidence p(y)=p(yx)p(x)dxp(\mathbf{y}) = \int p(\mathbf{y}\mid\mathbf{x})p(\mathbf{x})\,d\mathbf{x} — a normalizer ensuring the posterior sums to 1.

See base rates in action

Here’s a population of 400 people. A disease affects 5% (blue). A test with 90% sensitivity and 90% specificity flags the ringed people as positive. Look at the ringed group: most are actually healthy (false positives), because healthy people vastly outnumber sick ones. The posterior P(sick+)P(\text{sick} \mid +) is only ~32%, not 90%:

sketch Base rates: why a positive test isn't a diagnosis p5.js
400 people; 5% are sick (blue). A 90%-accurate test rings the positives. Among the ringed (test-positive) group, most are healthy false positives — so P(sick | positive) is only ~32%, far below the test's 90% accuracy.

The test didn’t lie — it just can’t overcome how rare the disease is. That’s why doctors retest, and why ignoring the prior (the base rate) is a classic reasoning error.

The pieces, connected

diagram Diagram mermaid

NumPy

bayes.py
import numpy as np
 
# Disease base rate and test accuracy
prior_sick = 0.05           # P(sick)
sensitivity = 0.90          # P(+ | sick)
specificity = 0.90          # P(- | healthy)  ->  P(+ | healthy) = 0.10
 
# Evidence via the sum rule: P(+) = P(+|sick)P(sick) + P(+|healthy)P(healthy)
p_pos = sensitivity * prior_sick + (1 - specificity) * (1 - prior_sick)
 
# Bayes: posterior P(sick | +)
posterior = (sensitivity * prior_sick) / p_pos
print(f"P(+)              = {p_pos:.3f}")
print(f"P(sick | +)       = {posterior:.3f}")   # ~0.321
print(f"prior was {prior_sick} -> posterior {posterior:.2f}")
bayes.py
import numpy as np
 
# Disease base rate and test accuracy
prior_sick = 0.05           # P(sick)
sensitivity = 0.90          # P(+ | sick)
specificity = 0.90          # P(- | healthy)  ->  P(+ | healthy) = 0.10
 
# Evidence via the sum rule: P(+) = P(+|sick)P(sick) + P(+|healthy)P(healthy)
p_pos = sensitivity * prior_sick + (1 - specificity) * (1 - prior_sick)
 
# Bayes: posterior P(sick | +)
posterior = (sensitivity * prior_sick) / p_pos
print(f"P(+)              = {p_pos:.3f}")
print(f"P(sick | +)       = {posterior:.3f}")   # ~0.321
print(f"prior was {prior_sick} -> posterior {posterior:.2f}")
text
P(+)              = 0.140
P(sick | +)       = 0.321
prior was 0.05 -> posterior 0.32
text
P(+)              = 0.140
P(sick | +)       = 0.321
prior was 0.05 -> posterior 0.32

Why this matters for ML

  • Bayesian inference is Bayes’ theorem applied to model parameters: prior belief + data likelihood → posterior over parameters (Chapters 9, 11).
  • Naive Bayes classifiers apply Bayes with a conditional-independence assumption — still a strong spam-filter baseline.
  • The evidence p(y)p(\mathbf{y}) powers model selection (Bayesian model comparison), and its intractability motivates variational inference and MCMC.

🧪 Try It Yourself

Exercise 1 – The sum rule (marginalize)

Exercise 2 – Bayes’ theorem

Exercise 3 – Product rule

Recap

  • All of probability follows from the sum rule (marginalize out a variable) and the product rule (factor a joint).
  • Bayes’ theorem inverts a likelihood into a posterior: posterior ∝ likelihood × prior, normalized by the evidence.
  • Base rates matter: a positive test on a rare condition is mostly false positives — ignoring the prior is a classic error.

Next: numbers that summarize distributions, and when variables carry independent information — Summary Statistics and Independence.

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did