Drag the points. Bend the line. Then open the hood: see exactly how a computer measures “wrong” and walks downhill to the best line.
For every point, the error is the gap between the line’s prediction (ŷ) and the real value (y). We square each gap so negatives don’t cancel and big misses hurt more, then average them. That average is the mean squared error (MSE), the number the computer tries to shrink.
MSE = (1/n) · Σ (ŷ − y)². Switch on Show squares above to literally see each squared error as a box. The line that makes the total box area smallest is the best fit.
Every possible line is one point on this map: slope m across, intercept b up. Bright means low error, dark means high. The best line sits at the bottom of the bowl. Drag the white dot to pick any line yourself, or let gradient descent roll it downhill.
The gradient tells which way is uphill. Each step moves the opposite way, by a distance set by the learning rate η. Too small is slow; too big overshoots and the loss explodes. Try it in the challenges below.
For a straight line there is no need to walk: the lowest point of the bowl can be calculated directly. This is called ordinary least squares. Gradient descent matters because bigger models have no such formula.
Everything on this page comes from four ideas. Every number below updates live with the line and points you are playing with above.
Move the sliders in the lab above and watch the triangle and numbers change.
Prediction minus reality, for one point. Positive means the line is too high, negative means too low.
Squaring removes the sign and punishes big misses more. This is the box you see with Show squares.
Below, the formula is applied by hand to the first few of your points using your current m and b.
“Learning” means choosing the m and b that make the MSE as small as possible:
Repeat until the gradient is about zero. η is the learning rate.
Setting both gradients to zero and solving gives this exact answer.
Add a point that fights the trend and watch the best line, and the squared box, get dragged.
Set η to 0.18 and run gradient descent. Watch the steps overshoot and the loss blow up.
Turn the noise up. The best line still exists, but R² drops: the line explains less.