b = Σ(x − x̄)(y − ȳ) / Σ(x − x̄)²
The least-squares line y = a + bx is the straight line closest to a scatter of paired values. Closest
here means that the sum of the squared vertical distances from the points to the line is as small as
possible, hence the name. The formula gives the slope b. It is a definition.
The intercept a then follows from the line always passing through the point (x̄, ȳ):
a = ȳ − b · x̄
That is a formula of its own, not a rearrangement of the first, so the card works out b only. Once b is
known, a is one line of arithmetic.
Worked examples
Five pairs. x = 1, 2, 3, 4, 5 and y = 2, 4, 5, 4, 5, the same pairs as on the
correlation coefficient page. There the numerator came to 6 and the x list’s
sum of squares to 10. The slope is b = 6 / 10 = 0.6.
The intercept. With x̄ = 3 and ȳ = 4, a = 4 − 0.6 · 3 = 2.2. The line is y = 2.2 + 0.6x.
A prediction. For x = 6 the line gives y = 2.2 + 0.6 · 6 = 5.8. That is reasonable as long as x stays
near the values the line was built on; for x = 100, 62.2 would be a guess the line has no grounds for.
Common mistakes
- Working out the intercept with the wrong slope. a = ȳ − b · x̄ holds only with the b that least
squares gives for the same lists.
- Extending the line far beyond the data. The line describes the relationship where the points are.
Beyond them, nothing says the relationship carries on.
- Swapping x and y. The line that predicts y from x is not the line that predicts x from y. The first
minimises vertical distances, the second horizontal ones.
Common questions
Why does the line always pass through (x̄, ȳ)?
Because that is how the intercept is chosen. Putting x = x̄ into y = a + bx with a = ȳ − b · x̄ gives
y = ȳ. That it is the right choice follows from least squares: a line that missed the centre point could
be moved there and get a smaller sum of squares.
How is the slope related to the correlation coefficient?
b = r · (s_y / s_x), where s_y and s_x are the lists’ standard deviations. They always have the same
sign: a positive correlation gives a rising line. For the example, r ≈ 0.775 and b = 0.6.
Why can I not just solve for the intercept from scratch?
Because a and b are settled together, by the two equations least squares produces. The formula card
solves one equation at a time, so it works out b from the lists, and a follows from b. To solve a
system of equations step by step, there is the
system of equations calculator.