Module 3 — Bayes & Random Variables
Module 2 addressed the question "given one item of evidence, what is the probability of the event?" This module introduces the mechanism for updating a belief as evidence is observed — Bayes' rule — together with the formalism of random variables used to describe uncertain quantities. These are the foundations of probabilistic reasoning under uncertainty.
Bayes' rule: inverting a conditional probability
It is frequently the case that one conditional probability is known but the other is required. A medical test provides \( P(\text{positive} \mid \text{sick}) \) — the probability of a positive result for a sick patient. The patient, however, requires \( P(\text{sick} \mid \text{positive}) \) — the probability of being sick given a positive result. Bayes' rule relates the two:
Read it as three named pieces:
- Prior \( P(H) \) — what you believed before the evidence (the base rate).
- Likelihood \( P(E \mid H) \) — how well the hypothesis explains the evidence.
- Posterior \( P(H \mid E) \) — your updated belief after seeing the evidence. It becomes the new prior for the next clue.
Before any evidence is observed, the appropriate belief is the prior — the base rate, and nothing further. A common error is to instead use the most salient available number. Consider: which value answers the question when no test has been performed?
This activity needs JavaScript. With no test run yet, \( P(\text{buggy}) \) is simply the base rate — 10%. The 90% and 20% are likelihoods that only matter once you observe a test result.
The base-rate fallacy
The most common error in probabilistic reasoning is neglecting the prior. For a test that is "99% accurate" applied to a disease with a prevalence of 1 in 1,000, a positive result is more often than not a false positive — because the healthy population is so much larger that even a small false-positive rate generates more false positives than true positives.
Put numbers to it. The prior is \( P(\text{sick}) = 0.001 \); the test is 99% accurate, so \( P(+ \mid \text{sick}) = 0.99 \) and the false-positive rate is \( 0.01 \). Writing Bayes' rule out in full:
Per 1,000 people, this corresponds to approximately 1 true positive against roughly 10 false positives — the low base rate dominates the result. The activity below demonstrates this: reduce the disease prevalence toward "rare" and observe that a positive test result remains predominantly incorrect.
This activity needs JavaScript. The lesson below still covers everything.
Sequential updating across multiple observations
Models typically observe many items of evidence rather than one. Bayes' rule permits these to be incorporated sequentially — the posterior from one update serves as the prior for the next. Each conditionally independent item of evidence multiplies the odds. In the activity below, add evidence one item at a time and observe the belief increase or decrease as the evidence accumulates.
This activity needs JavaScript.
Random variables: quantities with uncertain values
A random variable is a number whose value is uncertain — written with a capital letter such as \( X \). A fair die is the classic example: its outcome is \( X \in \{1, 2, 3, 4, 5, 6\} \), with \( P(X = k) = \tfrac{1}{6} \) for every face \( k \). Other random variables: whether an email is spam, \( X \in \{0, 1\} \); tomorrow's temperature, \( X \in \mathbb{R} \). We summarize one with its expectation \( E[X] \): the long-run average, computed by weighting each value by its probability.
For the die, \( E[X] = \sum_{k=1}^{6} k \cdot \tfrac{1}{6} = \dfrac{1+2+3+4+5+6}{6} = 3.5 \). No single roll is ever \( 3.5 \) — it is the value the average of many rolls settles toward. Roll it below and watch the running average converge.
This activity needs JavaScript. The lesson below still covers everything.
Two distributions show up constantly in ML:
- Bernoulli — a single yes/no trial with probability \( p \) of "yes." Every binary label (spam / not-spam, click / no-click) is a Bernoulli variable. A classifier's output is an estimate of that \( p \).
- Normal (the bell curve) — the distribution of things that pile up around an average: heights, measurement noise, the errors a model makes. You'll meet it again in Module 7.
This activity needs JavaScript.
Check your understanding
These questions emphasize accounting for the base rate before drawing conclusions from any item of evidence.
This activity needs JavaScript.