Probability I: random variables and distributions
The language of uncertainty, from coin flips to the Gaussian
A classifier does not output an answer, it outputs a distribution over answers; a language model is a distribution over the next token (7.6 Decoder-only: GPT and autoregressive generation); a diffusion model adds and removes Gaussian noise (8.9 Generative models II: Diffusion and flow matching). Probability is the language in which all of that is written. This lesson builds the vocabulary — random variables, PMFs and PDFs, expectation and variance, the standard distributions — and spends extra time on the Gaussian, the distribution that keeps reappearing for reasons you will finally understand.
You'll be able to
- State the probability axioms and distinguish discrete (PMF) from continuous (PDF) random variables
- Compute expectations, variances and covariances and use their linearity rules
- Recognize Bernoulli, Binomial, Categorical, Uniform, Gaussian, Exponential and Poisson distributions and where each appears in ML
- Work with the Gaussian density, standardization and the multivariate Gaussian's covariance geometry
Key ideas
- Axioms: P ≥ 0, P(Ω) = 1, additivity for disjoint events; everything else (complements, unions) follows.
- Discrete variables have PMFs that sum to 1; continuous variables have densities that integrate to 1 and can exceed 1 pointwise; the CDF covers both.
- E[X] is a weighted average and is always linear; Var(X) = E[X²] − μ², Var(aX + b) = a²Var(X); variances of independent variables add.
Show 4 more key ideas
- Bernoulli/Categorical are classifier output distributions; the Gaussian is the default noise model and prior.
- Gaussian density ∝ exp(−½((x − μ)/σ)²): a squared distance in the exponent, which is why Gaussian noise ⇒ squared-error loss.
- Multivariate Gaussian level sets are ellipsoids with axes along the covariance's eigenvectors.
- Gaussians are everywhere because of the CLT, maximum entropy and closure under sums, linear maps and conditioning.
Suggested path through this lesson
Why probability is the language of learning
Data are noisy, labels are ambiguous, and the future is uncertain. A model that commits to one answer cannot say how sure it is, cannot be calibrated and cannot be combined with other evidence. So models output distributions: logistic regression gives (3.3 Logistic regression and softmax classification), a softmax layer gives a distribution over classes (4.3 Loss functions catalog), and GPT gives a distribution over 100,000 possible next tokens (7.6 Decoder-only: GPT and autoregressive generation). Even training is probabilistic: minimizing a loss turns out to be maximizing the probability the model assigns to the data (1.7 Probability II: Bayes, independence, MLE and MAP).
Sample spaces, events and the axioms
A sample space is the set of all outcomes of an experiment; an event is a subset of . A probability assigns each event a number such that , , and when and cannot both happen. Everything else follows: and, for overlapping events, .
Random variables: PMF, PDF and CDF
A random variable attaches a number to each outcome. If takes countably many values, its probability mass function satisfies . If is continuous, single values have probability zero and we use a probability density with
A density is not a probability — it can exceed 1 — only its integrals are. The cumulative distribution function works for both kinds and is what scipy.stats calls cdf.
Expectation, variance and covariance
The expectation is the probability-weighted average, or , and for any function the law of the unconscious statistician gives — no need to find the distribution of . Expectation is linear: , always, whether or not and are independent. The variance measures spread,
and the standard deviation has the units of . For two variables the covariance measures linear co-movement and the correlation is its scale-free version — the Cauchy–Schwarz inequality of 1.1 Vectors, dot products and norms in probabilistic clothing. For independent variables ; in general you add .
Intuition. Expectation is the center of mass of the distribution; variance is its moment of inertia. Linearity of expectation is the single most-used fact in all of probability.
The distributions you will meet
| distribution | parameters | PMF or PDF | mean, variance | where in ML |
|---|---|---|---|---|
| Bernoulli | , | , | binary labels, logistic regression output | |
| Binomial | , | number of correct answers in trials | ||
| Categorical | for class | — | softmax outputs, next-token distribution | |
| Uniform | on | , | random initialization, sampling | |
| Gaussian | , | noise models, priors, diffusion, initialization | ||
| Exponential | , | , | waiting times, survival | |
| Poisson | , | counts per interval |
Bernoulli and Categorical are the output distributions of almost every classifier; the Gaussian is the default model of noise and the default prior over weights.
The Gaussian, up close
Write the density as with : the exponent is a negative squared distance from the mean in units of . That is why assuming Gaussian noise leads to squared-error losses (3.2 Linear regression, 1.7 Probability II: Bayes, independence, MLE and MAP). Standardizing, , lets one table (or norm.cdf) answer every Gaussian question; roughly 68%, 95% and 99.7% of the mass lies within 1, 2 and 3 standard deviations. Sums of independent Gaussians are Gaussian, with means and variances adding. In dimensions,
the level sets are ellipsoids whose axes are the eigenvectors of the covariance and whose semi-axis lengths are the square roots of its eigenvalues (1.3 Eigen-things, SVD and low rank); the determinant in the normalizer is the volume factor of 1.2 Matrices as transformations. The full treatment is in 1.7 Probability II: Bayes, independence, MLE and MAP.
Why Gaussians are everywhere
Three reasons. The central limit theorem: a sum of many small independent effects is approximately Gaussian whatever the effects' own distributions, so measurement noise usually is (1.8 Statistics essentials). Maximum entropy: among all distributions with a given mean and variance, the Gaussian assumes the least (1.9 Information theory: entropy, cross-entropy and KL divergence). Convenience: Gaussians stay Gaussian under sums, linear maps, conditioning and marginalizing, which keeps the math closed-form.
Worked example
A biased coin with is : and . Ten flips give with mean and variance ; the chance of no heads at all is . For heights in centimetres, — about one person in 44.
from scipy import stats
print(stats.binom(10, 0.3).pmf(0)) # 0.0282
print(stats.norm(170, 7).sf(184)) # 0.0228 (sf = 1 - cdf)
Where this shows up later
- Conditioning, Bayes and maximum likelihood — 1.7 Probability II: Bayes, independence, MLE and MAP; estimators and the CLT — 1.8 Statistics essentials; entropy of distributions — 1.9 Information theory: entropy, cross-entropy and KL divergence.
- Bernoulli and Categorical outputs — 3.3 Logistic regression and softmax classification, 4.3 Loss functions catalog; Gaussian noise and squared error — 3.2 Linear regression; Gaussian naive Bayes and mixtures — 3.7 Naive Bayes and generative vs discriminative models, 3.11 Clustering: k-means, GMMs/EM, DBSCAN and hierarchical.
- Gaussian initialization — 4.6 Initialization and normalization; VAEs and diffusion are built from Gaussians — 8.8 Generative models I: VAEs and GANs, 8.9 Generative models II: Diffusion and flow matching; sampling from the next-token Categorical — 8.5 Decoding and sampling.
Common pitfalls (4)
- Reading a density value as a probability — p(x) = 3 is fine; only integrals are probabilities.
- Assuming Var(X + Y) = Var(X) + Var(Y) without independence; the covariance term can be large.
- Confusing standard deviation (units of X) with variance (squared units) when reporting results.
- Thinking 'the data is Gaussian' is an observation; it is a modeling assumption that should be checked (plot a histogram or QQ plot).
Recap: all 7 key points
- Axioms: P ≥ 0, P(Ω) = 1, additivity for disjoint events; everything else (complements, unions) follows.
- Discrete variables have PMFs that sum to 1; continuous variables have densities that integrate to 1 and can exceed 1 pointwise; the CDF covers both.
- E[X] is a weighted average and is always linear; Var(X) = E[X²] − μ², Var(aX + b) = a²Var(X); variances of independent variables add.
- Bernoulli/Categorical are classifier output distributions; the Gaussian is the default noise model and prior.
- Gaussian density ∝ exp(−½((x − μ)/σ)²): a squared distance in the exponent, which is why Gaussian noise ⇒ squared-error loss.
- Multivariate Gaussian level sets are ellipsoids with axes along the covariance's eigenvectors.
- Gaussians are everywhere because of the CLT, maximum entropy and closure under sums, linear maps and conditioning.
Watch
Sample spaces, events and the naive definition — a rigorous but friendly start to the course this lesson follows.
Read
Try it live
Interactive demo
runs in your browserExercises
- By hand: moments and probabilities
For a die, compute , and . For the sum of two dice, find and using independence. For (IQ scores), find and the value with . Expected: , , , , .
- NumPy: simulate and compare
Draw 100,000 samples from Bernoulli(0.3), Binomial(10, 0.3) and with
np.random.default_rng. Compare the sample mean and variance with the formulas, and overlay histograms with the PMF/PDF fromscipy.stats. Expected: agreement to two decimals; the empirical is near 0.0228. - Experiment: variance of sums and the CLT
Sample pairs that are independent and pairs that are correlated (generate ), and compare with . Then average uniform random numbers and plot the histograms. Expected: the independence case matches; the averages look Gaussian by with variance shrinking like .
Quiz
Flashcards
These cards are now in your spaced-repetition queue. Review all due cards →