Regression
Regression finds the line (or curve) that best fits a set of data points, letting you predict one variable from another. The key result: the least-squares line minimizes the total squared vertical distance from points to the line. It applies anywhere you have paired numerical data — economics, biology, physics labs, machine learning. First developed by Francis Galton (1880s) and formalized using least squares by Gauss and Legendre (~1805).
Among all possible straight lines you could draw through a cloud of points, exactly one minimizes the sum of squared vertical errors. That line is unique, computable from simple sums of the data, and always passes through the point — the center of mass of the data. Squaring the errors (rather than taking absolute values) makes the problem solvable with calculus and uniquely determined.
Formal Statement
We assume each observed relates to by a true line plus random error:
Derivation hint: this is the assumed model, not derived — captures noise with mean zero. Here is the true intercept, the true slope, and the random error for point . We estimate with , giving the fitted line:
Derivation hint: is the predicted value; the hat denotes an estimate. The residual for point is the gap between observed and predicted:
Derivation hint: vertical distance from data point to the fitted line.
We choose and to minimize the sum of squared residuals :
Derivation hint: total squared vertical error as a function of the two unknowns.
The values of and that minimize are:
Derivation hint: covariance of divided by variance of ; comes from setting derivatives to zero.
Derivation hint: forces the line through . where and are the means.
The strength and direction of the linear relationship are measured by Pearson's :
Derivation hint: standardized covariance; always lies in . The coefficient of determination gives the fraction of variance in explained by :
Derivation hint: 1 minus (unexplained variation / total variation).
Why It Holds / Derivation
We minimize over and .
Step 1. Take the partial derivative with respect to and set it to zero:
Derivation hint: chain rule, derivative of inner w.r.t. is .
Step 2. Dividing by and rearranging gives the first normal equation:
Derivation hint: sum the constant across points. Dividing through by yields , which rearranges to equation [6].
Step 3. Take the partial derivative with respect to and set it to zero:
Derivation hint: derivative of inner w.r.t. is .
Step 4. This gives the second normal equation:
Derivation hint: distribute inside the sum.
Step 5. Substitute from Step 2 into [12], and after algebra (collecting terms) you obtain equation [5]. Because is a sum of squares — a convex paraboloid in — this critical point is the unique global minimum.
∎Gauss claimed to have used the method of least squares as early as 1795 to predict the orbit of the dwarf planet Ceres, famously relocating it after it disappeared behind the Sun. Adrien-Marie Legendre published the method first in 1805, sparking a priority dispute. Gauss later connected least squares to the normal (Gaussian) distribution of errors, showing the method is optimal when errors are normally distributed — a result underpinning the Gauss–Markov theorem.
Building Intuition
Picture a scatter of points and imagine threading a rigid rod through them. Connect each point to the rod with a vertical spring. Each spring stores energy proportional to the square of its stretch. The least-squares line is the resting position where total spring energy is minimal — the rod settles into the configuration where it pulls equally hard from above and below. Why square the errors rather than just add absolute distances? Squaring (1) makes large errors count disproportionately, so the line "cares" about outliers; (2) gives a smooth function we can differentiate; and (3) produces a unique, closed-form answer. The slope is essentially "how much tends to move per unit move in ," computed as the covariance of and normalized by the spread of .
```svg
```
Worked Examples
Given 5 students with hours studied and score :
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 52 | 58 | 67 | 71 | 82 |
Step 1 — Means: , .
Step 2 — Deviations and products:
| product | |||
|---|---|---|---|
Sums: , .
Step 3 — Slope (eq [5]): .
Step 4 — Intercept (eq [6]): .
Fitted line:
Prediction for 6 hours: points.
Given the same data. We need .
Step 1 — Apply eq [7]:
Step 2 — Coefficient of determination:
Interpretation: about 98.3% of the variation in exam scores is explained by hours studied — a very strong positive linear relationship.
Common Misconceptions
A high means and move together — it does not prove that causes . Ice-cream sales correlate with drowning deaths, but both are driven by a hidden third variable (hot weather). Regression describes association, not mechanism. Establishing causation requires controlled experiments or careful causal reasoning, not just a good fit.
The fitted line is only trustworthy within the range of observed values. Predicting an exam score for 20 hours of study with our line gives — impossible, since scores cap at 100. Extrapolation assumes the linear trend continues forever, which it almost never does.
In 1973 statistician Frank Anscombe constructed four datasets with identical means, identical slope (), identical intercept, and identical — yet they look completely different. One is a clean line, one is a perfect parabola, one is a line plus a single wild outlier, and one is a vertical cluster dragged by one extreme point. Lesson: never trust regression numbers without plotting the data first.
The line minimizing vertical errors (predicting from ) is not the same as the line minimizing horizontal errors (predicting from ). They have different slopes and only coincide when . Choosing which variable is the response matters.
Connections
Regression sits at a crossroads of several ideas. The slope formula in [5] is the covariance of and over the variance of , linking directly to Variance and Standard Deviation and Covariance. The derivation uses Partial Derivatives to minimize a function of two variables — an application of Optimization. The correlation coefficient relates to the Pearson Correlation and, when squared, to the analysis of variance decomposition of total variation. Extending [1] to several predictors gives multiple regression, expressible compactly with Matrix Algebra and the normal equations . Regression is also the simplest member of Supervised Learning in machine learning.
Real-World Applications
Historical Development
Subtleties & Limits
Ordinary least squares assumes errors are independent, have constant variance (homoscedasticity), and that is measured without error. Real data routinely violate all three: heteroscedasticity (spreading variance), autocorrelation (in time series), and measurement error in predictors each bias or destabilize estimates. Outliers can dominate the fit because errors are squared, motivating robust regression methods. With many correlated predictors, multicollinearity makes coefficients unstable — an active area mitigated by regularization (ridge, lasso). And linear regression can only capture linear structure; choosing the right transformation or model form for nonlinear data remains as much art as science.
See Also
Pearson Correlation · Covariance · Least Squares Method · Multiple Regression · Normal Distribution · Supervised Learning · Optimization