Textbook › Part 0 Mathematical Foundations
CHAPTER 0-3

Writing Down What Does Not Settle on One Answer — Probability

FREE18 min

Chapter 0-2 settled "which way makes it better." What is left is what counts as better. The training objective — the loss — did not fall from the sky. Write down one assumption in the language of probability and the loss is decided for you.

Probability shows up in deep learning in three places.

  1. The output itself. The 0.87 a classifier returns is a probability: how sure it is.
  2. The objective. Cross-entropy and squared error both follow from distributional assumptions (section 5).
  3. The gradient itself. A minibatch gradient is an estimate of the full-data gradient, and estimates carry error (section 6).

This chapter lays down exactly what those three need.

1. Sample space, events, three promises

Definition. The set of all possible outcomes is the sample space Ω\Omega; its subsets are events. A probability P(A)P(A) is a number satisfying:

  1. P(A)≥0P(A) \ge 0
  2. P(Ω)=1P(\Omega) = 1
  3. if A∩B=∅A \cap B = \varnothing (they cannot both happen) then P(A∪B)=P(A)+P(B)P(A \cup B) = P(A) + P(B)

That is all. What probability means — a long-run frequency, a degree of belief — is not in these three. The arithmetic needs only the three promises; the interpretation sits outside it.

The third gives a familiar form at once: AA and AcA^{c} cannot both happen and together make Ω\Omega, so P(Ac)=1−P(A)P(A^{c}) = 1 - P(A).

2. Conditional probability and Bayes — swapping the direction

Definition. For P(B)>0P(B) > 0, the probability of AA given that BB happened is

P(A∣B)  =  P(A∩B)P(B)P(A \mid B) \;=\; \frac{P(A \cap B)}{P(B)}
(1)

Equation (1) swaps the denominator. Stop measuring AA inside the whole world Ω\Omega and measure it inside BB instead.

Dividing the ends of P(A∩B)=P(A∣B)P(B)=P(B∣A)P(A)P(A \cap B) = P(A \mid B) P(B) = P(B \mid A) P(A) by P(B)P(B),

P(A∣B)  =  P(B∣A) P(A)P(B)P(A \mid B) \;=\; \frac{P(B \mid A)\, P(A)}{P(B)}
(2)

Bayes' theorem. The derivation is one line and the content is "the same P(A∩B)P(A \cap B) written two ways." It earns its keep because the direction you can measure and the direction you want rarely match. A test gives you P(+∣D)P(+ \mid D), the fraction of sick people who test positive; you want P(D∣+)P(D \mid +), the fraction of positives who are sick. Equation (2) swaps them, and the swap always costs you a P(A)P(A) — how likely it was beforehand. Dropping that term is this chapter's common misreading.

Independence. When P(A∩B)=P(A)P(B)P(A \cap B) = P(A)P(B), AA and BB are independent. Substituting into equation (1) gives P(A∣B)=P(A)P(A \mid B) = P(A): learning BB does not move your view of AA.

3. Random variables, expectation, variance

Attach a number to each outcome and you have a random variable (a die face, an image label, one example's gradient).

Definition. The expectation of a random variable XX is

E[X]  =  ∑xx p(x)(discrete),E[X]  =  ∫x p(x) dx(continuous)\mathbb{E}[X] \;=\; \sum_{x} x\, p(x) \quad \text{(discrete)}, \qquad \mathbb{E}[X] \;=\; \int x\, p(x)\, dx \quad \text{(continuous)}
(3)

and its variance is Var(X)=E[(X−E[X])2]\mathrm{Var}(X) = \mathbb{E}\big[(X - \mathbb{E}[X])^2\big].

Expectation is a weighted average. Variance measures spread around that average, and expanding gives

Var(X)  =  E[X2]−(E[X])2\mathrm{Var}(X) \;=\; \mathbb{E}[X^2] - \big(\mathbb{E}[X]\big)^2
(4)

Its square root σ\sigma is the standard deviation, carried in the same units as XX.

Worked numbers. For a fair die, E[X]=3.5\mathbb{E}[X] = 3.5, E[X2]=15.1667\mathbb{E}[X^2] = 15.1667, Var(X)=15.1667−12.25=2.9167 (=35/12)\mathrm{Var}(X) = 15.1667 - 12.25 = 2.9167\ (= 35/12), σ=1.7078\sigma = 1.7078.

Only two properties are needed here.

  • Linearity. E[aX+bY]=a E[X]+b E[Y]\mathbb{E}[aX + bY] = a\,\mathbb{E}[X] + b\,\mathbb{E}[Y] — this holds even when they are dependent.
  • Variances add when independent. If X,YX, Y are independent, Var(X+Y)=Var(X)+Var(Y)\mathrm{Var}(X + Y) = \mathrm{Var}(X) + \mathrm{Var}(Y). This one needs independence.

From the second, for the mean Xˉ=1B∑i=1BXi\bar{X} = \frac{1}{B}\sum_{i=1}^{B} X_i of BB independent observations,

E[Xˉ]=μ,Var(Xˉ)=1B2∑i=1BVar(Xi)=σ2B,sosd(Xˉ)=σB\mathbb{E}[\bar{X}] = \mu, \qquad \mathrm{Var}(\bar{X}) = \frac{1}{B^2}\sum_{i=1}^{B}\mathrm{Var}(X_i) = \frac{\sigma^2}{B}, \qquad \text{so} \quad \mathrm{sd}(\bar{X}) = \frac{\sigma}{\sqrt{B}}
(5)

The centre is right whatever BB is; only the spread shrinks, and it shrinks by B\sqrt{B}. Equation (5) is the workhorse of this chapter and returns in section 6 as minibatching. Note B\sqrt{B}, not BB.

4. Three distributions worth knowing

Bernoulli. One shot at 0 or 1. With p∈[0,1]p \in [0,1], P(y)=py(1−p)1−yP(y) = p^{y}(1-p)^{1-y} for y∈{0,1}y \in \{0,1\} — since yy is 0 or 1, one exponent is always 0 and that factor disappears. Mean pp, variance p(1−p)p(1-p). The σ(z)\sigma(z) that a binary classifier outputs is this pp.

Categorical. One of KK. P(y=k)=pkP(y = k) = p_k with pk≥0p_k \ge 0 and ∑kpk=1\sum_k p_k = 1. Softmax is the map that forces numbers into exactly those constraints: softmax(z)k=ezk/∑jezj\mathrm{softmax}(z)_k = e^{z_k}/\sum_j e^{z_j} makes them positive with the exponential and sums to one by dividing — one step per constraint.

Gaussian (normal). Continuous values.

p(y)  =  1σ2πexp⁡ ⁣(−(y−μ)22σ2)p(y) \;=\; \frac{1}{\sigma\sqrt{2\pi}} \exp\!\left(-\frac{(y-\mu)^2}{2\sigma^2}\right)

with mean μ\mu and variance σ2\sigma^2. Assume it in regression and squared error falls out (next section).

A distribution is not picked from a toolbox by taste; the shape of the output picks it. Zero-or-one: Bernoulli. One of $K$: categorical. A real number: Gaussian. Whatever you pick becomes the loss.

5. Maximum likelihood — where the loss came from

Given data (xi,yi)i=1m(x_i, y_i)_{i=1}^{m} and a model that emits a conditional distribution p(y∣x;θ)p(y \mid x; \theta), choose θ\theta so that the data you actually have is as likely as possible. That is maximum likelihood.

θ^  =  arg⁡max⁡θ∏i=1mp(yi∣xi;θ)  =  arg⁡min⁡θ  −1m∑i=1mlog⁡p(yi∣xi;θ)\hat{\theta} \;=\; \arg\max_{\theta} \prod_{i=1}^{m} p(y_i \mid x_i; \theta) \;=\; \arg\min_{\theta} \; -\frac{1}{m}\sum_{i=1}^{m} \log p(y_i \mid x_i; \theta)
(6)

Three things happen at that equals sign. Take logs (products become sums, and the underflow of multiplying mm small numbers goes away; log⁡\log is increasing so the arg max does not move). Flip the sign (maximise becomes minimise). Divide by mm (so the scale does not change with the number of examples).

Now substitute the distributions from section 4 and familiar losses appear.

Substituting Bernoulli. With p(y∣x)=py(1−p)1−yp(y \mid x) = p^{y}(1-p)^{1-y} and p=σ(z)p = \sigma(z), log⁡p(y∣x)=ylog⁡p+(1−y)log⁡(1−p)\log p(y\mid x) = y\log p + (1-y)\log(1-p), so equation (6) becomes

L(θ)  =  −1m∑i=1m[ yilog⁡pi+(1−yi)log⁡(1−pi) ]L(\theta) \;=\; -\frac{1}{m}\sum_{i=1}^{m}\Big[\, y_i \log p_i + (1-y_i)\log(1-p_i) \,\Big]
(7)

Cross-entropy. Chapter 1 of Part III reached the same expression by saying "we want pip_i large where the label is 1 and 1−pi1-p_i large where it is 0; written together in one line." What that "written together" was is the Bernoulli probability itself.

Check it numerically. For y=(1,0,1,1,0)y = (1,0,1,1,0) and p=(0.9, 0.2, 0.7, 0.6, 0.3)p = (0.9,\,0.2,\,0.7,\,0.6,\,0.3) the likelihood product is 0.9×0.8×0.7×0.6×0.7=0.211680.9 \times 0.8 \times 0.7 \times 0.6 \times 0.7 = 0.21168, and −log⁡(0.21168)/5=0.31053592-\log(0.21168)/5 = 0.31053592. Computing equation (7) directly also gives 0.310535920.31053592 — identical, to zero difference.

Substituting Gaussian. With y∣x∼N(y^,σ2)y \mid x \sim \mathcal{N}(\hat{y}, \sigma^2), one example's negative log-likelihood is

−log⁡p(y∣x)  =  (y−y^)22σ2  +  log⁡(σ2π)-\log p(y \mid x) \;=\; \frac{(y - \hat{y})^2}{2\sigma^2} \;+\; \log\big(\sigma\sqrt{2\pi}\big)
(8)

The second term has no y^\hat{y} in it. Minimise over y^\hat{y} and only squared error remains; fixing σ\sigma leaves nothing but a constant factor. For y=(2.0,−1.0,0.5)y = (2.0,-1.0,0.5), y^=(1.5,−0.7,0.9)\hat{y} = (1.5,-0.7,0.9), σ=1\sigma = 1, the mean negative log-likelihood is 1.0022721.002272 and the mean squared error 0.1666670.166667, differing by exactly 0.918939=log⁡2π0.918939 = \log\sqrt{2\pi} — a constant, as promised.

"Squared error for regression, cross-entropy for classification" is not etiquette; it is a restatement of an assumption. Using squared error says "the errors are Gaussian and equally spread everywhere." When it fails on data full of outliers, the loss is not a bad choice — the assumption does not hold.

6. Minibatches — the gradient is an estimate, not a measurement

One training step's gradient is really an average over all the data:

∇L=1m∑i=1m∇ℓi  =  E[∇ℓ]\nabla L = \frac{1}{m}\sum_{i=1}^{m} \nabla \ell_i \;=\; \mathbb{E}\big[\nabla \ell\big]

With mm in the millions, every single step would read millions of examples. So draw BB of them and use their average — that is a minibatch, and equation (5) applies verbatim. The centre is right (unbiased); the spread is σ/B\sigma/\sqrt{B}.

FIG 1Above, the population; below, the pile of means you get by drawing B values and averaging, over and over. Move B and the hill keeps its centre while its width shrinks — the two gold lines are ±σ/√B and the measured width lands on them. Four times the batch, half the spread. Switch the population to a skewed shape and the means still go bell-shaped

Measured. Two-dimensional logistic regression, 1,000 examples (seed 20261013), evaluated at w=(0.5,−0.3)w = (0.5, -0.3), b=0.1b = 0.1: I formed the per-example gradient ∂ℓi/∂w1\partial \ell_i/\partial w_1 for all of them. The full-data gradient is −0.083919-0.083919 and the per-example standard deviation is σ=0.375821\sigma = 0.375821. Drawing BB of them and averaging, 20,000 times for each BB:

BB measured standard error σ/B\sigma/\sqrt{B} ratio
1 0.371975 0.375821 0.990
4 0.187782 0.187911 0.999
16 0.092750 0.093955 0.987
64 0.046893 0.046978 0.998
256 0.023700 0.023489 1.009

At B=1B = 1 the error, 0.3720.372, is more than four times the gradient itself, 0.0840.084. Steps that point the wrong way are routine. Training still works because the estimate is unbiased: average enough of them and they pull the right way. The first half of equation (5) is that guarantee — and nothing guarantees any individual step.

Out of this comes an operational number. Four times the batch costs four times the compute and halves the error. Going B=16→64B = 16 \to 64 takes the error from 0.09280.0928 to 0.04690.0469, for four times the work. That is why bigger batches are not simply better.

A common misreading: "a 99%-accurate test came back positive, so I'm 99% likely to be ill"

Drop the P(A)P(A) in equation (2) — how likely it was beforehand — and this is what you get. The numbers make the gap plain.

Take a disease with prevalence 0.1%, a test with sensitivity (sick people who test positive) 99% and specificity (healthy people who test negative) 99%. Count out 10,000 people.

positive negative
ill (10 people) 9.9 0.1
healthy (9,990 people) 99.9 9,890.1

9.9+99.9=109.89.9 + 99.9 = 109.8 people test positive, of whom 9.99.9 are actually ill.

P(D∣+)  =  9.9109.8  =  9.0164%P(D \mid +) \;=\; \frac{9.9}{109.8} \;=\; 9.0164\%

Nine per cent, not ninety-nine. The test did nothing wrong. There are 9,990 healthy people, so even a 1% error rate produces 99.9 of them, swamping the 9.9 real cases. In equation (2), the small factor P(D)=0.001P(D) = 0.001 multiplies the numerator only.

Change the prevalence and the answer changes, with the test untouched.

prevalence P(D∣+)P(D \mid +)
0.1% 9.0164%
1% 50.0000%
10% 91.6667%
50% 99.0000%

At 1% it is exactly a coin flip; only at 50% does it reach 99%.

This is not confined to medicine. An imbalanced classifier breaks in precisely the same shape. On a problem where 0.1% of cases are positive, a model that says "negative" every time scores 99.9% accuracy. Accuracy measures $P(\text{correct})$ across everything, so the majority class all but decides it. What to read instead: precision (of those called positive, how many were) and recall (of the real ones, how many were found).

Exercises

The answers are folded away. Work them first.

Exercise 1. Apply the same test (sensitivity 99%, specificity 99%) to a population with prevalence 10%. (a) What is P(D∣+)P(D \mid +)? (b) Why does the answer change when the test did not? (c) What is the classifier equivalent of this story?

Show the answer

(a) 91.6667%. Out of 10,000: 1,000 ill and 9,000 healthy. Positives are 1000×0.99=9901000 \times 0.99 = 990 and 9000×0.01=909000 \times 0.01 = 90, so 1,080 in total, of which 990 are real. 990/1080=0.916667990/1080 = 0.916667.

(b) Because P(A)P(A) in equation (2) changed. A test hands you P(+∣D)P(+ \mid D) and P(+∣Dc)P(+ \mid D^{c}), neither of which contains the prevalence. The quantity you want, P(D∣+)P(D \mid +), multiplies by P(D)P(D) before taking the ratio. Same instrument, different population, different answer.

(c) The gap between the positive rate in the training data and the positive rate where the model is used. Train on a 50/50 sample of positives and deploy into a 0.1%-positive world and you travel between the top and bottom rows of the table in the text. The model has not changed; its precision in the field collapses anyway.

Exercise 2. At some point in training, the per-example gradient has standard deviation σ=0.4\sigma = 0.4. (a) The standard error at B=16B = 16. (b) What BB halves it? (c) What BB divides it by ten? (d) What does that tell you?

Show the answer

(a) 0.4/16=0.10.4/\sqrt{16} = 0.1.

(b) B=64B = 64 (four times). Halving σ/B\sigma/\sqrt{B} means doubling B\sqrt{B}, which means quadrupling BB.

(c) B=1600B = 1600 (a hundred times), since the error goes as 1/B1/\sqrt{B}.

(d) Buying accuracy with batch size gets worse and worse value. Compute grows with BB while error falls with B\sqrt{B}: pay 100× in compute, get 10× in error. Large batches win for a different reason — the same work packs into parallel hardware. Balancing that against the learning rate is Chapter 4.

Exercise 3. Regression assuming y∣x∼N(y^,σ2)y \mid x \sim \mathcal{N}(\hat y, \sigma^2). (a) Write one example's negative log-likelihood and point out the term with no y^\hat y in it. (b) Does doubling σ\sigma change the minimising y^\hat y? (c) What happens when the errors are not Gaussian but include outliers?

Show the answer

(a) −log⁡p(y∣x)=(y−y^)22σ2+log⁡(σ2π)-\log p(y\mid x) = \dfrac{(y-\hat y)^2}{2\sigma^2} + \log(\sigma\sqrt{2\pi}). The second term has no y^\hat y, so it differentiates away and cannot affect the minimiser.

(b) No. Changing σ\sigma scales the first term by 1/(2σ2)1/(2\sigma^2), and a positive constant factor does not move the location of a minimum. The size of the gradient does change, so at a fixed learning rate your effective step size changes.

(c) One outlier drags everything. Squared error punishes by the square of the miss, so a point off by 1010 weighs as much as a hundred points off by 11. Gaussian tails are exponentially thin — the assumption said large misses essentially never happen — so when one does, the assumption is what is wrong. Swap the assumed distribution, not the loss by taste: assume a heavy-tailed distribution and the same derivation hands you absolute error or the Huber loss.

What this chapter does not say

  • Entropy and KL divergence belong to the next chapter (0-4). This chapter stops at maximising likelihood; where the name "cross-entropy" comes from — its meaning as a quantity in information theory — is not touched. The formula itself is fully derived in section 5.
  • Bayesian inference (carrying a posterior around) is not covered. Equation (2) was used as a one-off calculation; putting priors on parameters and training distributions is a different programme.
  • Continuous random variables are not treated rigorously (measure, and how a density differs from a probability). A density is not a probability and can exceed 1; nothing computed here depends on that distinction.
  • The central limit theorem is shown in the figure, not stated or proved. Equation (5) — variance σ2/B\sigma^2/B — needs only independence; "it goes bell-shaped" is a separate claim.
  • The minibatch measurement is one point in parameter space and one component of the gradient. As training moves, σ\sigma moves too. What does not move is the 1/B1/\sqrt{B} shape.
  • Every number in the text was computed and checked (the die's variance, the Bayes table and all four prevalences, the five standard errors, the likelihood product against cross-entropy, and the gap between Gaussian negative log-likelihood and squared error being exactly log⁡2π\log\sqrt{2\pi}).

Going deeper

Comments

Sign in to comment