Lesson 6 of 15

Bayes' Theorem

Inverting Conditional Probabilities

Bayes' theorem lets you flip a conditional probability — turning P(E∣H)P(E \mid H) into P(H∣E)P(H \mid E):

P(H∣E)=P(E∣H)⋅P(H)P(E)P(H \mid E) = \frac{P(E \mid H) \cdot P(H)}{P(E)}

The denominator P(E)P(E) comes from the law of total probability:

P(E)=P(E∣H)⋅P(H)+P(E∣Hc)⋅P(Hc)P(E) = P(E \mid H) \cdot P(H) + P(E \mid H^c) \cdot P(H^c)

The Medical Test Example

A disease affects 1% of the population. A test has 99% sensitivity (true positive rate) and 5% false positive rate.

You test positive. What is the probability you have the disease?

P(H)=0.01,P(E∣H)=0.99,P(E∣Hc)=0.05P(H) = 0.01, \quad P(E \mid H) = 0.99, \quad P(E \mid H^c) = 0.05

P(E)=0.99×0.01+0.05×0.99=0.0594P(E) = 0.99 \times 0.01 + 0.05 \times 0.99 = 0.0594

P(H∣E)=0.99×0.010.0594≈0.1667P(H \mid E) = \frac{0.99 \times 0.01}{0.0594} \approx 0.1667

Only 17% — the low base rate dominates. This is the base-rate fallacy.

Terminology

  • P(H)P(H) — prior: your belief before seeing evidence
  • P(H∣E)P(H \mid E) — posterior: updated belief after evidence
  • P(E∣H)/P(E)P(E \mid H) / P(E) — likelihood ratio: how much the evidence updates you

Your Task

Implement bayes(p_h, p_e_given_h, p_e_given_not_h) that returns P(H∣E)P(H \mid E), rounded to 4 decimal places.

Pyodide loading...
Loading...
Click "Run" to execute your code.