Ordinary Least Squares
Using linear algebra to generalize the simple linear regression model.
Data 100/Data 200, Fall 2020 @ UC Berkeley
Anthony D. Joseph and Fernando Pérez
Content credit: Suraj Rampure, Ani Adhikari, Deborah Nolan, Joseph Gonzalez
LECTURE 13
Recap: Simple Linear Regression
Simple Linear Regression
In the last lecture, we re-introduced the simple linear regression model from Data 8.
Multiple Regression
We also extended this model to account for multiple features.
Today, we will learn how to find the optimal parameters (“thetas”) for �multiple regression, for any number of features.
Each xj is a separate feature.
Agenda
Lots of linear algebra!
Linear algebra formulation
Dot products
The dot product of two vectors and is defined as follows:
Multiple regression as a dot product
We previously stated that the multiple regression model was of the form
This can be restated as a dot product between two vectors.
Even though they don’t have arrows on top of them, x and θ are still vectors!
scalar
Design matrix
Our mean squared error involves all observations at once, so it may be valuable to express our model in terms of all observations, instead of just one observation. One step in that process is stacking all of our observations together into a design matrix. With n observations:
Columns correspond to features.
e.g. feature 1, for all data points
Rows correspond to observations.
e.g. all features for data point 3
Here, n = 708, and p = 3.
Example design matrix
Multiple regression as a matrix multiplication
This allows us to express our linear model on our entire dataset (not just one observation) as
Note: This means that represents an n-length vector containing all of our true y values.
Vector with dimensions n x 1.
Vector with dimensions (p + 1) x 1.
Matrix with dimensions n x (p + 1).
Multiple regression as a matrix multiplication
Moving forward:
Multiple regression as a matrix multiplication
For instance, to compute the predicted output for the second observation:
Or, more compactly:
X2 is a vector, not a matrix. That is why it is transposed.
Example
Consider the following design matrix and value of θ.
The predicted response (output) for the second observation:
Example
Consider the following design matrix and value of θ.
The predicted response vector:
Summary of notation
When looking at a single observation, our model is
In many settings, we assume that we have only p (and not p + 1) columns. One of those p columns may be “1” for each observation.
When looking at multiple observations, our model is
Problem statement
Vector norms
Consider the vector:
L2 vector norm:
L1 vector norm:
L2 vector norm
Residuals
Residuals are defined as being the difference between an actual and predicted value, in the regression context.
The red dotted lines represent residuals.
Residual vector
We can stack all n residuals into a vector, called the residual vector, e:
The residual vector is the “difference” between the two vectors containing our true y values and predicted y values.
Mean squared error, again
We are choosing our loss to be squared loss. This means, the average loss across our dataset is mean squared error.
We can write this in terms of the norm of the residual vector!
This is the residual vector!
Optimization procedure
As we did in the last lecture, we note that the value of theta that minimizes is the same value that minimizes
Therefore, our goal is to find the value of that minimizes the squared L2 norm of the residual vector. In other words, we want the “distance” between and to be minimized.
There are two ways we can determine the optimal here.
Geometric derivation
A linear combination of columns
The linear model represents as a linear combination of the columns of .
Span
Our prediction is a linear combination of the columns of X.
Recall, this is the residual vector,
.
Our goal is to minimize the L2 norm of the residual vector, i.e. we want our predictions to be “as close” to our true y values as possible.
How do we minimize this distance – the norm of the residual vector (squared)?
How do we minimize this distance – the norm of the residual vector (squared)?
The vector in span(X) that is closest to Y is the orthogonal projection of Y onto span(X).
We will not prove this: see Khan Academy.
How do we minimize this distance – the norm of the residual vector (squared)?
The vector in span(X) that is closest to Y is the orthogonal projection of Y onto span(X).
Thus, we should choose the θ that makes the residual vector orthogonal to span(X).
Orthogonality
We say two vectors are orthogonal if and only if their dot product is 0.
Suppose a vector is orthogonal to the span of the columns of a matrix.
Let . Suppose v is orthogonal to the span of the columns of M.
v is orthogonal to each column of M separately.
(Note, each column of M has length n, and v also has length n).
This product encapsulates all d of the equations on the left into a single equation. The quantity on the right is the zero vector (d-length vector full of 0s).
Residuals are orthogonal to the span of X
We want the θ such that the residual vector is orthogonal to .
By the definition of orthogonality:
Rearranging:
The normal equation:
Assuming is full rank:
Still the zero vector!
This result is so important, it deserves its own slide.
It is the least squares estimate for θ.
Summary
1. Choose a model. We chose the multiple linear regression model, formulated using a matrix.
2. Choose a loss function. We chose squared loss, and hence our average loss was
3. Minimize average loss to determine optimal model parameters. Done!
Residuals
Residual plots
Residual plots can tell us about the quality of our model.
Properties:
No pattern, even spread.
Clear quadratic relationship in the residuals.
No clear relationship, but uneven spread.
Residuals are orthogonal to the span of X
When using the optimal parameter vector, our residuals are orthogonal to .
We denote this column with . �
Since , it is also true that
Why?
Properties when our model has an intercept term
When our linear model has an intercept term (i.e. when our design matrix has a column of all 1s), the following properties hold true:
These properties are true when there is an intercept term, and not necessarily when there isn’t. You will prove them in a homework.
Existence of a unique solution
Does a solution always exist?
Does a unique solution always exist?
Understanding the solution matrices
Typically, n is much larger than p.
Understanding the solution matrices
Typically, n is much larger than p.
Understanding the solution matrices
The Normal Equation:
Our optimal parameter vector can be thought of as the solution to a set of p + 1 equations, with p + 1 unknowns.
Does a unique solution always exist?
Let’s consider our optimal θ for the multiple linear regression model:
Invertibility of
When is XTX invertible?
Invertibility of
When does our design matrix not have full column rank?
n
p + 1
Since the row rank and column rank of a matrix are equal, rank(X) ≤ min(n, p+1).
If n < p + 1, then the column rank cannot possibly be p + 1, meaning it cannot be full column rank.
Summary, what’s next
Summary
Moving forward
Think of this lecture as the third in a trilogy of lectures that establish the foundations of regression. From here, we will cover: