Tutorials › High-Dimensional Genomic Data Analysis › The Singular Value Decomposition Derivation of PCA

Dimension Reduction and Clustering · Tutorial 599 of 795

The Singular Value Decomposition Derivation of PCA

Learn how the SVD yields principal component directions and scores, and how its singular values determine the eigenvalues of the sample covariance matrix.

Intermediate 12 min read

What You'll Learn

  • Set up the centered data matrix and state the conditions for ordinary PCA.
  • Derive principal directions by maximizing sample variance over unit vectors.
  • Obtain directions and scores from the SVD, including rectangular-matrix dimensions.
  • Relate squared singular values to sample-covariance eigenvalues.
  • Recognize zero scores, tied directions, sign ambiguity, and common numerical checks.

From a Centered Data Matrix to Principal Components

Principal component analysis (PCA) replaces a set of possibly correlated measurements with orthogonal directions through the data. The algebraic link between PCA and the singular value decomposition (SVD) makes this transformation concrete: the right singular vectors of the data matrix give the principal directions, while multiplying the data by those directions gives the component scores. The singular values also quantify the sample variation along those directions.

This derivation assumes a complete numerical data matrix whose columns have been centered, so each feature has sample mean zero. The earlier material on centering and scaling explains how choices such as standardizing columns affect the analysis. Here, take the analyzed columns as given and derive PCA for ordinary, equally weighted observations using Euclidean distance. The derivation is algebraic; it does not require normally distributed measurements.

Definition: Let \(X\) be an \(n\times p\) matrix of centered measurements, with observations in rows and features in columns. Its sample covariance matrix is \(S=X^TX/(n-1)\), for \(n>1\). A principal component direction is a unit vector \(v\in\mathbb{R}^p\) that maximizes the sample variance of the projected observations, subject to being orthogonal to any directions already selected. The vector \(Xv\) contains the component scores for all \(n\) observations.

For a proposed unit direction \(v\), the projected observations are the entries of \(Xv\). Since the columns of \(X\) are centered, these scores have mean zero. Their sample variance is

$$ \frac{(Xv)^T(Xv)}{n-1} =\frac{v^TX^TXv}{n-1} =v^TSv. $$

Thus the first principal direction maximizes \(v^TSv\) over all \(v\) with \(v^Tv=1\). Subsequent directions maximize the same quantity subject to orthogonality to the previously chosen directions.

Why the Eigenvectors Give the Directions

The covariance matrix \(S\) is symmetric and positive semidefinite: for every vector \(v\), \(v^TSv=\|Xv\|^2/(n-1)\geq 0\). Its eigenvalues are therefore real and nonnegative. Write its eigenvalues in descending order as \(\lambda_1\geq\lambda_2\geq\cdots\geq\lambda_p\geq0\), with corresponding orthonormal eigenvectors \(v_1,\ldots,v_p\).

The maximizing property follows directly by expressing any unit vector in that eigenvector basis. If \(v=\sum_{j=1}^p a_jv_j\), then \(\sum_j a_j^2=1\), and

$$ v^TSv=\sum_{j=1}^p\lambda_j a_j^2 \leq \lambda_1\sum_{j=1}^p a_j^2 =\lambda_1. $$

Equality is attained by \(v=v_1\). After restricting \(v\) to be orthogonal to \(v_1\), its coefficient \(a_1\) is zero, so the same argument shows that the maximum is \(\lambda_2\), attained by \(v_2\). Repeating this reasoning establishes the successive directions. This is a complete variance-maximization justification, not an assumption about the data distribution.

Result: The principal directions are orthonormal eigenvectors of \(S\), ordered by descending eigenvalue. The sample variance of scores in direction \(v_j\) is \(v_j^TSv_j=\lambda_j\). If eigenvalues are tied, the maximizing direction within the tied eigenspace is not unique; any orthonormal basis of that eigenspace is valid.

Deriving PCA from the SVD

Take a full SVD of the centered matrix, with dimensions shown explicitly:

$$ X=U\Sigma V^T,\qquad U\in\mathbb{R}^{n\times n},\quad V\in\mathbb{R}^{p\times p},\quad \Sigma\in\mathbb{R}^{n\times p}. $$

The columns of \(U\) and \(V\) are orthonormal. The diagonal entries of the rectangular matrix \(\Sigma\) are the singular values \(\sigma_1\geq\cdots\geq\sigma_m\geq0\), where \(m=\min(n,p)\); entries outside this diagonal are zero. Multiplying out the covariance matrix gives

$$ S=\frac{X^TX}{n-1} =\frac{V\Sigma^TU^TU\Sigma V^T}{n-1} =V\left(\frac{\Sigma^T\Sigma}{n-1}\right)V^T. $$

Because \(U^TU=I\), the matrix inside this expression is diagonal: its first \(m\) diagonal entries are \(\sigma_j^2/(n-1)\), and any additional entries when \(p>n\) are zero. Consequently, the right singular vectors are eigenvectors of \(S\), and the corresponding eigenvalues are

$$ \lambda_j=\frac{\sigma_j^2}{n-1}\quad (1\leq j\leq m). $$

The score matrix, with component columns in the same order as the columns of \(V\), is \(XV=U\Sigma\). For \(j\leq m\), its \(j\)th column satisfies \(Xv_j=\sigma_j u_j\). If \(p>n\), \(V\) has additional columns \(v_j\) for \(j>n\); these vectors lie in the null space of \(X\), so \(Xv_j=0\). They have no corresponding columns in \(U\). A zero singular value among the first \(m\) also gives a zero score vector. These distinctions matter whenever the matrix is rectangular or rank deficient.

Worked Example: Directions, Scores, and Covariance Eigenvalues

Suppose a hypothetical centered data matrix for four observations and two features is

$$ X= \begin{pmatrix} 1&0.5\\ -1&-0.5\\ 1&-0.5\\ -1&0.5 \end{pmatrix}. $$

The first column has squared length \(4\), the second has squared length \(1\), and their inner product is \(0.5+0.5-0.5-0.5=0\). Thus

$$ X^TX= \begin{pmatrix}4&0\\0&1\end{pmatrix}, \qquad S=\frac{X^TX}{3} =\begin{pmatrix}4/3&0\\0&1/3\end{pmatrix}. $$

The right singular vectors, and hence the principal directions, are \(v_1=(1,0)^T\) and \(v_2=(0,1)^T\). The singular values are \(2\) and \(1\), so the covariance eigenvalues are \(2^2/3=4/3\) and \(1^2/3=1/3\). The scores are

$$ Xv_1=(1,-1,1,-1)^T,\qquad Xv_2=(0.5,-0.5,-0.5,0.5)^T. $$

Their sample variances are respectively \(4/3\) and \(1/3\), matching the eigenvalues. In this hypothetical example, the first feature direction has greater sample variance. The result describes these centered measurements; it does not by itself establish that either feature causes the other.

Worked Example: More Features Than Observations

Consider the hypothetical centered matrix with two observations and three features:

$$ X= \begin{pmatrix} 1&2&0\\ -1&-2&0 \end{pmatrix}. $$

Its rank is one. A unit right singular vector for its nonzero singular value is \(v_1=(1,2,0)^T/\sqrt{5}\), and a corresponding left singular vector is \(u_1=(1,-1)^T/\sqrt{2}\). Direct multiplication gives \(Xv_1=(\sqrt{5},-\sqrt{5})^T\), so the singular value is \(\sqrt{10}\), since \(\sqrt{10}u_1=(\sqrt{5},-\sqrt{5})^T\).

Two orthonormal right singular vectors in the null space are \(v_2=(-2,1,0)^T/\sqrt{5}\) and \(v_3=(0,0,1)^T\). Indeed, multiplication gives \(Xv_2=Xv_3=0\). The full SVD has \(U\) with two columns, \(V\) with three columns, and a \(2\times3\) matrix \(\Sigma\) whose diagonal entries are \(\sqrt{10}\) and \(0\). The third right singular vector has no third left singular vector: it gives a zero score, not an additional column of \(U\).

Here \(n-1=1\), and the covariance eigenvalues are \(10,0,0\). This agrees with \(X^TX\), which has rank one. The example illustrates why when features outnumber observations, many directions can have zero variance in the observed data.

Worked Example: Checking a Direction by Variance Maximization

For a third hypothetical centered four-observation data set, let the feature columns be \(x=(\sqrt{3},-\sqrt{3},0,0)^T\) and \(y=(\sqrt{3},0,-\sqrt{3},0)^T\). Each column sums to zero. Their squared lengths are \(6\), and their inner product is \(3\); hence the sample covariance matrix is

$$ S=\frac{1}{3} \begin{pmatrix}6&3\\3&6\end{pmatrix} =\begin{pmatrix}2&1\\1&2\end{pmatrix}. $$

The unit direction \(v_1=(1,1)^T/\sqrt{2}\) has score variance \(v_1^TSv_1=3\). The orthogonal unit direction \(v_2=(1,-1)^T/\sqrt{2}\) has score variance \(v_2^TSv_2=1\). They are eigenvectors with eigenvalues \(3\) and \(1\), respectively. Since \(n-1=3\), the associated singular values are \(\sqrt{9}=3\) and \(\sqrt{3}\). Thus the first direction attains the larger variance, exactly as the maximization argument requires.

Checks, Alternatives, and Limitations

The SVD derivation and direct eigendecomposition of the sample covariance matrix are mathematically equivalent: both return the same eigenvalues and principal directions, apart from sign choices and choices of basis in tied eigenspaces. The SVD works on \(X\) directly and is often preferable when \(p\) is large, particularly when \(p>n\); forming a \(p\times p\) covariance matrix can require substantial computation and storage. Computing the covariance matrix first can be convenient when \(p\) is modest, but it can magnify numerical conditioning problems because the eigenvalues of \(X^TX\) are squared singular values.

A few checks help detect mistakes. Confirm that the columns used really are centered and that the matrix orientation is observations by features. Check that the computed directions have unit length and are mutually orthogonal, and that \(Xv_j=\sigma_j u_j\) for the paired singular vectors where those vectors exist. The sample variance of each score column should equal \(\sigma_j^2/(n-1)\). Small discrepancies can arise from numerical rounding; a negative covariance eigenvalue beyond tiny rounding error signals a computational or data-handling problem because \(S\) is positive semidefinite.

PCA is sensitive to the units and scaling of the features: changing a feature's scale can change the directions. Scaling is therefore a modeling choice, not an automatic property of the SVD. Exact ties make individual directions within a tied subspace non-unique, and signs are always arbitrary: \(v_j\) and \(-v_j\) describe the same direction, with all scores changing sign accordingly. In addition, a zero score direction reflects no observed variation in that direction; it does not prove that the feature combination is universally constant outside this data set. Ordinary SVD-PCA also requires a complete matrix and does not, on its own, model missing values or outliers.

  • Common mistake: “Every right singular vector has a corresponding left singular vector, so \(Xv_j=\sigma_j u_j\) for every column of \(V\).” This fails when \(p>n\), because \(V\) then has more columns than \(U\). The correct paired-column identity applies for \(j\leq\min(n,p)\); extra right singular vectors when \(p>n\) lie in the null space of \(X\) and produce zero scores.
  • Another mistake: “The eigenvalue equals the singular value.” The eigenvalue of the sample covariance is \(\sigma_j^2/(n-1)\), not \(\sigma_j\). Squaring and applying the sample-covariance divisor are both necessary.
  • Sign and ties: A software package may return a direction with the opposite sign, or a different orthonormal basis for tied eigenvalues. These can represent the same PCA solution and are not necessarily errors.
Key takeaway: For centered \(X\), the right singular vectors of \(X\) are the principal directions, their score vectors are obtained by \(Xv_j\), and the corresponding sample-covariance eigenvalues are \(\sigma_j^2/(n-1)\). When \(p>n\), extra right singular vectors have zero scores and no paired columns in \(U\).

Check Your Understanding

Answer each question, then compare with the solutions below.

  1. If a centered data matrix has singular value \(4\) and \(n=5\), what is the corresponding sample-covariance eigenvalue?
  2. Why is maximizing score variance over unit directions an eigenvector problem?
  3. For \(p>n\), what is true of a right singular vector indexed beyond \(n\) in a full SVD?
  4. If a principal direction is multiplied by \(-1\), what changes about its scores and variance?

Solutions. (1) The eigenvalue is \(4^2/(5-1)=16/4=4\). (2) The score variance is \(v^TSv\); the eigenvectors of the symmetric covariance matrix maximize this quadratic form successively over unit vectors, as shown by expanding \(v\) in an orthonormal eigenvector basis. (3) It lies in the null space of \(X\), so its score vector is zero and it has no paired left singular vector in \(U\). (4) The score vector changes sign because \(X(-v)=-(Xv)\), but its variance is unchanged because \((-v)^TS(-v)=v^TSv\).

Clinical Biostats