TExcelTAD
Home › Probability Theory › Bayes' Theorem

Bayes' Theorem

Bayes' theorem reverses a conditional probability. Tests and classifiers tell us Pr⁡[evidence∣cause]\Pr[\text{evidence} \mid \text{cause}]; we want Pr⁡[cause∣evidence]\Pr[\text{cause} \mid \text{evidence}]. The theorem connects the two through the base rate of the cause, which intuition ignores far too often.

The formula

For events HH (a hypothesis) and EE (evidence):

Pr⁡[H∣E]=Pr⁡[E∣H] Pr⁡[H]Pr⁡[E]=Pr⁡[E∣H] Pr⁡[H]Pr⁡[E∣H] Pr⁡[H]+Pr⁡[E∣Hˉ] Pr⁡[Hˉ]\Pr[H \mid E] = \frac{\Pr[E \mid H] \, \Pr[H]}{\Pr[E]} = \frac{\Pr[E \mid H] \, \Pr[H]}{\Pr[E \mid H] \, \Pr[H] + \Pr[E \mid \bar{H}] \, \Pr[\bar{H}]}

The denominator is the law of total probability. In words: posterior ∝\propto likelihood ×\times prior.

The medical test

A disease affects 1 in 1000 people. A test detects it 99% of the time (sensitivity) and gives a false positive 2% of the time. You test positive. How likely are you to have the disease?

Pr⁡[D∣+]=0.99⋅0.0010.99⋅0.001+0.02⋅0.999=0.000990.02097≈0.047\Pr[D \mid +] = \frac{0.99 \cdot 0.001}{0.99 \cdot 0.001 + 0.02 \cdot 0.999} = \frac{0.00099}{0.02097} \approx 0.047

Under 5%. Most positives are false positives because healthy people vastly outnumber sick ones. A table of counts for 100,000 people makes it obvious:

python · try it yourself
N = 100_000
prior, sens, fpr = 0.001, 0.99, 0.02
sick = N * prior
healthy = N - sick
tp, fn = sick * sens, sick * (1 - sens)
fp, tn = healthy * fpr, healthy * (1 - fpr)
print(f"              positive   negative")
print(f"sick       {tp:10.0f} {fn:10.0f}")
print(f"healthy    {fp:10.0f} {tn:10.0f}")
print("P(sick | positive) =", round(tp / (tp + fp), 4))
print("P(healthy | negative) =", round(tn / (tn + fn), 6))
              positive   negative
sick               99          1
healthy          1998      97902
P(sick | positive) = 0.0472
P(healthy | negative) = 0.99999

Updating step by step

Bayes' theorem can be applied repeatedly: today's posterior is tomorrow's prior. If a second, independent test is also positive, start from 0.0470.047:

python · try it yourself
def update(prior, sens=0.99, fpr=0.02, positive=True):
    like_h = sens if positive else 1 - sens
    like_not = fpr if positive else 1 - fpr
    return like_h * prior / (like_h * prior + like_not * (1 - prior))

p = 0.001
for i, result in enumerate([True, True, True], 1):
    p = update(p, positive=result)
    print(f"after positive test {i}: P(sick) = {p:.4f}")
print("after a positive then a negative:", round(update(update(0.001), positive=False), 5))
after positive test 1: P(sick) = 0.0472
after positive test 2: P(sick) = 0.7104
after positive test 3: P(sick) = 0.9918
after a positive then a negative: 0.00051

Odds form

Bayes is simplest with odds O(H)=Pr⁡[H]/Pr⁡[Hˉ]O(H) = \Pr[H]/\Pr[\bar{H}]:

O(H∣E)=O(H)⋅Pr⁡[E∣H]Pr⁡[E∣Hˉ]O(H \mid E) = O(H) \cdot \frac{\Pr[E \mid H]}{\Pr[E \mid \bar{H}]}

The ratio on the right is the likelihood ratio (Bayes factor). For the test it is 0.99/0.02=49.50.99/0.02 = 49.5; prior odds 1:9991:999 become posterior odds 49.5:999≈1:2049.5 : 999 \approx 1 : 20, matching 4.7%. Independent pieces of evidence multiply their likelihood ratios, which is the idea behind naive Bayes classifiers.

Bayes with a continuous prior

A coin has unknown bias pp, with a uniform prior on [0,1][0, 1]. After kk heads in nn tosses, the posterior density is proportional to pk(1−p)n−kp^k (1-p)^{n-k}, a beta distribution (Gamma, Beta and Chi-Squared). Its mean, (k+1)/(n+2)(k+1)/(n+2), is Laplace's rule of succession: after 10 heads in 10 tosses, the probability of heads next time is 11/1211/12, not 1.

In software

Spam filters, fraud detection and medical AI all face the base-rate problem: with rare positives, even accurate classifiers produce mostly false alarms, which is why precision and recall are reported separately. Bayesian A/B testing, Bayesian optimization of hyperparameters and probabilistic programming all rest on this one formula.

Exercises

Exercise 1: Rarer disease

Repeat the medical test calculation with prevalence 1 in 10,000.

Answer

About 0.0049, roughly 0.5%.

Solution

0.99⋅0.00010.99⋅0.0001+0.02⋅0.9999=0.0000990.020097≈0.0049\frac{0.99 \cdot 0.0001}{0.99 \cdot 0.0001 + 0.02 \cdot 0.9999} = \frac{0.000099}{0.020097} \approx 0.0049.

Exercise 2: Which machine?

Machine A makes 70% of the parts with defect rate 2%; machine B makes 30% with defect rate 5%. A part is defective. What is the probability it came from B?

Answer

15/29≈0.51715/29 \approx 0.517.

Solution

0.3⋅0.050.7⋅0.02+0.3⋅0.05=0.0150.029\frac{0.3 \cdot 0.05}{0.7 \cdot 0.02 + 0.3 \cdot 0.05} = \frac{0.015}{0.029}.

Exercise 3: Check by simulation (code)

Simulate 1,000,000 people for the original medical test and estimate Pr⁡[sick∣positive]\Pr[\text{sick} \mid \text{positive}] with NumPy.

Answer

About 0.047.

Solution
python · try it yourself
import numpy as np

rng = np.random.default_rng(5)
n = 1_000_000
sick = rng.random(n) < 0.001
positive = np.where(sick, rng.random(n) < 0.99, rng.random(n) < 0.02)
print(round(sick[positive].mean(), 4), int(positive.sum()))
0.0477 20755

Test yourself

A test with 99% sensitivity and 2% false positive rate is used for a 1-in-1000 disease. A positive result means the disease is…

  1. 99% likely
  2. 98% likely
  3. about 5% likely
  4. about 50% likely

In odds form, the posterior odds equal…

  1. prior odds plus likelihood ratio
  2. prior odds times likelihood ratio
  3. likelihood ratio alone
  4. prior odds divided by likelihood ratio

With a uniform prior, after 10 heads in 10 tosses the rule of succession gives P(next head)P(\text{next head}) =

  1. 1
  2. 10/11
  3. 11/12
  4. 1/2