Eight days
The same eight days. In the loss function topic we tuned the line by hand; now we will compute the line with the smallest loss without any trial.
Means first
The mean temperature is x̄ = 21.88, the mean sales ȳ = 39.13. The yellow dot is made of these two. The least squares line always passes through this point.
One day's deviation
Day 8 is 10.13 degrees across and 16.88 ice creams up or down from the mean. The product of the two deviations is 170.86: positive if they point the same way, negative if they point opposite ways.
All the deviations
The products of all days are added up: Sxy = 531.13. The squares of the horizontal deviations are added up: Sxx = 334.88. Sxy measures how much temperature and sales change together, Sxx how much temperature changes on its own.
The slope formula
w = Sxy / Sxx = 1.59. The line passes through the mean point with this slope. If temperature and sales rise together, Sxy is positive and the line rises.
The intercept formula
Since the line passes through the mean point, ȳ = w · x̄ + b must hold. So b = ȳ − w · x̄ = 4.43.
This line's loss
The dashed lines are the errors, the loss is 4.31. No other line can give a smaller mean squared error on this data. The formula is found by setting the derivatives of the loss with respect to w and b to zero.
Drag a day
Grab a dot and drag it: the means, Sxy, Sxx and the line are recomputed at once. A dot far from the mean pulls the line more than one in the middle, because its deviation is large.
Why it matters
Least squares is the closed-form solution of linear regression and finishes in one step. For very large models no such formula exists or it is costly to compute; then gradient descent makes the same loss smaller step by step.