Skip to content

Linear Regression: The Pseudo-Inverse Solution in One Step | NTU Hsuan-Tien Lin Machine Learning Foundations Lecture 9

Sep 30, 20261 min
TL;DRLinear regression writes the squared error as (1/N)‖Xw − y‖², sets the gradient to zero, and gets w_LIN = X†y with no iteration. The hat matrix H = XX† projects y onto the space spanned by the columns of X, and trace(I − H) = N − (d+1), which gives average E_in = σ²(1 − (d+1)/N) and average E_out = σ²(1 + (d+1)/N). Squared error is also an upper bound on the 0/1 error, so w_LIN can be used for binary classification too.

Look up & save

Loading dictionary…

🌏 中文版

Video status: Videos included. This article is written from the official MOOC handout 09_handout.pdf and transcripts of this lecture's four official videos, with formulas following the handout. The transcripts are machine transcripts made for this article (Whisper), not official captions, and no human has proofread them. The videos are taught in Mandarin; the slides are in English. Each section opens with its official video and the approximate time ranges. Page numbers refer to the number at the lower right of each slide (0/23 to 23/23), and wherever the text says "in the lecture", it is paraphrasing what is said in the videos.

This is Foundations Lecture 9 in Reading NTU Hsuan-Tien Lin Machine Learning Foundations & Techniques. On the Roadmap on page 1 of the handout, this lecture begins the third of the four big questions: How Can Machines Learn? The conclusion of the previous question takes up one line on the same page: learning can happen as long as there is a target distribution P(y∣x)P(y \mid \mathbf{x}) and EinE_{\text{in}} can be made low with respect to the chosen error measure err. That is the conclusion of Foundations Lecture 8: Noise and Error Measures. The lecture opens with one more remark: the VC bound, proved with so much effort for binary classification, carries over to other error measures and to noisy settings, regression included, so attention can now go to how the algorithm is designed.

The prerequisites are matrix multiplication, transposes, inverses, partial derivatives, and, from earlier lectures, EinE_{\text{in}}, EoutE_{\text{out}}, and the perceptron hypothesis sign(w⊤x)\text{sign}(\mathbf{w}^\top\mathbf{x}). After reading, you should be able to write EinE_{\text{in}} in matrix form on your own, derive the gradient, solve for wLIN\mathbf{w}_{\text{LIN}}, and run the whole algorithm by hand on a small data set; to say what the hat matrix does and where the two learning-curve formulas come from; and to explain why the regression solution can be used directly for classification.

The output changes from "approve or not" to "how much credit"

YouTube:Linear Regression :: Linear Regression Problem @ Machine Learning Foundations (機器學習基石)

(Video 00:18–02:05 sets up the problem, 02:05–04:01 covers the hypothesis, 04:01–06:41 the illustration and residuals, 06:41–09:10 the error measure, and Fun Time follows.)

The credit limit problem

The running example in earlier lectures was a bank deciding whether to issue a credit card, with only two possible outputs. Page 2 of the handout changes it to the Credit Limit Problem: for the same customer (23 years old, female, annual salary NTD 1,000,000, 1 year in residence, 0.5 year in job, current debt 200,000), the bank now has to decide how large a limit to give, and the answer on the slide reads 100,000. In the lecture, Lin explains that this is no longer a yes/no question: this customer might get NTD 100,000 and another might get NTD 80,000.

The learning flow diagram is the same as before: the unknown target function ff (the ideal credit limit formula), the training examples D\mathcal{D} (historical records in the bank), the hypothesis set H\mathcal{H}, the algorithm A\mathcal{A}, and the final g≈fg \approx f. The only difference is written at the bottom of the page:

Y=R⟹regression\mathcal{Y} = \mathbb{R} \quad\Longrightarrow\quad \text{regression}

The output space is all of the real numbers, and problems of this kind are called regression. The lecture adds a side note: a credit limit can only be positive, since a bank does not hand out a negative limit, but it is more convenient to take a wider view of the output space and treat it as all of the reals.

The hypothesis: a perceptron without the sign

Page 3 picks the simplest hypothesis. Write the customer's features as x=(x0,x1,…,xd)\mathbf{x} = (x_0, x_1, \dots, x_d), where x0x_0 is the constant term added by convention, and approximate the desired credit limit with a weighted sum:

y≈∑i=0dwixi,h(x)=w⊤xy \approx \sum_{i=0}^{d} w_i x_i, \qquad h(\mathbf{x}) = \mathbf{w}^\top\mathbf{x}

The handout's note is "like perceptron, but without the sign". The perceptron (Foundations Lecture 2: Perceptrons and PLA) computes the score w⊤x\mathbf{w}^\top\mathbf{x} and then takes its sign to answer circle or cross; linear regression computes the score and outputs it as is, because the score is already a real number.

Page 4 uses two figures to show what this hypothesis looks like:

Left figure: x=(x)∈R\mathbf{x} = (x) \in \mathbb{R}Right figure: x=(x1,x2)∈R2\mathbf{x} = (x_1, x_2) \in \mathbb{R}^2
AxesHorizontal axis xx, vertical axis yyThe base plane is x1x_1, x2x_2, the height is yy
Data pointsA cloud of circles in the planeA cloud of circles in space
HypothesisA blue lineA blue plane
Red segmentsThe vertical distance from each point to the lineThe vertical distance from each point to the plane

The lecture points out that this differs from the earlier figures: a classification figure draws only the plane of x\mathbf{x} and uses circles and crosses for yy; here yy takes an axis of its own and every data point is drawn as a circle. The red segments are called residuals, and in the lecture Lin refers to them as the "remainder": the part that is still unexplained after the hypothesis has made its prediction. The handout sums up the job of linear regression in one line: find lines/hyperplanes with small residuals.

The error measure: squared error

Whether the residuals are small needs a way of measuring. Page 5 uses the squared error, which the handout calls the popular/historical error measure:

err(y^,y)=(y^−y)2\text{err}(\hat{y}, y) = (\hat{y} - y)^2

In the lecture, Lin says linear regression is a problem the statistics community has studied for a long time, and the squared error is the measure traditionally used most. Plugged into the framework of the previous lecture, the in-sample and out-of-sample errors are

Ein(w)=1N∑n=1N(h(xn)⏟w⊤xn−yn)2,Eout(w)=E(x,y)∼P(w⊤x−y)2E_{\text{in}}(\mathbf{w}) = \frac{1}{N}\sum_{n=1}^{N}\big(\underbrace{h(\mathbf{x}_n)}_{\mathbf{w}^\top\mathbf{x}_n} - y_n\big)^2, \qquad E_{\text{out}}(\mathbf{w}) = \mathop{\mathcal{E}}_{(\mathbf{x}, y)\sim P}\big(\mathbf{w}^\top\mathbf{x} - y\big)^2

Two details of notation are both addressed in the lecture. First, each w\mathbf{w} corresponds to one hh, so the handout writes Ein(w)E_{\text{in}}(\mathbf{w}) directly rather than Ein(h)E_{\text{in}}(h), which keeps later formulas a little simpler. Second, the expectation in EoutE_{\text{out}} is taken over (x,y)(\mathbf{x}, y) together: we are already in the noisy setting of the previous lecture, where x\mathbf{x} and yy are drawn jointly from one distribution.

This article adds a small example to show what squaring does (the numbers are made up for this article, not from the slides). Work in units of NTD 10,000: predict 9 when the actual value is 10, and the error is 11; predict 12 when the actual value is 8, and the error is 1616. The gap is 4 times larger and the error is 16 times larger, so the squared error cares especially about points that are far off. This property comes back in the last section, when regression is used for classification.

The last line of this page is "next: how to minimize Ein(w)E_{\text{in}}(\mathbf{w})?". In the lecture, Lin explains that if you believe the derivation of the VC bound, and believe it extends to other problems, then this is the only thing left to do.

Fun Time: which feature should get a positive weight

Question (page 6): Consider using the linear regression hypothesis h(x)=w⊤xh(\mathbf{x}) = \mathbf{w}^\top\mathbf{x} to predict the credit limit of customers. Which feature below shall have a positive weight in a good hypothesis for the task?

  1. birth month
  2. monthly income
  3. current debt
  4. number of credit cards owned

Answer: 2. The handout's explanation: customers with higher monthly income should naturally be given a higher credit limit, which is captured by the positive weight. A positive wiw_i means that each one-unit increase in xix_i raises the predicted limit by wiw_i.

The handout does not explain the other options; what follows is this article's reasoning. Birth month has no visible relation to the ability to repay, so a good hypothesis gives it a weight close to 0. The more current debt, the less room there is to borrow more, so the reasonable weight is negative. The direction for the number of credit cards is unclear: holding many cards may mean good credit, or it may mean a lot has already been borrowed, and nothing in this question says it has to be positive.

Set the gradient to zero and solve for the best w in one step

YouTube:Linear Regression :: Linear Regression Algorithm @ Machine Learning Foundations (機器學習基石)

(Video 00:00–05:00 writes EinE_{\text{in}} in matrix form, 05:00–08:01 explains convexity and the zero gradient, 08:01–12:52 computes the gradient, 12:52–17:35 solves the equation and introduces the pseudo-inverse, 17:35–18:51 gives the complete algorithm, and Fun Time follows.)

Writing E_in in matrix form

The formula for EinE_{\text{in}} carries a running index nn. Page 7 packs it into matrices in four steps, and in the lecture Lin says the only purpose is to make the notation cleaner later.

Step 1: swap the order of the inner product. w⊤xn\mathbf{w}^\top\mathbf{x}_n is the inner product of two vectors, and an inner product is commutative, so it equals xn⊤w\mathbf{x}_n^\top\mathbf{w}:

Ein(w)=1N∑n=1N(w⊤xn−yn)2=1N∑n=1N(xn⊤w−yn)2E_{\text{in}}(\mathbf{w}) = \frac{1}{N}\sum_{n=1}^{N}\big(\mathbf{w}^\top\mathbf{x}_n - y_n\big)^2 = \frac{1}{N}\sum_{n=1}^{N}\big(\mathbf{x}_n^\top\mathbf{w} - y_n\big)^2

Step 2: a sum of squares is the squared length of a vector. Stack the NN quantities being squared into an NN-dimensional vector; its squared length is the sum of the squares of its components:

Ein(w)=1N∥[x1⊤w−y1x2⊤w−y2⋮xN⊤w−yN]∥2E_{\text{in}}(\mathbf{w}) = \frac{1}{N}\left\lVert \begin{bmatrix} \mathbf{x}_1^\top\mathbf{w} - y_1 \\ \mathbf{x}_2^\top\mathbf{w} - y_2 \\ \vdots \\ \mathbf{x}_N^\top\mathbf{w} - y_N \end{bmatrix} \right\rVert^2

Step 3: factor out the common w\mathbf{w}. Every row contains w\mathbf{w}. Stack x1⊤,…,xN⊤\mathbf{x}_1^\top, \dots, \mathbf{x}_N^\top row by row into a matrix and y1,…,yNy_1, \dots, y_N into a vector. The lecture's picture is that the originally "tall" vector xn\mathbf{x}_n lies down to become a row.

Ein(w)=1N∥[— x1⊤ —— x2⊤ —⋮— xN⊤ —]w−[y1y2⋮yN]∥2E_{\text{in}}(\mathbf{w}) = \frac{1}{N}\left\lVert \begin{bmatrix} \text{---}\ \mathbf{x}_1^\top\ \text{---} \\ \text{---}\ \mathbf{x}_2^\top\ \text{---} \\ \vdots \\ \text{---}\ \mathbf{x}_N^\top\ \text{---} \end{bmatrix}\mathbf{w} - \begin{bmatrix} y_1 \\ y_2 \\ \vdots \\ y_N \end{bmatrix} \right\rVert^2

Step 4: give them names. Call the matrix X\mathrm{X} and the vector y\mathbf{y}:

Ein(w)=1N∥Xw−y∥2E_{\text{in}}(\mathbf{w}) = \frac{1}{N}\lVert \mathrm{X}\mathbf{w} - \mathbf{y} \rVert^2

The handout marks the sizes underneath: X\mathrm{X} is N×(d+1)N \times (d+1), one row per example and one column per feature; w\mathbf{w} is (d+1)×1(d+1) \times 1; y\mathbf{y} is N×1N \times 1. X\mathrm{X} and y\mathbf{y} are both given by the data, so the only thing in the formula that can move is w\mathbf{w}.

The condition at the lowest point: zero gradient

Page 8 first states three properties of the function Ein(w)E_{\text{in}}(\mathbf{w}): continuous, differentiable, convex. For convexity the lecture gives only a physical picture: the function looks like a valley, like a bowl, and the left side of the slide draws exactly that, a curve opening upward with w\mathbf{w} on the horizontal axis and a black dot at the lowest point.

What characterizes the bottom of the bowl? The lecture uses a ball: at the bottom of the valley, the ball cannot roll down in any direction. Each component of the gradient is the slope of the function along one direction, so the gradient at the bottom must be the zero vector:

∇Ein(w)≡[∂Ein∂w0(w)∂Ein∂w1(w)⋮∂Ein∂wd(w)]=[00⋮0]\nabla E_{\text{in}}(\mathbf{w}) \equiv \begin{bmatrix} \dfrac{\partial E_{\text{in}}}{\partial w_0}(\mathbf{w}) \\ \dfrac{\partial E_{\text{in}}}{\partial w_1}(\mathbf{w}) \\ \vdots \\ \dfrac{\partial E_{\text{in}}}{\partial w_d}(\mathbf{w}) \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \\ \vdots \\ 0 \end{bmatrix}

The handout writes this as the necessary condition of the 'best' w\mathbf{w}, with the note: not possible to 'roll down'. The task therefore becomes: find wLIN\mathbf{w}_{\text{LIN}} such that ∇Ein(wLIN)=0\nabla E_{\text{in}}(\mathbf{w}_{\text{LIN}}) = \mathbf{0}. The lecture makes a point of explaining the subscript: LIN stands for linear regression and has nothing to do with the lecturer's surname.

This article adds two points. First, the handout only says this is a necessary condition; for a convex function, a point with zero gradient is always a lowest point, so the wLIN\mathbf{w}_{\text{LIN}} we solve for really is the optimal solution. The lecture says no rigorous proof is given here. Second, why it is convex: for any direction v\mathbf{v}, v⊤X⊤Xv=∥Xv∥2≥0\mathbf{v}^\top\mathrm{X}^\top\mathrm{X}\mathbf{v} = \lVert\mathrm{X}\mathbf{v}\rVert^2 \ge 0, so the quadratic term always curves upward.

Computing the gradient: one variable first, then a vector

Page 9 begins by expanding the squared length. ∥a∥2=a⊤a\lVert\mathbf{a}\rVert^2 = \mathbf{a}^\top\mathbf{a}, so

∥Xw−y∥2=(Xw−y)⊤(Xw−y)=w⊤X⊤Xw−w⊤X⊤y−y⊤Xw+y⊤y\lVert\mathrm{X}\mathbf{w} - \mathbf{y}\rVert^2 = (\mathrm{X}\mathbf{w} - \mathbf{y})^\top(\mathrm{X}\mathbf{w} - \mathbf{y}) = \mathbf{w}^\top\mathrm{X}^\top\mathrm{X}\mathbf{w} - \mathbf{w}^\top\mathrm{X}^\top\mathbf{y} - \mathbf{y}^\top\mathrm{X}\mathbf{w} + \mathbf{y}^\top\mathbf{y}

The two middle terms are both scalars and are transposes of each other, so they are equal and combine into −2w⊤X⊤y-2\mathbf{w}^\top\mathrm{X}^\top\mathbf{y}. In the lecture, Lin compares this to the −2ab-2ab in the expansion of (a−b)2(a - b)^2. The handout gives temporary names to the three quantities determined by the data:

Ein(w)=1N(w⊤X⊤X⏟Aw−2w⊤X⊤y⏟b+y⊤y⏟c)E_{\text{in}}(\mathbf{w}) = \frac{1}{N}\Big(\mathbf{w}^\top\underbrace{\mathrm{X}^\top\mathrm{X}}_{\mathrm{A}}\mathbf{w} - 2\mathbf{w}^\top\underbrace{\mathrm{X}^\top\mathbf{y}}_{\mathbf{b}} + \underbrace{\mathbf{y}^\top\mathbf{y}}_{c}\Big)

A\mathrm{A} is a (d+1)×(d+1)(d+1) \times (d+1) matrix, b\mathbf{b} is a d+1d+1-dimensional vector, and cc is a number. The three terms are quadratic, linear, and constant in w\mathbf{w}.

In the lecture, Lin says many people get nervous at this point, feeling they never learned vector calculus properly. The handout's approach is to look at the case of a single ww first and then match it to the vector case:

One ww onlyVector w\mathbf{w}
EinE_{\text{in}}1N(aw2−2bw+c)\frac{1}{N}(aw^2 - 2bw + c)1N(w⊤Aw−2w⊤b+c)\frac{1}{N}(\mathbf{w}^\top\mathrm{A}\mathbf{w} - 2\mathbf{w}^\top\mathbf{b} + c)
∇Ein\nabla E_{\text{in}}1N(2aw−2b)\frac{1}{N}(2aw - 2b)1N(2Aw−2b)\frac{1}{N}(2\mathrm{A}\mathbf{w} - 2\mathbf{b})
Handout's notesimple!similar (derived by definition)

The left side is the high-school derivative of a quadratic in one variable. The right side looks identical, except that aa becomes a matrix and bb becomes a vector. The handout writes "derived by definition" and lists no steps; the lecture says working it out from the definition gives the result, and that the derivation uses the fact that A\mathrm{A} is a symmetric matrix. The derivation below is an addition in this article, done from the definition.

Take the partial derivative with respect to the kk-th component wkw_k. In the linear term w⊤b=∑iwibi\mathbf{w}^\top\mathbf{b} = \sum_i w_i b_i, only the i=ki = k term contains wkw_k, and its derivative is bkb_k. In the quadratic term w⊤Aw=∑i∑jwiAijwj\mathbf{w}^\top\mathrm{A}\mathbf{w} = \sum_i\sum_j w_i A_{ij} w_j, wkw_k appears in the terms with i=ki = k and also in the terms with j=kj = k:

∂∂wk∑i∑jwiAijwj=∑jAkjwj+∑iwiAik=(Aw)k+(A⊤w)k\frac{\partial}{\partial w_k}\sum_{i}\sum_{j} w_i A_{ij} w_j = \sum_{j} A_{kj} w_j + \sum_{i} w_i A_{ik} = (\mathrm{A}\mathbf{w})_k + (\mathrm{A}^\top\mathbf{w})_k

(The term with i=j=ki = j = k is Akkwk2A_{kk}w_k^2, whose derivative is 2Akkwk2A_{kk}w_k; it appears exactly once in each of the two sums, so nothing is counted twice.) The transpose of A=X⊤X\mathrm{A} = \mathrm{X}^\top\mathrm{X} is (X⊤X)⊤=X⊤X(\mathrm{X}^\top\mathrm{X})^\top = \mathrm{X}^\top\mathrm{X}, so it is symmetric, which gives (A⊤w)k=(Aw)k(\mathrm{A}^\top\mathbf{w})_k = (\mathrm{A}\mathbf{w})_k, and the partial derivative of the quadratic term is 2(Aw)k2(\mathrm{A}\mathbf{w})_k. Stack k=0,1,…,dk = 0, 1, \dots, d and put A\mathrm{A} and b\mathbf{b} back in their original form:

∇Ein(w)=1N(2Aw−2b)=2N(X⊤Xw−X⊤y)\nabla E_{\text{in}}(\mathbf{w}) = \frac{1}{N}(2\mathrm{A}\mathbf{w} - 2\mathbf{b}) = \frac{2}{N}\big(\mathrm{X}^\top\mathrm{X}\mathbf{w} - \mathrm{X}^\top\mathbf{y}\big)

Solving the equation: the pseudo-inverse

Page 10 sets the gradient to zero:

2N(X⊤Xw−X⊤y)=0⟺X⊤Xw=X⊤y\frac{2}{N}\big(\mathrm{X}^\top\mathrm{X}\mathbf{w} - \mathrm{X}^\top\mathbf{y}\big) = \mathbf{0} \quad\Longleftrightarrow\quad \mathrm{X}^\top\mathrm{X}\mathbf{w} = \mathrm{X}^\top\mathbf{y}

X⊤X\mathrm{X}^\top\mathrm{X} and X⊤y\mathrm{X}^\top\mathbf{y} can both be computed, the only unknown is w\mathbf{w}, and every unknown appears to the first power. In the lecture, Lin says this is simply a system of linear equations. Two cases follow.

Invertible X⊤X\mathrm{X}^\top\mathrm{X}. The handout writes: easy! unique solution. Multiply both sides on the left by the inverse:

wLIN=(X⊤X)−1X⊤⏟pseudo-inverse X† y\mathbf{w}_{\text{LIN}} = \underbrace{\big(\mathrm{X}^\top\mathrm{X}\big)^{-1}\mathrm{X}^\top}_{\text{pseudo-inverse } \mathrm{X}^\dagger}\,\mathbf{y}

The block (X⊤X)−1X⊤(\mathrm{X}^\top\mathrm{X})^{-1}\mathrm{X}^\top has a name, the pseudo-inverse, written X†\mathrm{X}^\dagger. In the lecture, Lin explains why it is called a "fake" inverse: if X\mathrm{X} were square and had an inverse, this block would simplify to X−1\mathrm{X}^{-1}; when X\mathrm{X} is not square, it still behaves much like an inverse. This article adds a property you can check right away: X†X=(X⊤X)−1X⊤X=I\mathrm{X}^\dagger\mathrm{X} = (\mathrm{X}^\top\mathrm{X})^{-1}\mathrm{X}^\top\mathrm{X} = \mathrm{I}, so multiplying on the left gives the identity matrix.

The handout says this case is often the case because N≫d+1N \gg d + 1. The lecture's explanation is that X⊤X\mathrm{X}^\top\mathrm{X} is only (d+1)×(d+1)(d+1) \times (d+1), the number of examples NN is usually much larger than dd, and the matrix has enough degrees of freedom, so it is invertible most of the time. This article makes that statement more concrete: if X⊤Xv=0\mathrm{X}^\top\mathrm{X}\mathbf{v} = \mathbf{0}, then v⊤X⊤Xv=∥Xv∥2=0\mathbf{v}^\top\mathrm{X}^\top\mathrm{X}\mathbf{v} = \lVert\mathrm{X}\mathbf{v}\rVert^2 = 0, which means Xv=0\mathrm{X}\mathbf{v} = \mathbf{0}. So X⊤X\mathrm{X}^\top\mathrm{X} fails to be invertible only when some feature happens to be an exact linear combination of the other features on all NN examples. The more data there is, the harder it is for that coincidence to occur.

Singular X⊤X\mathrm{X}^\top\mathrm{X}. The handout writes "many optimal solutions": the equation has many solutions, and each of them is optimal. In this case X†\mathrm{X}^\dagger is defined in other ways, and wLIN=X†y\mathbf{w}_{\text{LIN}} = \mathrm{X}^\dagger\mathbf{y} is still one of the solutions. In the lecture, Lin says the details of the definition are in linear algebra textbooks, and most numerical software already implements it.

Both cases are written as the same formula:

wLIN=X†y\mathbf{w}_{\text{LIN}} = \mathrm{X}^\dagger\mathbf{y}

The practical suggestion on this page sits in a yellow box: use a well-implemented †\dagger routine instead of computing (X⊤X)−1X⊤(\mathrm{X}^\top\mathrm{X})^{-1}\mathrm{X}^\top yourself, for numerical stability when the matrix is almost-singular. The lecture mentions one more benefit: you do not have to decide for yourself whether the matrix is invertible.

The complete algorithm

Page 11 wraps the whole thing into three steps:

  1. From the data D\mathcal{D}, construct the input matrix X\mathrm{X} (N×(d+1)N \times (d+1)) and the output vector y\mathbf{y} (N×1N \times 1).
  2. Calculate the pseudo-inverse X†\mathrm{X}^\dagger ((d+1)×N(d+1) \times N).
  3. Return wLIN=X†y\mathbf{w}_{\text{LIN}} = \mathrm{X}^\dagger\mathbf{y} ((d+1)×1(d+1) \times 1).

The handout's verdict is: simple and efficient with good †\dagger routine. In the lecture, Lin says the first step takes almost no work, since it only puts the inputs and outputs of the data in separate places; and that some people have only met regression in one or two dimensions, whereas the derivation here uses vectors and matrices throughout, so the same procedure works in any number of dimensions.

Numerical example: four customers, the whole algorithm by hand

The following is an example made up for this article, not from the slides. There are four customers, the only feature is annual income (in units of NTD 1 million), and the quantity to predict is the credit limit (in units of NTD 100,000):

CustomerAnnual income xx (NTD million)Credit limit yy (NTD 100,000)
112
223
335
446

Step 1: build X\mathrm{X} and y\mathbf{y}. Prepend x0=1x_0 = 1 to every example. Here N=4N = 4 and d=1d = 1:

X=[11121314],y=[2356]\mathrm{X} = \begin{bmatrix} 1 & 1 \\ 1 & 2 \\ 1 & 3 \\ 1 & 4 \end{bmatrix}, \qquad \mathbf{y} = \begin{bmatrix} 2 \\ 3 \\ 5 \\ 6 \end{bmatrix}

Step 2: compute A\mathrm{A}, b\mathbf{b}, cc. The entries of A\mathrm{A} are ∑1=4\sum 1 = 4, ∑xn=10\sum x_n = 10, and ∑xn2=1+4+9+16=30\sum x_n^2 = 1 + 4 + 9 + 16 = 30; the two components of b\mathbf{b} are ∑yn=16\sum y_n = 16 and ∑xnyn=2+6+15+24=47\sum x_n y_n = 2 + 6 + 15 + 24 = 47; c=4+9+25+36=74c = 4 + 9 + 25 + 36 = 74.

A=X⊤X=[4101030],b=X⊤y=[1647],c=74\mathrm{A} = \mathrm{X}^\top\mathrm{X} = \begin{bmatrix} 4 & 10 \\ 10 & 30 \end{bmatrix}, \qquad \mathbf{b} = \mathrm{X}^\top\mathbf{y} = \begin{bmatrix} 16 \\ 47 \end{bmatrix}, \qquad c = 74

Step 3: solve Aw=b\mathrm{A}\mathbf{w} = \mathbf{b}. That is, 4w0+10w1=164w_0 + 10w_1 = 16 and 10w0+30w1=4710w_0 + 30w_1 = 47. The determinant of A\mathrm{A} is 4×30−10×10=204 \times 30 - 10 \times 10 = 20, which is not 0, so the matrix is invertible:

A−1=120[30−10−104],wLIN=A−1b=120[30×16−10×47−10×16+4×47]=120[1028]=[0.51.4]\mathrm{A}^{-1} = \frac{1}{20}\begin{bmatrix} 30 & -10 \\ -10 & 4 \end{bmatrix}, \qquad \mathbf{w}_{\text{LIN}} = \mathrm{A}^{-1}\mathbf{b} = \frac{1}{20}\begin{bmatrix} 30 \times 16 - 10 \times 47 \\ -10 \times 16 + 4 \times 47 \end{bmatrix} = \frac{1}{20}\begin{bmatrix} 10 \\ 28 \end{bmatrix} = \begin{bmatrix} 0.5 \\ 1.4 \end{bmatrix}

If you follow the algorithm and compute the pseudo-inverse first, it is a 2×42 \times 4 matrix whose nn-th column is A−1\mathrm{A}^{-1} times (1,xn)(1, x_n):

X†=A−1X⊤=120[20100−10−6−226]\mathrm{X}^\dagger = \mathrm{A}^{-1}\mathrm{X}^\top = \frac{1}{20}\begin{bmatrix} 20 & 10 & 0 & -10 \\ -6 & -2 & 2 & 6 \end{bmatrix}

Multiply by y\mathbf{y}: the first row gives 120(40+30+0−60)=0.5\frac{1}{20}(40 + 30 + 0 - 60) = 0.5 and the second row gives 120(−12−6+10+36)=1.4\frac{1}{20}(-12 - 6 + 10 + 36) = 1.4, the same as above.

Step 4: look at the result. The hypothesis is h(x)=0.5+1.4xh(x) = 0.5 + 1.4x.

Customeryny_ny^n=0.5+1.4xn\hat{y}_n = 0.5 + 1.4x_nResidual yn−y^ny_n - \hat{y}_nSquared residual
121.90.10.01
233.3−0.30.09
354.70.30.09
466.1−0.10.01
Ein(wLIN)=0.01+0.09+0.09+0.014=0.05E_{\text{in}}(\mathbf{w}_{\text{LIN}}) = \frac{0.01 + 0.09 + 0.09 + 0.01}{4} = 0.05

Compute it once more with the formula in A\mathrm{A}, b\mathbf{b}, cc: w⊤Aw=73.8\mathbf{w}^\top\mathrm{A}\mathbf{w} = 73.8, 2w⊤b=147.62\mathbf{w}^\top\mathbf{b} = 147.6, and 14(73.8−147.6+74)=0.05\frac{1}{4}(73.8 - 147.6 + 74) = 0.05, which agrees.

What this result means. w1=1.4w_1 = 1.4 means that each additional NTD 1 million of annual income raises the predicted limit by NTD 140,000; w0=0.5w_0 = 0.5 is a base limit of NTD 50,000. The unit of Ein=0.05E_{\text{in}} = 0.05 is "(NTD 100,000) squared"; its square root is about 0.224, so on average the prediction is off from the actual value by a little over NTD 20,000. Comparing against a few other choices of w\mathbf{w} shows that the one with zero gradient is indeed the lowest:

w=(w0,w1)\mathbf{w} = (w_0, w_1)Ein(w)E_{\text{in}}(\mathbf{w})∇Ein(w)=24(Aw−b)\nabla E_{\text{in}}(\mathbf{w}) = \frac{2}{4}(\mathrm{A}\mathbf{w} - \mathbf{b})
(0,0)(0, 0)18.518.5(−8,−23.5)(-8, -23.5)
(1,1)(1, 1)0.50.5(−1,−3.5)(-1, -3.5)
(0,1.5)(0, 1.5)0.1250.125(−0.5,−1)(-0.5, -1)
(0.5,1.4)(0.5, 1.4)0.050.05(0,0)(0, 0)

The singular case. Add one redundant feature: x2=2x1x_2 = 2x_1 (the same annual income, now in units of NTD 500,000). The third column of X\mathrm{X} is twice the second, and the determinant of X⊤X\mathrm{X}^\top\mathrm{X} is 0. Now h(x)=w0+(w1+2w2)x1h(\mathbf{x}) = w_0 + (w_1 + 2w_2)x_1, and as long as w0=0.5w_0 = 0.5 and w1+2w2=1.4w_1 + 2w_2 = 1.4, the predictions are exactly the same as before and EinE_{\text{in}} is 0.05 in every case. (0.5,1.4,0)(0.5, 1.4, 0), (0.5,0,0.7)(0.5, 0, 0.7), and (0.5,3.4,−1)(0.5, 3.4, -1) are all optimal solutions, which is what the handout means by many optimal solutions.

Running it in NumPy: np.linalg.inv(X.T @ X) raises a Singular matrix error outright; np.linalg.pinv(X) @ y returns (0.5,0.28,0.56)(0.5, 0.28, 0.56), which satisfies 0.28+2×0.56=1.40.28 + 2 \times 0.56 = 1.4 and is one of that whole line of optimal solutions. The squared lengths of the four solutions are 2.212.21, 0.740.74, 12.8112.81, and 0.6420.642, and pinv gives the shortest one. This is a property of the Moore-Penrose pseudo-inverse; the handout does not mention it, and it is an addition in this article.

Fun Time: the matrix formula for the prediction vector

Question (page 12): After getting wLIN\mathbf{w}_{\text{LIN}}, we can calculate the predictions y^n=wLIN⊤xn\hat{y}_n = \mathbf{w}_{\text{LIN}}^\top\mathbf{x}_n. If all y^n\hat{y}_n are collected in a vector y^\hat{\mathbf{y}} similar to how we form y\mathbf{y}, what is its matrix formula?

  1. y\mathbf{y}
  2. XX⊤y\mathrm{X}\mathrm{X}^\top\mathbf{y}
  3. XX†y\mathrm{X}\mathrm{X}^\dagger\mathbf{y}
  4. XX†XX⊤y\mathrm{X}\mathrm{X}^\dagger\mathrm{X}\mathrm{X}^\top\mathbf{y}

Answer: 3. The handout's solution: y^=XwLIN\hat{\mathbf{y}} = \mathrm{X}\mathbf{w}_{\text{LIN}}, and substituting wLIN=X†y\mathbf{w}_{\text{LIN}} = \mathrm{X}^\dagger\mathbf{y} gives XX†y\mathrm{X}\mathrm{X}^\dagger\mathbf{y}. The nn-th component of Xw\mathrm{X}\mathbf{w} is xn⊤w\mathbf{x}_n^\top\mathbf{w}, which is exactly the prediction for the nn-th example.

The analysis of the other options is this article's. Option 1 says the predictions hit every label exactly, which is true only when all residuals are 0; in the four-customer example, y^=(1.9,3.3,4.7,6.1)\hat{\mathbf{y}} = (1.9, 3.3, 4.7, 6.1) is not equal to y\mathbf{y}. Option 2 wrongly replaces X†\mathrm{X}^\dagger with X⊤\mathrm{X}^\top, dropping (X⊤X)−1(\mathrm{X}^\top\mathrm{X})^{-1}; on the same example it comes out as (63,110,157,204)(63, 110, 157, 204). Option 4, because XX†X=X(X†X)=X\mathrm{X}\mathrm{X}^\dagger\mathrm{X} = \mathrm{X}(\mathrm{X}^\dagger\mathrm{X}) = \mathrm{X}, simplifies to the same formula as option 2.

After giving the answer, the lecture adds a reminder: only one matrix, XX†\mathrm{X}\mathrm{X}^\dagger, stands between y\mathbf{y} and y^\hat{\mathbf{y}}, and the analysis in the next section uses it throughout.

Does it count as learning without step-by-step improvement: the hat matrix and the learning curve

YouTube:Linear Regression :: Generalization Issue @ Machine Learning Foundations (機器學習基石)

(Video 00:00–03:50 discusses whether this counts as learning, 03:50–08:25 covers the average EinE_{\text{in}} and the hat matrix, 08:25–12:45 the geometric picture and the trace, 12:45–16:52 the noise argument and EoutE_{\text{out}}, 16:52–19:12 the learning curve, and Fun Time follows.)

An algorithm that gets there in one step

The lecture begins by asking the question on the students' behalf: is this really machine learning? The algorithm computes the answer all at once, as if it reached the top in a single step, and no learning process is visible. Page 13 sets the two sides next to each other:

No!Yes!
The solution is analytic (closed-form), 'instantaneous'Good EinE_{\text{in}}? Yes, optimal
Not improving EinE_{\text{in}} nor EoutE_{\text{out}} iterativelyGood EoutE_{\text{out}}? Yes, finite dVCd_{\text{VC}} like perceptrons
Improving iteratively? Somewhat, within an iterative pseudo-inverse routine

The second row on the right rests on Foundations Lecture 7: The VC Dimension: the dVCd_{\text{VC}} of dd-dimensional perceptrons is d+1d + 1, which is finite. In the lecture, Lin says the model has only d+1d + 1 variables, and with enough data a good EinE_{\text{in}} carries over into a good EoutE_{\text{out}}. For the third row, the handout says the pseudo-inverse routine is iterative inside; the example in the lecture is Gaussian elimination for computing an inverse, which is itself a loop, and if you laid out the intermediate steps you would see EinE_{\text{in}} getting better step by step as well. The routine is just packaged so well that you do not see it.

The handout's conclusion is: if Eout(wLIN)E_{\text{out}}(\mathbf{w}_{\text{LIN}}) is good, learning 'happened'! The lecture uses an exam as the analogy: whether it studied in secret under the covers or read aloud to you step by step, if it does well on the final exam (EoutE_{\text{out}}), learning took place in between.

Another kind of guarantee: the average E_in

The VC bound can guarantee EoutE_{\text{out}}. This section gives a different kind of guarantee, under the handout's title 'Simpler-than-VC' Guarantee. It is available because linear regression has an analytic solution and the formulas can be written down. The object of the analysis is the average of EinE_{\text{in}} (page 14):

Ein‾=ED∼PN{Ein(wLIN w.r.t. D)} =to be shown noise level⋅(1−d+1N)\overline{E_{\text{in}}} = \mathop{\mathcal{E}}_{\mathcal{D}\sim P^N}\Big\{E_{\text{in}}(\mathbf{w}_{\text{LIN}} \text{ w.r.t. } \mathcal{D})\Big\} \ \overset{\text{to be shown}}{=}\ \text{noise level}\cdot\Big(1 - \frac{d+1}{N}\Big)

The average is taken over data sets. The lecture reuses the earlier analogy: grab a handful of data from the bin and compute EinE_{\text{in}} once, grab another handful and compute it again, and average over every handful you could possibly grab. The lecture also names the difference from VC: VC looks at an individual EinE_{\text{in}}, and this looks at the average. The noise level is the strength of the noise in the data, the part that, as the previous lecture said, even the best target function cannot get right.

First write down EinE_{\text{in}} for a single data set. Use y^=XX†y\hat{\mathbf{y}} = \mathrm{X}\mathrm{X}^\dagger\mathbf{y} from the previous Fun Time:

Ein(wLIN)=1N∥y−y^∥2=1N∥y−XX†y∥2=1N∥(I−XX†)y∥2E_{\text{in}}(\mathbf{w}_{\text{LIN}}) = \frac{1}{N}\lVert\mathbf{y} - \hat{\mathbf{y}}\rVert^2 = \frac{1}{N}\lVert\mathbf{y} - \mathrm{X}\mathrm{X}^\dagger\mathbf{y}\rVert^2 = \frac{1}{N}\lVert(\mathrm{I} - \mathrm{X}\mathrm{X}^\dagger)\mathbf{y}\rVert^2

The last step writes y\mathbf{y} as Iy\mathrm{I}\mathbf{y} and factors it out. The handout calls XX†\mathrm{X}\mathrm{X}^\dagger the hat matrix, written H\mathrm{H}, for the reason printed on the slide: because it puts ∧ on y\mathbf{y}. In the lecture, Lin says the name comes from the statistics community: multiply y\mathbf{y} by it and a hat grows on its head, turning it into y^\hat{\mathbf{y}}, which makes it easy to remember.

The hat matrix is a projection

Page 15 draws a figure in RN\mathbb{R}^N. Note that this space has NN dimensions: y=(y1,…,yN)\mathbf{y} = (y_1, \dots, y_N) is one vector in it, with one axis per example, which is a different thing from the earlier figures whose axes were features. There are four things in the figure:

  • The pink region: the space spanned by the d+1d + 1 columns of X\mathrm{X} (span of X\mathrm{X}). Each column is also an NN-dimensional vector.
  • The purple y\mathbf{y}: starts at the origin and points at a slant out of the pink region.
  • The black y^\hat{\mathbf{y}}: lies inside the pink region.
  • The green dashed line y−y^\mathbf{y} - \hat{\mathbf{y}}: goes straight up from the tip of y^\hat{\mathbf{y}} to the tip of y\mathbf{y}.

The handout lists four statements. Taking them one at a time:

  1. y^=XwLIN\hat{\mathbf{y}} = \mathrm{X}\mathbf{w}_{\text{LIN}} is within the span. The lecture's explanation: X\mathrm{X} times any w\mathbf{w} takes the components of w\mathbf{w} as coefficients and forms a linear combination of the columns of X\mathrm{X}, so the result always lands in the span. Different w\mathbf{w} correspond to different points in the span.
  2. y−y^\mathbf{y} - \hat{\mathbf{y}} is to be smallest, so it is perpendicular to the span. Linear regression wants ∥y−y^∥\lVert\mathbf{y} - \hat{\mathbf{y}}\rVert as small as possible, that is, the point in the span closest to y\mathbf{y}. The closest point is the one directly below, the perpendicular projection.
  3. H\mathrm{H} projects y\mathbf{y} to y^\hat{\mathbf{y}} in the span.
  4. I−H\mathrm{I} - \mathrm{H} transforms y\mathbf{y} to y−y^\mathbf{y} - \hat{\mathbf{y}}, perpendicular to the span. In the lecture, Lin describes this as taking the "remainder".

This article adds the algebraic version of statement 2. Being perpendicular to the span means having inner product 0 with every column of X\mathrm{X}, which in matrix form is X⊤(y−y^)=0\mathrm{X}^\top(\mathbf{y} - \hat{\mathbf{y}}) = \mathbf{0}. Expand it: X⊤y−X⊤XwLIN=0\mathrm{X}^\top\mathbf{y} - \mathrm{X}^\top\mathrm{X}\mathbf{w}_{\text{LIN}} = \mathbf{0}, which is exactly the equation obtained in the algorithm section by setting the gradient to zero. "The gradient is zero" and "the residual is perpendicular to the span" are two ways of stating the same condition. The four-customer example can be checked directly: the residual is (0.1,−0.3,0.3,−0.1)(0.1, -0.3, 0.3, -0.1), its inner product with the first column (1,1,1,1)(1, 1, 1, 1) is 0.1−0.3+0.3−0.1=00.1 - 0.3 + 0.3 - 0.1 = 0, and its inner product with the second column (1,2,3,4)(1, 2, 3, 4) is 0.1−0.6+0.9−0.4=00.1 - 0.6 + 0.9 - 0.4 = 0.

Why trace(I − H) is N − (d + 1)

Page 15 ends with one line: claim: trace(I−H)=N−(d+1)\text{trace}(\mathrm{I} - \mathrm{H}) = N - (d + 1). Why? :-) The trace is the sum of all the entries on the diagonal of a matrix. The lecture gives no proof, only the physical meaning, and asks everyone to write out the proof on their own: y\mathbf{y} originally has NN degrees of freedom in the NN-dimensional space; the span it is projected onto has at most d+1d + 1 dimensions; and what is left after taking the remainder has at most N−(d+1)N - (d + 1) degrees of freedom.

The proof below is an addition in this article and covers only the case where X⊤X\mathrm{X}^\top\mathrm{X} is invertible. It uses one property: the trace of a product of two matrices is unchanged when the order is swapped, trace(PQ)=trace(QP)\text{trace}(\mathrm{P}\mathrm{Q}) = \text{trace}(\mathrm{Q}\mathrm{P}), because both sides equal ∑i∑jPijQji\sum_i\sum_j P_{ij}Q_{ji}. Take P=X\mathrm{P} = \mathrm{X} and Q=(X⊤X)−1X⊤\mathrm{Q} = (\mathrm{X}^\top\mathrm{X})^{-1}\mathrm{X}^\top:

trace(H)=trace(X(X⊤X)−1X⊤)=trace((X⊤X)−1X⊤X)=trace(Id+1)=d+1\text{trace}(\mathrm{H}) = \text{trace}\big(\mathrm{X}(\mathrm{X}^\top\mathrm{X})^{-1}\mathrm{X}^\top\big) = \text{trace}\big((\mathrm{X}^\top\mathrm{X})^{-1}\mathrm{X}^\top\mathrm{X}\big) = \text{trace}(\mathrm{I}_{d+1}) = d + 1

After the swap, the large N×NN \times N matrix becomes the (d+1)×(d+1)(d+1) \times (d+1) identity matrix, which has d+1d + 1 ones on its diagonal. Therefore

trace(I−H)=trace(IN)−trace(H)=N−(d+1)\text{trace}(\mathrm{I} - \mathrm{H}) = \text{trace}(\mathrm{I}_N) - \text{trace}(\mathrm{H}) = N - (d + 1)

In the four-customer example, the hat matrix is

H=XX†=[0.70.40.1−0.20.40.30.20.10.10.20.30.4−0.20.10.40.7]\mathrm{H} = \mathrm{X}\mathrm{X}^\dagger = \begin{bmatrix} 0.7 & 0.4 & 0.1 & -0.2 \\ 0.4 & 0.3 & 0.2 & 0.1 \\ 0.1 & 0.2 & 0.3 & 0.4 \\ -0.2 & 0.1 & 0.4 & 0.7 \end{bmatrix}

The diagonal sums to 0.7+0.3+0.3+0.7=2=d+10.7 + 0.3 + 0.3 + 0.7 = 2 = d + 1, and trace(I−H)=4−2=2\text{trace}(\mathrm{I} - \mathrm{H}) = 4 - 2 = 2. Multiply the first row by y=(2,3,5,6)\mathbf{y} = (2, 3, 5, 6): 1.4+1.2+0.5−1.2=1.91.4 + 1.2 + 0.5 - 1.2 = 1.9, which is y^1\hat{y}_1.

I − H turns noise into the residual

Page 16 is titled An Illustrative 'Proof', and the quotation marks are the handout's own. The lecture also says no complete proof will be given, only the main idea inside it. The figure has two more things than the previous page: a red label f(X)f(\mathrm{X}) inside the span, and a black dashed segment from f(X)f(\mathrm{X}) to y\mathbf{y} labeled noise.

The assumption. y\mathbf{y} comes from some ideal f(X)f(\mathrm{X}) plus noise, and f(X)f(\mathrm{X}) lies in the span:

y=f(X)+noise,f(X)∈span\mathbf{y} = f(\mathrm{X}) + \textbf{noise}, \qquad f(\mathrm{X}) \in \text{span}

f(X)∈spanf(\mathrm{X}) \in \text{span} means there is some wf\mathbf{w}_f with f(X)=Xwff(\mathrm{X}) = \mathrm{X}\mathbf{w}_f, that is, the ideal target itself is linear.

Step 1: I−H\mathrm{I} - \mathrm{H} wipes out f(X)f(\mathrm{X}). A vector already in the span is unchanged by projection: HXwf=X(X⊤X)−1X⊤Xwf=Xwf\mathrm{H}\mathrm{X}\mathbf{w}_f = \mathrm{X}(\mathrm{X}^\top\mathrm{X})^{-1}\mathrm{X}^\top\mathrm{X}\mathbf{w}_f = \mathrm{X}\mathbf{w}_f. So (I−H)f(X)=0(\mathrm{I} - \mathrm{H})f(\mathrm{X}) = \mathbf{0}, and

y−y^=(I−H)y=(I−H)f(X)+(I−H) noise=(I−H) noise\mathbf{y} - \hat{\mathbf{y}} = (\mathrm{I} - \mathrm{H})\mathbf{y} = (\mathrm{I} - \mathrm{H})f(\mathrm{X}) + (\mathrm{I} - \mathrm{H})\,\textbf{noise} = (\mathrm{I} - \mathrm{H})\,\textbf{noise}

The lecture makes the same point from the figure: apply the remainder transform to the black dashed noise segment and you get the same green dashed line.

Step 2: compute the average squared length. The handout writes the result directly:

Ein(wLIN)=1N∥y−y^∥2=1N∥(I−H) noise∥2=1N(N−(d+1))σ2E_{\text{in}}(\mathbf{w}_{\text{LIN}}) = \frac{1}{N}\lVert\mathbf{y} - \hat{\mathbf{y}}\rVert^2 = \frac{1}{N}\lVert(\mathrm{I} - \mathrm{H})\,\textbf{noise}\rVert^2 = \frac{1}{N}\big(N - (d+1)\big)\sigma^2

About the noise, the handout only writes "per-dimension 'noise level' σ2\sigma^2". The last equality actually holds only after averaging over the noise, and the lecture says so explicitly, describing it as the result of averaging over all the various messy noise. The calculation below is an addition in this article, and the assumption it needs is that the NN components of the noise, ϵ1,…,ϵN\epsilon_1, \dots, \epsilon_N, are independent of each other, all with mean 0 and all with variance σ2\sigma^2.

Write I−H\mathrm{I} - \mathrm{H} as M\mathrm{M}. H\mathrm{H} is symmetric and H2=H\mathrm{H}^2 = \mathrm{H} (the Fun Time in this section verifies this), so M\mathrm{M} is symmetric too, and M⊤M=M2=I−2H+H2=M\mathrm{M}^\top\mathrm{M} = \mathrm{M}^2 = \mathrm{I} - 2\mathrm{H} + \mathrm{H}^2 = \mathrm{M}. Therefore

∥Mϵ∥2=ϵ⊤M⊤Mϵ=ϵ⊤Mϵ=∑i∑jMij ϵiϵj\lVert\mathrm{M}\boldsymbol{\epsilon}\rVert^2 = \boldsymbol{\epsilon}^\top\mathrm{M}^\top\mathrm{M}\boldsymbol{\epsilon} = \boldsymbol{\epsilon}^\top\mathrm{M}\boldsymbol{\epsilon} = \sum_{i}\sum_{j} M_{ij}\,\epsilon_i\epsilon_j

Take the expectation. When i≠ji \ne j, independence together with mean 0 gives E[ϵiϵj]=0\mathcal{E}[\epsilon_i\epsilon_j] = 0; when i=ji = j, E[ϵi2]=σ2\mathcal{E}[\epsilon_i^2] = \sigma^2. Only the diagonal survives:

E ∥Mϵ∥2=σ2∑iMii=σ2 trace(I−H)=σ2(N−(d+1))\mathcal{E}\,\lVert\mathrm{M}\boldsymbol{\epsilon}\rVert^2 = \sigma^2\sum_{i} M_{ii} = \sigma^2\,\text{trace}(\mathrm{I} - \mathrm{H}) = \sigma^2\big(N - (d+1)\big)

Divide by NN:

Ein‾=σ2⋅(1−d+1N)\overline{E_{\text{in}}} = \sigma^2\cdot\Big(1 - \frac{d+1}{N}\Big)

In the lecture, Lin describes the trace as the "energy" of I−H\mathrm{I} - \mathrm{H}: the noise starts with σ2\sigma^2 in each of NN directions, and after passing through I−H\mathrm{I} - \mathrm{H} only N−(d+1)N - (d+1) shares of it remain.

The formula for EoutE_{\text{out}}. The same page lists

Eout‾=σ2⋅(1+d+1N)\overline{E_{\text{out}}} = \sigma^2\cdot\Big(1 + \frac{d+1}{N}\Big)

The handout marks it "complicated!" and gives no derivation; the lecture says that it can be worked out in a similar way if you do the calculation seriously, only with more effort. What the lecture offers is intuition: EinE_{\text{in}} is computed on the data we can see, and the fitted line leans a little toward the noise in this particular batch, which makes EinE_{\text{in}} look a little better, and that is where subtracting d+1N\frac{d+1}{N} comes from; EoutE_{\text{out}} looks at new data, the line has already leaned this way, and the noise in the new data may fall on the other side, so the two effects stack and the term is added.

This article adds a simplified version that can be computed in full. It corresponds to the remark in the lecture, made while discussing EoutE_{\text{out}}, that it is "a bit like another NN points". Suppose the test data uses the same xn\mathbf{x}_n and only redraws the noise, ϵ′\boldsymbol{\epsilon}', independent of the training noise ϵ\boldsymbol{\epsilon}. The new labels are y′=f(X)+ϵ′\mathbf{y}' = f(\mathrm{X}) + \boldsymbol{\epsilon}', the predictions are still y^=Hy=f(X)+Hϵ\hat{\mathbf{y}} = \mathrm{H}\mathbf{y} = f(\mathrm{X}) + \mathrm{H}\boldsymbol{\epsilon}, and the difference between them is ϵ′−Hϵ\boldsymbol{\epsilon}' - \mathrm{H}\boldsymbol{\epsilon}:

E ∥ϵ′−Hϵ∥2=E ∥ϵ′∥2+E ∥Hϵ∥2=Nσ2+σ2 trace(H)=σ2(N+(d+1))\mathcal{E}\,\lVert\boldsymbol{\epsilon}' - \mathrm{H}\boldsymbol{\epsilon}\rVert^2 = \mathcal{E}\,\lVert\boldsymbol{\epsilon}'\rVert^2 + \mathcal{E}\,\lVert\mathrm{H}\boldsymbol{\epsilon}\rVert^2 = N\sigma^2 + \sigma^2\,\text{trace}(\mathrm{H}) = \sigma^2\big(N + (d+1)\big)

The cross term has expectation 0 because the two batches of noise are independent and have mean 0; E∥Hϵ∥2\mathcal{E}\lVert\mathrm{H}\boldsymbol{\epsilon}\rVert^2 is computed the same way as in the passage on M\mathrm{M} above. Dividing by NN gives σ2(1+d+1N)\sigma^2(1 + \frac{d+1}{N}). Each of the two terms has its own source: Nσ2N\sigma^2 is the new data's own noise, and (d+1)σ2(d+1)\sigma^2 is the price of wLIN\mathbf{w}_{\text{LIN}} having been pulled off course by the old noise.

This article checks these three formulas with a NumPy simulation (d=2d = 2, σ2=1\sigma^2 = 1, features drawn from the standard normal distribution, a randomly drawn linear function as the target, and more than 40,000 repetitions for each NN):

NNAverage EinE_{\text{in}}: simulation / formulaSame x\mathbf{x}, new noise: simulation / formulaAverage EoutE_{\text{out}} with x\mathbf{x} redrawn too: simulation
100.703 / 0.7001.301 / 1.300about 1.47
200.849 / 0.8501.147 / 1.150about 1.18
500.940 / 0.9401.061 / 1.060about 1.06
1000.971 / 0.9701.031 / 1.030about 1.03

The first two columns match the formulas. Only the last column is the EoutE_{\text{out}} the handout defines (new x\mathbf{x} drawn from the distribution as well): when NN is small it is higher than σ2(1+d+1N)\sigma^2(1 + \frac{d+1}{N}), 1.47 against 1.30 at N=10N = 10, and from NN of 50 upward the two nearly coincide. So the simplified version above only shows where the "plus d+1N\frac{d+1}{N}" term comes from and cannot stand as a proof of the general case. This column is a simulation result of this article under this particular setting (normally distributed features), not content from the handout; the handout gives no derivation for the general case.

The learning curve

Page 17 plots the two formulas, with the number of data points NN on the horizontal axis and Expected Error on the vertical axis:

  • A horizontal line in the middle, at height σ2\sigma^2.
  • The red Eout‾\overline{E_{\text{out}}} comes down from very high and approaches the horizontal line.
  • The blue Ein‾\overline{E_{\text{in}}} starts at the position marked d+1d + 1 on the horizontal axis (where its value is σ2⋅0=0\sigma^2 \cdot 0 = 0), rises, and approaches the horizontal line.

This figure is called the learning curve. The handout makes two points. First, both converge to σ2\sigma^2 for N→∞N \to \infty. Second, the expected generalization error is 2(d+1)N\frac{2(d+1)}{N}, similar to the worst-case guarantee from VC. The lecture fills in how the two differ and how they are alike: VC describes the worst case and this describes the average; what they share is that both depend on the degrees of freedom d+1d + 1 and both shrink as NN grows.

One detail to watch: subtracting the two formulas on the previous page gives the gap

Eout‾−Ein‾=σ2⋅2(d+1)N\overline{E_{\text{out}}} - \overline{E_{\text{in}}} = \sigma^2\cdot\frac{2(d+1)}{N}

Both formulas on this page of the handout are multiplied by the noise level, but the line underneath reads "expected generalization error: 2(d+1)N\frac{2(d+1)}{N}", the summary on page 23 reads "Eout−Ein≈2(d+1)NE_{\text{out}} - E_{\text{in}} \approx \frac{2(d+1)}{N} on average", and what is said aloud in the lecture is also roughly twice d+1N\frac{d+1}{N}; none of the three multiplies by the noise level. Going by the subtraction, there should be a factor of σ2\sigma^2 in front; the two ways of writing it agree numerically only when σ2=1\sigma^2 = 1. When plugging in numbers, use the version with σ2\sigma^2, which is how self-check question 2 is computed.

The handout's conclusion is: linear regression (LinReg): learning 'happened'! In the lecture, Lin explains that when NN is large enough we are already doing about as well as the noise level allows, and if there is not too much noise, learning really did happen. The lecture also takes a stance: the mathematical details matter less, and what Lin hopes people remember is the geometric and physical meaning, namely that with only NN points, the line leans a little toward those NN points.

Trying it on a scenario (made up for this article). Suppose there are 12 features, so d+1=13d + 1 = 13, which is the dimension of the cpusmall data set used in the exercises below:

NNEin‾/σ2\overline{E_{\text{in}}}/\sigma^2Eout‾/σ2\overline{E_{\text{out}}}/\sigma^2Gap /σ2/\sigma^2
13022
320.5941.4060.813
1000.871.130.26
2,0000.99351.00650.013

At N=32N = 32, the error seen on the training data is only about 60% of the noise level while on new data it is 1.4 times the noise level, so looking at EinE_{\text{in}} alone seriously overrates the model. At N=13N = 13, EinE_{\text{in}} is 0: with 13 unknowns and 13 examples every example can be hit exactly, yet the average EoutE_{\text{out}} is twice the noise level. To bring the gap below 5% of the noise level you need 26N≤0.05\frac{26}{N} \le 0.05, that is, N≥520N \ge 520. These numbers assume the target really is linear plus noise, which real data will not match exactly, so they are better used for comparing the shape of the curves.

Fun Time: properties of the hat matrix

Question (page 18): Which of the following properties of H\mathrm{H} is not true?

  1. H\mathrm{H} is symmetric
  2. H2=H\mathrm{H}^2 = \mathrm{H} (double projection = single one)
  3. (I−H)2=I−H(\mathrm{I} - \mathrm{H})^2 = \mathrm{I} - \mathrm{H} (double residual transform = single one)
  4. none of the above (all three are true)

Answer: 4. The handout's solution only says that 2 and 3 can be concluded from their physical meanings. In the lecture, Lin explains: after projecting once you are already in that space, and projecting again changes nothing; a vector whose remainder has been taken once is already perpendicular to that space, and taking the remainder again gives the same vector. Symmetry can more or less be read off the formula. The lecture also says nobody is required to prove these.

This article verifies each of the three once, in the case where X⊤X\mathrm{X}^\top\mathrm{X} is invertible:

  • Symmetry: H⊤=(X(X⊤X)−1X⊤)⊤=X((X⊤X)−1)⊤X⊤=H\mathrm{H}^\top = \big(\mathrm{X}(\mathrm{X}^\top\mathrm{X})^{-1}\mathrm{X}^\top\big)^\top = \mathrm{X}\big((\mathrm{X}^\top\mathrm{X})^{-1}\big)^\top\mathrm{X}^\top = \mathrm{H}, because the inverse of a symmetric matrix is also symmetric.
  • H2=X(X⊤X)−1X⊤X(X⊤X)−1⏟IX⊤=H\mathrm{H}^2 = \mathrm{X}(\mathrm{X}^\top\mathrm{X})^{-1}\underbrace{\mathrm{X}^\top\mathrm{X}(\mathrm{X}^\top\mathrm{X})^{-1}}_{\mathrm{I}}\mathrm{X}^\top = \mathrm{H}.
  • (I−H)2=I−2H+H2=I−2H+H=I−H(\mathrm{I} - \mathrm{H})^2 = \mathrm{I} - 2\mathrm{H} + \mathrm{H}^2 = \mathrm{I} - 2\mathrm{H} + \mathrm{H} = \mathrm{I} - \mathrm{H}.

The H\mathrm{H} of the four customers is symmetric at a glance. The top-left entry of H2\mathrm{H}^2 is the inner product of the first row with itself: 0.49+0.16+0.01+0.04=0.70.49 + 0.16 + 0.01 + 0.04 = 0.7, equal to the top-left entry of H\mathrm{H}. Checking the whole matrix in NumPy, all three hold.

What justifies using the regression solution for classification

YouTube:Linear Regression :: for Binary Classification @ Machine Learning Foundations (機器學習基石)

(Video 00:00–02:51 compares the two problems and proposes the approach, 02:51–05:30 compares the two errors, 05:30–09:03 connects to the VC bound and the uses, 09:03–10:00 is Fun Time, and the summary of the whole lecture follows.)

A lazy approach that looks workable

Page 19 puts the two linear models side by side:

Linear classificationLinear regression
Output spaceY={−1,+1}\mathcal{Y} = \{-1, +1\}Y=R\mathcal{Y} = \mathbb{R}
Hypothesish(x)=sign(w⊤x)h(\mathbf{x}) = \text{sign}(\mathbf{w}^\top\mathbf{x})h(x)=w⊤xh(\mathbf{x}) = \mathbf{w}^\top\mathbf{x}
Errorerr(y^,y)=⟦y^≠y⟧\text{err}(\hat{y}, y) = \llbracket\hat{y} \ne y\rrbracketerr(y^,y)=(y^−y)2\text{err}(\hat{y}, y) = (\hat{y} - y)^2
SolvingNP-hard to solve in generalEfficient analytic solution

The handout then points out that {−1,+1}⊂R\{-1, +1\} \subset \mathbb{R}. In the lecture, Lin puts it this way: +1+1 and −1-1 are just two special values in the vast sea of real numbers. That suggests an idea:

  1. Run linear regression directly on the binary classification data D\mathcal{D}, treating yny_n as the real numbers ±1\pm 1.
  2. Return g(x)=sign(wLIN⊤x)g(\mathbf{x}) = \text{sign}(\mathbf{w}_{\text{LIN}}^\top\mathbf{x}).

The lecture explains the intuition: the w\mathbf{w} that regression finds will probably give values greater than 0 where the label is +1+1 and values less than 0 where it is −1-1; and unlike PLA or pocket, it does not have to run for a long time, oscillating back and forth, since in software such as MATLAB it is two or three lines. The handout calls this a heuristic and asks: but explanation of this heuristic? In the lecture, Lin says it sounds reasonable, but that does not yet make it a mathematical reason.

The relation between the two errors

Both models first compute the score w⊤x\mathbf{w}^\top\mathbf{x} and differ only in how the error is computed afterward. Page 20 writes both errors as functions of the score:

err0/1=⟦sign(w⊤x)≠y⟧,errsqr=(w⊤x−y)2\text{err}_{0/1} = \llbracket\text{sign}(\mathbf{w}^\top\mathbf{x}) \ne y\rrbracket, \qquad \text{err}_{\text{sqr}} = (\mathbf{w}^\top\mathbf{x} - y)^2

It then draws two figures with w⊤x\mathbf{w}^\top\mathbf{x} on the horizontal axis and the error on the vertical axis, the blue line being the 0/1 error and the red line the squared error:

Desired y=+1y = +1Desired y=−1y = -1
Blue line (0/1)1 when the score is below 0, 0 when it is above 0, a single step1 when the score is above 0, 0 when it is below 0
Red line (squared)A parabola with its lowest point at score =1= 1, where the value is 0A parabola with its lowest point at score =−1= -1
Relation of the two linesThe red line lies above the blue lineThe red line lies above the blue line

The conclusion is written at the bottom of the page:

err0/1≤errsqr\text{err}_{0/1} \le \text{err}_{\text{sqr}}

The lecture calls this a proof by picture. This article adds the algebraic version. Let s=w⊤xs = \mathbf{w}^\top\mathbf{x}. Because y∈{−1,+1}y \in \{-1, +1\}, y2=1y^2 = 1, so

(s−y)2=y2(s−y)2=(ys−y2)2=(ys−1)2(s - y)^2 = y^2(s - y)^2 = (ys - y^2)^2 = (ys - 1)^2

Look at the two cases separately. When the classification is correct, err0/1=0\text{err}_{0/1} = 0 and a square cannot be negative, so the inequality holds. When the classification is wrong, ss and yy have different signs, ys≤0ys \le 0, so ys−1≤−1ys - 1 \le -1 and (ys−1)2≥1=err0/1(ys - 1)^2 \ge 1 = \text{err}_{0/1}.

Plug in a few scores for y=+1y = +1:

Score ss−2-2−0.5-0.50.50.51133
err0/1\text{err}_{0/1}11000
errsqr\text{err}_{\text{sqr}}92.250.2504

The rightmost column is the right half of the parabola: a score of 3 is a completely correct classification, yet its squared error is 4. The squared error only registers how far the score is from 1, whichever side it is on.

Connecting to the VC bound

Page 21 puts the inequality into the VC bound. For the same w\mathbf{w}:

classification Eout(w) ≤VC classification Ein(w)+⋯⋯≤ regression Ein(w)+⋯⋯\begin{aligned} \text{classification } E_{\text{out}}(\mathbf{w}) \ &\overset{\text{VC}}{\le}\ \text{classification } E_{\text{in}}(\mathbf{w}) + \sqrt{\cdots\cdots} \\ &\le\ \text{regression } E_{\text{in}}(\mathbf{w}) + \sqrt{\cdots\cdots} \end{aligned}

The first line is the VC bound, and the square root holds the term containing dVCd_{\text{VC}}. The second line uses the fact that the 0/1 error of every example is no larger than its squared error, and so the same is true after averaging. The lecture's conclusion: the classification EoutE_{\text{out}} is bounded by the regression EinE_{\text{in}} plus the same complexity term, so working hard to make the regression EinE_{\text{in}} small is also one way of making the classification EoutE_{\text{out}} good.

The handout says it once more in the language of the previous lecture: take errsqr\text{err}_{\text{sqr}} as err^\widehat{\text{err}}, a (loose) upper bound used to approximate err0/1\text{err}_{0/1}. Foundations Lecture 8: Noise and Error Measures said that the err^\widehat{\text{err}} an algorithm actually optimizes can be chosen to be easy to solve, and this is an example. The price is stated in one line: trade bound tightness for efficiency.

The handout suggests two uses: wLIN\mathbf{w}_{\text{LIN}} is a useful baseline classifier, or it can serve as the initial PLA/pocket vector. In the lecture, Lin says that in practice it often does not perform too badly; and if the bound seems too loose, you can take it as w0\mathbf{w}_0 and let PLA or pocket refine it from a decent starting point, which may need fewer corrections.

Numerical example: when the regression classifier goes wrong

The following is an example made up for this article, not from the slides. Keep the X\mathrm{X} of the four customers and change the output to whether the card is approved: annual incomes 1 and 2 are labeled −1-1, 3 and 4 are labeled +1+1, so y=(−1,−1,+1,+1)\mathbf{y} = (-1, -1, +1, +1). X†\mathrm{X}^\dagger was already computed earlier:

wLIN=X†y=120[−20−10+0−106+2+2+6]=[−20.8]\mathbf{w}_{\text{LIN}} = \mathrm{X}^\dagger\mathbf{y} = \frac{1}{20}\begin{bmatrix} -20 - 10 + 0 - 10 \\ 6 + 2 + 2 + 6 \end{bmatrix} = \begin{bmatrix} -2 \\ 0.8 \end{bmatrix}

The four scores are −1.2-1.2, −0.4-0.4, 0.40.4, and 1.21.2, every sign is right, and the classification EinE_{\text{in}} is 0. The squared errors are 0.040.04, 0.360.36, 0.360.36, and 0.040.04, and the regression EinE_{\text{in}} is 0.20.2. 0≤0.20 \le 0.2, so the upper bound holds; the boundary is at −2+0.8x=0-2 + 0.8x = 0, that is, x=2.5x = 2.5, exactly midway between the two groups.

Now add a fifth customer: annual income 12, labeled +1+1. The data is still linearly separable, and a boundary between 2 and 3 gets everything right. Recompute:

A=[52222174],b=[116],det⁡A=386,wLIN=1386[174−352−22+80]=[−89/19329/193]≈[−0.4610.150]\mathrm{A} = \begin{bmatrix} 5 & 22 \\ 22 & 174 \end{bmatrix}, \quad \mathbf{b} = \begin{bmatrix} 1 \\ 16 \end{bmatrix}, \quad \det\mathrm{A} = 386, \quad \mathbf{w}_{\text{LIN}} = \frac{1}{386}\begin{bmatrix} 174 - 352 \\ -22 + 80 \end{bmatrix} = \begin{bmatrix} -89/193 \\ 29/193 \end{bmatrix} \approx \begin{bmatrix} -0.461 \\ 0.150 \end{bmatrix}
Annual income xxyyScore wLIN⊤x\mathbf{w}_{\text{LIN}}^\top\mathbf{x}err0/1\text{err}_{0/1}errsqr\text{err}_{\text{sqr}}
1−1-1−0.311-0.31100.475
2−1-1−0.161-0.16100.705
3+1+1−0.010-0.01011.021
4+1+10.1400.14000.740
12+1+11.3421.34200.117

The classification EinE_{\text{in}} is 15=0.2\frac{1}{5} = 0.2 and the regression EinE_{\text{in}} is 118193≈0.611\frac{118}{193} \approx 0.611. The upper bound still holds, but the regression classifier gets the customer with annual income 3 wrong, even though a line that classifies everything correctly clearly exists for this data.

What this result means. The cause is the right half of the parabola. The fifth customer is far from the boundary: if the slope of the line stayed at 0.8, that customer's score would be 7.6, with squared error (7.6−1)2≈43.6(7.6 - 1)^2 \approx 43.6. To push down this one example, regression flattens the line, the boundary moves from 2.5 to about 3.07, and the customer with annual income 3 falls on the wrong side. This is what the handout means by loose: making the upper bound small does not guarantee that the 0/1 error we actually care about is minimized.

The second use the handout suggests shows its effect here. Run PLA from Foundations Lecture 2: Perceptrons and PLA on these five examples (this article's procedure: each time, scan down from the first example for the first misclassified point, correct with w←w+ynxn\mathbf{w} \leftarrow \mathbf{w} + y_n\mathbf{x}_n, then go back to the first example and scan again; a score of exactly 0 counts as a mistake). The result: starting from the zero vector takes 23 corrections to get everything right, and starting from wLIN\mathbf{w}_{\text{LIN}} takes 6. The counts depend on the order in which mistakes are looked for, and a different visiting scheme gives different numbers; this is an observation on one data set, not a guarantee.

Fun Time: which functions are upper bounds of the 0/1 error

Question (page 22): Which of the following functions are upper bounds of the pointwise 0/1 error ⟦sign(w⊤x)≠y⟧\llbracket\text{sign}(\mathbf{w}^\top\mathbf{x}) \ne y\rrbracket for y∈{−1,+1}y \in \{-1, +1\}?

  1. exp⁡(−yw⊤x)\exp(-y\mathbf{w}^\top\mathbf{x})
  2. max⁡(0, 1−yw⊤x)\max(0,\ 1 - y\mathbf{w}^\top\mathbf{x})
  3. log⁡2(1+exp⁡(−yw⊤x))\log_2\big(1 + \exp(-y\mathbf{w}^\top\mathbf{x})\big)
  4. all of the above

Answer: 4. The handout's solution is to plot the curves and you'll see; it adds that all three can be used for binary classification, that each connects to a very important algorithm in machine learning, and that one of them will be discussed in the next lecture.

This article adds a verification that needs no plot. Let z=yw⊤xz = y\mathbf{w}^\top\mathbf{x}; a correct classification is z>0z > 0 and a wrong one is z≤0z \le 0. None of the three functions is ever negative, so the bound always holds when the classification is correct (0/1 error 0); all that needs checking is whether each is at least 1 when z≤0z \le 0:

  • exp⁡(−z)\exp(-z): when z≤0z \le 0, −z≥0-z \ge 0 and exp⁡(−z)≥exp⁡(0)=1\exp(-z) \ge \exp(0) = 1.
  • max⁡(0,1−z)\max(0, 1 - z): when z≤0z \le 0, 1−z≥11 - z \ge 1.
  • log⁡2(1+exp⁡(−z))\log_2(1 + \exp(-z)): when z≤0z \le 0, 1+exp⁡(−z)≥21 + \exp(-z) \ge 2, and after log⁡2\log_2 it is at least 1.
zz−2-2−1-1001122
0/1 error11100
exp⁡(−z)\exp(-z)7.3892.71810.3680.135
max⁡(0,1−z)\max(0, 1 - z)32100
log⁡2(1+exp⁡(−z))\log_2(1 + \exp(-z))3.0691.89510.4520.183

The base 2 in the third function is there for a reason: with the natural logarithm, the value at z=0z = 0 would be ln⁡2≈0.693\ln 2 \approx 0.693, which is less than 1, and it would no longer be an upper bound. The one the handout promises for the next lecture is the third: the error function of Foundations Lecture 10: Logistic Regression is ln⁡(1+exp⁡(−yw⊤x))\ln(1 + \exp(-y\mathbf{w}^\top\mathbf{x})). Which algorithms the other two correspond to is not written in this handout; the lecture only says that after finishing this course and the one that follows (Machine Learning Techniques), you will have learned all three. What follows is an addition in this article, checked against the Techniques handouts: max⁡(0,1−z)\max(0, 1 - z) is written err^SVM\widehat{\text{err}}_{\text{SVM}} in the handout of Techniques Lecture 5: Kernel Logistic Regression and called the hinge error measure, the error that appears once soft-margin SVM is rewritten in unconstrained form; exp⁡(−z)\exp(-z) is written err^ADA\widehat{\text{err}}_{\text{ADA}} in the handout of Techniques Lecture 11: Gradient Boosted Decision Trees and called the exponential error measure, and that lecture explains that AdaBoost can be viewed as minimizing this error. Both handouts also note that these are upper bounds of err0/1\text{err}_{0/1}.

The summary on page 23 condenses each of the four sections into one line: use hyperplanes to approximate real values; analytic solution with pseudo-inverse; Eout−Ein≈2(d+1)NE_{\text{out}} - E_{\text{in}} \approx \frac{2(d+1)}{N} on average; 0/1 error ≤ squared error. The preview at the bottom of the page is "binary classification, regression, and then?" In the lecture, Lin says the next lecture still uses a linear model but solves a different kind of problem.

Self-check

1. The hypothesis has no constant term, h(x)=wxh(x) = wx, with a single parameter. The data is (x,y)=(1,1)(x, y) = (1, 1), (2,3)(2, 3), (3,7)(3, 7). Find wLINw_{\text{LIN}}, the three predictions, and EinE_{\text{in}}.

Answer: X\mathrm{X} has a single column (1,2,3)(1, 2, 3), X⊤X=1+4+9=14\mathrm{X}^\top\mathrm{X} = 1 + 4 + 9 = 14, X⊤y=1+6+21=28\mathrm{X}^\top\mathbf{y} = 1 + 6 + 21 = 28, and wLIN=2814=2w_{\text{LIN}} = \frac{28}{14} = 2. The predictions are (2,4,6)(2, 4, 6), the residuals are (−1,−1,1)(-1, -1, 1), and Ein=1+1+13=1E_{\text{in}} = \frac{1 + 1 + 1}{3} = 1. Check: the inner product of the residuals with (1,2,3)(1, 2, 3) is −1−2+3=0-1 - 2 + 3 = 0, so they are perpendicular. The general formula is wLIN=∑xnyn∑xn2w_{\text{LIN}} = \frac{\sum x_n y_n}{\sum x_n^2}.

2. A regression problem has 4 features (5 weights in total with the constant term), noise level σ2=4\sigma^2 = 4, and N=20N = 20 examples. By the handout's two formulas, what are the average EinE_{\text{in}}, the average EoutE_{\text{out}}, and the gap between them? How much data is needed to halve the gap?

Answer: d+1N=520=0.25\frac{d+1}{N} = \frac{5}{20} = 0.25. The average Ein=4×0.75=3E_{\text{in}} = 4 \times 0.75 = 3, the average Eout=4×1.25=5E_{\text{out}} = 4 \times 1.25 = 5, and the gap is 2. The gap is σ2⋅2(d+1)N=40N\sigma^2 \cdot \frac{2(d+1)}{N} = \frac{40}{N}, and bringing it to 1 requires N=40N = 40.

3. When N=d+1N = d + 1 and X\mathrm{X} is an invertible square matrix, what is H\mathrm{H}? What is EinE_{\text{in}}? Is this consistent with the handout's formula? Can we say the learning went well in this case?

Answer: when X\mathrm{X} is invertible, X†=(X⊤X)−1X⊤=X−1(X⊤)−1X⊤=X−1\mathrm{X}^\dagger = (\mathrm{X}^\top\mathrm{X})^{-1}\mathrm{X}^\top = \mathrm{X}^{-1}(\mathrm{X}^\top)^{-1}\mathrm{X}^\top = \mathrm{X}^{-1}, so H=XX−1=I\mathrm{H} = \mathrm{X}\mathrm{X}^{-1} = \mathrm{I}, y^=y\hat{\mathbf{y}} = \mathbf{y}, and Ein=0E_{\text{in}} = 0. The formula gives σ2(1−1)=0\sigma^2(1 - 1) = 0, which is consistent; trace(I−H)=0\text{trace}(\mathrm{I} - \mathrm{H}) = 0 matches as well. We cannot say the learning went well: the same pair of formulas gives an average EoutE_{\text{out}} of 2σ22\sigma^2, and Ein=0E_{\text{in}} = 0 only because there are as many unknowns as examples, so even the noise has been fitted.

4. Take y^\hat{\mathbf{y}} as the new labels and run linear regression again on the same X\mathrm{X}. What are the resulting weights and EinE_{\text{in}}? Which property of the hat matrix does this correspond to?

Answer: the new weights are X†y^=X†XX†y=X†y=wLIN\mathrm{X}^\dagger\hat{\mathbf{y}} = \mathrm{X}^\dagger\mathrm{X}\mathrm{X}^\dagger\mathbf{y} = \mathrm{X}^\dagger\mathbf{y} = \mathbf{w}_{\text{LIN}} (using X†X=I\mathrm{X}^\dagger\mathrm{X} = \mathrm{I}), the same as before. The new predictions are Hy^=H2y=Hy=y^\mathrm{H}\hat{\mathbf{y}} = \mathrm{H}^2\mathbf{y} = \mathrm{H}\mathbf{y} = \hat{\mathbf{y}}, identical to the new labels, so Ein=0E_{\text{in}} = 0. The corresponding property is H2=H\mathrm{H}^2 = \mathrm{H}: y^\hat{\mathbf{y}} is already in the span, and projecting it again does not move it.

5. Use linear regression for binary classification. For one example with y=+1y = +1, three different w\mathbf{w} give it scores 0.90.9, 33, and −0.5-0.5. What are the 0/1 error and the squared error in each case? Which one does regression most want to fix? What does this show?

Rubric: the three pairs are (0,0.01)(0, 0.01), (0,4)(0, 4), and (1,2.25)(1, 2.25). The largest squared error belongs to the score 3, a case that is classified completely correctly. The answer should say that the squared error penalizes "how far from +1+1", and a score that is too large is penalized too; so minimizing the regression EinE_{\text{in}} may sacrifice points near the boundary in order to pull back correct points that are far away. This is where the upper bound is loose, and it is what happened in the five-customer example.

Exercises and corresponding reading

How the course versions line up. On the Fall 2024 course page the slides for this lecture are 09u, which has only three subsections and drops the MOOC version's Linear Regression for Binary Classification; Fall 2024's Lecture 11 (11u) in turn opens with Linear Models for Binary Classification. This article still follows the MOOC version and covers all four sections. The Fall 2026 course page schedules Lectures 9 and 10 in W5 (10/07); when checked on 2026-09-30, that week's 09u slides were not yet public.

Textbook. According to the Fall 2024 course page, this lecture corresponds to Section 3.2 of Learning from Data (LFD).

How far self-study can go. The videos and handouts are public; the Fall 2024 homework PDFs and the LIBSVM data sets are public too, so you can work the problems yourself, but there are no official solutions and grading is for enrolled students only.

Homework. The problems in Fall 2024 HW3 (released 2024-10-07, due 10-21) related to this lecture:

  • Q2: one-dimensional linear regression without x0x_0, h(x)=wxh(x) = wx; what is the optimal wlinw_{\text{lin}}. Self-check question 1 is a numerical version of this form.
  • Q3: which operation on X\mathrm{X} (scaling one row, scaling one column, multiplying everything by 2, adding several columns to the first column) may change the hat matrix H\mathrm{H}. Hint: H\mathrm{H} is the projection onto the span, so first think about whether each operation changes the span.
  • Q8: change x0x_0 of every example from 1 to 1126 and run linear regression again; prove that the new and old solutions differ by a diagonal matrix D\mathrm{D}.
  • Q10–12 (programming): use the cpusmall_scale data set from the LIBSVM site (8,192 examples, 12 features). Q10 takes N=32N = 32, runs linear regression 1,126 times, and draws a scatter plot of (Ein,Eout)(E_{\text{in}}, E_{\text{out}}); Q11 averages 16 runs for each of N=25,50,…,2000N = 25, 50, \dots, 2000 and draws the learning curve; Q12 repeats this with only the first 2 features and compares with Q11.

How to do them. For the programming problems, first write np.linalg.pinv(X) @ y yourself, then cross-check the result with np.linalg.lstsq or scikit-learn's LinearRegression. Compare the shape of the learning curve from Q11 with the d+1=13d + 1 = 13 table in the learning-curve section; Q12 has d+1=3d + 1 = 3, so by the formula the gap is much smaller, but whether the horizontal line that EinE_{\text{in}} and EoutE_{\text{out}} converge to gets higher is the thing worth looking at. You can also redo this article's singular experiment: generate random data with N=100N = 100 and d=5d = 5, compare pinv with computing inv(X.T @ X) @ X.T @ y yourself, then change one column into twice another column and see which one breaks. The guide to the whole assignment is in Foundations Homework.

Further reading. Stanford CS229 Lecture 2: The Supervised Learning Setup and Linear Regression covers the same problem along two routes, gradient descent and the normal equation, and can be read alongside this lecture's treatment.

Next lecture. Foundations Lecture 10: Logistic Regression changes the output to a probability and the error function to the third option of the last Fun Time. It has no analytic solution: the zero-gradient equation cannot be solved, so it has to switch to gradient descent, which walks downhill one step at a time.

Course video sources

Original videos: Linear Regression Problem, Linear Regression Algorithm, Generalization Issue, Linear Regression for Binary Classification

Checked: 2026-10-13. The video IDs, titles, and lengths were taken from the official playlist that day; the videos have no caption track on YouTube, and what this article reports from the lecture comes from transcripts made for it.

Update Log

  • 2026-10-13: Previously combined with Lecture 10 in one article; changed to one article per lecture and rewritten from the handout and the video transcripts.
  • 2026-10-10: Rechecked the video status. The embedded videos match the lectures in the official playlist.
  • 2026-10-10: Marked the video status and verified the recording sources and how they were obtained.

References