Skip to main content

30  Principal Component Analysis (PCA)

An observation with \(p\) measured features is a point in \(\fR^p\). A collection of observations may vary strongly along a few directions and very little along the others. Principal component analysis chooses orthogonal directions that retain as much of this variation as possible.

We first subtract the mean observation so that variation is measured about the origin. We then express the variance along a direction as a Rayleigh quotient. The SVD identifies the directions and gives the error caused by retaining only some of them.

30.1 Definitions and Preprocessing

NoteDefinition: Data Centering

Data matrix \(\bX \in \fR^{n\times p}\) (\(n\geq2\) samples, \(p\) features) is first centered: \[ \begin{align} X_{ij} \leftarrow X_{ij} - \bar{x}_j, \quad \bar{x}_j = \frac{1}{n}\sum_{i=1}^n X_{ij}. \end{align} \] Throughout this section, \(\bX\) is assumed centered, and singular values beyond \(\min(n,p)\) are understood to be zero. A variance ratio is defined only when \(\|\bX\|_F>0\).

NoteDefinition: Sample Covariance Matrix

The matrix \(\bS = \frac{1}{n-1}\bX^T\bX \in \fR^{p\times p}\).

  • Diagonal: \(S_{jj}\) is the variance of feature \(j\).

  • Off-diagonal: \(S_{ij}\) is the covariance between features \(i\) and \(j\).

  • Property: \(\bS\) is symmetric positive semidefinite (SPSD).

WarningExercise
  1. Show that centering makes each column of \(\bX\) have mean zero.

  2. Prove that the diagonal entries of \(\bS\) are sample variances.

  3. Prove that \(\bS\) is symmetric positive semidefinite by computing \(\bz^T\bS\bz\).

30.2 Principal Directions and Scores

For a unit direction \(\bv\in\fR^p\), the projected observations are the entries of \(\bX\bv\). Their mean is zero because the columns of \(\bX\) are centered. Their sample variance is therefore \[ \begin{align} \frac{\|\bX\bv\|_2^2}{n-1}=\bv^T\bS\bv. \end{align} \] The first principal direction maximizes this variance. By the spectral theorem, it is an eigenvector for the largest eigenvalue of \(\bS\). Subsequent directions maximize variance subject to orthogonality to the preceding ones.

WarningExercise

Take centered observations \((2,0)\), \((-2,0)\), \((0,1)\), and \((0,-1)\) as rows of \(\bX\). Compute the variance along \(\bv=(\cos\theta,\sin\theta)^T\) and find its maximum. Identify the first and second principal directions. What changes if every second coordinate is multiplied by three?

NoteTheorem: Principal Directions and Scores

Let \(\bX = \bU\bsigma\bV^T\) be the SVD of the centered data matrix.

  • Principal Directions (Loadings): Columns of \(\bV\) (eigenvectors of \(\bS\)).

  • Principal Components (Scores): \(\bZ = \bX\bV = \bU\bsigma\).

  • Variances: \(\lambda_i = \text{Var}(\bz_i) = \frac{\sigma_i^2}{n-1}\).

Substituting the SVD \(\bX = \bU\bsigma\bV^T\) into the sample covariance matrix \(\bS\): \[ \begin{align} \bS &= \frac{1}{n-1} \bX^T\bX \\ &= \frac{1}{n-1} \left( \bV\bsigma^T\bU^T \right) \left( \bU\bsigma\bV^T \right) \\ &= \frac{1}{n-1} \bV \bsigma^T (\bU^T\bU) \bsigma \bV^T \\ &= \bV \left( \frac{\bsigma^T\bsigma}{n-1} \right) \bV^T. \end{align} \] Because \(\bV\) is orthogonal and \(\frac{\bsigma^T\bsigma}{n-1}\) is diagonal with non-negative entries \(\frac{\sigma_i^2}{n-1}\), this is the spectral decomposition of \(\bS\). The columns \(\bv_i\) are eigenvectors of \(\bS\) with eigenvalues \(\lambda_i = \sigma_i^2 / (n-1)\). The covariance eigenvalues are therefore squared singular values divided by \(n-1\). This division is positive because the sample-size assumption is \(n\geq2\). If a singular value is zero, the corresponding direction has zero measured sample variance.

The principal component score matrix is \[ \begin{align} \bZ = \bX\bV = \left( \bU\bsigma\bV^T \right) \bV = \bU\bsigma (\bV^T\bV) = \bU\bsigma. \end{align} \] The sample covariance matrix of the scores is \[ \begin{align} \bS_{\bZ} = \frac{1}{n-1} \bZ^T\bZ = \frac{1}{n-1} (\bsigma^T\bU^T)(\bU\bsigma) = \frac{\bsigma^T\bsigma}{n-1} = \operatorname{diag}(\lambda_1, ..., \lambda_p), \end{align} \] confirming that distinct score columns \(\bz_i, \bz_j\) have sample covariance zero for \(i \neq j\).

Because the centered columns of \(\bX\) have zero sums, \(\mathbf1^T\bZ=\mathbf1^T\bX\bV=\bzero^T\). Thus the score columns are centered too, making \(\bZ^T\bZ/(n-1)\) their sample covariance without an additional centering step. Its diagonal form proves that distinct score columns are uncorrelated. It does not imply statistical independence, which is a stronger claim. See Jolliffe (2002) for the statistical interpretation.

TipRemark

(PCA stability) Forming \(\bX^T\bX\) squares the 2-norm condition number: \(\kappa_2(\bX^T\bX)=\kappa_2(\bX)^2\) when \(\bX\) has full column rank. Compute the singular vectors directly from an SVD when small singular values or numerical rank matter.

WarningExercise

Show that centering is left multiplication by \(\bC=\bI-\mathbf1\mathbf1^T/n\). Prove that \(\bC^2=\bC=\bC^T\), deduce \(\operatorname{rank}(\bX)\leq n-1\), and explain why at most \(n-1\) principal components can have positive variance even when \(p>n\).

30.3 Variance and Dimensionality Reduction

NoteDefinition: Proportion of Variance Explained (PVE)

For the first \(k\) components: \[ \begin{align} \text{PVE}_k = \frac{\sum_{i=1}^k \sigma_i^2}{\sum_{i=1}^p \sigma_i^2}. \end{align} \]

NoteTheorem: Optimal Approximation

The rank-\(k\) matrix \[ \begin{align} \bX_k = \sum_{i=1}^k \sigma_i \bu_i \bv_i^T \end{align} \] is the optimal \(k\)-dimensional linear approximation of the data in the Frobenius norm, by the Eckart–Young theorem.

WarningExercise
  1. Use the referenced result to prove the referenced result.

  2. Show that \(\|\bX-\bX_k\|_F^2=\sum_{i>k}\sigma_i^2\).

  3. Show that \(\text{PVE}_k=1-\|\bX-\bX_k\|_F^2/\|\bX\|_F^2\).

  4. Explain why a PVE threshold is a modeling choice, not a theorem.

30.4 Implementation Details

TipRemark

(Standardization) If features have different units, PCA will be dominated by large-scale features. When equal feature weighting is intended, standardize nonconstant columns by their standard deviations: \(X_{ij} \leftarrow (X_{ij} - \bar{x}_j)/s_j\).

TipRemark

(Interpretation) Principal components are directions of large variance. A large loading identifies a direction of variation in the data matrix. It does not by itself explain why that variation occurs.

NoteDefinition: Dual PCA for \(p \gg n\)

When the number of features greatly exceeds the number of samples, we may diagonalize the \(n\times n\) Gram matrix \(\mathbf{K}=\bX\bX^T\) instead of the \(p\times p\) covariance matrix. For each nonzero singular value, the feature-space principal direction is recovered from \(\bv_i=\bX^T\bu_i/\sigma_i\). Kernel PCA replaces this linear Gram matrix by a centered nonlinear kernel matrix.

30.5 Further Exercises

WarningExercise

Center \(\bX = \begin{pmatrix} 2 & 1 \\ -1 & 3 \\ -1 & -4 \end{pmatrix}\) and compute \(\bS\). Find the PVE for \(k=1\).

WarningExercise

If two largest singular values are equal, show that any orthonormal basis of their right singular subspace gives the same variance and reconstruction error. Explain which PCA subspace is determined and which individual directions are not uniquely determined.

WarningExercise

Load the Iris dataset. Plot the data in the basis of the first two PCs. Do species cluster?

WarningExercise

Verify that the sum of variances \(\sum \lambda_i\) equals the total variance \(\text{Tr}(\bS)\).

NoteFurther reading

Jolliffe is a standard statistical reference for principal component analysis (Jolliffe 2002). The Golub–Kahan paper gives the bidiagonalization behind modern large-scale SVD calculations (Golub and Kahan 1965).