# Principal Component Analysis

The principal components of a collection of points in a real coordinate space are a sequence of $p$ unit vectors, where the $i$-th vector is the direction of a line that best fits the data while being orthogonal to the first $i-1$ vectors. Here, a best-fitting line is defined as one that minimizes the average squared distance from the points to the line. These directions constitute an orthonormal basis in which different individual dimensions of the data are linearly uncorrelated. Principal component analysis (PCA) is the process of computing the principal components and using them to perform a change of basis on the data, sometimes using only the first few principal components and ignoring the rest.

In data analysis, the first principal component of a set of $p$ variables, presumed to be jointly normally distributed, is the derived variable formed as a linear combination of the original variables that explains the most variance. The second principal component explains the most variance in what is left once the effect of the first component is removed, and we may proceed through $p$ iterations until all the variance is explained. PCA is most commonly used when many of the variables are highly correlated with each other and it is desirable to reduce their number to an independent set.

PCA is used in exploratory data analysis and for making predictive models. It is commonly used for dimensionality reduction by projecting each data point onto only the first few principal components to obtain lower-dimensional data while preserving as much of the data's variation as possible. The first principal component can equivalently be defined as a direction that maximizes the variance of the projected data. The $i$-th principal component can be taken as a direction orthogonal to the first $i-1$ principal components that maximizes the variance of the projected data.

For either objective, it can be shown that the principal components are eigenvectors of the data's covariance matrix. Thus, the principal components are often computed by eigendecomposition of the data covariance matrix or singular value decomposition of the data matrix. PCA is the simplest of the true eigenvector-based multivariate analyses and is closely related to factor analysis.

## Intuition

PCA can be thought of as fitting a $p$-dimensional ellipsoid to the data, where each axis of the ellipsoid represents a principal component. If some axis of the ellipsoid is small, then the variance along that axis is also small.

To find the axes of the ellipsoid, we must first center the values of each variable in the dataset on 0 by subtracting the mean of the variable's observed values from each of those values. These transformed values are used instead of the original observed values for each of the variables. Then, we compute the covariance matrix of the data and calculate the eigenvalues and corresponding eigenvectors of this covariance matrix. Then we must normalize each of the orthogonal eigenvectors to turn them into unit vectors. Once this is done, each of the mutually-orthogonal unit eigenvectors can be interpreted as an axis of the ellipsoid fitted to the data. This choice of basis will transform the covariance matrix into a diagonalized form, in which the diagonal elements represent the variance of each axis. The proportion of the variance that each eigenvector represents can be calculated by dividing the eigenvalue corresponding to that eigenvector by the sum of all eigenvalues.

## Details

PCA is defined as an orthogonal linear transformation that transforms the data to a new coordinate system such that the greatest variance by some scalar projection of the data comes to lie on the first coordinate (called the first principal component), the second greatest variance on the second coordinate, and so on.[1]

Consider an $n \times p$ data matrix, $\mathbf{X}$, with column-wise zero empirical mean (the sample mean of each column has been shifted to zero), where each of the $n$ rows represents a different repetition of the experiment, and each of the $p$ columns gives a particular kind of feature (say, the results from a particular sensor).

Mathematically, the transformation is defined by a set of size $l$ of $p$-dimensional vectors of weights or coefficients $\mathbf{w}_{(k)} = (w_1, \dots, w_p)_{(k)}$ that map each row vector $\mathbf{x}_{(i)}$ of $\mathbf{X}$ to a new vector of principal component scores $\mathbf{t}_{(i)} = (t_1, \dots, t_l)_{(i)}$, given by

[$]{t_{k}}_{(i)} = \mathbf{x}_{(i)} \cdot \mathbf{w}_{(k)} \qquad \mathrm{for} \qquad i = 1,\dots,n \qquad k = 1,\dots,l [$]

in such a way that the individual variables $\mathbf{t}_1, \dots, \mathbf{t}_l$ of $\mathbf{t}$ considered over the data set successively inherit the maximum possible variance from $\mathbf{X}$, with each coefficient vector $\mathbf{w}$ constrained to be a unit vector (where $l$ is usually selected to be strictly less than $p$ to reduce dimensionality).

### First component

In order to maximize variance, the first weight vector w(1) thus has to satisfy

[$]\mathbf{w}_{(1)} = \arg\max_{\Vert \mathbf{w} \Vert = 1} \,\left\{ \sum_i (t_1)^2_{(i)} \right\} = \arg\max_{\Vert \mathbf{w} \Vert = 1} \,\left\{ \sum_i \left(\mathbf{x}_{(i)} \cdot \mathbf{w} \right)^2 \right\}[$]

Equivalently, writing this in matrix form gives

[$]\mathbf{w}_{(1)} = \arg\max_{\left\| \mathbf{w} \right\| = 1} \left\{ \left\| \mathbf{Xw} \right\|^2 \right\} = \arg\max_{\left\| \mathbf{w} \right\| = 1} \left\{ \mathbf{w}^\mathsf{T} \mathbf{X}^\mathsf{T} \mathbf{X w} \right\}[$]

Since $\mathbf{w}_{(1)}$ has been defined to be a unit vector, it equivalently also satisfies

[$]\mathbf{w}_{(1)} = \arg\max \left\{ \frac{\mathbf{w}^\mathsf{T} \mathbf{X}^\mathsf{T} \mathbf{X w}}{\mathbf{w}^\mathsf{T} \mathbf{w}} \right\}[$]

The quantity to be maximised can be recognised as a Rayleigh quotient. A standard result for a positive semidefinite matrix such as $\mathbf{X}^\mathsf{T}\mathbf{X}$ is that the quotient's maximum possible value is the largest eigenvalue of the matrix, which occurs when $\mathbf{w}$ is the corresponding eigenvector.

With $\mathbf{w}_{(1)}$ found, the first principal component of a data vector $\mathbf{x}_i$ can then be given as a score $t_{(i)}=\mathbf{x}_{(i)}$$w_{(1)}$ in the transformed co-ordinates, or as the corresponding vector in the original variables, {$x_{(i)} \cdot w_{(1)}$} $w_{(1)}$.

### Further components

The $k$-th component can be found by subtracting the first $k-1$ principal components from $\mathbf{X}$:

[$]\mathbf{\hat{X}}_k = \mathbf{X} - \sum_{s = 1}^{k - 1} \mathbf{X} \mathbf{w}_{(s)} \mathbf{w}_{(s)}^{\mathsf{T}} [$]

and then finding the weight vector which extracts the maximum variance from this new data matrix

[$]\mathbf{w}_{(k)} = \mathop{\operatorname{arg\,max}}_{\left\| \mathbf{w} \right\| = 1} \left\{ \left\| \mathbf{\hat{X}}_{k} \mathbf{w} \right\|^2 \right\} = \arg\max \left\{ \tfrac{\mathbf{w}^\mathsf{T} \mathbf{\hat{X}}_{k}^\mathsf{T} \mathbf{\hat{X}}_{k} \mathbf{w}}{\mathbf{w}^{\mathsf{T}} \mathbf{w}} \right\}[$]

It turns out that this gives the remaining eigenvectors of $\mathbf{X}^\mathsf{T}\mathbf{X}$, with the maximum values for the quantity in brackets given by their corresponding eigenvalues. Thus the weight vectors are eigenvectors of $\mathbf{X}^\mathsf{T}\mathbf{X}$.

The $k$-th principal component of a data vector $\mathbf{x}_{(i)}$ can therefore be given as a score $t_{k(i)} = \mathbf{x}_{(i)}\cdot \mathbf{w}_{(k)}$ in the transformed coordinates, or as the corresponding vector in the space of the original variables, {x(i)w(k)} w(k), where $\mathbf{w}_{(k)}$ is the $k$th eigenvector of $\mathbf{X}^\mathsf{T}\mathbf{X}$. The full principal components decomposition of $\mathbf{X}$ can therefore be given as

[$]\mathbf{T} = \mathbf{X} \mathbf{W}[$]

where $\mathbf{W}$ is a $p$-by-$p$ matrix of weights whose columns are the eigenvectors of $\mathbf{X}^\mathsf{T}\mathbf{X}$. The transpose of $\mathbf{W}$ is sometimes called the whitening or sphering transformation. Columns of $\mathbf{W}$ multiplied by the square root of corresponding eigenvalues, that is, eigenvectors scaled up by the variances, are called loadings in PCA or in Factor analysis.

### Covariances

$\mathbf{X}^\mathsf{T}\mathbf{X}$ itself can be recognized as proportional to the empirical sample covariance matrix of the dataset $\mathbf{X}^{\mathsf{T}}$.[1]:30–31

The sample covariance $Q$ between two of the different principal components over the dataset is given by:

[]\begin{align*} Q(\mathrm{PC}_{(j)}, \mathrm{PC}_{(k)}) & \propto (\mathbf{X}\mathbf{w}_{(j)})^\mathsf{T} (\mathbf{X}\mathbf{w}_{(k)}) \\ & = \mathbf{w}_{(j)}^\mathsf{T} \mathbf{X}^\mathsf{T} \mathbf{X} \mathbf{w}_{(k)} \\ & = \mathbf{w}_{(j)}^\mathsf{T} \lambda_{(k)} \mathbf{w}_{(k)} \\ & = \lambda_{(k)} \mathbf{w}_{(j)}^\mathsf{T} \mathbf{w}_{(k)} \end{align*}[]

where the eigenvalue property of $\mathbf{w}_k$ has been used to move from line 2 to line 3. However eigenvectors $\mathbf{w}_j$ and $\mathbf{w}_k$ corresponding to eigenvalues of a symmetric matrix are orthogonal (if the eigenvalues are different), or can be orthogonalised (if the vectors happen to share an equal repeated value). The product in the final line is therefore zero; there is no sample covariance between different principal components over the dataset.

Another way to characterise the principal components transformation is therefore as the transformation to coordinates which diagonalise the empirical sample covariance matrix.

In matrix form, the empirical covariance matrix for the original variables can be written

[$]\mathbf{Q} \propto \mathbf{X}^\mathsf{T} \mathbf{X} = \mathbf{W} \mathbf{\Lambda} \mathbf{W}^\mathsf{T}[$]

The empirical covariance matrix between the principal components becomes

[$]\mathbf{W}^\mathsf{T} \mathbf{Q} \mathbf{W} \propto \mathbf{W}^\mathsf{T} \mathbf{W} \, \mathbf{\Lambda} \, \mathbf{W}^\mathsf{T} \mathbf{W} = \mathbf{\Lambda} [$]

where $\mathbf{\Lambda}$ is the diagonal matrix of eigenvalues $\lambda_{(k)}$ of $\mathbf{X}^\mathsf{T}\mathbf{X}$. $\lambda_{(k)}$ is equal to the sum of the squares over the dataset associated with each component $k$, that is,

[$]\lambda_{(k)} = \sum_i t_{k(i)}^2 = \sum_i (\mathbf{x}_i \cdot \mathbf{w}_k)^2.[$]

### Dimensionality reduction

The transformation $\mathbf{T}=\mathbf{X}\mathbf{W}$ maps a data vector $\mathbf{x}_i$ from an original space of $p$ variables to a new space of $p$ variables which are uncorrelated over the dataset. However, not all the principal components need to be kept. Keeping only the first $L$ principal components, produced by using only the first $L$ eigenvectors, gives the truncated transformation

[$]\mathbf{T}_L = \mathbf{X} \mathbf{W}_L[$]

where the matrix $\mathbf{T}_L$now has $n$ rows but only $L$ columns. In other words, PCA learns a linear transformation $t = W_L^\mathsf{T} x, x \in \mathbb{R}^p, t \in \mathbb{R}^L,$ where the columns of $p \times L$ matrix $W_L$ form an orthogonal basis for the $L$ features (the components of representation t) that are decorrelated.[2] By construction, of all the transformed data matrices with only $L$ columns, this score matrix maximises the variance in the original data that has been preserved, while minimising the total squared reconstruction error $\|\mathbf{T}\mathbf{W}^{\mathsf{T}} - \mathbf{T}_L\mathbf{W}^{\mathsf{T}}_L\|_2^2$ or $\|\mathbf{X} - \mathbf{X}_L\|_2^2$.

Such dimensionality reduction can be a very useful step for visualising and processing high-dimensional datasets, while still retaining as much of the variance in the dataset as possible. For example, selecting $L=2$ and keeping only the first two principal components finds the two-dimensional plane through the high-dimensional dataset in which the data is most spread out, so if the data contains clusters these too may be most spread out, and therefore most visible to be plotted out in a two-dimensional diagram; whereas if two directions through the data (or two of the original variables) are chosen at random, the clusters may be much less spread apart from each other, and may in fact be much more likely to substantially overlay each other, making them indistinguishable.

Similarly, in regression analysis, the larger the number of explanatory variables allowed, the greater is the chance of overfitting the model, producing conclusions that fail to generalise to other datasets. One approach, especially when there are strong correlations between different possible explanatory variables, is to reduce them to a few principal components and then run the regression against them, a method called principal component regression.

Dimensionality reduction may also be appropriate when the variables in a dataset are noisy. If each column of the dataset contains independent identically distributed Gaussian noise, then the columns of $\mathbf{T}$ will also contain similarly identically distributed Gaussian noise (such a distribution is invariant under the effects of the matrix $\mathbf{W}$, which can be thought of as a high-dimensional rotation of the co-ordinate axes). However, with more of the total variance concentrated in the first few principal components compared to the same noise variance, the proportionate effect of the noise is less—the first few components achieve a higher signal-to-noise ratio. PCA thus can have the effect of concentrating much of the signal into the first few principal components, which can usefully be captured by dimensionality reduction; while the later principal components may be dominated by noise, and so disposed of without great loss. If the dataset is not too large, the significance of the principal components can be tested using parametric bootstrap, as an aid in determining how many principal components to retain.[3]

### Singular value decomposition

The principal components transformation can also be associated with another matrix factorization, the singular value decomposition (SVD) of $\mathbf{X}$,

[$]\mathbf{X} = \mathbf{U}\mathbf{\Sigma}\mathbf{W}^{\mathsf{T}}[$]

Here Σ is an $n$-by-p rectangular diagonal matrix of positive numbers $\sigma_{(k)}$, called the singular values of $\mathbf{X}$; $\mathbf{U}$ is an $n$-by-$n$ matrix, the columns of which are orthogonal unit vectors of length $n$ called the left singular vectors of $\mathbf{X}$; and $\mathbf{W}$ is a $p$-by-$p$ whose columns are orthogonal unit vectors of length $p$ and called the right singular vectors of $\mathbf{X}$.

In terms of this factorization, the matrix $\mathbf{X}^\mathsf{T}\mathbf{X}$ can be written

[]\begin{align*} \mathbf{X}^{\mathsf{T}}\mathbf{X} & = \mathbf{W}\mathbf{\Sigma}^\mathsf{T} \mathbf{U}^\mathsf{T} \mathbf{U}\mathbf{\Sigma}\mathbf{W}^\mathsf{T} \\ & = \mathbf{W}\mathbf{\Sigma}^\mathsf{T} \mathbf{\Sigma} \mathbf{W}^\mathsf{T} \\ & = \mathbf{W}\mathbf{\hat{\Sigma}}^2 \mathbf{W}^\mathsf{T} \end{align*}[]

where $\mathbf{\hat{\Sigma}}$ is the square diagonal matrix with the singular values of $\mathbf{X}$ and the excess zeros chopped off that satisfies $\mathbf{\hat{\Sigma}^2}=\mathbf{\Sigma}^\mathsf{T} \mathbf{\Sigma}$. Comparison with the eigenvector factorization of $\mathbf{X}^\mathsf{T}\mathbf{X}$ establishes that the right singular vectors $\mathbf{W}$ of $\mathbf{X}$ are equivalent to the eigenvectors of $\mathbf{X}^\mathsf{T}\mathbf{X}$, while the singular values $\sigma_{(k)}$ of $\mathbf{{X}}$ are equal to the square-root of the eigenvalues $\lambda_{(k)}$ of $\mathbf{X}^\mathsf{T}\mathbf{X}$.

Using the singular value decomposition the score matrix $\mathbf{T}$ can be written

[]\begin{align*} \mathbf{T} & = \mathbf{X} \mathbf{W} \\ & = \mathbf{U}\mathbf{\Sigma}\mathbf{W}^\mathsf{T} \mathbf{W} \\ & = \mathbf{U}\mathbf{\Sigma} \end{align*}[]

so each column of $\mathbf{T}$ is given by one of the left singular vectors of $\mathbf{X}$ multiplied by the corresponding singular value. This form is also the polar decomposition of $\mathbf{T}$.

Efficient algorithms exist to calculate the SVD of $\mathbf{X}$ without having to form the matrix $\mathbf{X}^\mathsf{T}\mathbf{X}$, so computing the SVD is now the standard way to calculate a principal components analysis from a data matrix, unless only a handful of components are required.

As with the eigen-decomposition, a truncated $n \times L$ score matrix $\mathbf{T}_L$ can be obtained by considering only the first $L$ largest singular values and their singular vectors:

[$]\mathbf{T}_L = \mathbf{U}_L\mathbf{\Sigma}_L = \mathbf{X} \mathbf{W}_L [$]

The truncation of a matrix $\mathbf{M}$ or $\mathbf{T}$ using a truncated singular value decomposition in this way produces a truncated matrix that is the nearest possible matrix of rank $L$ to the original matrix, in the sense of the difference between the two having the smallest possible Frobenius norm, a result known as the Eckart–Young theorem [1936].

## Further considerations

The singular values (in Σ) are the square roots of the eigenvalues of the matrix $\mathbf{X}^\mathsf{T}\mathbf{X}$. Each eigenvalue is proportional to the portion of the "variance" (more correctly of the sum of the squared distances of the points from their multidimensional mean) that is associated with each eigenvector. The sum of all the eigenvalues is equal to the sum of the squared distances of the points from their multidimensional mean. PCA essentially rotates the set of points around their mean in order to align with the principal components. This moves as much of the variance as possible (using an orthogonal transformation) into the first few dimensions. The values in the remaining dimensions, therefore, tend to be small and may be dropped with minimal loss of information (see below). PCA is often used in this manner for dimensionality reduction. PCA has the distinction of being the optimal orthogonal transformation for keeping the subspace that has largest "variance" (as defined above). This advantage, however, comes at the price of greater computational requirements if compared, for example, and when applicable, to the discrete cosine transform, and in particular to the DCT-II which is simply known as the "DCT". Nonlinear dimensionality reduction techniques tend to be more computationally demanding than PCA.

PCA is sensitive to the scaling of the variables. If we have just two variables and they have the same sample variance and are completely correlated, then the PCA will entail a rotation by 45° and the "weights" (they are the cosines of rotation) for the two variables with respect to the principal component will be equal. But if we multiply all values of the first variable by 100, then the first principal component will be almost the same as that variable, with a small contribution from the other variable, whereas the second component will be almost aligned with the second original variable. This means that whenever the different variables have different units (like temperature and mass), PCA is a somewhat arbitrary method of analysis. (Different results would be obtained if one used Fahrenheit rather than Celsius for example.) Pearson's original paper was entitled "On Lines and Planes of Closest Fit to Systems of Points in Space" – "in space" implies physical Euclidean space where such concerns do not arise. One way of making the PCA less arbitrary is to use variables scaled so as to have unit variance, by standardizing the data and hence use the autocorrelation matrix instead of the autocovariance matrix as a basis for PCA. However, this compresses (or expands) the fluctuations in all dimensions of the signal space to unit variance.

Mean subtraction (a.k.a. "mean centering") is necessary for performing classical PCA to ensure that the first principal component describes the direction of maximum variance. If mean subtraction is not performed, the first principal component might instead correspond more or less to the mean of the data. A mean of zero is needed for finding a basis that minimizes the mean square error of the approximation of the data.[4]

Mean-centering is unnecessary if performing a principal components analysis on a correlation matrix, as the data are already centered after calculating correlations. Correlations are derived from the cross-product of two standard scores (Z-scores) or statistical moments (hence the name: Pearson Product-Moment Correlation). Also see the article by Kromrey & Foster-Johnson (1998) on "Mean-centering in Moderated Regression: Much Ado About Nothing". Since covariances are correlations of normalized variables (Z- or standard-scores) a PCA based on the correlation matrix of $\mathbf{X}$ is equal to a PCA based on the covariance matrix of $\mathbf{Z}$, the standardized version of $\mathbf{X}$.

## Table of symbols and abbreviations

Symbol Meaning Dimensions Indices
$\mathbf{X} = \{ X_{ij} \}$ data matrix, consisting of the set of all data vectors, one vector per row $n \times p$ $i = 1 \ldots n$
$j = 1 \ldots p$
$n$ the number of row vectors in the data set $1 \times 1$ scalar
$p$ the number of elements in each row vector (dimension) $1 \times 1$ scalar
$L$ the number of dimensions in the dimensionally reduced subspace, $1 \le L \le p$ $1 \times 1$ scalar
$\mathbf{u} = \{ u_j \}$ vector of empirical means, one mean for each column j of the data matrix $p \times 1$ $j = 1 \ldots p$
$\mathbf{s} = \{ s_j \}$ vector of empirical standard deviations, one standard deviation for each column j of the data matrix $p \times 1$ $j = 1 \ldots p$
$\mathbf{h} = \{ h_i \}$ vector of all 1's $1 \times n$ $i = 1 \ldots n$
$\mathbf{B} = \{ B_{ij} \}$ deviations from the mean of each column j of the data matrix $n \times p$ $i = 1 \ldots n$
$j = 1 \ldots p$
$\mathbf{Z} = \{ Z_{ij} \}$ z-scores, computed using the mean and standard deviation for each row m of the data matrix $n \times p$ $i = 1 \ldots n$
$j = 1 \ldots p$
$\mathbf{C} = \{ C_{jj'} \}$ covariance matrix $p \times p$ $j = 1 \ldots p$
$j' = 1 \ldots p$
$\mathbf{R} = \{ R_{jj'} \}$ correlation matrix $p \times p$ $j = 1 \ldots p$
$j' = 1 \ldots p$
$\mathbf{V} = \{ V_{jj'} \}$ matrix consisting of the set of all eigenvectors of C, one eigenvector per column $p \times p$ $j = 1 \ldots p$
$j' = 1 \ldots p$
$\mathbf{D} = \{ D_{jj'} \}$ diagonal matrix consisting of the set of all eigenvalues of C along its principal diagonal, and 0 for all other elements ( note $\mathbf{\Lambda}$ used above ) $p \times p$ $j = 1 \ldots p$
$j' = 1 \ldots p$
$\mathbf{W} = \{ W_{jl} \}$ matrix of basis vectors, one vector per column, where each basis vector is one of the eigenvectors of C, and where the vectors in $\mathbf{W}$ are a sub-set of those in V $p \times L$ $j = 1 \ldots p$
$l = 1 \ldots L$
$\mathbf{T} = \{ T_{il} \}$ matrix consisting of $n$ row vectors, where each vector is the projection of the corresponding data vector from matrix $\mathbf{X}$ onto the basis vectors contained in the columns of matrix $\mathbf{W}$. $n \times L$ $i = 1 \ldots n$
$l = 1 \ldots L$

## References

1. Jolliffe, I. T. (2002). Principal Component Analysis. Springer Series in Statistics (in English). New York: Springer-Verlag. doi:10.1007/b98835. ISBN 978-0-387-95442-4.
2. Bengio, Y. (2013). "Representation Learning: A Review and New Perspectives". IEEE Transactions on Pattern Analysis and Machine Intelligence 35 (8): 1798–1828. doi:10.1109/TPAMI.2013.50. PMID 23787338.
3. Forkman J., Josse, J., Piepho, H. P. (2019). "Hypothesis tests for principal component analysis when variables are standardized". Journal of Agricultural, Biological, and Environmental Statistics 24 (2): 289–308. doi:10.1007/s13253-019-00355-5.
4. A. A. Miranda, Y. A. Le Borgne, and G. Bontempi. New Routes from Minimal Approximation Error to Principal Components, Volume 27, Number 3 / June, 2008, Neural Processing Letters, Springer