How this instrument works
Least squares regression answers a question two points never have to ask: what is the best straight line through data that does not lie on any line at all? Given exactly two points, one line passes through both and there is nothing to optimize — that is the ordinary slope formula. Give it three points instead, and unless they happen to be collinear, no line hits all three. Least squares picks the one line, y = mx + b, that makes the sum of the squared vertical gaps between each point and the line as small as it can possibly be.
The 'squared' is doing real work. Write S(m, b) as the sum of (yᵢ − mxᵢ − b)² across the points, and calculus gives a single clean route to the minimum: set the partial derivative with respect to m to zero, set the one with respect to b to zero, and solve the resulting pair of linear equations. That solving step is exactly where m = (nΣxy − ΣxΣy) ⁄ (nΣx² − (Σx)²) and b = (Σy − mΣx) ⁄ n come from — this sheet has simply written those sums out for a fixed n = 3 rather than leaving a summation symbol open-ended. Absolute-value error has no such shortcut, since it bends sharply at zero and leaves the calculus with nothing to differentiate there.
One property falls straight out of the derivation: the residuals — the leftover gaps between each actual y and the line's predicted y — always sum to exactly zero for the fitted m and b. Push the line up and it overshoots some points more; push it down and it undershoots others; the least-squares choice is the balance point where the overs and unders cancel precisely. The formula also has a blind spot worth knowing: if all three x-values coincide, the denominator nΣx² − (Σx)² collapses to zero and no finite slope exists, the same vertical-line singularity that stops the two-point slope formula cold.
- Enter your first point's coordinates into x₁ and y₁.
- Enter the second point into x₂ and y₂, and the third into x₃ and y₃ — any order is fine, decimals and negatives both work.
- Read Slope, m — the fitted line's rise per unit of run, the same m in y = mx + b.
- Read Intercept, b — where that fitted line crosses the y-axis, at x = 0.
- If x₁, x₂, and x₃ all match, spread at least one of them apart first — an identical trio makes the slope's denominator zero.
Worked example — the points (1, 2), (2, 3), (3, 5)
Three points, none of them collinear: (1, 2), (2, 3), (3, 5). The sums: n = 3, Σx = 1+2+3 = 6, Σy = 2+3+5 = 10, Σxy = (1·2)+(2·3)+(3·5) = 2+6+15 = 23, Σx² = 1+4+9 = 14. Slope: m = (3·23 − 6·10) ⁄ (3·14 − 6²) = (69 − 60) ⁄ (42 − 36) = 9 ⁄ 6 = 1.5. Intercept: b = (10 − 1.5·6) ⁄ 3 = (10 − 9) ⁄ 3 = 1 ⁄ 3 ≈ 0.3333333.
The fitted line is y = 1.5x + 0.3333333, and it passes through none of the three points exactly: at x = 1 it predicts 1.8333, a miss of +0.1667 against the actual 2; at x = 2 it predicts 3.3333, a miss of −0.3333 against the actual 3; at x = 3 it predicts 4.8333, a miss of +0.1667 against the actual 5. Add those three misses and they cancel to zero, as the derivation guarantees — no other line through these three points keeps its squared misses smaller in total.
Questions
What does least squares regression actually minimize?
The sum of the squared vertical distances between each data point and the fitted line — how far off the line's prediction is at each point's own x, not the shortest distance to the line. A related technique, total least squares, minimizes perpendicular distance instead and generally returns a different line; this sheet uses the ordinary, vertical-distance version, the one meant for predicting y from x.
Why square the residuals instead of just adding up the raw misses?
Squaring makes the error function smooth everywhere, including at zero, so a single derivative gives one clean minimum instead of a jagged search. It also punishes a large miss more than several small ones combined, which pulls the line toward reducing the biggest gaps first. Summing the raw signed misses would not even work as a target — a line can make that sum zero while still fitting terribly, since positive and negative misses cancel.
Why does this calculator only take three points?
It is the general n-point least squares formula with n fixed at 3, so the sums Σx, Σy, Σxy, and Σx² each run over exactly three terms instead of a variable list. The algebra behind m and b is identical for any n — this sheet has just spelled out the three-term case by hand rather than hiding it behind a summation symbol.
What happens if the three points already lie on one straight line?
The fitted line passes through all three exactly, with zero leftover error. Feed in (1, 1), (2, 2), and (3, 3) and the formula returns slope 1 and intercept 0 — the actual line the data sits on. Least squares only has to compromise when the points disagree about which line to follow; with no disagreement, there is nothing to average away.
What's the most common mistake people make with this formula?
Mixing up which variable is being predicted. Regressing y on x, as this sheet does, and regressing x on y are generally different lines, because each one minimizes distance measured along a different axis — vertical misses in the first case, horizontal misses in the second. They only coincide when the points are perfectly collinear. Decide first which variable you're trying to predict, then keep every x paired with its own y.
Who came up with the method of least squares?
Adrien-Marie Legendre published it first, in 1805, to fit the orbits of comets. Carl Friedrich Gauss claimed he had already been using it since 1795 and published his own account in 1809, tying the method to the normal distribution of errors — a priority dispute historians still discuss. Gauss's probabilistic justification, that squared-error minimizing is what a bell-curve error model favors, is largely why the method outlasted its rivals.