The least-squares line minimises the squared vertical distances from the points to it. The slope is the covariance of x and y divided by the variance of x, and the intercept follows from the line having to pass through the mean point — which it always does, whatever the data looks like.
What least squares minimises
The line is chosen to make the total squared distance from the points to it as small as possible. Two details in that sentence do real work.
Squared, not absolute. Squaring makes the problem solvable in closed form — the answer is a formula rather than a search. It also means a point twice as far off contributes four times as much, so a single outlier can move the line a long way.
Vertical, not perpendicular. The distance measured is straight up or down to the line, not the shortest distance to it. That encodes an assumption: x is known and y is the thing measured with error.
When both variables carry error, that assumption is wrong and a different method — total least squares, which does use perpendicular distances — is the honest one. Most textbook problems quietly assume it does not matter.
Two regressions, not one
This follows directly, and it surprises people: regressing x on y is a different line.
Not the same line rearranged. Swapping the roles swaps which distances are being minimised, so the fit changes. The y-on-x slope is not the reciprocal of the x-on-y slope, and this page reports both so the gap is visible.
The two lines always cross at the mean point, and the angle between them narrows as the correlation strengthens. They coincide only when the fit is perfect — when every point is already on a line and there is nothing left to minimise.
Which one to use is decided by the question, not by the data. Predicting y from x wants y on x. Predicting x from y wants x on y. Using the wrong one is a real error and it biases the prediction towards the mean.
What r² does not say
r² is the share of the variation in y accounted for by the straight-line relationship with x. It runs from 0 to 1, and it is over-read constantly.
It does not say the relationship is linear. Curved data can produce a high r² over a short range. The residuals are what reveal that — if they bend, a straight line was the wrong model however good the number looks.
It does not say anything about causation. Two series that both grow over time correlate beautifully and may have nothing to do with one another.
It is inflated by few points. Two points always give r² = 1, because a line through both has no residuals at all. That is not a good fit; it is no information.
Anscombe’s quartet is the standard demonstration: four datasets with the same slope, intercept and r², one of which is a clean line and one of which is a curve. Plotting them is the only thing that tells them apart.
When it cannot be done
Three cases where this page refuses rather than returning something.
Every point shares an x value. The best fit is a vertical line, and a vertical line has no slope. It is not that the arithmetic is hard — the vertical distances being minimised are infinite, so the problem as posed has no answer.
Fewer than two points. One point has infinitely many lines through it.
Every point identical. There is no variation to explain, so there is nothing to fit.
A near-vertical set is worth watching too. The arithmetic succeeds, but the slope is enormous and extremely sensitive to any one point — the answer is technically right and practically meaningless.
Where it gets used
Calibration. Measure known standards, fit a line, and use it to convert future readings. The intercept is the offset and the slope is the scale.
Trend estimation. Sales against month, temperature against year. The slope is the rate of change, and the standard error says how confident that rate is.
The basis of nearly everything else. Multiple regression, logistic regression and most of machine learning start from this idea and generalise it. Least squares is also a projection — the fitted values are the projection of the data onto the space of straight lines, which is why the residuals come out perpendicular to it.
Sources and methodology
Least squares and its interpretation are standard statistics; these are the references.
Method. The slope is computed from centred sums rather than from raw sums of squares, which is the numerically stable arrangement — the raw form subtracts two large nearly-equal quantities and loses precision on data far from the origin. A point set sharing a single x value is refused with the reason rather than returning an infinity: least squares minimises vertical distances, and for a vertical line those are unbounded. The suite re-derives every fit from the raw-sum formula and checks the two agree. That engine is verified on every change against 119 hand-written assertions, including that the fitted line passes through the mean point on every one of five hundred generated datasets, and that the x-on-y slope differs from the reciprocal of the y-on-x slope whenever the fit is imperfect. The count and the per-case breakdown are published on the formula verification page.
Read the guide
r² is expressed as a share of the total variation in y, which is what the Standard Deviation Calculator measures directly.