Tutorials › Statistical Methods for Single-Cell RNA Sequencing › PCA From the Singular Value Decomposition

Dimension Reduction and Embeddings · Tutorial 203 of 700

PCA From the Singular Value Decomposition

Learn how the SVD produces PCA scores and gene-loading directions, how to check their dimensions and relationships, and what the singular values summarize.

Intermediate 10 min read

What You'll Learn

  • Write the SVD of a centered cell-by-gene matrix and identify the dimensions of its factors.
  • Derive cell scores as products of the centered matrix with gene-loading directions.
  • Relate singular values to the eigenvalues of the gene covariance matrix.
  • Interpret truncated scores and loadings through their matrix dimensions.
  • Diagnose centering, rank, sign, and repeated-singular-value issues.
  • Compare direct SVD with covariance-matrix eigendecomposition.

From a Centered Expression Matrix to PCA Coordinates

PCA represents cells using a smaller set of coordinates while retaining directions of variation in the input data. The singular value decomposition (SVD) gives a direct algebraic route to those coordinates and to the corresponding directions across genes. Understanding which SVD factor represents cells, which represents genes, and how the singular values connect them prevents a common source of confusion: treating scores and loadings as if they were interchangeable.

Let \(X\) be a centered matrix with \(n\) cells in rows and \(p\) genes in columns. Here, “centered” means that each gene column has mean zero across the cells. Each entry \(X_{ij}\) is the centered value for cell \(i\) and gene \(j\), on whatever expression scale was chosen before centering. No probability distribution for the entries is required for the matrix identities below. The values must be finite real numbers, and the same \(X\) must be used throughout the calculation.

Definition: A singular value decomposition of \(X\) is a factorization \(X=UDV^{T}\), where the columns of \(U\) and \(V\) are orthonormal, \(D\) contains nonnegative singular values, and \(T\) denotes transpose. In the compact SVD, if \(X\) has rank \(r\), then \(U\) is \(n\) by \(r\), \(D\) is \(r\) by \(r\), and \(V\) is \(p\) by \(r\). The singular values are conventionally ordered \(d_1\ge d_2\ge\cdots\ge d_r>0\).

The columns of \(U\) are cell-space directions, and the columns of \(V\) are gene-space directions. The \(j\)th column \(v_j\) of \(V\) gives one direction in the \(p\)-gene coordinate system. The corresponding \(j\)th column \(u_j\) of \(U\) gives a unit-length pattern across the \(n\) cells. The singular value \(d_j\) sets the scale linking these two directions.

Scores, Loadings, and the SVD

For PCA, the gene-space directions are commonly called loading directions, or simply loadings. The coordinates of cells along those directions are the PCA scores. With the SVD convention above, the score matrix is \(T=XV\). Substituting the SVD shows directly that \(T=UD\). Thus, scores are not \(U\) alone unless the singular values happen to equal one.

$$ T=XV=(UDV^T)V=UD $$

The dimensions make the roles concrete. The score matrix \(T\) is \(n\) by \(r\): one row per cell and one column per retained component. The loading matrix \(V\) is \(p\) by \(r\): one row per gene and one column per component. A cell’s score on component \(j\) is a single entry \(T_{ij}\); a gene’s loading on component \(j\) is \(V_{gj}\). The reconstruction identity is \(X=TV^T\), since \(UDV^T=TV^T\).

If only the first \(k\) components are retained, with \(k\le r\), write \(U_k\), \(V_k\), and \(D_k\) for the corresponding first \(k\) columns or singular values. The retained score matrix is \(T_k=U_kD_k=XV_k\), with dimensions \(n\) by \(k\). The retained loadings \(V_k\) have dimensions \(p\) by \(k\), and \(T_kV_k^T\) is an \(n\) by \(p\) matrix on the original centered scale. This identity describes how the retained coordinates map back to gene space; it does not, by itself, prescribe how many components to keep.

Key relationship: For component \(j\), the loading direction \(v_j\) has length one, the score vector is \(t_j=Xv_j=d_j u_j\), and \(Xv_j=d_j u_j\). Thus the score vector contains cell coordinates, while the loading vector contains gene coefficients.

Worked Example: Reading Scores and Loadings From a Small SVD

Suppose a hypothetical centered matrix has three cells and two genes:

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

Both columns sum to zero, as required for gene-centering. The first column \(x_1=(1,-1,0)^T\) and second column \(x_2=(1,1,-2)^T\) are orthogonal: \(x_1^Tx_2=1-1+0=0\). Their squared lengths are \(x_1^Tx_1=2\) and \(x_2^Tx_2=6\). Therefore the larger singular value is \(\sqrt{6}\), paired with the direction of the second gene, and the other is \(\sqrt{2}\), paired with the first gene.

One valid compact SVD is \(U=[x_2/\sqrt{6},\,x_1/\sqrt{2}]\), \(D=\operatorname{diag}(\sqrt{6},\sqrt{2})\), and \(V=\begin{bmatrix}0&1\\1&0\end{bmatrix}\). The first loading direction has one unit of weight on gene 2 and none on gene 1; the second has one unit of weight on gene 1 and none on gene 2.

Multiplying \(U\) by \(D\) gives the score matrix:

$$ T=UD= \begin{bmatrix} 1&1\\ 1&-1\\ -2&0 \end{bmatrix}. $$

For example, cell 3 has score \(-2\) on the first component and \(0\) on the second. The first component’s cell scores are the centered values of gene 2, because its loading direction selects gene 2 exactly. The score matrix has three rows and two component columns, while the loading matrix has two gene rows and two component columns.

Singular Values and the Covariance Matrix

The SVD also explains why its right singular vectors are the PCA loading directions. For the usual sample covariance matrix of the centered genes, \(C=X^TX/(n-1)\), the SVD gives:

$$ C=\frac{1}{n-1}VD^2V^T, \qquad Cv_j=\frac{d_j^2}{n-1}v_j. $$

To verify the second identity, use \(X=UDV^T\), \(U^TU=I\), and \(V^TV=I\):

$$ Cv_j =\frac{X^TXv_j}{n-1} =\frac{VDU^TUDV^Tv_j}{n-1} =\frac{VD^2e_j}{n-1} =\frac{d_j^2}{n-1}v_j. $$

Here \(e_j\) is the \(j\)th coordinate vector. In particular, \(v_j\) is an eigenvector of the gene covariance matrix and its eigenvalue is \(d_j^2/(n-1)\). The corresponding score vector has squared length \(t_j^Tt_j=d_j^2\), because \(t_j=d_ju_j\) and \(u_j^Tu_j=1\). These relationships use the sample-covariance denominator \(n-1\); using a different covariance convention changes that denominator, not the SVD itself.

Centering also constrains the possible rank. Since every column of \(X\) sums to zero, its columns lie in the \((n-1)\)-dimensional subspace perpendicular to the all-ones vector. Consequently, \(r\le\min(n-1,p)\). If some genes or cells are linearly dependent after centering, the rank can be smaller still. Components beyond the rank have zero singular value and do not provide additional nonzero score directions.

Worked Example: Nontrivial Gene Loadings

Consider another hypothetical centered matrix defined by its two columns, \(x_1=a+b\) and \(x_2=a-b\), where \(a=(2,-2,0)^T\) and \(b=(1,1,-2)^T/\sqrt{3}\). Both vectors sum to zero, their inner product is zero, and their squared lengths are \(8\) and \(2\), respectively. Thus this construction gives a centered three-cell, two-gene matrix.

The gene cross-product matrix can be computed from those inner products:

$$ X^TX= \begin{bmatrix} x_1^Tx_1&x_1^Tx_2\\ x_2^Tx_1&x_2^Tx_2 \end{bmatrix} = \begin{bmatrix} 10&6\\ 6&10 \end{bmatrix}. $$

For example, \(x_1^Tx_1=(a+b)^T(a+b)=8+2=10\), and \(x_1^Tx_2=(a+b)^T(a-b)=8-2=6\). The unit eigenvectors of this matrix are \(v_1=(1,1)^T/\sqrt{2}\) and \(v_2=(1,-1)^T/\sqrt{2}\), with eigenvalues \(16\) and \(4\). Therefore \(d_1=4\) and \(d_2=2\).

The score directions are \(u_j=Xv_j/d_j\). In particular, \(Xv_1=\sqrt{2}a\) and \(Xv_2=\sqrt{2}b\), so \(u_1=a/(2\sqrt{2})\) and \(u_2=b/\sqrt{2}\). Their lengths are both one: \(\lVert a\rVert/(2\sqrt{2})=\sqrt{8}/(2\sqrt{2})=1\), and \(\lVert b\rVert/\sqrt{2}=\sqrt{2}/\sqrt{2}=1\). The score columns are then \(4u_1=\sqrt{2}a\) and \(2u_2=\sqrt{2}b\). This example shows that a loading can combine several genes; it need not select one gene.

Truncation, Sign, and Numerical Checks

A truncated SVD keeps \(k\) components and forms \(T_kV_k^T\). This produces a matrix with the same dimensions as \(X\), while \(T_k\) still has one row per cell and \(V_k\) one row per gene. In applications with many more genes than cells, the centered matrix has at most \(n-1\) nonzero components. A computation may therefore report only a selected subset rather than constructing every column of a full SVD.

Worked Example: Reconstructing With One Component

Return to the first example and keep only its first component, whose score vector is \(t_1=(1,1,-2)^T\) and loading vector is \(v_1=(0,1)^T\). The one-component reconstruction is the outer product:

$$ \widehat{X}_1=t_1v_1^T = \begin{bmatrix} 1\\ 1\\ -2 \end{bmatrix} \begin{bmatrix} 0&1 \end{bmatrix} = \begin{bmatrix} 0&1\\ 0&1\\ 0&-2 \end{bmatrix}. $$

The result is \(3\) by \(2\), matching \(X\). Subtracting it from \(X\) leaves \(\begin{bmatrix}1&0\\-1&0\\0&0\end{bmatrix}\), the discarded second component in this example. Its squared Frobenius norm, the sum of squared entries, is \(1^2+(-1)^2=2\). This calculation illustrates the reconstruction identity for a retained component; it does not establish a general rule for selecting \(k\).

SVD vectors have a sign ambiguity. If \(u_j\) and \(v_j\) are both multiplied by \(-1\), then \(d_ju_jv_j^T\) is unchanged, as are the reconstructed data and the component’s underlying direction. Scores and loadings for that component both change sign. Therefore, a positive score or loading has no universal meaning without specifying the chosen sign convention and examining the associated gene direction.

If two or more singular values are equal, the individual vectors within their shared subspace are not uniquely determined: different orthonormal bases of that subspace give valid SVDs. Comparisons of such components across software runs or data perturbations should not assume that each basis vector must match. The subspace can be stable even when its particular basis changes.

Useful checks include confirming that the input columns are centered, that score and loading matrices have the expected dimensions, and that the factors reproduce the input to numerical precision. For a compact SVD, verify \(U^TU=I\), \(V^TV=I\), and \(X\) approximately equals \(UDV^T\). Also check that singular values are nonnegative and ordered, and that the number of reported nonzero values does not exceed \(\min(n-1,p)\) for centered data. Small numerical deviations from exact zero or exact orthogonality can arise from floating-point computation.

Choosing a Computational Route and Avoiding Misinterpretation

A direct SVD factors \(X\) and yields scores and loading directions without first forming a covariance matrix. An alternative is to eigendecompose \(X^TX\), a \(p\) by \(p\) matrix: its eigenvectors give the loading directions and its nonnegative eigenvalues are the squared singular values. When \(p\) is large, forming this matrix can be costly; it can also magnify numerical difficulties associated with small singular values. An eigendecomposition of \(XX^T\), an \(n\) by \(n\) matrix, can be more convenient when there are many genes and fewer cells, after which loading directions can be recovered from \(X^Tu_j/d_j\) for nonzero \(d_j\). These are algebraically related routes, but their computational costs differ.

The SVD is exact as a factorization, but the scientific interpretation depends on the input matrix. Centering gene columns defines the reference level for each gene, and prior transformations or scaling determine which differences contribute to the decomposition. A large loading magnitude indicates a substantial coefficient in that component’s gene direction; it does not alone establish a gene’s biological importance, a causal role, or a statistically significant effect. Scores are coordinates relative to the chosen directions, not cell labels.

A frequent mistake is to call the columns of \(U\) the PCA scores and stop there. Those columns are unit-length cell-space vectors; the scores are \(UD\), so each column is multiplied by its singular value. A second mistake is to interpret a loading as a cell coordinate. Check dimensions: rows of the score matrix index cells, and rows of the loading matrix index genes. Keeping these roles distinct makes both computation and interpretation clearer.

Key takeaway: For a centered cell-by-gene matrix \(X=UDV^T\), the gene loading directions are the columns of \(V\), the cell score matrix is \(T=XV=UD\), and the singular values in \(D\) connect the two. Their dimensions and algebraic relationships provide practical checks on a PCA computation.

Check Your Understanding

Answer each question, then compare your reasoning with the solutions.

  1. If \(X\) has 40 cells and 12 genes and rank 9, what are the compact dimensions of \(U\), \(D\), \(V\), and the score matrix?
  2. For a singular value \(d_j=3\) and \(n=10\), what is the corresponding eigenvalue of \(X^TX/(n-1)\)?
  3. Why do the score and loading vectors for one component have to change sign together without changing the reconstructed matrix?
  4. After centering, can a matrix with 5 cells have 5 nonzero singular values? Explain.

Solutions. (1) \(U\) is \(40\) by \(9\), \(D\) is \(9\) by \(9\), \(V\) is \(12\) by \(9\), and \(T=UD\) is \(40\) by \(9\). (2) The eigenvalue is \(d_j^2/(n-1)=9/9=1\). (3) The component contribution is \(d_ju_jv_j^T\); replacing both vectors by their negatives leaves this outer product unchanged. (4) No. Centered columns lie in a subspace of dimension at most \(n-1=4\), so the rank and number of nonzero singular values are at most 4.

Clinical Biostats