S021 · Probability — Random Variables, Distributions, Expectation
Uncertainty, quantified: random variables, the distributions you'll actually meet (Bernoulli, binomial, normal, exponential, Poisson), expectation, variance, and Bayes' rule.
🎯 Model an uncertain outcome as a random variable, pick the right distribution, compute expectation and variance, and apply Bayes' rule to a real diagnostic-test problem.
Why this session exists
Every modern engineering discipline is drowning in uncertainty. A/B tests, ML model outputs, load forecasts, tail latencies, credit-card fraud detection, LLM sampling — all of them are probability calculations wearing engineering clothes. The difference between "I have a feeling this is a lot" and "the p95 is 340 ms with 95% CI [335, 345]" is fluency in random variables, distributions, and Bayes' rule. This session gets you the vocabulary, and — crucially — the intuition to spot when someone (or you) is using it wrongly.
- Define a random variable, its distribution, and its PMF/PDF/CDF — and translate between them.
- Recognise Bernoulli / binomial / normal / exponential / Poisson in the wild and pick the right one.
- Compute expectation E[X] and variance Var(X) — both from formula and by simulation.
- State and apply Bayes' rule to update beliefs given evidence (the medical-test canonical example).
- Explain the Central Limit Theorem in one sentence and know why it underlies everything from confidence intervals to A/B tests.
Prerequisites
- S016 · Discrete math — counting/combinatorics underlies discrete probability.
- S017 · Vectors — you'll compute expected values as dot products with probabilities.
- High-school algebra, integration is optional (we'll use NumPy for anything continuous).
(a) Intuition · 5 min
You spin a roulette wheel. The wheel has some pockets — some hold big prizes, some small, some nothing. The random variable is the amount you'd win on any given spin. It's not one number; it's a distribution — a description of every possible value and how likely each is.
Two questions matter about any random variable: on average, what do I win? (expectation) And how much does the outcome swing around that average? (variance). Every probability calculation you'll ever do reduces to some version of those two questions.
In code, a random variable is a function that returns a random number when called. In NumPy: rng.binomial(n=10, p=0.3). Its distribution is the theoretical rule for how those returned numbers are shaped. Its expectation and variance are numbers you can compute either from a formula (E[Bin(n,p)] = np) or by calling it a million times and averaging.
The whole subject exists so you can reason about outputs of things you haven't observed yet. It's the language of prediction.
The five distributions you'll see 100× a year
- Bernoulli(p) — one flip of a biased coin. Outcome 0 or 1. Foundation of every yes/no event.
- Binomial(n, p) — n independent coin flips; count the heads. Number of clicks in n ad impressions, defects in n parts.
- Poisson(λ) — number of independent rare events in a fixed interval. Arrivals per second, errors per hour.
- Normal(μ, σ) — the bell curve. Result of many small independent additive effects. Heights, IQ, sum of many things.
- Exponential(λ) — waiting time between Poisson events. Time until next request, time until next failure.
A quick history
- 1654Pascal & Fermat · gambling correspondenceTwo French mathematicians exchange letters about dice games — accidentally invent probability.
- 1763Bayes' theorem · posthumousReverend Thomas Bayes' work is published two years after his death. Ignored for 150 years, now central to everything.
- 1810Laplace · Central Limit TheoremPierre-Simon Laplace proves the sum of many random variables tends to normal. The bell curve conquers the world.
- 1933Kolmogorov · axioms of probabilityAndrey Kolmogorov gives probability its rigorous foundation as measure theory. All modern textbooks descend from this.
- 1990sMCMC + Bayesian revivalCheap computing makes Bayesian inference practical. PyMC/Stan let anyone fit hierarchical models.
- 2020sLLM sampling · everything is probabilisticGPT-4/Claude output token distributions. Temperature, top-k, top-p are all probability operations.
(b) Visual walkthrough · 15 min
PMF vs PDF vs CDF
The five distributions at a glance
Distribution cheat sheet
Expectation and variance in one paragraph
For discrete X: E[X] = Σ x · P(X=x). For continuous X: E[X] = ∫ x · f(x) dx. Just a weighted average with weights = probabilities.
Average squared deviation from the mean. Measures spread. Standard deviation σ = √Var(X).
Always. Works whether X and Y are independent or not. This is a huge trick — most complex problems reduce via linearity.
Variance adds only when independent. Otherwise you need Cov(X, Y). This is where ‘correlation matters’ in portfolio math.
Bayes' rule — the update formula
P(A | B) = P(B | A) · P(A) / P(B)
= likelihood × prior / evidence
What you believed before
- Base rate of the hypothesis
- Prevalence of the disease
- Fraction of spam in your inbox
How well does A explain B?
- Sensitivity of the test
- Probability of this word given it's spam
- Model's assigned probability to the data
How likely is B overall?
- Total probability across all hypotheses
- P(B) = Σ P(B|Aᵢ) P(Aᵢ) — the normaliser
- Often the hardest thing to compute directly
Updated belief
- What you should now believe
- Rerun the formula with new evidence — the posterior becomes the next prior
- This is how learning works, formally
The Central Limit Theorem, in one sentence
"A test that's 99% accurate means a positive result is 99% likely to be correct. If my classifier flags a transaction as fraud with 99% accuracy, it's almost certainly fraud."
Those are different conditional probabilities. P(positive result | fraud) is a property of the detector; P(fraud | positive result) also depends on the base rate. When fraud is rare, the false positives drawn from the enormous legitimate population swamp the true positives, and a "99% accurate" detector can be wrong on the large majority of its alerts.
Because in the balanced examples used to teach classification, the two quantities really are close, so the distinction never surfaces. And English makes it worse — "99% accurate" is a sentence that reads as if it describes the answer rather than the instrument. The sticky part is that reversing a conditional feels harmless in a way that reversing an equation obviously wouldn't. This one error is behind most of the disappointment in deployed classifiers: a model that looked strong on a balanced evaluation set becomes an alert stream nobody trusts, because production has a 0.1% positive rate and nobody recomputed precision at that base rate. It is also why accuracy is the wrong headline metric for any rare event: always predicting "no" on a 0.1% positive class scores 99.9%.
Run the arithmetic on a realistic base rate — the number is genuinely counterintuitive:
base = 0.001 # 0.1% of transactions are fraud
tpr = 0.99 # catches 99% of fraud
fpr = 0.01 # flags 1% of legitimate ones
n = 1_000_000
tp = n * base * tpr # 990 true alerts
fp = n * (1 - base) * fpr # 9,990 false alerts
print(tp / (tp + fp)) # ~0.09 -> 9% of alerts are real fraudWhy does the Central Limit Theorem hold — why do sample means become normally distributed regardless of the shape of the underlying distribution? This is a strong claim, and it is the reason nearly all of applied statistics works.
- 1A sample mean is a sum of independent random variables, divided by n.forced by · that is the definition of an average, and division by a constant only rescales
- 2Summing independent variables averages out their individual idiosyncrasies: an unusually high draw tends to be offset by a typical or low one.forced by · independence means the deviations are uncorrelated, so they partially cancel rather than accumulate
- 3The variance of the sum grows like n, so the standard deviation grows like √n, and the mean's standard deviation therefore shrinks like 1/√n.forced by · variances of independent variables add, and dividing by n scales variance by 1/n²
- 4What remains after that rescaling depends only on the first two moments of the original distribution — its mean and variance. Higher-order features like skew and specific shape are suppressed at a rate governed by n.forced by · the deviation from normality contributed by skew shrinks faster than the leading term as terms accumulate
- 5So the limiting shape is the same for every distribution with finite variance. That shape is the normal distribution — the unique attractor of this summing-and-rescaling operation.forced by · the normal is stable under summing (a sum of normals is normal) and everything else is drawn toward it
Therefore the normal distribution is not an assumption we impose on data for convenience; it is the shape that averages are forced into by summation. This is why we can build confidence intervals for a mean without knowing the population's distribution at all.
And note exactly what this predicts — including where it fails, which is the part that matters operationally. The 1/√n rate means halving your error bar requires 4× the data, which is why A/B tests on small effects need enormous samples. Finite variance is required, so heavy-tailed data (latencies, incomes, request sizes) converges slowly or not at all — which is precisely why you report p99 latency rather than mean latency, and why a mean over a heavy-tailed distribution is a genuinely unstable statistic. And independence is required, so correlated samples (repeated measurements on the same user, autocorrelated time series) break it, making your confidence intervals far too narrow and your results look significant when they aren't.
Any probabilistic judgement is two numbers multiplied, never one. Start with the prior — how common is this thing before any evidence? — then apply the likelihood ratio — how much more often does this evidence appear when the thing is true than when it isn't? Evidence updates a prior; it never replaces it.
The practical consequence is that for rare events, evidence must be extraordinarily strong to move you far. A detector that's ten times more likely to fire on fraud than on legitimate traffic still leaves you far from certain when fraud is one in a thousand — it moves the odds by a factor of ten, and you needed a factor of a thousand.
- P(A|B) ≠ P(B|A). Write both down explicitly whenever you catch yourself reasoning about a test, a classifier, or a symptom.
- Accuracy is meaningless for imbalanced classes. Report precision and recall at your production base rate, and be explicit about which errors you are choosing to make.
- Independence is the assumption that quietly fails and it always fails in the direction of overconfidence — correlated failures, shared upstream dependencies, repeated measures on the same subject.
- Expected value alone is not a decision. Variance and tail behaviour matter, especially when one tail is catastrophic and unrecoverable rather than merely expensive.
Fire this model the moment you see: a rare-event classifier reporting high accuracy · alert fatigue on a monitoring system · "the model is 95% confident" · an A/B test that reached significance in a day · anything described as a false positive rate without a base rate attached.
Setting the threshold on a fraud/anomaly detector: optimise for precision, for recall, or calibrate the probabilities and decide per case?
Calibrate whenever you can, because a calibrated score lets the threshold be a decision with an owner rather than an artefact of whoever trained the model. An uncalibrated score forces every downstream consumer to guess what 0.8 means, and they will all guess differently.
The failure mode nobody measures: when precision is too low, humans stop reading the alerts, and the system's real-world recall silently drops to near zero while its measured recall stays high. Any threshold decision for a human-facing system is therefore partly a decision about attention, not just about statistics — and that means monitoring the investigation rate alongside the model metrics.
(c) Hands-on · 25 min
You'll simulate five distributions, verify expectation-formula = sample-mean, apply Bayes' rule to a diagnostic-test scenario, and watch CLT emerge. Save as probability_demo.py.
Run it:
uv run --with numpy python probability_demo.pyExpected output (numbers vary slightly per run):
What each block does
Anatomy of the code
Change the disease prevalence and see how the posterior changes:
for prev in [0.0001, 0.001, 0.01, 0.05, 0.10, 0.50]:
p_pos = 0.99 * prev + 0.01 * (1 - prev)
posterior = (0.99 * prev) / p_pos
print(f"prevalence {prev*100:>5.2f}% → P(disease|+) = {posterior*100:>5.1f}%")Notice how the posterior rises non-linearly. At 0.01% prevalence a positive test still means only ~1% chance of disease — the ‘base-rate fallacy’ that fools most people (including doctors — see the classic Gigerenzer studies).
(d) Production reality · 15 min
Product teams ran A/B tests, peeked at the p-value early, and stopped the experiment ‘when it looked significant’. This ‘optional stopping’ dramatically inflates false-positive rates: a genuinely-null experiment tested repeatedly WILL eventually appear significant just by chance.
Result: many ‘winning’ features didn't actually win. Metrics moved randomly, teams celebrated, launched, and didn't move real user behaviour.
Where this shows up in the rest of the plan
(e) Recall + stretch · 10 min
Explain-out-loud test
- What is expectation, in one sentence, with one example?
- State Bayes' rule and name the four pieces.
- Why is a positive medical test on a rare disease often not proof of the disease?
What comes next
Hub: The 6-Month Learning Plan
Part of a 130-session evergreen learning series. Session structure: intuition → visual → hands-on → production war stories → recall. Duration: 90 minutes.