Part 02 / Topic 5 of 7
Probability basics
A model's output is usually a probability, so you need four ideas:
- Distribution: the possible values and how likely each is.
- Mean (expected value): the long-run average. A die's mean is 3.5.
- Variance: how spread out the values are (std is its square root).
- Conditional probability: P(A given B) — probabilities change when you learn something.
import numpy as np
rolls = np.random.default_rng(0).integers(1, 7, 100_000)
print(rolls.mean().round(3), rolls.var().round(3)) # 3.498 2.91
Bayes' rule updates a belief with evidence. Classic example: a disease affects 1% of people; a test catches 95% of cases but false-alarms on 5% of healthy people. Test positive — what is the chance you are ill?
p = (0.95 * 0.01) / (0.95 * 0.01 + 0.05 * 0.99)
print(round(p, 3)) # 0.161
Only 16% — because the disease is rare, most positives are false alarms. The same base-rate effect shows up in ML: on imbalanced data (Part 8), a "95% accurate" model can be useless.
🧪 Quick quiz
- A test is 95% sensitive with 5% false positives for a disease affecting 1% of people. Roughly what fraction of positive tests are real cases?
Show the answer
About 16% (0.161 by Bayes' rule). Rare condition + imperfect test = most positives are false alarms.
Done here? The practice set for Part 2 — 4 exercises with solutions — is on the part page.