Lesson 6 of 15

Bayes' Theorem

Inverting Conditional Probabilities

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

P(HE)=P(EH)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(EH)P(H)+P(EHc)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(EH)=0.99,P(EHc)=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(HE)=0.99×0.010.05940.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(HE)P(H \mid E)posterior: updated belief after evidence
  • P(EH)/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(HE)P(H \mid E), rounded to 4 decimal places.

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