Back to Home
Mathematics

Dimensionality Reduction with Principal Component Analysis

A deep dive into the mathematical foundations of Principal Component Analysis (PCA) for dimensionality reduction.

Working directly with high-dimensional data, such as images, comes with some difficulties: It is hard to analyze, interpretation is difficult, visualization is nearly impossible, and storage of the data vectors can be expensive. However, high-dimensional data often has properties that we can exploit. For example, high-dimensional data is often overcomplete, i.e., many dimensions are redundant and can be explained by a combination of other dimensions. Furthermore, dimensions in high-dimensional data are often correlated so that the data possesses an intrinsic lower-dimensional structure. Dimensionality reduction exploits structure and correlation and allows us to work with a more compact representation of the data, ideally without losing information. We can think of dimensionality reduction as a compression technique, similar to jpeg or mp3, which are compression algorithms for images and music.

In this post, we will discuss principal component analysis (PCA), an algorithm for linear dimensionality reduction. PCA has been around for more than 100 years and is still one of the most commonly used techniques for data compression and data visualization.

Problem Setting

In PCA, we are interested in finding projections x~n\tilde{x}_nx~n​ of data points xnx_nxn​ that are as similar to the original data points as possible, but which have a significantly lower intrinsic dimensionality.

More concretely, we consider an i.i.d. dataset X={x1,…,xN}X = \{x_1, \dots, x_N\}X={x1​,…,xN​}, xn∈RDx_n \in \mathbb{R}^Dxn​∈RD, with mean 0 that possesses the data covariance matrix

S=1N∑n=1Nxnxn⊤S = \frac{1}{N} \sum_{n=1}^N x_n x_n^\topS=N1​n=1∑N​xn​xn⊤​

Furthermore, we assume there exists a low-dimensional compressed representation (code)

zn=B⊤xn∈RMz_n = B^\top x_n \in \mathbb{R}^Mzn​=B⊤xn​∈RM

of xnx_nxn​, where we define the projection matrix

B:=[b1,…,bM]∈RD×MB := [b_1, \dots, b_M] \in \mathbb{R}^{D \times M}B:=[b1​,…,bM​]∈RD×M

We assume that the columns of BBB are orthonormal so that bi⊤bj=0b_i^\top b_j = 0bi⊤​bj​=0 if and only if i≠ji \neq ji=j and bi⊤bi=1b_i^\top b_i = 1bi⊤​bi​=1. We seek an MMM-dimensional subspace U⊆RD,dim⁡(U)=M<DU \subseteq \mathbb{R}^D, \dim(U) = M < DU⊆RD,dim(U)=M<D onto which we project the data. We denote the projected data by x~n∈U\tilde{x}_n \in Ux~n​∈U, and their coordinates (with respect to the basis vectors b1,…,bMb_1, \dots, b_Mb1​,…,bM​ of UUU) by znz_nzn​. Our aim is to find projections x~n∈RD\tilde{x}_n \in \mathbb{R}^Dx~n​∈RD (or equivalently the codes znz_nzn​ and the basis vectors b1,…,bMb_1, \dots, b_Mb1​,…,bM​) so that they are as similar to the original data xnx_nxn​ and minimize the loss due to compression.

Maximum Variance Perspective

If we interpret information content in the data as how "space filling" the dataset is, then we can describe the information contained in the data by looking at the spread of the data. We know that the variance is an indicator of the spread of the data, and we can derive PCA as a dimensionality reduction algorithm that maximizes the variance in the low-dimensional representation of the data to retain as much information as possible.

We maximize the variance of the low-dimensional code using a sequential approach. We start by seeking a single vector b1∈RDb_1 \in \mathbb{R}^Db1​∈RD that maximizes the variance of the projected data, i.e., we aim to maximize the variance of the first coordinate z1z_1z1​ of z∈RMz \in \mathbb{R}^Mz∈RM so that

V1:=V[z1]=1N∑n=1Nz1n2V_1 := \mathbb{V}[z_1] = \frac{1}{N} \sum_{n=1}^N z_{1n}^2V1​:=V[z1​]=N1​n=1∑N​z1n2​

is maximized, where we exploited the i.i.d. assumption of the data and defined z1nz_{1n}z1n​ as the first coordinate of the low-dimensional representation zn∈RMz_n \in \mathbb{R}^Mzn​∈RM of xn∈RDx_n \in \mathbb{R}^Dxn​∈RD. Note that first component of znz_nzn​ is given by

z1n=b1⊤xnz_{1n} = b_1^\top x_nz1n​=b1⊤​xn​

i.e., it is the coordinate of the orthogonal projection of xnx_nxn​ onto the one-dimensional subspace spanned by b1b_1b1​. We substitute this into the variance equation, which yields

V1=1N∑n=1N(b1⊤xn)2=b1⊤Sb1V_1 = \frac{1}{N} \sum_{n=1}^N (b_1^\top x_n)^2 = b_1^\top S b_1V1​=N1​n=1∑N​(b1⊤​xn​)2=b1⊤​Sb1​

where SSS is the data covariance matrix.

Notice that arbitrarily increasing the magnitude of the vector b1b_1b1​ increases V1V_1V1​. Therefore, we restrict all solutions to ∥b1∥2=1\|b_1\|^2 = 1∥b1​∥2=1, which results in a constrained optimization problem in which we seek the direction along which the data varies most.

With the restriction of the solution space to unit vectors the vector b1b_1b1​ that points in the direction of maximum variance can be found by the constrained optimization problem

max⁡b1b1⊤Sb1subject to∥b1∥2=1\max_{b_1} b_1^\top S b_1 \quad \text{subject to} \quad \|b_1\|^2 = 1b1​max​b1⊤​Sb1​subject to∥b1​∥2=1

Following the method of Lagrange multipliers, we obtain the Lagrangian

L(b1,λ1)=b1⊤Sb1+λ1(1−b1⊤b1)\mathcal{L}(b_1, \lambda_1) = b_1^\top S b_1 + \lambda_1 (1 - b_1^\top b_1)L(b1​,λ1​)=b1⊤​Sb1​+λ1​(1−b1⊤​b1​)

Setting the partial derivatives to 0 gives us the relations

Sb1=λ1b1S b_1 = \lambda_1 b_1Sb1​=λ1​b1​ b1⊤b1=1b_1^\top b_1 = 1b1⊤​b1​=1

By comparing this with the definition of an eigenvalue decomposition, we see that b1b_1b1​ is an eigenvector of the data covariance matrix SSS, and the Lagrange multiplier λ1\lambda_1λ1​ plays the role of the corresponding eigenvalue. This eigenvector property allows us to rewrite our variance objective as

V1=b1⊤Sb1=λ1b1⊤b1=λ1V_1 = b_1^\top S b_1 = \lambda_1 b_1^\top b_1 = \lambda_1V1​=b1⊤​Sb1​=λ1​b1⊤​b1​=λ1​

i.e., the variance of the data projected onto a one-dimensional subspace equals the eigenvalue that is associated with the basis vector b1b_1b1​ that spans this subspace. Therefore, to maximize the variance of the low-dimensional code, we choose the basis vector associated with the largest eigenvalue of the data covariance matrix. This eigenvector is called the first principal component.