math.wiki
Statistics › Bivariate data › Least squares regression › The least squares criterion and its line
Open on the map
Statistics

The least squares criterion and its line

Three people draw three different lines through the same scatter plot. What exact rule picks one line as the best, so that everyone who follows it gets the same line?

Where you are 5 in Least squares regression

Why this exists

Itziar, Iker and Maialen counted how often fireflies flashed on six summer evenings and noted the air temperature each time (made-up data):

x (°F)606570758085
y81113141721

Here x is the temperature in degrees Fahrenheit and y the flashes per minute.

Each of them drew a line by eye through the scatter plot, and the three lines differ. Looking harder will not settle it. What settles it is a number that scores any line, and a rule that the best line is the one with the best score. The least-squares criterion is that rule. With it, “the regression line” names one line that anyone computes the same way, and everything that follows in regression (slope from r, r squared, computer output) describes that one line.

This lesson takes two days. Part 1 sets up the score: square each vertical miss and add, and see why squares rather than the misses themselves or their sizes. Part 2 finds the line with the lowest score: it passes through the point of averages (\bar{x}, \bar{y}), its slope sits at the vertex of a parabola, and because the misses it counts are vertical, predicting x from y needs a different line. Examples 1 to 3 go with Part 1, Examples 4 to 6 with Part 2.

Definition

Take data points (x_1, y_1), \dots, (x_n, y_n) and any candidate line \hat{y} = a + bx. The residual of the ith point is actual minus predicted (Interpreting a residual),

y_i - \hat{y}_i = y_i - (a + bx_i),

the vertical gap from the line to the dot: positive above the line, negative below. The sum of squared errors of the line is

\text{SSE} = \sum_{i=1}^{n} \bigl(y_i - (a + bx_i)\bigr)^2 .

“Error” means miss, not mistake. With the data fixed, \text{SSE} is a function of the line: each choice of a and b gives one number, in the squared units of y.

The least-squares criterion: among all lines, choose the one whose \text{SSE} is smallest. That line is the least-squares regression line of y on x. “Of y on x” says whose misses are counted: the misses in y, measured vertically, because the line is for predicting y from x.

Intuition

A squared residual is the area of a square whose side is the vertical miss. Draw one on every miss and the criterion becomes a picture: find the line that makes the total shaded area smallest.

60 65 70 75 80 85 90 5 10 15 20 temperature (°F) flashes per minute Itziar's line: squares total 3.75 60 65 70 75 80 85 90 5 10 15 20 temperature (°F) flashes per minute Least-squares line: squares total 3.2
Figure 1Itziar’s fireflies (made-up data). Each squared residual is the area of a square; the least-squares line makes the total area as small as it can be.

Areas grow with the square of the side. A miss of 2 costs 4, a miss of 4 costs 16. So the criterion would rather miss two dots by 2 each (total 8) than hit one exactly and miss the other by 4 (total 16), where a score built on sizes alone, |y - \hat{y}|, calls those two even at 4 apiece. Squares spread the misses out and treat a big one as much worse than two halves.

Why not a simpler score?

  • The plain sum of residuals fails at once: misses above cancel misses below. A flat line at \bar{y} has residuals adding to 0 while ignoring the trend entirely (Example 2), and in Part 2 it turns out every line through (\bar{x}, \bar{y}) has residuals adding to 0.
  • The sum of absolute residuals is a fair score, and statisticians do use it. But it can leave a whole stretch of lines tied (Example 3), and its best line has no formula; it is found by search. Squares pick exactly one line, found by the algebra of a parabola, and they tie the line to the mean: the best flat line is \hat{y} = \bar{y}.

Results

Write the three sums of deviations

\begin{aligned} S_{xx} &= \textstyle\sum (x_i - \bar{x})^2, \\ S_{yy} &= \textstyle\sum (y_i - \bar{y})^2, \\ S_{xy} &= \textstyle\sum (x_i - \bar{x})(y_i - \bar{y}), \end{aligned}

and suppose the x-values are not all equal, so S_{xx} > 0.

  1. Flat lines. Among lines \hat{y} = c, the \text{SSE} is least at c = \bar{y}, where it equals S_{yy}.
  2. Through the point of averages. For any fixed slope b, the best intercept is a = \bar{y} - b\bar{x}, so the best line of every slope passes through (\bar{x}, \bar{y}), and its residuals add to 0.
  3. The least-squares line. Exactly one line has the smallest \text{SSE}. Its slope and intercept are b = S_{xy}/S_{xx} and a = \bar{y} - b\bar{x}, and its \text{SSE} is S_{yy} - S_{xy}^2/S_{xx}.
  4. Swapping the roles. The least-squares line of x on y, \hat{x} = c + dy, makes the horizontal misses x_i - \hat{x}_i small. It has d = S_{xy}/S_{yy}, also passes through (\bar{x}, \bar{y}), and is a different line from the line of y on x unless all the points lie on one line that is neither flat nor vertical.
Why it is true

The mean makes squared deviations least. For numbers u_1, \dots, u_n with mean \bar{u} and any number c, write u_i - c = (u_i - \bar{u}) + (\bar{u} - c) and square:

\sum (u_i - c)^2 = \sum (u_i - \bar{u})^2 + n(\bar{u} - c)^2 .

The cross term 2(\bar{u} - c)\sum (u_i - \bar{u}) is 0 because deviations from a mean add to 0 (Mean as balance point). The last term is never negative and is 0 only when c = \bar{u}. With u_i = y_i this is Result 1.

Fixed slope. If b is fixed, \text{SSE} = \sum \bigl((y_i - bx_i) - a\bigr)^2, which is the same problem with u_i = y_i - bx_i. Its mean is \bar{y} - b\bar{x}, so the best a is \bar{y} - b\bar{x}, and the residuals are then deviations from a mean, which add to 0. That is Result 2.

Best slope. Put a = \bar{y} - b\bar{x} into a residual: it becomes (y_i - \bar{y}) - b(x_i - \bar{x}). Squaring and adding,

\begin{aligned} \text{SSE}(b) &= S_{yy} - 2bS_{xy} + b^2 S_{xx} \\ &= S_{xx}\Bigl(b - \frac{S_{xy}}{S_{xx}}\Bigr)^2 + S_{yy} - \frac{S_{xy}^2}{S_{xx}}, \end{aligned}

a parabola in b opening upward because S_{xx} > 0, least only at its vertex b = S_{xy}/S_{xx} (The vertex and the axis of symmetry). Every line off (\bar{x}, \bar{y}) loses to the line of its own slope through that point, and among those this slope wins, so the least-squares line is unique. That is Result 3.

Swapping. The horizontal miss x_i - (c + dy_i) is the same expression with the letters x and y traded, so the same steps give d = S_{xy}/S_{yy} through (\bar{x}, \bar{y}). Drawn with x across, that line has slope S_{yy}/S_{xy} against S_{xy}/S_{xx} for the line of y on x. These agree only when S_{xy}^2 = S_{xx}S_{yy}, which happens exactly when every deviation y_i - \bar{y} is the same multiple of x_i - \bar{x}, that is, when the points lie on one line.

Worked examples

Example 1

Scoring Itziar’s line

Itziar’s line by eye is \hat{y} = -22 + 0.5x. Find its residuals for the six evenings, its \text{SSE}, and the plain sum of its residuals.

Solution

Put each temperature into the line, subtract the prediction from the actual count, and square:

xy\hat{y}y - \hat{y}(y - \hat{y})^2
608800
651110.50.50.25
70131300
751415.5-1.52.25
801718-11
852120.50.50.25

The squares add to \text{SSE} = 3.75 (flashes per minute, squared). These are the squares in the top panel of the figure above. The plain residuals add to -1.5: on balance the line runs a little high, predicting more flashes than were seen.

Example 2

A sum of zero is not enough

Iker’s line is \hat{y} = -18 + 0.45x. Maialen draws the flat line \hat{y} = 14 at the mean flash rate and says, “My residuals add to 0, so my line misses least.” Rank the three lines by the least-squares criterion.

Solution

Iker’s predictions are 9, 11.25, 13.5, 15.75, 18, 20.25, so his residuals are -1, -0.25, -0.5, -1.75, -1, 0.75, and

\begin{aligned} \text{SSE} &= 1 + 0.0625 + 0.25 \\ &\quad + 3.0625 + 1 + 0.5625 \\ &= 5.9375 . \end{aligned}

Maialen’s residuals are -6, -3, -1, 0, 3, 7. They do add to 0, but

\text{SSE} = 36 + 9 + 1 + 0 + 9 + 49 = 104 .

The ranking is Itziar (3.75), Iker (5.9375), Maialen (104). A residual sum of 0 says only that misses above and below balance; Maialen’s line predicts 14 flashes on every evening whatever the temperature, and its squares are huge at both ends. Itziar’s line is the best of the three, but nothing yet says it is the best of all lines. That is Part 2.

Example 3

The best flat line: squares against sizes

Among flat lines \hat{y} = c for the six evenings, find \text{SSE} as a function of c and the best c. Then find the best c if the score is the total of absolute residuals instead.

Solution

The data give \sum y = 84 and \sum y^2 = 1280, so

\text{SSE}(c) = \sum (y - c)^2 = 1280 - 168c + 6c^2 .

Completing the square (Completing the square, leading coefficient not one), 6c^2 - 168c = 6(c - 14)^2 - 1176, so

\text{SSE}(c) = 6(c - 14)^2 + 104 .

It is least at c = 14, which is \bar{y}, with \text{SSE} = 104: Maialen’s line is the best flat line, as Result 1 says.

For absolute residuals, take any c from 13 to 14. Three counts lie at or below c and three at or above, so

\begin{aligned} &(c - 8) + (c - 11) + (c - 13) \\ &\quad + (14 - c) + (17 - c) + (21 - c) = 20, \end{aligned}

the same total for every such c. Outside that stretch the total grows (at c = 15 it is 22). Absolute values tie a whole interval of lines, the interval of medians; squares pick exactly one, the mean.

Example 4

The best line with Itziar’s slope

Keep Itziar’s slope 0.5 and choose the intercept by the criterion. Find the line, its \text{SSE}, and check that it passes through (\bar{x}, \bar{y}) = (72.5, 14).

Solution

With b = 0.5 fixed, \text{SSE} = \sum (u - a)^2 where u = y - 0.5x. The six values of u are -22, -21.5, -22, -23.5, -23, -21.5, which add to -133.5, so the best intercept is their mean, a = -22.25. As Result 2 promises, that is \bar{y} - 0.5\bar{x} = 14 - 36.25.

The line \hat{y} = -22.25 + 0.5x gives -22.25 + 36.25 = 14 at x = 72.5, so it passes through the point of averages. Its residuals are the deviations of u from -22.25: 0.25, 0.75, 0.25, -1.25, -0.75, 0.75. They add to 0, and their squares add to

\begin{aligned} \text{SSE} &= 0.0625 + 0.5625 + 0.0625 \\ &\quad + 1.5625 + 0.5625 + 0.5625 \\ &= 3.375 . \end{aligned}

Itziar’s line sat 0.25 higher, and the identity in “Why it is true” prices that exactly: 3.375 + 6(0.25)^2 = 3.75, her total from Example 1.

Example 5

Finding the least-squares line

Find the least-squares line for the six evenings and its \text{SSE}.

Solution

Work in deviations from \bar{x} = 72.5 and \bar{y} = 14:

x - \bar{x}y - \bar{y}product
-12.5-675
-7.5-322.5
-2.5-12.5
2.500
7.5322.5
12.5787.5

So S_{xy} = 210, S_{xx} = 2(156.25 + 56.25 + 6.25) = 437.5, and S_{yy} = 104. By Result 2 the winner passes through (72.5, 14), and for a line of slope b through that point

\begin{aligned} \text{SSE}(b) &= 437.5b^2 - 420b + 104 \\ &= 437.5(b - 0.48)^2 + 3.2 . \end{aligned}

The vertex is at b = \dfrac{420}{2(437.5)} = 0.48, the lowest point of the curve below. Then a = 14 - 0.48(72.5) = -20.8, and the least-squares line is

\hat{y} = -20.8 + 0.48x .
0.32 0.40 0.48 0.56 0.64 0 4 8 12 16 slope b least SSE for that slope least: b = 0.48, SSE = 3.2
Figure 2Every slope gets its best intercept; the totals form a parabola, and its vertex is the least-squares slope.

Check: its predictions are 8, 10.4, 12.8, 15.2, 17.6, 20, the residuals 0, 0.6, 0.2, -1.2, -0.6, 1 add to 0, and their squares add to 3.2, below Itziar’s 3.75 and Example 4’s 3.375 (which is \text{SSE}(0.5)). No line does better. In context, the line predicts 0.48 more flashes per minute for each degree warmer (Interpreting the slope and intercept).

Example 6

Predicting temperature from flashes

In a second meadow the friends recorded five evenings (made-up data): temperatures 62, 66, 70, 74, 78 °F with 10, 15, 11, 13, 16 flashes per minute. Maialen will count flashes on a new evening, gets 16 per minute, and wants the temperature. Iker says: solve the line of y on x for x. Find his answer, then the line of x on y and its answer, compare the two lines’ misses, and say which answer fits her question.

Solution

Here \bar{x} = 70, \bar{y} = 13, the deviations are -8, -4, 0, 4, 8 and -3, 2, -2, 0, 3, so S_{xx} = 160, S_{yy} = 26, S_{xy} = 24 - 8 + 0 + 0 + 24 = 40.

Line of y on x: b = 40/160 = 0.25, a = 13 - 0.25(70) = -4.5, so \hat{y} = -4.5 + 0.25x. Iker’s answer solves 16 = -4.5 + 0.25x: x = 82 °F.

Line of x on y: d = 40/26 = \tfrac{20}{13} \approx 1.538 and c = 70 - \tfrac{20}{13}(13) = 50, so \hat{x} = 50 + \tfrac{20}{13}y. At y = 16, \hat{x} = 50 + \tfrac{320}{13} \approx 74.6 °F.

Totals of squared misses for each line, each way:

Lineverticalhorizontal
y on x16256
x on y41.6\tfrac{1280}{13} \approx 98.5

Each line wins its own contest and loses the other. Maialen’s question is about temperature, so the misses that matter are misses in temperature, the horizontal ones in this picture: the line of x on y answers it, about 74.6 °F. Iker’s 82 °F is hotter than any evening in the data; the line he inverted was built to keep flash predictions close, not temperature predictions.

58 62 66 70 74 78 82 6 8 10 12 14 16 18 20 temperature (°F) flashes per minute y on x: vertical misses 58 62 66 70 74 78 82 6 8 10 12 14 16 18 20 temperature (°F) flashes per minute x on y: horizontal misses
Figure 3The second meadow (made-up data). Counting vertical misses and counting horizontal misses give two different lines.

Boundaries

  • The x-values must not all be equal. Then S_{xx} = 0, the dots stand in a vertical column, and every line through (\bar{x}, \bar{y}) with any slope scores the same, so the criterion names no line. The line of x on y needs the y-values not all equal, for the same reason.
  • The criterion finds the best line, not whether a line belongs. Curved data still get a least-squares line; whether a line was the right shape is the job of Making and reading residual plots.
  • Vertical misses mean predicting y from x. To predict x from y, fit x on y; the two lines agree only when the points lie exactly on a line.
  • Squares let one far dot pull hard. A dot missed by 10 adds 100, so the line bends toward it (Outliers, leverage and influential points).
  • “Best” is best by this score, for these data. Another score, such as total absolute residual, can name a different line, and a new evening changes \text{SSE} and so the line.

Where this goes next

The slope S_{xy}/S_{xx} can be rewritten with the correlation and the two standard deviations; that form, and what it says about predictions drifting toward the mean, is Slope from r and the spreads, and regression to the mean. The leftover \text{SSE} of the winning line, compared with S_{yy} for the flat line at \bar{y}, becomes r squared and the standard deviation of the residuals (r squared and the standard deviation of the residuals). Software reports a and b directly (Fitting linear and exponential models with technology, Reading regression output). The same criterion fits curves, and in linear algebra it chooses the closest answer to a system with no exact solution (Least squares: the best approximate solution of Ax = b).

Exercises

Exercises 1 to 5, 10 to 12 and 16 use Iker’s five evenings in a third field (made-up data):

x (°F)6468727680
y (flashes per minute)912121418

Part 1: scoring lines

  1. Find the residuals and the \text{SSE} of the line \hat{y} = -24 + 0.5x.
  2. Find the residuals and the \text{SSE} of \hat{y} = -27 + 0.55x. Which of the lines in Exercises 1 and 2 does the criterion prefer?
  3. Find the residuals of the flat line \hat{y} = 13, their sum, and the \text{SSE}.
  4. Write \text{SSE}(c) for the flat line \hat{y} = c in completed-square form, and find the best c and its \text{SSE}.
  5. Find the flat line \hat{y} = c with the smallest total of absolute residuals, and that total. What is the total of absolute residuals at c = 13?
  6. For some data, line P misses each of five points by exactly 1. Line Q passes through four of the points and misses the fifth by 4. Which line does \text{SSE} prefer, and which does the total of absolute residuals prefer?

Part 1: explaining

  1. Explain why the plain sum of the residuals cannot serve as the score for choosing a line.
  2. When is the \text{SSE} of a line exactly 0? Can the least-squares line have \text{SSE} = 0 when the dots do not lie on one line?
  3. Explain, without computing, why the \text{SSE} of the least-squares line can never be larger than S_{yy}.

Part 2: finding the line

  1. Keep the slope at 0.4. Find the best intercept and the \text{SSE} of that line, and check that it passes through (\bar{x}, \bar{y}).
  2. Write \text{SSE}(b) for lines through (\bar{x}, \bar{y}) in completed-square form, and find the least-squares line.
  3. Find the residuals of the line from Exercise 11, their sum and their \text{SSE}, and compare with Exercises 1 and 2.
  4. Maialen summarizes a fourth field: \bar{x} = 68 °F, \bar{y} = 11.5 flashes per minute, and least-squares slope b = 0.42. Find the intercept.
  5. A report gives \hat{y} = 3 + 0.2x as the least-squares line for data with \bar{x} = 70 and \bar{y} = 16. Can that be right?
  6. Three evenings give (60, 7), (70, 12) and (80, 14). Find the least-squares line, its residuals and its \text{SSE}.
  7. For Iker’s five evenings, find the line of x on y. Predict the temperature on an evening with 16 flashes per minute from it, and from solving the line of Exercise 11 for x.

Part 2: explaining

  1. Show that every line through (\bar{x}, \bar{y}) has residuals that add to 0. What does this say about using the plain sum as a score?
  2. A park ranger wants to estimate temperature from a flash count. Explain why she should use the line of x on y rather than solve the line of y on x for x.
  3. Show that if every point lies on the line y = 2 + 0.5x, the line of y on x and the line of x on y are the same line.
  4. Iker says the least-squares line always passes through at least one data point. Test his claim on the second meadow of Example 6.
  5. Explain why a single dot far from the others can move the least-squares line much more than it would move a line chosen by total absolute residual.
Answers
  1. Residuals 1, 2, 0, 0, 2; \text{SSE} = 9. The predictions are 8, 10, 12, 14, 16.
  2. Predictions 8.2, 10.4, 12.6, 14.8, 17, residuals 0.8, 1.6, -0.6, -0.8, 1, and 0.64 + 2.56 + 0.36 + 0.64 + 1, so \text{SSE} = 5.2. The criterion prefers this line, since 5.2 < 9.
  3. Residuals -4, -1, -1, 1, 5, sum 0, \text{SSE} = 16 + 1 + 1 + 1 + 25 = 44.
  4. \sum y = 65 and \sum y^2 = 889, so \text{SSE}(c) = 5c^2 - 130c + 889, which is 5(c - 13)^2 + 44. Best c = 13 = \bar{y}, with \text{SSE} = 44.
  5. c = 12, the median, with total 3 + 0 + 0 + 2 + 6 = 11. At c = 13 the total is 4 + 1 + 1 + 1 + 5 = 12. With five values the median is a single number, so here absolute values do pick one flat line, but not the mean.
  6. \text{SSE}: P scores 5, Q scores 16, so P. Absolute: P scores 5, Q scores 4, so Q.
  7. Positive and negative residuals cancel, so a poor line can sum to 0: Maialen’s flat line in Example 2 did, and by Exercise 17 every line through (\bar{x}, \bar{y}) does. A score that ties infinitely many lines cannot choose one.
  8. Exactly when every residual is 0, that is, every dot is on the line, since a sum of squares is 0 only if each square is. If the dots are not on one line, every line misses some dot, so every \text{SSE}, the least one included, is positive.
  9. The flat line \hat{y} = \bar{y} is one of the candidates, and its \text{SSE} is S_{yy}. The least-squares line has the smallest \text{SSE} of all candidates, so it is at most S_{yy}.
  10. a = \bar{y} - 0.4\bar{x} = 13 - 28.8 = -15.8. The values y - 0.4x are -16.6, -15.2, -16.8, -16.4, -14, with mean -15.8; their deviations -0.8, 0.6, -1, -0.6, 1.8 give \text{SSE} = 5.6. At x = 72, -15.8 + 28.8 = 13.
  11. Deviations -8, -4, 0, 4, 8 and -4, -1, -1, 1, 5 give S_{xx} = 160, S_{xy} = 80, S_{yy} = 44, so \text{SSE}(b) = 160b^2 - 160b + 44, which is 160(b - 0.5)^2 + 4. Then b = 0.5, a = 13 - 36 = -23: \hat{y} = -23 + 0.5x.
  12. Predictions 9, 11, 13, 15, 17; residuals 0, 1, -1, -1, 1; sum 0; \text{SSE} = 4, below 9 and 5.2.
  13. a = 11.5 - 0.42(68), which is 11.5 - 28.56 = -17.06.
  14. No. The least-squares line passes through (70, 16), but this line gives 3 + 0.2(70) = 17 at x = 70.
  15. \bar{x} = 70, \bar{y} = 11; S_{xx} = 200, S_{xy} = 40 + 0 + 30 = 70, so b = 0.35 and a = 11 - 24.5 = -13.5: \hat{y} = -13.5 + 0.35x. Residuals -0.5, 1, -0.5; \text{SSE} = 1.5.
  16. d = 80/44 = \tfrac{20}{11} and c = 72 - \tfrac{20}{11}(13), which is \tfrac{532}{11} \approx 48.36, so \hat{x} = 48.36 + 1.818y. At y = 16: 72 + \tfrac{60}{11} \approx 77.45 °F. Solving 16 = -23 + 0.5x gives 78 °F.
  17. Through (\bar{x}, \bar{y}) the line is \hat{y} = \bar{y} + b(x - \bar{x}), so the residuals are (y_i - \bar{y}) - b(x_i - \bar{x}). Deviations from a mean add to 0, so the sum is 0 - b \cdot 0 = 0 for every b. The plain sum ties every slope through that point.
  18. The line of y on x makes misses in flash rate small; solved for x, nothing about it keeps misses in temperature small. The line of x on y is the one chosen to make the temperature misses least, as the table in Example 6 shows (98.5 against 256).
  19. Every residual of y = 2 + 0.5x is 0, so its \text{SSE} is 0, the least possible, and it is the line of y on x. Written as x = -4 + 2y, every horizontal miss is 0 as well, so it is also the line of x on y.
  20. False. The line \hat{y} = -4.5 + 0.25x predicts 11, 12, 13, 14, 15 for the counts 10, 15, 11, 13, 16, so it passes through none of the five points.
  21. A miss counts by its square, so a miss of 10 adds 100 to \text{SSE} and moving the line toward that dot cuts the total sharply. Under absolute residuals the same miss adds only 10, and moving toward it saves only as much as the line moves.
kg1128 · updated 2026-10-20 · CC-BY-SA-4.0