01 / 20

Least Squares &
Normal Equations

Master the foundation of regression analysis: find the best-fit line by minimizing the sum of squared residuals and derive the closed-form solution via the normal equations.

Statistics Linear Algebra

What Is Least Squares Estimation?

Given a set of data points \((x_1,y_1),\ldots,(x_n,y_n)\), we want to find a linear model \(\hat{y} = \beta_0 + \beta_1 x\) that best fits the data. But what does "best" mean?

A residual \(e_i = y_i - \hat{y}_i\) is the vertical distance between an observed value and the fitted value. Squaring ensures all errors are positive and penalizes large deviations heavily.

Visualizing Residuals

\(x\)
\(y\)
Data points Best-fit line Residuals

Why Squared Errors?

Cancellation prevention
Positive and negative residuals would cancel out if simply summed. Squaring makes all terms non-negative.
Differentiability
The squared error function \(S(\beta)\) is smooth and differentiable everywhere, enabling calculus-based optimization.
Closed-form solution
Minimizing a quadratic objective yields a system of linear equations (the normal equations) with an exact solution.

The Matrix Formulation

We rewrite the linear model \(y = X\beta + \varepsilon\) in matrix form. For \(n\) observations and \(p\) parameters (e.g., an intercept and \(p-1\) predictors):

$$\underset{n \times 1}{\mathbf{y}} = \underset{n \times p}{\mathbf{X}} \;\underset{p \times 1}{\boldsymbol{\beta}} + \underset{n \times 1}{\boldsymbol{\varepsilon}}$$

The objective becomes minimizing the scalar quantity:

$$S(\boldsymbol{\beta}) = (\mathbf{y} - \mathbf{X}\boldsymbol{\beta})^T(\mathbf{y} - \mathbf{X}\boldsymbol{\beta}) = \|\mathbf{y} - \mathbf{X}\boldsymbol{\beta}\|^2$$

Deriving the Normal Equations

1
Expand the objective: \(S = \mathbf{y}^T\mathbf{y} - 2\boldsymbol{\beta}^T\mathbf{X}^T\mathbf{y} + \boldsymbol{\beta}^T\mathbf{X}^T\mathbf{X}\boldsymbol{\beta}\).
2
Differentiate with respect to \(\boldsymbol{\beta}\): \(\frac{\partial S}{\partial \boldsymbol{\beta}} = -2\mathbf{X}^T\mathbf{y} + 2\mathbf{X}^T\mathbf{X}\boldsymbol{\beta}\).
3
Set to zero and solve: \(\mathbf{X}^T\mathbf{X}\hat{\boldsymbol{\beta}} = \mathbf{X}^T\mathbf{y}\).

Geometric Interpretation

\(\operatorname{Col}(\mathbf{X})\)
\(\mathbf{y}\)
\(\mathbf{X}\hat{\boldsymbol{\beta}} = \hat{\mathbf{y}}\)
\(\mathbf{e}\)
\(\mathbf{0}\)

OLS vs. GLS

Ordinary Least Squares Generalized Least Squares
Assumption \(\operatorname{Cov}(\boldsymbol{\varepsilon}) = \sigma^2 \mathbf{I}\) \(\operatorname{Cov}(\boldsymbol{\varepsilon}) = \sigma^2 \boldsymbol{\Omega}\)
Estimator \((\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\mathbf{y}\) \((\mathbf{X}^T\boldsymbol{\Omega}^{-1}\mathbf{X})^{-1}\mathbf{X}^T\boldsymbol{\Omega}^{-1}\mathbf{y}\)
Error structure Homoscedastic, uncorrelated Heteroscedastic and/or correlated
When to use Default: errors are iid When error covariance is known

How to Apply the Normal Equations

1
Construct \(\mathbf{X}\): Build the design matrix with a column of 1s (intercept) and columns for each predictor.
2
Compute \(\mathbf{X}^T\mathbf{X}\): Multiply the transpose of \(\mathbf{X}\) by \(\mathbf{X}\) to get a \(p \times p\) matrix.
3
Compute \(\mathbf{X}^T\mathbf{y}\): Multiply the transpose of \(\mathbf{X}\) by the response vector.
4
Solve the system: \(\hat{\boldsymbol{\beta}} = (\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\mathbf{y}\). Use matrix inversion or, more stably, Cholesky/QR decomposition.
5
Interpret: \(\hat{\beta}_0\) is the intercept, \(\hat{\beta}_j\) is the effect of predictor \(x_j\) on \(y\), holding others constant.

Example 1: Simple Linear Regression

Data: \((1,2),\;(2,3),\;(3,5)\). Fit \(\hat{y} = \beta_0 + \beta_1 x\).

Example 1: Fitted Line

1 2 3 1 2 3 4 5
\(x\)
\(y\)
(1, 2) (2, 3) (3, 5)

Example 2: Multiple Regression

Data with two predictors \(x_1, x_2\). Three observations:

Obs \(x_1\) \(x_2\) \(y\)
1101
2012
3114

Example 2: Solution

Solving \(\mathbf{X}^T\mathbf{X}\hat{\boldsymbol{\beta}} = \mathbf{X}^T\mathbf{y}\):

$$\begin{pmatrix} 3 & 2 & 2 \\ 2 & 2 & 1 \\ 2 & 1 & 2 \end{pmatrix} \hat{\boldsymbol{\beta}} = \begin{pmatrix} 7 \\ 5 \\ 6 \end{pmatrix}$$

Properties of \(\hat{\boldsymbol{\beta}}\)

Unbiased
\(E[\hat{\boldsymbol{\beta}}] = \boldsymbol{\beta}\). On average, OLS hits the true parameter value.
BLUE
Best Linear Unbiased Estimator (Gauss-Markov Theorem): lowest variance among all linear unbiased estimators.
Variance
\(\operatorname{Cov}(\hat{\boldsymbol{\beta}}) = \sigma^2(\mathbf{X}^T\mathbf{X})^{-1}\). Variance shrinks as \(n\) grows.

Common Mistakes

✕ Forgetting the intercept column
The design matrix \(\mathbf{X}\) must include a column of 1s for \(\beta_0\). Omitting it forces the regression through the origin.
✕ Inverting a singular matrix
\(\mathbf{X}^T\mathbf{X}\) is singular when columns of \(\mathbf{X}\) are linearly dependent (multicollinearity). Check rank before inverting.
✕ Confusing \(\mathbf{X}^T\mathbf{X}\) with \(\mathbf{X}\mathbf{X}^T\)
\(\mathbf{X}^T\mathbf{X}\) is \(p \times p\) (normal equations). \(\mathbf{X}\mathbf{X}^T\) is \(n \times n\) (used in the hat matrix). Mixing them up changes dimensions entirely.

Summary of Key Formulas

Concept Formula Dimensions
Objective \(\min_{\boldsymbol{\beta}} \|\mathbf{y} - \mathbf{X}\boldsymbol{\beta}\|^2\) Scalar
Normal equations \(\mathbf{X}^T\mathbf{X}\hat{\boldsymbol{\beta}} = \mathbf{X}^T\mathbf{y}\) \(p \times p\) system
OLS estimator \(\hat{\boldsymbol{\beta}} = (\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\mathbf{y}\) \(p \times 1\)
Hat matrix \(\mathbf{H} = \mathbf{X}(\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\) \(n \times n\)
Residuals \(\mathbf{e} = (\mathbf{I} - \mathbf{H})\mathbf{y}\) \(n \times 1\)
Variance of \(\hat{\boldsymbol{\beta}}\) \(\sigma^2(\mathbf{X}^T\mathbf{X})^{-1}\) \(p \times p\)

Question 1 of 5

The normal equations are given by \(\mathbf{X}^T\mathbf{X}\hat{\boldsymbol{\beta}} = \mathbf{X}^T\mathbf{y}\). What condition on \(\mathbf{X}\) guarantees a unique solution?

Question 2 of 5

In the geometric interpretation of least squares, the residual vector \(\mathbf{e}\) is:

Question 3 of 5

Given \(\mathbf{X}^T\mathbf{X} = \begin{pmatrix} 4 & 2 \\ 2 & 2 \end{pmatrix}\) and \(\mathbf{X}^T\mathbf{y} = \begin{pmatrix} 6 \\ 4 \end{pmatrix}\), what is \(\hat{\boldsymbol{\beta}}\)?

Question 4 of 5

The Gauss-Markov Theorem states that under standard assumptions, the OLS estimator is BLUE. What does BLUE stand for?

Question 5 of 5

The hat matrix \(\mathbf{H} = \mathbf{X}(\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\) projects \(\mathbf{y}\) onto the column space of \(\mathbf{X}\). Which property does \(\mathbf{H}\) not possess?