Learn machine learning from scratch

Start at chapter 1 and build up: what a model is, how it measures its own mistakes, and how it learns. Everything is worked out in plain Python, with the maths shown rather than assumed. Two shorter tracks hold reference notes on the maths and on modern architectures.

Track Progress: 0 Completed
0%
Maths for MLLEVEL 1 · BEGINNERModule 5 · Probability and statistics

Common distributions: Bernoulli, categorical, Gaussian

Yes-or-no, one of K, and the bell curve. Three distributions that cover classifier outputs, language-model sampling and noise.

9 min Prerequisites: Lesson 24 (expectation and variance)

1. The idea

A distribution says how likely each value of a random variable is. A handful of them do almost all the work in machine learning.

The Bernoulli distribution is a single yes-or-no outcome with P(1) = p: a coin flip, a click, a binary label. The categorical distribution extends it to K outcomes whose probabilities add up to 1: the next token, the class of an image. A classifier's sigmoid output is a Bernoulli p; its softmax output is a categorical distribution.

The Gaussian or normal distribution 𝒩(μ, σ²) is the bell curve, centred on μ with spread σ. It is a density: p(x) is a height, not a probability, and probabilities are areas under the curve, so p(x) can be above 1 when σ is small. Gaussians describe measurement noise, the starting weights of a network and the noise that a diffusion model adds and learns to remove.

2. Try it: three shapes of chance

Switch between the three. For the Gaussian, shrink σ until the peak passes 1, which a probability never could; the shaded band is μ ± σ.

Buttons choose a distribution. Bernoulli and categorical show one bar per outcome with sliders for the probabilities; the Gaussian shows a bell curve with sliders for mu and sigma and a shaded band one standard deviation either side of the mean.

3. The math

Bernoulli
P(X=1)=p,P(X=0)=1−p,E[X]=pP(X = 1) = p, \quad P(X = 0) = 1 - p, \quad \mathbb{E}[X] = p
Its variance is p(1 − p).
Categorical
P(X=k)=πk,∑k=1Kπk=1P(X = k) = \pi_k, \qquad \sum_{k=1}^{K}\pi_k = 1
π here is a list of probabilities, not 3.14.
Gaussian
p(x)=1σ2πexp⁡ ⁣(−(x−μ)22σ2)p(x) = \frac{1}{\sigma\sqrt{2\pi}}\exp\!\left(-\frac{(x - \mu)^2}{2\sigma^2}\right)
Written x ~ 𝒩(μ, σ²).
Probabilities are areas
P(a≤X≤b)=∫abp(x) dxP(a \le X \le b) = \int_a^b p(x)\,dx
The total area under any density is 1.
The 68–95 rule
P(∣X−μ∣≤σ)≈0.68,P(∣X−μ∣≤2σ)≈0.95P(|X - \mu| \le \sigma) \approx 0.68, \qquad P(|X - \mu| \le 2\sigma) \approx 0.95
True of every Gaussian, whatever μ and σ.

4. The code (python)

mm5_distributions.py
import numpy as np
from math import erf, sqrt

rng = np.random.default_rng(0)
print(round((rng.random(100_000) < 0.7).mean(), 3))                    # 0.701
counts = np.bincount(rng.choice(4, size=100_000, p=[0.4, 0.3, 0.2, 0.1]), minlength=4)
print((counts / counts.sum()).round(2).tolist())                       # [0.4, 0.3, 0.2, 0.1]

x = rng.normal(loc=2.0, scale=0.5, size=100_000)                       # x ~ N(2, 0.5^2)
print(round(x.mean(), 2), round(x.std(), 2))                           # 2.0 0.5
print(round(np.mean(np.abs(x - 2.0) <= 0.5), 3), round(erf(1 / sqrt(2)), 3))   # 0.683 0.683

5. You’ll see this in

6. Check yourself

Answer all 4 questions correctly to complete the chapter · 0 / 4 done
Q1/4 A classifier's softmax output over 10 classes is which distribution?
Q2/4 X is Bernoulli with p = 0.2. What is 𝔼[X]?
Q3/4 Can a Gaussian density have p(x) = 1.6 at some x?
Q4/4 About what share of a Gaussian lies within two standard deviations of its mean?

Finished this chapter?

Weekly newsletter

What changed in ML architectures this week

New models worth knowing, benchmark moves, and one architecture explained in plain words. Free, once a week, unsubscribe anytime.