Linear Regression With Matrices

Formulate simple linear regression as a matrix equation Aβ=yA\boldsymbol\beta = \mathbf{y} and find the coefficients of the line of best fit by solving the normal equations ATAβ=ATyA^TA\,\boldsymbol\beta = A^T\mathbf{y}.

Step 1 of 119%

Tutorial

The Matrix Form of Linear Regression

Linear regression seeks to model a linear relationship between an independent variable xx and a dependent variable yy:

y=β0+β1x,y = \beta_0 + \beta_1 x,

where β0\beta_0 is the intercept and β1\beta_1 is the slope.

Given nn data points (x1,y1),(x2,y2),,(xn,yn),(x_1,y_1),(x_2,y_2),\ldots,(x_n,y_n), we want to find the coefficients β0\beta_0 and β1.\beta_1. Substituting each data point into the model gives nn equations:

β0+β1x1=y1β0+β1x2=y2β0+β1xn=yn.\begin{align*} \beta_0 + \beta_1 x_1 &= y_1 \\ \beta_0 + \beta_1 x_2 &= y_2 \\ &\,\,\vdots \\ \beta_0 + \beta_1 x_n &= y_n. \end{align*}

We can write this system compactly as Aβ=y,A\boldsymbol\beta = \mathbf{y}, where

A=[1x11x21xn],β=[β0β1],y=[y1y2yn].A = \begin{bmatrix} 1 & x_1 \\ 1 & x_2 \\ \vdots & \vdots \\ 1 & x_n \end{bmatrix},\qquad \boldsymbol\beta = \begin{bmatrix} \beta_0 \\ \beta_1 \end{bmatrix},\qquad \mathbf{y} = \begin{bmatrix} y_1 \\ y_2 \\ \vdots \\ y_n \end{bmatrix}.

The matrix AA is called the design matrix. Its first column is all 11s (these multiply the intercept β0\beta_0), and its second column holds the xx-values of the data.

For example, the data points (2,5),(4,6),(7,10)(2,5),(4,6),(7,10) give the matrix equation

[121417][β0β1]=[5610].\begin{bmatrix} 1 & 2 \\ 1 & 4 \\ 1 & 7 \end{bmatrix}\begin{bmatrix} \beta_0 \\ \beta_1 \end{bmatrix} = \begin{bmatrix} 5 \\ 6 \\ 10 \end{bmatrix}.
navigate · Enter open · Esc close · ⌘K/Ctrl K toggle