The maths you need · lesson 10 of 12
Parts in this lesson
A(faces * p).sum()an expected value
Bx.var(), x.std()variance and standard deviation
Crng.choice(..., p=probs), rng.normal(mu, sigma)sampling from a distribution
Dnp.log(normal_pdf(data, mu, sigma)).sum()a log-likelihood

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, X, and a value it takes as a small x.

Its distribution says how likely each value is. For a fair die, p(X=x)=16 for each x 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 X, written 𝔼[X] or μ (mu), is the average you'd get over very many tries:

𝔼[X]=∑xxp(x)

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: 1+2+3+4+5+66=3.5. No single roll gives 3.5, but the average of many rolls closes in on it:

dice.ipynb

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.

Var⁡(X)=𝔼[(X−μ)2]=∑xp(x)(x−μ)2

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: σ=Var⁡(X). In NumPy: x.var() and x.std().

dice.ipynb

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: p(X=1)=φ, p(X=0)=1−φ. Spam or not, Gentoo or not. Logistic regression (Stage 3) predicts φ.
  • Categorical: one of k 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.
distributions.ipynb

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:

𝒩(x;μ,σ2)=12πσ2exp⁡(−(x−μ)22σ2)

In words: the further x 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. exp⁡(a) means ea. 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.

Bell builderμ = 3.5, σ = 0.3
3 kg3.5 kg4 kg4.5 kg5 kg

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, ∏i𝒩(xi;μ,σ2) (∏ 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:

log⁡L(μ,σ)=∑i=1nlog⁡𝒩(xi;μ,σ2)

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().

likelihood.ipynb

The log-likelihood is −7.57 at μ=3.5, −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:

units.ipynb

"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 σ2 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 𝒩(μ,σ2) 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

A spinner gives 0 with probability 0.5, 2 with probability 0.25 and 4 with probability 0.25. What is its expected value?
Which distribution fits “which of 10 digits is in this picture”?
Predict, then check with the bell builder: with σ fixed, where does μ give the highest log-likelihood for the five penguins?

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.

All sources for Simple Machine Learning