Least Squares · Foundational

Least Squares Finds the Closest Model

How squared residuals turn a cloud of observations into an estimated line or linear model.

No straight line will usually pass through every noisy observation. Least squares replaces the demand for a perfect match with a precise compromise: choose the parameters whose predictions have the smallest total squared discrepancy.

The objective

For the line y^i=β0+β1xi\hat y_i=\beta_0+\beta_1x_i, define

S(β0,β1)=i=1n[yi(β0+β1xi)]2.S(\beta_0,\beta_1)=\sum_{i=1}^{n}\left[y_i-(\beta_0+\beta_1x_i)\right]^2.

The least-squares estimates β^0\hat\beta_0 and β^1\hat\beta_1 minimize SS.

The fitted line

For ordinary simple linear regression,

β^1=(xixˉ)(yiyˉ)(xixˉ)2,β^0=yˉβ^1xˉ.\hat\beta_1=\frac{\sum (x_i-\bar x)(y_i-\bar y)}{\sum (x_i-\bar x)^2}, \qquad \hat\beta_0=\bar y-\hat\beta_1\bar x.

The slope is large when xx and yy vary together relative to the variation in xx. The intercept places the fitted line through (xˉ,yˉ)(\bar x,\bar y).

The matrix view

Many linear models can be written as

y=Xβ+ε.\mathbf y=X\boldsymbol\beta+\boldsymbol\varepsilon.

Least squares minimizes yXβ2\|\mathbf y-X\boldsymbol\beta\|^2. Its solution satisfies the normal equations

XTXβ^=XTy.X^TX\hat{\boldsymbol\beta}=X^T\mathbf y.

Geometrically, the fitted vector Xβ^X\hat{\boldsymbol\beta} is the projection of y\mathbf y onto the space of predictions the model can produce.

Assumptions and uncertainty

Least squares can calculate a line without a probability model. Statistical intervals and tests additionally rely on assumptions such as independent errors, constant error variance, and an appropriate mean structure. Normal errors are commonly assumed for exact small-sample inference.

Derive the simple-regression normal equations

Differentiate S(β0,β1)S(\beta_0,\beta_1) with respect to both coefficients and set the derivatives to zero:

(yiβ0β1xi)=0,\sum (y_i-\beta_0-\beta_1x_i)=0, xi(yiβ0β1xi)=0.\sum x_i(y_i-\beta_0-\beta_1x_i)=0.

The first equation says residuals sum to zero; the second says residuals are orthogonal to xx. Solving gives the familiar formulas for slope and intercept.

Check your understanding

Why does rescaling xx from meters to centimeters change the numerical slope but not fitted values?

Show the reasoning

One meter equals 100 centimeters, so a one-centimeter change is 1/100 as large. The slope per centimeter is the slope per meter divided by 100; multiplying by the rescaled xx restores the same predictions.

Continue exploring

Connections

Related concepts

ANOVAANOVA Tables Partition Variation in RegressionCurve FittingCurve Fitting: From Data to ModelNonlinear RegressionNonlinear Curve Fitting Is an Iterative SearchFit DiagnosticsGoodness of Fit Requires More Than R²

Applications

  • linear regression
  • calibration
  • trend estimation