Distributions
Last updated
A distribution says how likely each value is: each face of a die, each species, each body mass. This lesson describes one by its mean and spread, meets the three distributions the course uses most (Bernoulli for yes-or-no, categorical for one-of-several, and normal for measurements), and ends with likelihood: how well a distribution explains some data. Training a model in Stage 3 means making that likelihood as high as it can go.
By the end of this lesson you can
- Work out an expected value, a variance and a standard deviation, by formula and with NumPy
- Say which of Bernoulli, categorical and normal fits a kind of data, and sample from each
- Compute the likelihood and log-likelihood of data under a normal distribution
- Report spread in the data’s own units, with the standard deviation
Random variables
A random variable is a quantity whose value depends on chance: the face a die lands on, the species of a penguin picked at random, its body mass. Maths writes it as a capital, , and a value it takes as a small .
Its distribution says how likely each value is. For a fair die, for each from 1 to 6. All the probabilities together add up to 1, because the die has to land on something. For a measurement like body mass, which can be any number in a range, the distribution is a curve called a density instead; step 4 meets one.
Mean and expected value
The expected value of , written or (mu), is the average you'd get over very many tries:
In words: multiply each value by its probability, and add up. In NumPy: (values * probs).sum(), a dot product (lesson 1.3). For a die: . No single roll gives 3.5, but the average of many rolls closes in on it:
100,000 rolls average 3.497. That's the link between the two meanings of "mean": the mean of data you collected (Stage 0) estimates the expected value of the distribution it came from.
Variance and standard deviation
The variance measures spread: how far values typically land from the mean, squared.
In words: for each value, square how far it is from the mean, weight by its probability, and add up. The standard deviation, (sigma), is its square root: . In NumPy: x.var() and x.std().
A die's variance is 2.9167 and its standard deviation 1.7078; the 100,000 rolls give 2.924 and 1.71. (On data, NumPy divides by the number of values by default. Statistics books often divide by one less, x.std(ddof=1), for a slightly better estimate from a small sample. With 100,000 values the two are the same to three decimals.)
Bernoulli, categorical and normal
Three distributions cover nearly every kind of label and measurement in this course:
- Bernoulli: yes or no. One number, (phi), is the chance of yes: , . Spam or not, Gentoo or not. Logistic regression (Stage 3) predicts .
- Categorical: one of options, each with its own probability, adding up to 1. Which of three species. Softmax (Stage 3) predicts these probabilities.
- Normal (or Gaussian): a measurement that clusters round a mean, with a bell-shaped density. Body mass, flipper length.
Each sample lands close to the probabilities it was drawn with: 0.296 against 0.3, and 0.4439, 0.1988, 0.3573 against 0.44, 0.20, 0.36. The normal distribution's density is:
In words: the further is from the mean , measured in standard deviations, the lower the curve, falling off fast. The fraction in front just makes the area under the curve come to 1. means . In NumPy: np.exp(-(x - mu) ** 2 / (2 * sigma ** 2)) / np.sqrt(2 * np.pi * sigma ** 2).
Likelihood
Five penguins weigh 3.9, 4.1, 4.4, 3.7 and 4.0 kg. Which normal distribution explains them best? Try some. The bar at each data point is the curve's height there: its likelihood, how well the curve expects that point.
Bar heights (likelihoods): 0.547, 0.18, 0.015, 1.065, 0.332. Log-likelihood, the sum of their logs: −7.57. Move μ to make it higher (less negative).
The likelihood of all the data is the product of the bar heights, ( is like , but multiplies). As lesson 1.1 showed, products of many small numbers underflow, so everyone works with the log-likelihood, the sum of the logs:
In words: take the log of each point's likelihood and add them up. Higher (less negative) is better. In NumPy: np.log(normal_pdf(data, mu, sigma)).sum().
The log-likelihood is −7.57 at , −0.07 at 4.0, and −6.46 at 4.5. It's highest when is the data's mean, 4.02 (the 0000000000005 is floating-point rounding, as in lesson 1.6). Choosing the settings that make the data most likely is maximum likelihood, and it's how Stage 3 trains models: the losses you'll minimise there are negative log-likelihoods.
Wrong way: variance where standard deviation was meant
Here are seven penguins' masses, in grams:
"The masses vary by about 704,082" is wrong, and not just big. Variance squares the differences, so its units are grams squared: 704,081.6 g². That isn't a mass at all. The standard deviation, 839.1 g, is back in grams, so you can say "most of these penguins are within about 840 g of the mean of 4,086 g".
Use variance inside the maths, where squares are convenient (the normal density above has in it). Report spread with the standard deviation. The same slip turns up in libraries: NumPy's rng.normal(mu, sigma) wants the standard deviation, while the formula writes with the variance. Passing a variance where a standard deviation was wanted gives samples spread far too wide (or too narrow, if the variance is below 1).
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Goodfellow, Bengio and Courville, Deep Learning, chapter 3: Probability and Information Theory: random variables (3.1), expectation and variance (3.8), and the Bernoulli, multinoulli and normal distributions (3.9)
- Goodfellow, Bengio and Courville, Deep Learning, chapter 5: Machine Learning Basics: maximum likelihood estimation (5.5)
- NumPy: Random Generator: integers, random, choice and normal
- NumPy: numpy.std: the standard deviation, and ddof for the sample version