The squared-loss gradient, and the optimum it points at
gradient▲▲△All values rounded to 4 d.p.
STATEMENT
Derive the gradient of the squared loss for one-dimensional linear regression through the origin, evaluate it at on three data points, and confirm that the parameter at which the gradient vanishes is the one the normal equation gives.
GIVEN
The model , with a single scalar parameter . The loss
and the three points
FIND
A closed form for ; its value at ; the minimiser ; and a demonstration that the two agree.
STRATEGY
Differentiate under the sum, then set the result to zero. Both steps are legal here because the sum is finite and each term is differentiable everywhere — worth saying once, because for the absolute-error loss the second step is not available.
SOLUTION
Step 1 — differentiate one term. Write for the -th residual. By the chain rule (The chain rule 0.MC.03), with :
The factor in the loss exists precisely to cancel the that differentiating a square produces. It is a convenience, not a modelling choice, and it changes no minimiser.
Step 2 — sum. Differentiation is linear, so the derivative of the average is the average of the derivatives:
\frac{\mathrm{d}\loss}{\mathrm{d}w} = \frac{1}{n}\sum_{i=1}^{n} x_i\,(w x_i - y_i) \tag{I.1.5}
Read what this says: each point pulls on in proportion to its own and to how wrong the prediction currently is. A point with exerts no pull at all, whatever its target.
Step 3 — evaluate at . The three residuals are
so
Negative, so increasing decreases the loss. That matches the residuals: every prediction is below its target.
Step 4 — set the gradient to zero. Because the loss is a quadratic in a single variable with a positive leading coefficient, its unique stationary point is its minimum.
This is the normal equation for the through-origin case. It is not a separate result to be looked up: it is what step 2 becomes when set to zero.
Step 5 — evaluate.
Answer
The gradient is a scalar in units of loss per unit ; is dimensionless here because and share units.
Check — numeric · i-1-b03-linear-regression-gradient.py
def grad(w): return sum(x * (w * x - y) for x, y in data) / len(data)
w_star = sum(x * y for x, y in data) / sum(x * x for x, y in data)Prints grad at w=1 -3.6667, w* = Sxy/Sxx 1.7857, and grad at w* 0.0000.
Executed in CI. The digits above are the digits it printed.
Check — sanity
The gradient vanishes at the claimed optimum. Substituting into (I.1.5) gives to four decimal places — which is the definition of the optimum, computed independently of the closed form that produced it.
The loss falls. and . If the “optimum” had a higher loss than an arbitrary starting point, the sign of the derivation would be wrong somewhere.
The answer is bracketed by the per-point ratios. Each point alone would give : that is , and . The least-squares fit, , lies inside — as any weighted compromise must. A value outside that interval would mean an arithmetic slip.
Where this breaks
The step from “gradient is zero” to “this is the minimum” uses convexity, and convexity here comes from the model being linear in the parameter — not from the loss being squared. Keep the squared loss but make the model with replaced by for a new parameter , and the loss in is no longer convex: stationary points can be maxima or saddles. Every network after Chapter I.5 is in that second regime, and this is the last chapter in which “set the derivative to zero” is a solution method rather than a hope.
Variation
Redo the derivation for with two parameters. You will get two equations in two unknowns; solve them for the same three points, and say what the extra parameter bought in terms of the final loss.