Two measurements, one number to predict. Below, a flat plane is fitted to forty points in two ways at once. Before it plays: will the step-by-step fit and the one-step formula end on the same plane?
Fitting a plane to points
Two features and a target; gradient descent on the mean squared loss, η = 0.5, beside the normal equations.
40 readings of two features x₁, x₂ and a target y, and a plane ŷ = w₁x₁ + w₂x₂ + b, flat at 0 to start (the features are centred). Right: the loss over the two weights as contours, with b at its best.
Describe this picture
Two panels. On the left, 40 readings in three dimensions, two features and across the floor, each from −1 to 1, and the target up, with a plane through them; the view turns slowly. The plane starts flat at 0, with the features centred. On the right, a contour map of the loss against the two weights and , with at its best, and a ball on it. In the clip gradient descent takes 30 steps with learning rate 0.5 from all weights zero. Step 1 lifts to the average of the targets, 3.109, where it stays. Then the plane tilts into the cloud on the left while the ball crosses the contours to the bottom on the right, and the plane stops where the ball stops. Readouts give the step and two lists of : “gradient descent after t steps”, which changes at every step, and “normal equations”, which does not. After 30 steps they agree to three decimals: . There is a play button and no other control.
The model and the data matrix
Each example now has features, , collected in the vector , and one target , a number. There are examples; statistics writes for a sample size, and on these pages counts examples. The model is a weighted sum of the features plus a constant:
The weights and the bias term are the parameters, . With one feature this is the line of Linear regression; with two it is a plane over the floor of the two features, the plane in the picture; with more, the same sum, though no longer something to draw.
The bias term is a weight on a feature that is always 1. So put a 1 into every as an extra feature and into as its weight; from here on holds too. Stack the examples as the rows of the data matrix , rows and columns, one of them all ones, and the targets as . All predictions are then one product:
The loss and its gradient
The loss is the average squared residual of Models and loss, now over a plane:
Its gradient comes entry by entry, as in Partial derivatives and the gradient. The weight appears in each prediction once, multiplied by , so the slope of along is . Averaging over the examples and stacking the entries,
The loss is a bowl in the weights, and the gradient is zero at its bottom, where
Two routes to the bottom
In one step. Those are the normal equations, equations in unknowns. Solve them and the answer is exact, at once.
In linear algebra · the normal equations
Least squares in the linear algebra course reaches the same equations from the other side: the best is the projection of onto the columns of the data matrix, and the residual is at right angles to every column. There the matrix is and the unknowns .
Step by step. Or walk down the bowl by Gradient descent: , with counting steps. Every step costs one pass through the data.
Now the answer to the question. Both routes end on the same plane: the bowl has one bottom, and the normal equations name it while gradient descent walks to it. In the clip, after 30 steps the two readouts agree to three decimals.
In the clip the two features were centred first: their averages were subtracted, so each feature column adds up to zero. That makes the column of ones square to the two feature columns, so the bias term separates from the weights. One step with puts at the average of the targets, 3.109, and from then on the bowl in on the right is the whole story. The readings were made from plus noise, so the fitted weights land near 2 and −1, at 1.975 and −0.886, not on them: forty noisy readings do not pin them exactly.
Which route to take depends on the size of the problem. With a few features, solving the normal equations is quick and exact. With very many features, or data too large to hold at once or arriving all the time, steps are cheaper, and Stochastic gradient descent takes them on a few examples at a time. And many models have no normal equations at all: from Logistic regression on, stepping is the only route.
Reading the weights
Each weight says how much the prediction changes per unit of its feature, with the others held still: is in units of the target per unit of . So the size of a weight depends on the units. Measure a distance in kilometres instead of metres and its weight becomes 1000 times larger, for exactly the same model. A bigger weight does not mean a more important feature.
Features that move together are a second trap. If two columns of are equal, the model can raise one weight and lower the other by the same amount without changing a single prediction: there are infinitely many best answers, and cannot be inverted. If two columns are only nearly equal, the bowl becomes a long, flat-bottomed valley:
Describe this picture
Two square maps of the loss over the weights and , with at its best, each centred on its best weights, with contours at the same heights above the least loss: 0.25, 1, 2.25 and 4. On the left, “x₂ square to x₁”: the worked example below, whose two features are at right angles, gives four evenly spaced circles round , a round bowl. On the right, “x₂ nearly a copy of x₁”: the same four targets with a second feature beside the first, , give long, thin ellipses running from upper left to lower right and out of the map at both ends, a valley 8.3 times longer than wide. Its best weights sit far out, at . There is no control.
The two features in the valley differ only in their middle two readings, yet the weights they lead to, −5.75 and 8.75, are far from the round bowl’s 1.25 and 1.75, and of opposite signs. Along the valley floor the loss hardly changes, so the data cannot tell the two weights apart: a little new noise slides the bottom far along the valley, and the weights swing while the predictions barely move. Conditioning measures how long the valley is. On such a bowl, gradient descent zig-zags as it did on the stretched bowl of Gradient descent.
Watch out
A weight’s size depends on its feature’s units, and the weights of features that move together are unstable. Neither says how much a feature matters.
Does another feature help?
Add a feature to a least-squares fit and the training error can only fall or stay: the fit could always set the new weight to 0 and do as well as before. That is exactly why the training error is a poor judge of a feature. Here is a feature of pure noise, a random number with nothing to do with the target, added to the two real ones.
- two features
- two features + one of pure noise
Describe this picture
A bar chart of mean squared error, from 0 to 0.12, each bar the average of 100 repeats with fresh training sets of 40 readings. Two groups, “training error” and “error on 40 new readings”, each with two bars: “two features” and “two features + one of pure noise”. With the noise feature the training error is a little lower, 0.0806 against 0.0831, and it fell in all 100 sets; the error on new readings is a little higher, 0.0997 against 0.0963, and it rose in 67 of the 100. There is no control.
The noise feature helped on the training readings in every one of the hundred repeats, because the fit found some chance pattern in it to use. On new readings that pattern is not there, and the error rose, in 67 of the 100 repeats and on average. Bias and variance of a model explains the size of both changes: each fitted coefficient takes about off the training error and adds about as much to the error on new data, where is the variance of the noise, here , and .
Noise and least squares
Why squares? Suppose each target is the model’s value plus noise, , where the are independent, bell-shaped, with mean 0 and the same spread. Then the weights under which the readings are most likely are exactly the least-squares weights.
In probability and statistics · maximum likelihood is least squares
Maximum likelihood in the statistics course shows it: with bell-shaped noise of fixed width, making the likelihood largest is making the sum of squared errors smallest.
Worked example
Four examples : , , and . With columns ones, , , and :
The normal equations. The three columns are at right angles to each other, and each has squared length 4, so . And . So :
Check. The predictions are , then 2.25, 3.25 and −0.25. The residuals are , , and , so the loss is , down from with every weight zero.
Gradient descent. At zero, . One step with goes to , exactly halfway. One step with lands on , the answer. That works because this bowl is round: its curvature is , 2 in every direction, so every step multiplies the distance to the bottom by , which is 0 at . Any converges; at it bounces for ever.
A prediction. At the model predicts the bias term, 2.75.
Units. If were in kilometres rather than metres, would be per kilometre for the same predictions.
Where you’ll meet this
Energy use. A building’s daily energy use predicted from the outdoor temperature and the number of people inside, one weight each.
Sensor correction. A sensor whose reading drifts with temperature and supply voltage is corrected by a weighted sum of both at once.
Filters. The Wiener filter and linear prediction choose filter taps by the same normal equations, with past samples as the features.
In signal processing · taps by least squares
The Wiener filter and Parametric models and linear prediction in the signal processing course solve normal equations for filter taps: each tap is a weight, and each delayed sample a feature.
Reference card
| Quantity | Formula | Notes |
|---|---|---|
| Model | features and a 1 in ; in | |
| All predictions | a column of ones in , in | |
| Loss | the mean squared residual | |
| Gradient | ||
| Normal equations | the bottom, in one solve | |
| Descent | the same bottom, step by step | |
| A weight | change in per unit of its feature | depends on units |
| Equal columns | no single answer | cannot be inverted |
| Another feature | training error never rises | judge on new data |