← All notes

Note · Quantitative Finance / Econometrics

The Geometry of Ordinary Least Squares

Visualizing the workhorse model for regression analysis.

A linear model can be expressed in the form:

y=Xb+e\begin{align} \pmb y = X \pmb b + \pmb e \end{align}

Where y∈RN\pmb y \in \mathbb{R}^N, X∈RN×KX \in \mathbb{R}^{N\times K}, b∈RK\pmb b \in R^K, and e∈RN\pmb e \in \mathbb{R}^N.

Mathematically, the equation y=Xb+e\pmb y = X\pmb b + \pmb e is nothing more than a statement until we define what the problem is. In OLS, we’re given y\pmb y and XX and asked to find bb. What criteria defines b\pmb b? We say we want to minimize the squared error terms.

Minimizing the squared error terms by select b\pmb b can be thought of in two ways.

  1. Selecting b\pmb b to minimize ∑i=1Nei2=∑i=1N(yi−xi′b)2=∑i=1N(yi−y^i)2\sum\limits_{i=1}^N e_i^2 = \sum\limits_{i=1}^N (y_i - \pmb x_i'\pmb b)^2 = \sum\limits_{i=1}^N (y_i - \hat y_i)^2. We write this problem as:
arg min⁡b ∑i=1Nei2\begin{align} \argmin_b \ \sum\limits_{i=1}^N e_i^2 \end{align}
  1. Selecting bb to minimize e′e=(y−Xb)′(y−Xb)=(y−y^)′(y−y^)\pmb e'\pmb e = (\pmb y - X\pmb b)'(\pmb y - X\pmb b) = (\pmb y - \pmb {\hat y})'(\pmb y - \pmb{\hat y}). We write this as:
arg min⁡b e′e\begin{align} \argmin_{\pmb{b}} \ \pmb e'\pmb e \end{align}

I’ll refer to the setup (1) as the row way of thinking about the problem and (2) as the column way of thinking about the problem.

The Row Interpretation (Elementary)

Thinking about the problem as minimizing ∑i=1Nei2=∑i=1N(yi−xi′b)2\sum\limits_{i=1}^N e_i^2 = \sum\limits_{i=1}^N (y_i - \pmb x_i'\pmb b)^2 is intuitive and, consequentially, the first way students are taught to think about regression. Consider row ii of the NN rows that form the y=Xb+e\pmb y = X\pmb b + \pmb e.

By convention XX with each row representing and “observation” with each column representing some “feature” of the observation. Each xix_i has the corresponding scalar outcome yiy_i. We can make a tuple (xi,yi(\pmb x_i, y_i) and plot it in the space RK+1\mathbb{R}^{K+1}.

One features case (K=1)

When we only have one feature our points live in R2\mathbb{R}^2. This is our standard xy-coordinate space from middle school. After we put on all the dots on our paper defined by (xi,yi)(x_i,y_i), our goal is draw a line that minimizes the sum of squared y-distances from (xi,yi)(x_i,y_i) to the line defined by (xi,xib)=(xi,y^i)(x_i,x_ib) = (x_i, \hat y_i). Since ei2=(yi−y^i)2e_i^2 = (y_i - \hat y_i)^2 we can think of each ei2e_i^2 as a collection of squares of which we’re trying to minimize the total area.

Two feature case (K=2)

When x∈R2x \in \mathbb R^2 we now have points that live in R3\mathbb R^3. Writing down each point as the tuple ([x1i,x2i],yi)=(xi,yi)\left([x_{1i},x_{2i}],y_i\right) = (\pmb x_i, y_i) should convey the idea that the part xx defines the location of the point along the “base” of the graph, i.e. the xy-coordinates, while the yy coordinate pins it down to a specific location along the z-axis. Since ei=yi−xi′be_i = y_i - \pmb x_i'\pmb b we see that x′bx'b defines the plane of possible predictions and eie_i is the vertical distance along the z-axis to the plane. Again, these squared vertical distances define a collection of squares whose aggregate area we want to minimize.

Higher dimensions (K>2)

I wrote out case K=2 in a way that visualizing dimensions that aren’t visualizable should be intuitive. Recapping, xx defines the “base” of the point given by (xi,yi)(x_i,y_i) while yy pins the point to an exact location along that final orthogonal (“vertical”) axis. We find the bb that traces out that best hyperplane (the surface of the hyperplane is given by x′bx'b). In least squares land, the “best” hyperplane is the hyperplane where no other hyperplane could make the area of our collection of squares any smaller.

The Column Interpretation (Advanced)

Minimizing e′e\pmb e' \pmb e

What does it mean to minimize e′e\pmb e'\pmb e? Note that minimizing e′e\sqrt{\pmb e'\pmb e} is the same as minimizing e′e\pmb e'\pmb e because the square-root operator is a monotonic transformation on e′e\pmb e'\pmb e. A monotonic transformation is any transformation T(⋅)T(\cdot) that preserves the order of the set it’s acting upon. In our case, if e1′e1>e2′e2\pmb e_1'\pmb e_1 > \pmb e_2'\pmb e_2 and T(⋅)T(\cdot) is monotonic transformation then T(e1′e1)>T(e2′e2)T(\pmb e_1'\pmb e_1) > T(\pmb e_2'\pmb e_2). Thus,

arg min⁡b e′e≡arg min⁡b e′e\begin{align} \argmin_{\pmb b} \ \pmb e'\pmb e \equiv \argmin_{\pmb b} \ \sqrt{\pmb e'\pmb e} \end{align}

Now, e′e=∑ei\sqrt{\pmb e'\pmb e} = \sqrt{\sum e_i} which is the definition of euclidean length. Euclidean length is just an extension of the idea of length in R2\mathbb R^2 and R3\mathbb R^3. Therefore we know we’re trying to minimize the length of the error vector.

Choosing b\pmb b

The following derivation should make it clear that b\pmb b multiplies the columns of XX to produce a vector of predictions y^\hat{\pmb y} that is close to the observed outcomes y\pmb y.

Recall our original set up:

[y1y2⋮yn]=[x1,1x1,2…x1,kx2,1x2,2…x2,k⋮⋮⋱⋮xn,1xn,2…xn,k][b1b2⋮bk]+[e1e2⋮en]\begin{align} \begin{bmatrix}y_1 \\ y_2 \\ \vdots \\ y_n\end{bmatrix} &= \begin{bmatrix}x_{1,1} & x_{1,2} & \dots & x_{1,k} \\ x_{2,1} & x_{2,2} & \dots & x_{2,k} \\ \vdots & \vdots & \ddots & \vdots \\ x_{n,1} & x_{n,2} & \dots & x_{n,k} \end{bmatrix} \begin{bmatrix}b_1 \\ b_2 \\ \vdots \\ b_k\end{bmatrix} +\begin{bmatrix}e_1 \\e_2 \\ \vdots \\ e_n \end{bmatrix} \end{align}

Which is equivalent to:

y=[x1x2…xk][b1b2⋮bk]+e=b1x1+b2x2+⋯+bkxk+e=y^+e  ⟹  e=y−y^e=y+(−y^)\begin{align} \pmb y &= \begin{bmatrix}\pmb x_1 & \pmb x_2 & \dots & \pmb x_k\end{bmatrix} \begin{bmatrix}b_1 \\ b_2 \\ \vdots \\ b_k\end{bmatrix}+ \pmb e \\\\ &=b_1 \pmb x_1 + b_2 \pmb x_2 + \dots + b_k \pmb x_k + \pmb e \\ &= \pmb{ \hat y } + \pmb e \\ \implies \pmb e &=\pmb y - \pmb{ \hat y } \\ \pmb e &=\pmb y + (-\pmb{ \hat y }) \end{align}

Multiplying a vector by a scalar has the action of scaling the vector. Vector addition has the action of putting vectors “tip-to-tail”. Thus, the goal of OLS is to select b\pmb b that orients the column vectors of XX in the way that minimizes the length of the vector e\pmb e. Where e\pmb e is the vector that connects the prediction vector, y^\pmb{ \hat y }, to the outcome vector, y\pmb y.

OLS Projection Matrices

There are a couple famous projection matrices associated with OLS.

The first projection matrix is called the prediction maker.

Recall b=(X′X)−1X′y\pmb b = (X'X)^{-1}X'\pmb y. Now:

y^=Xb=X(X′X)−1X′y=Py\begin{align} \pmb{ \hat y}&= X\pmb b \\ &= X(X'X)^{-1}X'\pmb y \\ &= P\pmb y \end{align}

Where P=X(X′X)−1X′P = X(X'X)^{-1}X' is the prediction maker.

We can verify PP is projection matrix. PP=[X(X′X)−1X′][X(X′X)−1X′]=X(X′X)−1X′=PPP = [X(X'X)^{-1}X'][X(X'X)^{-1}X']= X(X'X)^{-1}X'=P. We call it the prediction maker because PyP\pmb y generates our vector of predictions.

Learn to recognize PP in terms of XX.

The second projection matrix is called the residual maker.

e=y−y^=y−Py=(I−P)y=My\begin{align} \pmb e &= \pmb y - \pmb{\hat y} \\ &= \pmb y - P\pmb y \\ &= (I-P)\pmb y \\ &= M \pmb y \end{align}

Where M=I−P=I−X(X′X)−1X′M =I-P = I - X(X'X)^{-1}X' is the residual maker.

We can quickly verify that MM is a projection matrix. MM=(I−P)(I−P)=I−2P+P=I−P=MMM = (I-P)(I-P)= I -2P + P = I-P = M. We call MM the residual maker because My=eM\pmb y= \pmb e.

Orthogonality of y^\pmb{\hat y} and e\pmb e.

We saw that y^=Py\hat{ \pmb y} = P\pmb y and that e=My=(I−P)y\pmb e = M\pmb y = (I - P)\pmb y where PP and MM are projection matrices. It’s easy to see that PM=MP=0PM = MP = 0. Therefore the projection matrices map to orthogonal subspaces.

Orthogonality is the generalization of perpendicular. One interpretation for perpendular is that the vectors for a right-angle with each other. Noting that y^⊥e\hat{\pmb y} \perp \pmb e and that y=y^+e\pmb y = \hat{\pmb y} + \pmb e, it becomes clear that y\pmb y, y^\hat{\pmb y}, and e\pmb e form a right triangle in RN\mathbb R^N with y\pmb y as the hypotenuse.

In conclusion, we can say that b\pmb b maps the column vectors of XX to a single vector whose tip is directly “below” y\pmb y.