Skip to content
ml-recall

Topic notes

Probability

The probability toolkit for ML — rules and Bayes' theorem, independence, random variables, expectation and variance, common distributions, the law of large numbers and the CLT.

3-min read AI-checked

Practise 85 questions Take a test

Key ideas

Rules. Probabilities are non-negative, the sample space has probability 1, and disjoint events add; hence P(¬A)=1−P(A)P(\lnot A)=1-P(A) and P(∅)=0P(\emptyset)=01. For any two events P(A∪B)=P(A)+P(B)−P(A∩B)P(A\cup B)=P(A)+P(B)-P(A\cap B). If only P(A)P(A) and P(B)P(B) are known, the Fréchet bounds give the range of the joint events4 (see Formulas).

Conditioning. P(B∣A)=P(A,B)/P(A)P(B\mid A)=P(A,B)/P(A). The chain rule P(A,B,C)=P(A,B∣C) P(C)=P(A∣B,C) P(B∣C) P(C)P(A,B,C)=P(A,B\mid C)\,P(C)=P(A\mid B,C)\,P(B\mid C)\,P(C) needs no independence assumption. Marginalise with P(B)=∑aP(B,A=a)=∑aP(B∣A=a)P(A=a)P(B)=\sum_a P(B,A=a)=\sum_a P(B\mid A=a)P(A=a) — the law of total probability: average the conditional probabilities over the cases1. From a two-way table, P(¬T∣O)=1−P(O∩T)/P(O)P(\lnot T\mid O)=1-P(O\cap T)/P(O). Bayes' theorem turns P(evidence∣cause)P(\text{evidence}\mid \text{cause}) into P(cause∣evidence)P(\text{cause}\mid\text{evidence}); for a rare condition, even an accurate test gives a modest posterior because most positives are false positives (base-rate effect)1.

Independence. A⊥BA\perp B iff P(A,B)=P(A)P(B)P(A,B)=P(A)P(B). Mutually exclusive events with positive probabilities are dependent (knowing one happened rules out the other). Conditional independence P(A,B∣C)=P(A∣C)P(B∣C)P(A,B\mid C)=P(A\mid C)P(B\mid C) neither implies nor follows from independence1. Independent trials have no memory: after three even rolls, the next is still even with probability 1/2. Continuous X,YX,Y are independent exactly when the joint density factorises, p(x,y)=pX(x) pY(y)p(x,y)=p_X(x)\,p_Y(y); then their covariance is 0. For a point drawn uniformly from a region, a probability is a ratio of areas (favourable area / total area).

Random variables. A pmf gives probabilities; a density p(x)p(x) is not a probability and can exceed 1 (Uniform(0, 0.5) has density 2) — only its integral over an interval is2. The CDF F(x)=P(X≤x)F(x)=P(X\le x) rises from 0 to 12.

Expectation and variance1,2: EE is linear, E[aX+b]=aE[X]+bE[aX+b]=aE[X]+b, E[X+Y]=E[X]+E[Y]E[X+Y]=E[X]+E[Y] always. Var(X)=E[X2]−E[X]2\mathrm{Var}(X)=E[X^2]-E[X]^2, Var(aX+b)=a2Var(X)\mathrm{Var}(aX+b)=a^2\mathrm{Var}(X), and for independent X,YX,Y: Var(X±Y)=VarX+VarY\mathrm{Var}(X\pm Y)=\mathrm{Var}X+\mathrm{Var}Y — variances add, standard deviations do not. Cov(X,Y)=E[XY]−E[X]E[Y]\mathrm{Cov}(X,Y)=E[XY]-E[X]E[Y]; correlation is covariance divided by both SDs and lies in [−1,1][-1,1]. Independence ⇒ zero covariance, but not conversely (Y=X2Y=X^2 with XX uniform on {−1,0,1}\{-1,0,1\})2. For a random vector with covariance Σ\Sigma, Var(α⊤X)=α⊤Σα\mathrm{Var}(\alpha^\top X)=\alpha^\top\Sigma\alpha.

Distributions3:

Distribution Use Mean Variance
Bernoulli(pp) one yes/no trial pp p(1−p)p(1-p)
Binomial(n,pn,p) successes in nn fixed, independent, same-pp trials npnp np(1−p)np(1-p)
Poisson(λ\lambda) counts of rare events (binomial limit) λ\lambda λ\lambda
Uniform(a,ba,b) flat density (a+b)/2(a+b)/2 (b−a)2/12(b-a)^2/12
Normal(μ,σ2\mu,\sigma^2) sums/averages (CLT) μ\mu σ2\sigma^2
Exponential(λ\lambda) waiting times, memoryless6 1/λ1/\lambda 1/λ21/\lambda^2
Geometric(pp) trial of the first success, P(k)=(1−p)k−1pP(k)=(1-p)^{k-1}p7 1/p1/p (1−p)/p2(1-p)/p^2
Hypergeometric(N,K,nN,K,n) successes in nn draws without replacement8 nK/NnK/N smaller than binomial

Counting. (nk)=n!k!(n−k)!\binom nk=\frac{n!}{k!(n-k)!} ways to choose kk of nn. Binomial: P(X=k)=(nk)pk(1−p)n−kP(X=k)=\binom nk p^k(1-p)^{n-k}3; get "at least kk" by summing or via the complement. Hypergeometric: P(X=k)=(Kk)(N−Kn−k)/(Nn)P(X=k)=\binom Kk\binom{N-K}{n-k}/\binom Nn8. A count is binomial only if the trials are independent with one fixed pp — defects that come in batches, for example, are not. A Poisson(λ\lambda) count with integer λ\lambda is a sum of λ\lambda independent Poisson(1) counts, so the CLT makes it nearly normal for large λ\lambda.

Limits. The law of large numbers: sample averages converge to the expectation. The central limit theorem: the standardised sum (or mean) of many i.i.d. variables with finite variance is approximately normal, whatever their distribution3. The mean of nn draws has standard deviation σ/n\sigma/\sqrt n, so estimation error shrinks like 1/n1/\sqrt n1. Epistemic uncertainty (about a parameter, e.g. a bent coin's bias) shrinks with data; aleatoric uncertainty (the next toss) does not1.

Sampling. Inverse transform: draw u∼U(0,1)u\sim U(0,1) and return x=F−1(u)x=F^{-1}(u); for the exponential, x=−ln⁡(1−u)/λx=-\ln(1-u)/\lambda5.

Formulas

P(H∣D)=P(D∣H) P(H)P(D∣H) P(H)+P(D∣¬H) P(¬H)P(H\mid D)=\frac{P(D\mid H)\,P(H)}{P(D\mid H)\,P(H)+P(D\mid\lnot H)\,P(\lnot H)} max⁡(0, P(A)+P(B)−1)≤P(A∩B)≤min⁡(P(A),P(B)),max⁡(P(A),P(B))≤P(A∪B)≤min⁡(1, P(A)+P(B))\max(0,\,P(A)+P(B)-1)\le P(A\cap B)\le\min(P(A),P(B)),\qquad \max(P(A),P(B))\le P(A\cup B)\le\min(1,\,P(A)+P(B))

Chebyshev: P(∣X−μ∣≥kσ)≤1/k2P(|X-\mu|\ge k\sigma)\le 1/k^2 for any distribution with finite variance2.

Worked example

Prevalence 1%, sensitivity P(+∣D)=0.95P(+\mid D)=0.95, false-positive rate P(+∣¬D)=0.05P(+\mid\lnot D)=0.05. P(+)=0.95(0.01)+0.05(0.99)=0.059P(+)=0.95(0.01)+0.05(0.99)=0.059, so P(D∣+)=0.0095/0.059≈0.161P(D\mid +)=0.0095/0.059\approx0.161 — only 16%. A second positive test that is conditionally independent given the disease status raises it to 0.952(0.01)/(0.952(0.01)+0.052(0.99))≈0.7850.95^2(0.01)/(0.95^2(0.01)+0.05^2(0.99))\approx0.785.

In code

import numpy as np

rng = np.random.default_rng(0)
lam = 2.0
u = rng.uniform(size=100_000)
x = -np.log(1 - u) / lam           # inverse-CDF sample of Exponential(2)
print(round(x.mean(), 3), round(x.var(), 3))   # 0.499 0.25  (theory: 0.5, 0.25)

Common pitfalls

References

  1. Dive into Deep Learning — Sec. 2.6.1 A Simple Example — Tossing Coins; 2.6.2 A More Formal Treatment; 2.6.4 Multiple Random Variables; 2.6.5 An Example; 2.6.6 Expectations; 2.6.7 Discussion
  2. Dive into Deep Learning — Sec. 22.6.1.2 Probability Density Functions; 22.6.1.3 Cumulative Distribution Functions; 22.6.1.4–22.6.1.6 Means, Variances, Standard Deviations; 22.6.1.10 Covariance; 22.6.1.11 Correlation
  3. Dive into Deep Learning — Sec. 22.8.1 Bernoulli; 22.8.3 Continuous Uniform; 22.8.4 Binomial; 22.8.5 Poisson; 22.8.6 Gaussian
  4. Wikipedia: Fréchet inequalities — Introduction (bounds for a conjunction and a disjunction of two events)
  5. Wikipedia: Inverse transform sampling — The method; Examples (exponential distribution)
  6. Wikipedia: Exponential distribution — Infobox (mean, variance); Properties (Memorylessness)
  7. Wikipedia: Geometric distribution — Infobox and Definition (pmf, mean, variance); Properties (Memorylessness)
  8. Wikipedia: Hypergeometric distribution — Infobox; Definitions (sampling without replacement)