01 / 21

Linear Regression
in Matrix Form

Understand how ordinary least squares becomes an elegant matrix equation, and why the solution is a geometric projection onto the column space of \(\mathbf{X}\).

Statistics Linear Algebra

Why Write Regression in Matrix Form?

In a typical regression course you see equations with summation symbols: \(\sum x_i y_i\), \(\sum x_i^2\), and so on. As the number of predictors grows, these sums become unwieldy. Matrices let us express everything in one compact equation.

Compact
One equation replaces dozens of summation formulas, regardless of the number of predictors.
General
Works for simple, multiple, and polynomial regression: same formula every time.
Geometric
Reveals that OLS is a projection: the fitted values are the closest point in the column space.

Setting Up the Matrix Equation

We have \(n\) observations and \(p\) predictors. Stack everything into matrices:

$$ \underbrace{\mathbf{y}}_{n \times 1} = \underbrace{\mathbf{X}}_{n \times (p+1)} \underbrace{\boldsymbol{\beta}}_{(p+1) \times 1} + \underbrace{\boldsymbol{\varepsilon}}_{n \times 1} $$

Inside the Design Matrix \(\mathbf{X}\)

$$ \mathbf{X} = \begin{bmatrix} 1 & x_{11} & x_{12} & \cdots & x_{1p} \\ 1 & x_{21} & x_{22} & \cdots & x_{2p} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 1 & x_{n1} & x_{n2} & \cdots & x_{np} \end{bmatrix} $$
Rows
Each row is one observation: all \(p\) predictor values plus a leading 1 for the intercept.
Columns
Each column (after the first) corresponds to one predictor variable. The first column is the intercept column.

The Least Squares Objective

We want to choose \(\boldsymbol{\beta}\) to minimize the sum of squared residuals. In matrix notation this is a single expression:

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

Deriving the Normal Equations

1
Expand \(S = \mathbf{y}^\top\mathbf{y} - 2\boldsymbol{\beta}^\top\mathbf{X}^\top\mathbf{y} + \boldsymbol{\beta}^\top\mathbf{X}^\top\mathbf{X}\boldsymbol{\beta}\).
2
Differentiate with respect to \(\boldsymbol{\beta}\) and set to zero: \(\frac{\partial S}{\partial \boldsymbol{\beta}} = -2\mathbf{X}^\top\mathbf{y} + 2\mathbf{X}^\top\mathbf{X}\boldsymbol{\beta} = \mathbf{0}\).
3
Rearrange to obtain the normal equations:
$$ \mathbf{X}^\top\mathbf{X}\,\hat{\boldsymbol{\beta}} = \mathbf{X}^\top\mathbf{y} $$

The OLS Estimator

If \(\mathbf{X}^\top\mathbf{X}\) is invertible (columns of \(\mathbf{X}\) are linearly independent), we can solve directly:

$$ \hat{\boldsymbol{\beta}} = (\mathbf{X}^\top\mathbf{X})^{-1}\mathbf{X}^\top\mathbf{y} $$

Projection Geometry

\(\text{Col}(\mathbf{X})\) \(\mathbf{y}\) \(\hat{\mathbf{y}}\) \(\mathbf{e}\) Origin

The Hat Matrix \(\mathbf{H}\)

The fitted values are a linear function of \(\mathbf{y}\). The matrix that performs this projection is called the hat matrix:

$$ \hat{\mathbf{y}} = \mathbf{H}\mathbf{y}, \qquad \mathbf{H} = \mathbf{X}(\mathbf{X}^\top\mathbf{X})^{-1}\mathbf{X}^\top $$
Symmetric
\(\mathbf{H}^\top = \mathbf{H}\). The projection is the same from either side.
Idempotent
\(\mathbf{H}^2 = \mathbf{H}\). Projecting twice gives the same result.
Trace = rank
\(\text{tr}(\mathbf{H}) = p + 1\), the number of estimated parameters.

Key Property: Orthogonality

The normal equations \(\mathbf{X}^\top\mathbf{X}\hat{\boldsymbol{\beta}} = \mathbf{X}^\top\mathbf{y}\) can be rewritten as:

$$ \mathbf{X}^\top(\mathbf{y} - \mathbf{X}\hat{\boldsymbol{\beta}}) = \mathbf{X}^\top\mathbf{e} = \mathbf{0} $$

Scalar vs Matrix Notation

Scalar (summation) Matrix
Model \(y_i = \beta_0 + \beta_1 x_i + \varepsilon_i\) \(\mathbf{y} = \mathbf{X}\boldsymbol{\beta} + \boldsymbol{\varepsilon}\)
Objective \(\sum_{i=1}^n (y_i - \hat{y}_i)^2\) \(\|\mathbf{y} - \mathbf{X}\boldsymbol{\beta}\|^2\)
Solution \(\hat{\beta}_1 = \frac{\sum x_i y_i - n\bar{x}\bar{y}}{\sum x_i^2 - n\bar{x}^2}\) \((\mathbf{X}^\top\mathbf{X})^{-1}\mathbf{X}^\top\mathbf{y}\)
Fitted values \(\hat{y}_i = \hat{\beta}_0 + \hat{\beta}_1 x_i\) \(\hat{\mathbf{y}} = \mathbf{H}\mathbf{y}\)
Scales to \(p\) predictors? Messy Identical formula

How to Apply the Formula

1
Build \(\mathbf{X}\): add a column of 1's for the intercept, then list each predictor as a column.
2
Compute \(\mathbf{X}^\top\mathbf{X}\): this is a \((p+1)\times(p+1)\) symmetric matrix.
3
Compute \(\mathbf{X}^\top\mathbf{y}\): this is a \((p+1)\times 1\) vector.
4
Solve \(\mathbf{X}^\top\mathbf{X}\,\hat{\boldsymbol{\beta}} = \mathbf{X}^\top\mathbf{y}\) for \(\hat{\boldsymbol{\beta}}\) (invert or use a solver).
5
Fitted values: \(\hat{\mathbf{y}} = \mathbf{X}\hat{\boldsymbol{\beta}}\). Residuals: \(\mathbf{e} = \mathbf{y} - \hat{\mathbf{y}}\).

Example 1: Simple Regression

Data: \((1,2),\; (2,3),\; (3,5)\). Find \(\hat{\boldsymbol{\beta}}\).

$$ \mathbf{X} = \begin{bmatrix}1&1\\1&2\\1&3\end{bmatrix}, \quad \mathbf{y} = \begin{bmatrix}2\\3\\5\end{bmatrix} $$
$$ \mathbf{X}^\top\mathbf{X} = \begin{bmatrix}3&6\\6&14\end{bmatrix}, \quad \mathbf{X}^\top\mathbf{y} = \begin{bmatrix}10\\23\end{bmatrix} $$

Example 2: Verify Orthogonality

Continuing from Example 1: compute the residuals and check \(\mathbf{X}^\top\mathbf{e} = \mathbf{0}\).

$$ \hat{\mathbf{y}} = \begin{bmatrix}\frac{11}{6}\\ \frac{10}{3}\\ \frac{29}{6}\end{bmatrix}, \quad \mathbf{e} = \mathbf{y} - \hat{\mathbf{y}} = \begin{bmatrix}\frac{1}{6}\\-\frac{1}{3}\\ \frac{1}{6}\end{bmatrix} $$

Example 3: Multiple Regression

With two predictors \(x_1, x_2\) and 4 observations, the same recipe applies. The design matrix is \(4 \times 3\):

$$ \mathbf{X} = \begin{bmatrix}1&1&2\\1&2&1\\1&3&3\\1&4&2\end{bmatrix},\;\; \mathbf{y} = \begin{bmatrix}5\\6\\9\\8\end{bmatrix} $$

Common Mistakes

✕ Forgetting the intercept column
If you omit the column of 1's in \(\mathbf{X}\), your model is forced through the origin: \(\hat\beta_0 = 0\).
✕ Inverting when singular
If columns of \(\mathbf{X}\) are linearly dependent (perfect multicollinearity), \(\mathbf{X}^\top\mathbf{X}\) is singular and has no inverse.
✕ Wrong transpose order
Writing \(\mathbf{X}\mathbf{X}^\top\) instead of \(\mathbf{X}^\top\mathbf{X}\) gives an \(n \times n\) matrix, not the \((p+1) \times (p+1)\) one you need.

Question 1 of 5

If \(\mathbf{X}\) is an \(n \times (p+1)\) matrix, what are the dimensions of \(\mathbf{X}^\top\mathbf{X}\)?

Question 2 of 5

Which property does the hat matrix \(\mathbf{H}\) not possess?

Question 3 of 5

The equation \(\mathbf{X}^\top\mathbf{e} = \mathbf{0}\) tells us that:

Question 4 of 5

In the projection diagram, \(\hat{\mathbf{y}}\) is:

Question 5 of 5

Given \(\mathbf{X}^\top\mathbf{X} = \begin{bmatrix}3&6\\6&14\end{bmatrix}\) and \(\mathbf{X}^\top\mathbf{y} = \begin{bmatrix}10\\23\end{bmatrix}\), what is \(\hat\beta_1\) (the slope)?