# Intuition

Imagine, that you have a dataset of points. Your goal is to choose orthogonal axes, that describe your data the most informative way. To be precise, we choose first axis in such a way, that maximize the variance (expressiveness) of the projected data. All the following axes have to be orthogonal to the previously chosen ones, while satisfy largest possible variance of the projections.

Let’s take a look at the simple 2d data. We have a set of blue points on the plane. We can easily see that the projections on the first axis (red dots) have maximum variance at the final position of the animation. The second (and the last) axis should be orthogonal to the previous one. source

This idea could be used in a variety ways. For example, it might happen, that projection of complex data on the principal plane (only 2 components) bring you enough intuition for clustering. The picture below plots projection of the labeled dataset onto the first to principal components (PC’s), we can clearly see, that only two vectors (these PC’s) would be enogh to differ Finnish people from Italian in particular dataset (celiac disease (Dubois et al. 2010)) source

# Problem

The first component should be defined in order to maximize variance. Suppose, we’ve already normalized the data, i.e. $\sum\limits_i a_i = 0$, then sample variance will become the sum of all squared projections of data points to our vector ${\mathbf{w}}_{(1)}$, which implies the following optimization problem:

or

since we are looking for the unit vector, we can reformulate the problem:

It is known, that for positive semidefinite matrix $A^\top A$ such vector is nothing else, but eigenvector of $A^\top A$, which corresponds to the largest eigenvalue. The following components will give you the same results (eigenvectors).

So, we can conclude, that the following mapping:

describes the projection of data onto the $k$ principal components, where $W$ contains first (by the size of eigenvalues) $k$ eigenvectors of $A^\top A$.

Now we’ll briefly derive how SVD decomposition could lead us to the PCA.

Firstly, we write down SVD decomposition of our matrix:

and to its transpose:

Then, consider matrix $A A^\top$:

Which corresponds to the eigendecomposition of matrix $A^\top A$, where $W$ stands for the matrix of eigenvectors of $A^\top A$, while $\Sigma^2$ contains eigenvalues of $A^\top A$.

At the end:

The latter formula provide us with easy way to compute PCA via SVD with any number of principal components:

# Examples

## 🌼 Iris dataset

Consider the classical Iris dataset source We have the dataset matrix $A \in \mathbb{R}^{150 \times 4}$