ML Foundations Lecture 13 of 14
Part V — Probability and Statistics · Lecture 13

Random Variables, Expectation, and Variance

Every loss you minimise is an expectation you cannot compute, estimated from a sample. Understanding what that sentence means — and how wrong the estimate can be — is the difference between running experiments and understanding them.

By the end you can

  • State the probability axioms and define a random variable properly
  • Prove linearity of expectation, and use it without assuming independence
  • Compute variance and covariance, and distinguish independent from uncorrelated
  • Prove Markov's and Chebyshev's inequalities and use them to bound errors
  • Show that a mini-batch gradient is unbiased with variance \(\propto 1/B\)

Assumed

  • Lectures 1–2: functions, logs
  • Lecture 8: vectors and dot products (for covariance matrices)
  • Basic counting; comfort with sums and integrals as 'weighted totals'

1Why probability, in one equation

What we actually want to minimise is the true risk — the average loss over the whole data-generating distribution \(\mathcal{D}\):

\[R(\vt) = \E_{(\vx,y)\sim\mathcal{D}}\big[\ell\big(f_{\vt}(\vx),\,y\big)\big].\]

We cannot compute it. \(\mathcal{D}\) is unknown — it is the process that generates houses, or photographs, or sentences. All we have is a finite sample. So we minimise the empirical risk instead:

\[\hat R(\vt) = \frac1n\sum_{i=1}^{n}\ell\big(f_{\vt}(\vx_i),y_i\big).\]

Three questions follow immediately, and all of them are probability questions:

  1. Is \(\hat R\) close to \(R\)? (Concentration — this lecture, and all of Phase C.)
  2. Does minimising \(\hat R\) minimise \(R\)? (Generalisation — Phase C.)
  3. If we use a random subset of the sum, do we still descend? (Stochastic gradients — this lecture.)

2The axioms

Definition — probability space

A probability space is a triple \((\Omega,\mathcal{F},P)\):

  1. \(P(A)\ge0\) for every event \(A\);
  2. \(P(\Omega)=1\);
  3. if \(A_1,A_2,\dots\) are pairwise disjoint then \(P\!\left(\bigcup_i A_i\right)=\sum_i P(A_i)\) (countable additivity).

Everything else is a consequence. \(P(A^c)=1-P(A)\), because \(A\) and \(A^c\) are disjoint and their union is \(\Omega\). \(P(\emptyset)=0\). \(P(A\cup B)=P(A)+P(B)-P(A\cap B)\), the overlap subtracted once because it was counted twice. Three axioms, an entire subject.

On measure theory

Why is \(\mathcal{F}\) a "collection of subsets" rather than all subsets? Because for continuous \(\Omega\) it is impossible to assign a consistent probability to every subset — there are non-measurable sets. Handling this properly is measure theory, and it is genuinely deferred: it changes nothing about the calculations in Phases A and B, and it belongs after Phase C when you need it for stochastic processes and rigorous concentration arguments. Ask and we will do it, but not yet.

3Random variables

Definition — random variable

A random variable is a function \(X:\Omega\to\RR\). It is not a variable and it is not random — it is a deterministic function that assigns a number to each outcome. The randomness lives in which \(\omega\) occurs.

Common trap

A density is not a probability. \(f(x)\) can exceed 1 — a uniform distribution on \([0,0.1]\) has \(f=10\) throughout. Only the area under it is a probability, which is why \(P(X=c)=0\) for any single point of a continuous distribution: the area over a point of zero width is zero. This is why continuous likelihoods can exceed 1 and log-likelihoods can be positive, which surprises people the first time a model reports a "probability" of 4.7.

Distribution explorer — shape, mean, varianceInteractive
density / massmean μμ ± σ
0
1.00
1.00
Shaded region is μ ± σ. For the Gaussian it holds about 68% of the mass, always. For the exponential it does not — that distribution is skewed, and its mean is not its typical value. That gap between 'mean' and 'typical' is behind a great deal of confusion about model performance metrics.

4Expectation

Definition — expectation

\[ \E[X] = \sum_x x\,p(x) \quad\text{(discrete)}, \qquad \E[X] = \int_{-\infty}^{\infty} x f(x)\,dx \quad\text{(continuous)}. \]

It is the probability-weighted average value, and physically it is the centre of mass of the distribution.

LOTUS (the law of the unconscious statistician): for any function \(g\),

\[ \E[g(X)] = \sum_x g(x)\,p(x) \quad\text{— you do not need the distribution of } g(X).\]

Linearity of expectation — the most useful theorem in probability0 / 5
\[\E[aX+b] = \sum_x (ax+b)p(x)\]
Apply LOTUS with \(g(x)=ax+b\). No assumptions yet.
\[= a\sum_x x\,p(x) + b\sum_x p(x) = a\,\E[X] + b\]
Split and simplify. The first sum is \(\E[X]\) by definition; the second is \(\sum_x p(x)=1\) by the axioms.
\[\E[X+Y] = \sum_{x}\sum_{y}(x+y)\,p(x,y)\]
Two variables. Now we sum over the joint distribution \(p(x,y)=P(X=x,Y=y)\), because \(X\) and \(Y\) may be dependent in any way at all.
\[= \sum_x x\sum_y p(x,y) \;+\; \sum_y y\sum_x p(x,y) = \sum_x x\,p_X(x) + \sum_y y\,p_Y(y)\]
Split and marginalise. Summing the joint over one variable gives the marginal of the other: \(\sum_y p(x,y)=p_X(x)\). This step used only the axioms.
\[\boxed{\;\E[X+Y] = \E[X]+\E[Y]\;} \qquad\textbf{no independence required}\]
The theorem. Read the boxed caveat twice. \(X\) and \(Y\) may be perfectly correlated, anti-correlated, or one may be a function of the other — expectation still adds. This is what makes expectation so much friendlier than variance, which does not add in general. Nearly every slick argument in probability is linearity of expectation applied to a cleverly chosen decomposition. ∎

5Variance and covariance

Definition — variance, covariance, correlation

\[ \Var(X) = \E\big[(X-\E[X])^2\big], \qquad \Cov(X,Y) = \E\big[(X-\E X)(Y-\E Y)\big], \qquad \rho_{XY} = \frac{\Cov(X,Y)}{\sqrt{\Var X\,\Var Y}}. \]

The standard deviation is \(\sigma_X=\sqrt{\Var(X)}\), which has the same units as \(X\) — the reason it is reported rather than the variance.

The four identities you will use constantly0 / 4
\[\Var(X) = \E[X^2] - \big(\E[X]\big)^2\]
The computational form. Expand: \(\E[(X-\mu)^2]=\E[X^2-2\mu X+\mu^2] = \E[X^2]-2\mu\E[X]+\mu^2 = \E[X^2]-\mu^2\), using linearity and \(\E[X]=\mu\). Cheaper to compute, and it makes the inequality \(\E[X^2]\ge(\E X)^2\) obvious — variance is non-negative.
\[\Var(aX+b) = a^2\Var(X)\]
Scaling and shifting. Shifting by \(b\) moves the mean by \(b\) too, so the deviations \(X-\E X\) are unchanged — variance ignores location. Scaling by \(a\) scales the deviations by \(a\) and hence the squared deviations by \(a^2\). Note the square: doubling a variable quadruples its variance.
\[\Var(X+Y) = \Var(X) + \Var(Y) + 2\Cov(X,Y)\]
Variance does NOT simply add. Expand \(\E[((X-\mu_X)+(Y-\mu_Y))^2]\) and you get the two squares plus twice the cross term, which is the covariance by definition. Compare \((a+b)^2=a^2+b^2+2ab\) — the algebra is identical, which is not a coincidence: variance is a squared length and covariance is an inner product.
\[X \perp\!\!\!\perp Y \;\Longrightarrow\; \Cov(X,Y)=0 \;\Longrightarrow\; \Var(X+Y)=\Var(X)+\Var(Y)\]
Independence kills the cross term. If \(X\) and \(Y\) are independent then \(\E[XY]=\E[X]\E[Y]\), so \(\Cov=\E[XY]-\E X\E Y = 0\). The converse is false — see the quiz. Independence is much stronger than zero correlation.
Check yourself
Let \(X\) be uniform on \(\{-1,0,1\}\) and let \(Y=X^2\). Are they independent? Are they uncorrelated?
Correct. Correct, and this example is worth remembering.

Uncorrelated: \(\E[X]=\frac{-1+0+1}{3}=0\), and \(\E[XY]=\E[X^3]=\frac{-1+0+1}{3}=0\). So \(\Cov(X,Y)=\E[XY]-\E X\E Y = 0-0=0\).

Not independent: \(P(Y=0)=\tfrac13\), but \(P(Y=0\mid X=1)=0\). Knowing \(X\) tells you \(Y\) exactly. They could hardly be more dependent.

Why it happens: covariance only detects linear association. The relationship here is a perfect parabola, symmetric about zero, so the positive and negative contributions cancel exactly.

Why it matters in ML: a correlation heatmap showing zero does not license the conclusion "these features are unrelated". PCA, which uses only the covariance matrix, is blind to exactly this kind of structure — which is why nonlinear methods (kernel PCA, autoencoders, t-SNE) exist.
Not quite. Zero covariance does not imply independence. Independence requires \(P(X=x,Y=y)=P(X=x)P(Y=y)\) for every pair, which fails badly here: \(P(X=1,Y=0)=0\) while \(P(X=1)P(Y=0)=\tfrac13\cdot\tfrac13=\tfrac19\). The implication runs one way only: independent ⟹ uncorrelated.

(There is one important exception: for jointly Gaussian variables, zero correlation does imply independence. That special case is so heavily used that people forget it is special.)
Not quite. Determinism and correlation are different things. Compute it: \(\E[XY]=\E[X^3]=0\) and \(\E[X]\E[Y]=0\cdot\tfrac23=0\), so the covariance is exactly zero. The dependence is real but purely nonlinear, and covariance cannot see it.

6Concentration: how far can a sample mean stray?

The empirical risk is a sample mean. So the question "is \(\hat R\) close to \(R\)?" is the question "how close is a sample mean to the true mean?". Two inequalities answer it with no assumptions beyond finite moments.

Markov's inequality, then Chebyshev's0 / 4
\[X \ge 0, \quad a>0: \qquad \E[X] = \int_0^\infty x f(x)\,dx \;\ge\; \int_a^\infty x f(x)\,dx\]
Throw away part of the integral. The integrand \(xf(x)\) is non-negative because \(X\ge0\), so discarding \([0,a)\) can only decrease the total.
\[\ge \int_a^\infty a\,f(x)\,dx = a\int_a^\infty f(x)\,dx = a\,P(X\ge a)\]
Bound the integrand below. On the region \([a,\infty)\) we have \(x\ge a\), so replace \(x\) by \(a\) and pull it out. The remaining integral is exactly \(P(X\ge a)\).
\[\boxed{\;P(X\ge a) \le \frac{\E[X]}{a}\;} \qquad\textbf{(Markov)}\]
Rearrange. Two crude steps and we have a universal bound: a non-negative random variable cannot often be much larger than its mean. For instance, at most 10% of a population can earn ten times the average income. No distributional assumptions whatsoever. ∎
\[P\big(|X-\mu|\ge k\big) = P\big((X-\mu)^2 \ge k^2\big) \le \frac{\E[(X-\mu)^2]}{k^2} = \frac{\sigma^2}{k^2}\]
Chebyshev, in one line. Apply Markov to the non-negative variable \((X-\mu)^2\) with threshold \(k^2\). The first equality holds because squaring is monotone on non-negatives, so the two events are literally the same event. ∎
Setting \(k=t\sigma\) gives the memorable form \(P(|X-\mu|\ge t\sigma)\le 1/t^2\): at most 25% of any distribution lies more than two standard deviations from the mean, at most 11% beyond three.
Theorem — weak law of large numbers

Let \(X_1,\dots,X_n\) be independent with mean \(\mu\) and variance \(\sigma^2\), and \(\bar X_n=\frac1n\sum_i X_i\). Then for any \(\epsilon>0\),

\[ P\big(|\bar X_n-\mu|\ge\epsilon\big) \le \frac{\sigma^2}{n\epsilon^2} \;\xrightarrow[n\to\infty]{}\; 0. \]

Proof. By linearity, \(\E[\bar X_n]=\frac1n\sum_i\E[X_i]=\mu\) — the sample mean is unbiased. By independence and the scaling rule,

\[ \Var(\bar X_n) = \frac{1}{n^2}\sum_{i=1}^n \Var(X_i) = \frac{n\sigma^2}{n^2} = \frac{\sigma^2}{n}. \]

Apply Chebyshev to \(\bar X_n\) with \(k=\epsilon\). ∎

Note where each hypothesis was used: linearity needed nothing, but the variance step needed independence to kill the covariance terms.

The \(\sigma^2/n\) is the single most important formula in applied statistics. Error shrinks like \(\sigma/\sqrt n\), so quadrupling your data halves your error — an expensive exchange rate, and the reason benchmark differences of 0.2% on a 1000-example test set are noise.

Theorem — central limit theorem

Under the same conditions,

\[ \frac{\bar X_n - \mu}{\sigma/\sqrt{n}} \;\xrightarrow{\;d\;}\; \mathcal{N}(0,1) \quad\text{as } n\to\infty. \]

Whatever the shape of the original distribution — skewed, discrete, bimodal — the sample mean becomes Gaussian. This is why the Gaussian is everywhere, and why confidence intervals are \(\pm 1.96\,\sigma/\sqrt n\). The proof needs characteristic functions and is deferred; the figure below is the empirical demonstration.

Law of large numbers and the central limit theorem, simulatedInteractive
histogram of sample meansGaussian with matched μ, σ/√ntrue mean μ
0
0
Each bar counts how often a sample mean of n draws landed in that bin, over 4000 repetitions. At n = 1 you see the source distribution itself. Raise n and two things happen at once: the histogram NARROWS like 1/√n (law of large numbers) and its SHAPE becomes Gaussian (central limit theorem) no matter how un-Gaussian the source was. The heavy-tailed source converges much more slowly — a warning that the CLT is asymptotic and n = 30 is not a magic number.

7Mini-batch gradients: unbiased, and noisy in a way you can control

Now the result that makes modern deep learning possible. The full gradient over \(n\) examples costs \(O(n)\) per step, which is prohibitive. Instead we sample a batch \(\mathcal{B}\) of size \(B\) uniformly at random and use

\[\vg_{\mathcal{B}} = \frac{1}{B}\sum_{i\in\mathcal{B}}\grad_{\vt}\,\ell_i(\vt).\]

The mini-batch gradient is unbiased, with variance σ²/B0 / 4
\[\vg_{\text{full}} = \frac1n\sum_{i=1}^{n}\grad\ell_i, \qquad \vg_{\mathcal{B}} = \frac{1}{B}\sum_{i\in\mathcal{B}}\grad\ell_i\]
Set up. Sampling \(B\) indices uniformly at random, with replacement (which makes the algebra clean; without replacement the variance is slightly smaller by a finite-population correction).
\[\E[\vg_{\mathcal{B}}] = \frac1B\sum_{j=1}^{B}\E\big[\grad\ell_{I_j}\big] = \frac1B \cdot B \cdot \left(\frac1n\sum_{i=1}^n \grad\ell_i\right) = \vg_{\text{full}}\]
Unbiased. Each sampled index \(I_j\) is uniform on \(\{1,\dots,n\}\), so \(\E[\grad\ell_{I_j}] = \sum_i \frac1n\grad\ell_i = \vg_{\text{full}}\). Linearity of expectation does the rest — and note we needed no independence between the terms of the loss, only uniform sampling. On average, the mini-batch gradient points exactly where the full gradient points.
\[\Var(\vg_{\mathcal{B}}) = \frac{1}{B^2}\sum_{j=1}^{B}\Var\big(\grad\ell_{I_j}\big) = \frac{\sigma^2}{B}, \qquad \sigma^2 := \frac1n\sum_i\|\grad\ell_i - \vg_{\text{full}}\|^2\]
Variance. The \(B\) draws are independent, so variances add and the \(1/B^2\) prefactor leaves \(\sigma^2/B\). This is the law of large numbers again, applied to gradients instead of scalars.
\[\text{noise scale} = \frac{\sigma}{\sqrt{B}}\]
The practical consequence. To halve the gradient noise you must quadruple the batch size, at four times the compute cost per step. That poor exchange rate is exactly why small batches are used in practice: the extra steps you can afford are worth more than the noise reduction. It is also why the 'linear scaling rule' (double the batch, double the learning rate) works to first order — you can afford a bigger step when the direction is more reliable.
Where this shows up in ML

Gradient noise is not purely a cost. There is good evidence that it acts as an implicit regulariser: it lets the iterate escape sharp minima and saddle plateaus, and there is a persistent empirical finding that very large batches generalise worse unless the learning rate and schedule are adjusted. The mechanism is still debated, and this is an active research area rather than settled theory — I flag it as such rather than as a fact.

Verifying everything numericallyPython · NumPy

8Exercises

★

A fair six-sided die is rolled. Let \(X\) be the value shown.

  1. Compute \(\E[X]\) and \(\Var(X)\).
  2. Let \(Y=2X+3\). Compute \(\E[Y]\) and \(\Var(Y)\) two ways: from the rules, and directly from the distribution of \(Y\).
  3. Two independent dice, \(S=X_1+X_2\). Compute \(\E[S]\) and \(\Var(S)\).

1. \(\E[X]=\frac{1+2+\cdots+6}{6}=\frac{21}{6}=3.5\).
\(\E[X^2]=\frac{1+4+9+16+25+36}{6}=\frac{91}{6}\approx15.1667\).
\(\Var(X)=\frac{91}{6}-3.5^2 = 15.1667-12.25 = \frac{35}{12}\approx 2.9167\).

2. By the rules: \(\E[Y]=2(3.5)+3=10\), \(\Var(Y)=2^2\cdot\frac{35}{12}=\frac{35}{3} \approx11.667\).
Directly: \(Y\) is uniform on \(\{5,7,9,11,13,15\}\), mean \(\frac{60}{6}=10\) ✓. And \(\E[Y^2]=\frac{25+49+81+121+169+225}{6}=\frac{670}{6}\approx111.667\), so \(\Var(Y)=111.667-100=11.667\) ✓.

3. \(\E[S]=3.5+3.5=7\) by linearity (which would hold even for loaded or correlated dice). \(\Var(S)=\frac{35}{12}+\frac{35}{12}=\frac{35}{6}\approx5.833\) — this one does require independence.
Note \(\sigma_S=\sqrt{5.833}\approx2.415\), while \(\sigma_X\approx1.708\). The sum's spread grew by a factor of \(\sqrt2\), not 2: independent noise adds in quadrature. That is the same \(\sqrt n\) that governs everything in this lecture.

★★

Prove that among all constants \(c\), the value minimising \(\E[(X-c)^2]\) is \(c=\E[X]\).

Then explain what this says about (a) why the mean is the "right" summary for squared-error problems, and (b) what a model trained with squared error is actually predicting when the target is noisy.

Define \(g(c)=\E[(X-c)^2]\). Expand using linearity:

\[g(c) = \E[X^2] - 2c\,\E[X] + c^2.\]

This is a quadratic in \(c\) with positive leading coefficient, so Lecture 7's machinery applies directly:

\[g'(c) = -2\E[X]+2c = 0 \;\Longrightarrow\; c=\E[X], \qquad g''(c)=2>0,\]

so it is the unique global minimum, and the minimum value is \(g(\E[X]) = \E[X^2]-(\E X)^2 = \Var(X)\). ∎

A neat corollary: the best constant predictor has squared error exactly equal to the variance. Variance is the irreducible error of the best constant model.

(a) Whenever your loss is squared error, the optimal single number to output is the mean. (Contrast: the optimal constant under absolute error is the median, and under 0–1 loss it is the mode. The loss function chooses the summary statistic, not your intuition.)

(b) This is the important one. Extend the argument conditionally: for each \(\vx\), the best prediction under squared error is \(\E[Y\mid \mX=\vx]\). So a model trained with MSE is estimating the conditional mean of the target, not a typical or plausible value of it.

Consequences you will actually meet: an MSE-trained image model produces blurry outputs, because the average of all plausible completions is blurry. An MSE-trained forecaster underestimates volatility. If you want a plausible sample rather than an average, you need a different objective — which is precisely the motivation for GANs, diffusion models, and likelihood-based generative models.

★★

The sample variance is often defined with \(n-1\) in the denominator:

\[ s^2 = \frac{1}{n-1}\sum_{i=1}^n (X_i-\bar X)^2. \]

Prove that \(\E[s^2]=\sigma^2\), and that using \(n\) instead would give \(\frac{n-1}{n}\sigma^2\) — biased low. Explain intuitively where the lost degree of freedom goes.

Write \(T=\sum_i (X_i-\bar X)^2\). The trick is to insert and remove \(\mu\):

\[X_i-\bar X = (X_i-\mu)-(\bar X-\mu).\]

Square and sum, writing \(D_i = X_i-\mu\) and \(\bar D = \bar X - \mu\):

\[T = \sum_i D_i^2 - 2\bar D\sum_i D_i + n\bar D^2 = \sum_i D_i^2 - 2\bar D(n\bar D) + n\bar D^2 = \sum_i D_i^2 - n\bar D^2,\]

using \(\sum_i D_i = n\bar D\). Now take expectations, using \(\E[D_i^2]=\sigma^2\) and \(\E[\bar D^2]=\Var(\bar X)=\sigma^2/n\):

\[\E[T] = n\sigma^2 - n\cdot\frac{\sigma^2}{n} = (n-1)\sigma^2.\]

So \(\E\!\left[\frac{T}{n-1}\right]=\sigma^2\) ✓, while \(\E\!\left[\frac{T}{n}\right] =\frac{n-1}{n}\sigma^2<\sigma^2\). ∎

Where the degree of freedom goes. The deviations are measured from \(\bar X\), which was computed from the same data and is therefore pulled towards the sample. By the previous exercise, \(\bar X\) is the constant that minimises \(\sum(X_i-c)^2\), so \(\sum(X_i-\bar X)^2 \le \sum(X_i-\mu)^2\) always — the sum of squared deviations about the sample mean is systematically too small. Dividing by \(n-1\) corrects exactly for that.

The residuals also satisfy one linear constraint, \(\sum_i(X_i-\bar X)=0\): given any \(n-1\) of them the last is determined. Only \(n-1\) are free, hence the name.

Relevance. The same phenomenon is why training error underestimates test error: the parameters were fitted to the very data you are evaluating on. Least squares with \(d\) parameters has \(n-d\) residual degrees of freedom, and the unbiased noise estimate is \(\frac{\|\vr\|^2}{n-d}\), not \(\frac{\|\vr\|^2}{n}\). Overfitting is this bias taken to an extreme.

★★★

Coding + analysis. Investigate how gradient noise interacts with the learning rate.

  1. Set up linear regression on \(n=4096\) points with noisy targets. Compute the exact optimum with lstsq.
  2. Run SGD with batch sizes \(B\in\{1,8,64,512,4096\}\), each for the same number of epochs, with a fixed learning rate. Record the final distance to the optimum.
  3. Repeat with the linear scaling rule: \(\eta \propto B\). What changes?
  4. With \(B=1\) and constant \(\eta\), does SGD converge to the optimum, or to a distribution around it? Measure the stationary variance and see how it scales with \(\eta\).
import numpy as np
rng = np.random.default_rng(0)
n, d = 4096, 10
X = rng.normal(size=(n, d)); w_true = rng.normal(size=d)
y = X @ w_true + 2.0*rng.normal(size=n)
w_star = np.linalg.lstsq(X, y, rcond=None)[0]

def sgd(B, eta, epochs=30, seed=1):
    r = np.random.default_rng(seed); w = np.zeros(d)
    for _ in range(epochs):
        perm = r.permutation(n)
        for s in range(0, n, B):
            idx = perm[s:s+B]
            g = 2 * X[idx].T @ (X[idx] @ w - y[idx]) / len(idx)
            w = w - eta*g
            if not np.isfinite(w).all(): return None
    return w

print("--- fixed eta ---")
for B in (1, 8, 64, 512, 4096):
    w = sgd(B, 0.002)
    print(f"B={B:5d}  dist to optimum = "
          f"{'DIVERGED' if w is None else f'{np.linalg.norm(w-w_star):.5f}'}")

print("\\n--- linear scaling rule: eta proportional to B ---")
for B in (1, 8, 64, 512, 4096):
    w = sgd(B, 0.002*B/8)
    print(f"B={B:5d}  eta={0.002*B/8:.5f}  dist = "
          f"{'DIVERGED' if w is None else f'{np.linalg.norm(w-w_star):.5f}'}")

print("\\n--- stationary noise floor at B=1 ---")
for eta in (0.0002, 0.0005, 0.001, 0.002):
    w = sgd(1, eta, epochs=60)
    if w is None: print(f"eta={eta}: DIVERGED"); continue
    tail = []
    r = np.random.default_rng(7)
    for _ in range(4000):                     # keep stepping, record the wander
        i = r.integers(0, n)
        g = 2 * X[i] * (X[i] @ w - y[i])
        w = w - eta*g; tail.append(w.copy())
    tail = np.array(tail)
    print(f"eta={eta:.4f}  mean dist {np.linalg.norm(tail.mean(0)-w_star):.5f}"
          f"   stationary sd {np.sqrt(tail.var(0).sum()):.5f}")

2. With a fixed \(\eta\), larger batches get closer to the optimum for the same number of epochs — the steps are less noisy — but they take far fewer steps, so per unit of compute the small batches usually make more progress early. Full batch (\(B=4096\)) with a small \(\eta\) barely moves in 30 epochs, because 30 epochs is only 30 steps.

3. Under linear scaling the large-batch runs improve dramatically and the final distances become comparable across \(B\) — until \(\eta\) approaches \(2/\lambda_{\max}\) of the Hessian \(2\mX^\top\mX/n\) (Lecture 11), at which point the largest-batch run diverges. That is precisely the documented failure of the linear scaling rule at very large batch: eventually stability, not noise, becomes the binding constraint.

4. With constant \(\eta\), SGD does not converge to a point. It reaches a stationary distribution around \(\vw^\star\): the mean of the iterates sits at the optimum (unbiased, as proved above) but the spread does not shrink. Measured, the stationary standard deviation scales roughly as \(\sqrt\eta\), which is what the Ornstein–Uhlenbeck approximation predicts — the noise injected per step is \(O(\eta^2\sigma^2)\) and the restoring force is \(O(\eta\lambda)\), giving a stationary variance of order \(\eta\sigma^2/\lambda\).

Consequence: constant-learning-rate SGD has a noise floor. To actually converge you must decay \(\eta\) — which is why every training recipe has a schedule, and why the Robbins–Monro conditions \(\sum\eta_t=\infty\), \(\sum\eta_t^2<\infty\) are the classical requirement for almost-sure convergence. The characteristic loss curve that plateaus and then drops sharply when the learning rate is cut is exactly this noise floor being lowered.


Next: the final lecture of Phase A. Conditional probability and Bayes' theorem, the distributions you will meet constantly, and maximum likelihood — which will let us finally derive squared error, cross-entropy, and ridge regression from a single principle.