AAmit’s LibraryLEARN · PRACTISE · REFLECT

Linear algebra · Worked lesson

From vectors to a line that fits the data

Start here: You need means and the idea that a line has a slope and an intercept. You do not need calculus. The data are invented so you can check the result by hand.

Represent three observations as vectors

A small reading exercise records the number of practice units completed and a short quiz score. The table contains three training examples.

Three training examples
ExamplePractice units xScore y
A01
B12
C22

The input vector is x = (0, 1, 2). The observed score vector is y = (1, 2, 2). Order matters: the second element of each vector belongs to the same learner. If you reorder x without reordering y, you fit the wrong pairs.

For a line with intercept a and slope b, the three predictions are a + b x. In matrix form use a column of ones for the intercept:

[1  0] [a]   [a      ]
[1  1] [b] = [a + b  ]
[1  2]       [a + 2b ]

The design matrix is 3 by 2: three observations and two coefficients. Multiplying it by a two-element coefficient vector produces three predictions.

Fit by least squares

The vertical errors cannot all be zero because the three points do not lie on one line. Least squares chooses coefficients that make the sum of squared residuals as small as possible. A residual here is observed score minus predicted score.

Center x around its mean of 1 to get (−1, 0, 1). The dot product of this centered x with y is (−1 × 1) + (0 × 2) + (1 × 2) = 1. The dot product of centered x with itself is 2. For a one-input fit with an intercept, divide the first by the second: b = 1/2. Then set the line through the means: a = 5/3 − (1/2)(1) = 7/6.

Fitted scores and residuals
ExamplePredictionResidual
A7/6−1/6
B5/31/3
C13/6−1/6

The squared residuals sum to 1/6. In this ordinary least-squares fit with an intercept, the residuals sum to zero, so adding signed residuals alone would misleadingly suggest a perfect fit.

Check the fit with two dot products

The residual vector is r = (−1/6, 1/3, −1/6). Test it against the very columns used to make the predictions:

Orthogonality checks for the fitted line
Column of the design matrixDot product with rWhat it checks
Ones: (1, 1, 1)−1/6 + 1/3 − 1/6 = 0Moving every prediction up or down cannot improve the fit.
Practice units: (0, 1, 2)0 + 1/3 − 2/6 = 0Tilting the fitted line cannot improve the fit.

Zero dot products mean the residual is orthogonal to both columns. Here’s why that proves we found a minimum, not just a line with canceling residuals. Changing the intercept by u and slope by v changes the prediction vector by d = u(1, 1, 1) + v(0, 1, 2). So the new residual is r − d. When you square each component and add, you get the old squared error, plus the squared size of d, minus twice the dot product of r and d. That last dot product is zero because r is orthogonal to both columns. Any change that produces different predictions adds a positive amount to the old error.

This also shows when the two coefficients cannot be pinned down. If everyone completes exactly one practice unit, the two columns are identical. Centered x is all zeros, and the slope formula would divide by zero. Only the sum a + b is determined: many slopes and intercepts give the same fitted scores. Do not force a slope from data with no variation in the input.

Check a new observation

A fourth learner, not used for fitting, completes 3 practice units. The line predicts 8/3. If the observed score is 3, the residual is 1/3. It is an easy calculation but a weak evaluation: it is one point, and 3 practice units lies outside the training range. Check a larger held-out set and do not infer that practice caused the score difference from these data alone.

Try it yourself

What happens to the fitted slope if you add 1 to all three training scores?

It stays 1/2. The centered input vector sums to zero, so the dot product with y does not change. The intercept rises to 13/6.

Keep the fitted slope but move the intercept up by 1. Each of the three predictions moves by 1.

The squared error becomes 1/6 + 3 = 19/6, without recalculating any individual residual. Compare this with the naïve check that only sums signed errors.

Glossary

vector
is an ordered list of values.
dot product
multiplies corresponding elements and sums the products.
design matrix
contains one row per observation and one column per model input or intercept.
Least squares
minimizes the sum of squared residuals.
Held-out data
are observations not used to fit the model.