Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Principal components analysis

Suppose we conduct an experiment nn times and collect pp pieces of information from each experiment. Then we can suppose that we collected a dataset D={(x11,x12,⋯ ,x1p),(x21,x22,⋯ ,x2p),⋯ ,(xn1,xn2,⋯ ,xnp)}\mathcal{D} = \{ (x_{11},x_{12},\cdots, x_{1p} ), (x_{21},x_{22},\cdots, x_{2p} ), \cdots, (x_{n1},x_{n2},\cdots, x_{np} ) \} and can organize this dataset into the following dataframe

F=[x11x12⋯x1px21x22⋯x2p⋮⋮⋮⋱xn1xn2⋯xnp]\begin{align} \mathcal{F} = \begin{bmatrix} x_{11} & x_{12} & \cdots & x_{1p}\\ x_{21} & x_{22} & \cdots & x_{2p}\\ \vdots & \vdots & \vdots & \ddots \\ x_{n1} & x_{n2} & \cdots & x_{np}\\ \end{bmatrix} \end{align}

It is important to acknowledge that a single observation is the tuple (xi1⋯x1p)(x_{i1} \cdots x_{1p}) and so we assume that a tuple of random variables jointly generated this single observation. In other words, it may not be appropriate to treat each piece of information (often called a covariate) as being generated independently of other covariates: they are generated together.

The goal of principal components analysis is to find combinations of covariates that “best” describe our original dataset D\mathcal{D}.

In this lesson we will aim to understand:

  1. How principal components defines “best”

  2. The interplay between the (linear) algebra and geometry of principal components

  3. How to interpret the outputs of out principal components analysis

  4. How principal components analysis differs from linear regression

Simulated data

We will first build a simulated dataset to illustrate the main concepts in principal components analysis. Let the random variables [X1,X2][X_{1}, X_{2}] have a multivariate normal distribution (MVN)

[X1X2]∼MVN(μ,Σ)μ=[−11];  Σ=[10.30.32]\begin{align} &\begin{bmatrix} X_{1} \\ X_{2} \end{bmatrix} \sim \text{MVN}( \mu, \Sigma ) \\ & \mu = \begin{bmatrix} -1 \\ 1 \end{bmatrix} ; \; \Sigma = \begin{bmatrix} 1 & 0.3 \\ 0.3 & 2 \end{bmatrix} \end{align}

As a recap, the vector μ\mu decribes the expected value for X1X_{1} (the value -1) and the expected value for X2X_{2} (the value 1). The covariance matrix Σ\Sigma describes the variance for X1X_{1} (the value 1), the variance for X2X_{2} (the value 2), and the covariance between X1X_{1} and X2X_{2} (the value 0.3).

We can use numpy to help use generate n=100n=100 observations from the above MVN where every observation contains two pieces of information (i.e. two covariates). In addition to storing our dataset, we will visualize this information by scatterplotting values of the first covariate versus values of the second covariate.

Plotting and centering our data

Below, you will see two plots side-by-side. The first plot includes the scatter (blue circles) as well as a vertical and horizontal line at the values 0 (dashed blue lines). Though we typically draw the horizontal and vertical axes for a plot at 0, the value zero is arbitrary. A more reasonable place to draw our axes may be the expected values for the first (-1) and second (1) covariate. We drew those axes as solid blue lines.

The solid blue lines (expected values) look to be better centered over our dataset than the dashed lines (zeros).

The above is the motivation behind centering. Instead of plotting our raw data D={(x11,x12),(x21,x22),⋯ ,(xn1,xn2)}\mathcal{D} = \{ (x_{11}, x_{12}),(x_{21}, x_{22}),\cdots, (x_{n1}, x_{n2}) \} we can plot a centered dataset that describes the distance of each observation from their expected value or (xi1−μ1,xi2−μ2)(x_{i1} - \mu_{1}, x_{i2}-\mu_{2}) for all ii observations. Another way to look at this is with our dataframe

F−=[x11−μ1x12−μ2x21−μ1x22−μ2⋮⋮xn1−μ1xn2−μ2]=Fn×2−μ1×2\begin{align} \underset{-}{\mathcal{F}} = \begin{bmatrix} x_{11} - \mu_{1} & x_{12} - \mu_{2} \\ x_{21} - \mu_{1} & x_{22} - \mu_{2} \\ \vdots & \vdots \\ x_{n1}- \mu_{1} & x_{n2} - \mu_{2} \\ \end{bmatrix} = \mathcal{F}_{n \times 2} - \mu_{1 \times 2} \end{align}

Importantly, the original dataset describes the distance of each covariate from the (arbitrary) value zero. In other words,

F=[x11x12x21x22⋮⋮xn1xn2]=[x11−0x12−0x21−0x22−0⋮⋮xn1−0xn2−0]=Fn×2−01×2\begin{align} \mathcal{F} = \begin{bmatrix} x_{11} & x_{12} \\ x_{21} & x_{22} \\ \vdots & \vdots \\ x_{n1} & x_{n2} \\ \end{bmatrix} = \begin{bmatrix} x_{11} - 0 & x_{12} - 0 \\ x_{21} - 0 & x_{22} - 0 \\ \vdots & \vdots \\ x_{n1}- 0 & x_{n2} - 0 \\ \end{bmatrix} = \mathcal{F}_{n \times 2} - 0_{1 \times 2} \end{align}

The rightmost panel in our figure below plots the centered dataset (solid blue circles) as well as vertical and horizontal axes at zero. The reason that these horizonal/vertical lines at zero are centered over the data now is because the value zero for our centered dataset represents the expected value. For example, the ithi^{\text{th}} observation is (x1=xi1−μ1,x2=xi1−μ2)(x_{1}= x_{i1}-\mu_{1}, x_{2} = x_{i1}-\mu_{2} ). When x1=0x_{1}=0 then xi1−μ1=0x_{i1}-\mu_{1}=0 or xi1=μ1x_{i1}=\mu_{1}. The same argument could be used for the second covariate as well. The centered datset describes the distance from each covariate’s expected value.

<Figure size 900x400 with 2 Axes>

Geometric intuition behind Principal Components Analysis

Centering our dataset (right panel above) helped us more naturally descirbe our dataset. The zero horizonal/vertical values now correspond to the expected value for the first/second covariate. However, centering only allows us to shift our axes up/down and left/right to better decribe our data. What about rotation?

Geometrically, Principal Components Analysis asks “Can we rotate our axes to better characterize our dataset?”. Well, if we are willing to consider rotating our axes then we need to decide on what we mean when say better.

Lets start with just the horizontal axis and no vertical axis.

Below is a short video that shows several iterations of the following steps:

  1. (Top left) Produce a scatter plot of x1x_{1} on the horizonal axis and x2x_{2} on the vertical axis.

  2. (Top right) Include only the horizontal axis and project all of the points onto this axis. To be clear, this projection maps every data point (x1,x2)→x1(x_{1},x_{2}) \to x_{1}.

  3. (Bottom left) Plot a histogram of the projected points. In other words, produce a histogram of the x1x_{1} values and include a red shaded region that charazterizing 95% probability. From this sample of x1x_{1} points we will compute the variance or Var(x1,x2,⋯ ,xn)\text{Var}(x_{1},x_{2},\cdots,x_{n}). This value is titled at the top of the plot.

  4. (Bottom right) Append this variance value to a list of all the variances, one for each iteration, and plot a histogram of these variances.

Its important here to note what objects are random and which are not. We can see in this video os simultions that randomness does not come from the projection (our choise of the horizontal axis). Over each iteration the horizontal axis never changes. Instead, the randomness comes from our sample of data points.

Loading...

Now that we see how to map a projection of xx onto the horizontal axis, it is intuitive that we might want to consider projections onto more complicated directions than just the horizontal (or vertical for that matter) projection.

A projection of one vector xx onto another direction uu

Suppose we have a vector xx (in red) and want to project this vector onto another called uu (in blue). See the picture below. Then we know that this projection, called xux_{u} must be in the same direction as uu. We can write this down mathemtically as

xu=αu\begin{align} x_{u} = \alpha u \end{align}

where α\alpha defines how far in the direction of uu we should move. But, we are not sure how far in the direction of uu we should extend this vector to be considered a “projection” of xx onto uu.

One reasonable property we might suggest for our projection xux_{u}, is that this vector will be in the direction of uu and as close as possible to our original xx. The vector that is closest to xx but in the direction of uu is the on such that x−ux-u and uu are orthogonal to one another (instead or orthogonal, perpendicular is ok to use here as well).

To see why, intuitively, we plotted the difference between two projections onto u. The first difference we plotted as a black dashed line that is perpendicular to uu. The second difference, drawn as a purple dashed-dot line is not perpendicular to uu. You can see that the line that is perpendicular to uu is shorter in length than the other.

We can also see why this is the case algebraically by looking at the distance between xx and the projection of xx onto uu or xux_{u}. The typical way to define the length of a vector is

∣∣x∣∣=(xTx)1/2.\begin{align} ||x|| & = (x^{T}x)^{1/2}. \\ \end{align}

It is important to note that if the length of xx is smallest than the squared length is smallest too. The squared length is almost always better to work with because

∣∣x∣∣2=(xTx)\begin{align} ||x||^{2} & = (x^{T}x) \end{align}

In other words, we want to find the value α\alpha that minimizes

∣∣x−xu∣∣2=∣∣x−αu∣∣2=(x−αu)T(x−αu)=xTx−2αxTu+α2uTu\begin{align} ||x - x_{u}||^{2} = ||x - \alpha u||^{2} & = (x-\alpha u)^{T} (x-\alpha u) \\ &= x^{T}x - 2\alpha x^{T} u + \alpha^{2} u^{T}u \end{align}

Lets take the derivative and set this to zero to find that

f(α)=xTx−2αxtu+α2uTudfdα=−2xTu+2αuTu=0α=xTuuTu\begin{align} f(\alpha) &= x^{T}x - 2\alpha x^{t} u + \alpha^{2} u^{T}u\\ \frac{df}{d \alpha} = - 2 x^{T} u + 2 \alpha u^{T}u = 0 \alpha = \frac{x^{T} u }{ u^{T} u } \end{align}

Now when we look at the vector uu and the residual (x−αu)(x - \alpha u) we see that

uT(x−xTuuTuu)=uTx−xTuuTuuTu=uTx−xTu=0\begin{align} u^{T} \left ( x - \frac{x^{T} u }{ u^{T} u } u \right) &= u^{T} x - \frac{x^{T} u}{u^{T} u} u^{T} u \\ &= u^{T} x - x^{T} u = 0 \end{align}

There is an important lesson here. When we want to choose a vector in the direction of uu such that it is closest to xx then the vector that we need to choose is one such that uu and the residual, (x−αu)(x-\alpha u), are orthogonal.

<Figure size 640x480 with 1 Axes>

Recap If i want to project the vector xx onto (or in the direction of) the vector uu then that projection is

xu=xTuuTuu=uTxuTuu\begin{align} x_{u} = \frac{ x^{T}u }{u^{T}u} u = \frac{ u^{T}x }{u^{T}u} u \end{align}

We can also extend this idea to project many vectors onto the vector uu. For example, suppose that we have three vectors: x1x_{1},x2x_{2}, and x3x_{3} and wish to project them all onto uu. Then we can form the following matrix multiplication to do this in one swoop.

its important to note below that we need to divide the vector uu by uTuu^{T}u

Let u~=uuTu\tilde{u} = \frac{u}{u^{T}u} then

Xu~=XuuTu=[x1Tu/uTux2Tu/uTu⋮xnTu/uTu]=[α1α2⋮αn]\begin{align} X \tilde{u} = \frac{Xu}{u^{T}u} = \begin{bmatrix} x_{1}^{T} u \Big / u^{T}u \\ x_{2}^{T} u \Big / u^{T}u \\ \vdots \\ x_{n}^{T} u \Big / u^{T}u \\ \end{bmatrix} = \begin{bmatrix} \alpha_{1} \\ \alpha_{2} \\ \vdots\\ \alpha_{n} \\ \end{bmatrix} \end{align}

Projecting onto different directions

Back to our data as a stacked matrix of vectors or

X=[x1x2⋮xn]\begin{align} X = \begin{bmatrix} \rule[0.5ex]{1.2em}{0.4pt} x_{1} \rule[0.5ex]{1.2em}{0.4pt} \\ \rule[0.5ex]{1.2em}{0.4pt} x_{2} \rule[0.5ex]{1.2em}{0.4pt}\\ \vdots \\ \rule[0.5ex]{1.2em}{0.4pt} x_{n} \rule[0.5ex]{1.2em}{0.4pt}\\ \end{bmatrix} \end{align}

Given a direction vector, uu, we can project our data onto this uu by first computing u~=u/(uTu)\tilde{u} = u / (u^{T}u) and and then computing Xu=Xu X_{u} = X u. The notation XuX_{u} stands for the vectors x1,x2,⋯ ,xnx_{1}, x_{2}, \cdots, x_{n} projected in the direction of uu.

Lets see how this works for the horizontal and vertical axes. The horizontal axis is in the direction u=[1,0]u = [1,0], and we see that uTu=[1,0]T[1,0]=12+02=1u^{T}u = [1,0]^{T}[1,0] = 1^{2} + 0^{2} = 1 and so we can project our data onto the horizontal axis by computing

X[1,0]=X[10]=[x11x12x21x12⋮⋮xn1xn2][10]=[x11x21⋮xn1]\begin{align} X_{ [1,0] } &= X \begin{bmatrix} 1 \\ 0 \end{bmatrix} \\ &= \begin{bmatrix} x_{11} & x_{12} \\ x_{21} & x_{12} \\ \vdots & \vdots \\ x_{n1} & x_{n2} \\ \end{bmatrix}\begin{bmatrix} 1 \\ 0 \end{bmatrix} = \begin{bmatrix} x_{11} \\ x_{21} \\ \vdots\\ x_{n1} \\ \end{bmatrix} \end{align}

In other words “projecting” onto the horizontal axis is just selecting the first entry in each vector. In this 2-dimensional example, we can also project onto the vertical axis by choosing the direction vector u=[0,1]Tu = [0,1]^{T}, and so then

X[0,1]=[x12x22⋮xn2]\begin{align} X_{ [0,1] } = \begin{bmatrix} x_{12} \\ x_{22} \\ \vdots\\ x_{n2} \\ \end{bmatrix} \end{align}

Below we will project our data, XX, onto three different uu vectors: (Top right) u=[1,0]Tu=[1,0]^{T}, (Bottom Left) u=[1,−1]Tu = [1,-1]^{T}, and (Bottom Right) u=[1,1]Tu=[1,1]^{T}. The panel on the Top Left shows the typical visualization of our data not projected onto a uu vector.

<Figure size 600x600 with 4 Axes>
E(∣∣Xn×p−αn×1u1×p∣∣)\begin{align} \mathbb{E}( || X_{n \times p} - \alpha_{n \times 1} u_{1 \times p} || ) \end{align}
E[(X−αu)(X−αu)T]=E[XXT−2XαuT+uuT]\begin{align} \mathbb{E} \left[ ( X-\alpha u )( X-\alpha u )^{T} \right] = \mathbb{E} \left[ XX^{T} - 2X\alpha u^{T} + uu^{T} \right] \end{align}
xiu=α⋅u(xiu−u)Tx=0(xiu)Tu−uTx=0α⋅uTu−uTx=0α=uTxuTu=xTuuTu\begin{align} x^{u}_{i} &= \alpha \cdot u \\ (x^{u}_{i} - u)^{T}x &= 0 \\ (x^{u}_{i})^{T}u - u^{T}x &= 0 \\ \alpha \cdot u^{T}u - u^{T}x &= 0 \\ \alpha &= \frac{u^{T}x}{u^{T}u} = \frac{x^{T}u}{u^{T}u} \\ \end{align}
Var(αu)=Var[(Xu)u]=E([(Xu)u]T[(Xu)u])−[E((Xu)u)]2=E((Xu)2[u]T[u])−02=E[(Xu)2]=E[α2]\begin{align} Var(\alpha u ) &= Var\left[ (Xu)u \right]\\ &=\mathbb{E}\left( \left[ (Xu) u\right]^{T} \left[ (Xu) u\right]\right) - \left[\mathbb{E}( (Xu) u) \right]^{2} \\ &=\mathbb{E}\left( (Xu)^{2} \left[u\right]^{T} \left[u\right]\right) - 0^{2}\\ &=\mathbb{E}\left[(Xu)^{2}\right] \\ &=\mathbb{E}\left[\alpha^{2}\right] \\ \end{align}
X=αu+(x−αu)∣∣X∣∣=∣∣αu∣∣+∣∣(X−αu)∣∣XTX=(αu)T(αu)+∣∣(X−αu)∣∣XTX=α2uTu+∣∣(X−αu)∣∣XTX=α2+∣∣(xi−αiu)∣∣E(XTX)=E[α2]+E[∣∣(xi−αiu)∣∣]Var(X)=Var(αu)+E[∣∣(xi−αiu)∣∣]\begin{align} X = \alpha u + (x - \alpha u)\\ || X || = || \alpha u || + || (X - \alpha u) ||\\ X^{T} X = (\alpha u)^{T}(\alpha u) + || (X - \alpha u) ||\\ X^{T} X = \alpha^{2} u^{T}u + || (X - \alpha u) ||\\ X^{T} X = \alpha^{2} + || (x_{i} - \alpha_{i}u) ||\\ \mathbb{E}( X^{T} X ) = \mathbb{E}\left[ \alpha^{2} \right] + \mathbb{E}\left[ || (x_{i} - \alpha_{i}u) ||\right] \\ \text{Var}(X) = \text{Var}( \alpha u ) + \mathbb{E}\left[ || (x_{i} - \alpha_{i}u) ||\right] \\ \end{align}
Var(αu)Var((Xu)u)E[((Xu)u)2]E[uTu((Xu))2]E[((Xu))2]E[((Xu))T(Xu)]E[uTXTXu]uTΣu\begin{align} \text{Var}( \alpha u )\\ \text{Var}( (Xu) u )\\ \mathbb{E}\left[ ( (Xu) u )^{2} \right] \\ \mathbb{E}\left[ u^{T}u ( (Xu) )^{2} \right] \\ \mathbb{E}\left[ ( (Xu) )^{2} \right] \\ \mathbb{E}\left[ ( (Xu) )^{T}(Xu) \right] \\ \mathbb{E}\left[ u^{T} X^{T}Xu \right] \\ u^{T} \Sigma u \\ \end{align}
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[11], line 2
      1 l,u         = np.linalg.eigh(COV)
----> 2 l_est,u_est = np.linalg.eigh( d_centered.T @ d_centered/N )

NameError: name 'N' is not defined