Tutorials › Statistical Methods for Single-Cell RNA Sequencing › Computing Leading PCs With Truncated SVD

Dimension Reduction and Embeddings · Tutorial 209 of 700

Computing Leading PCs With Truncated SVD

Learn how to compute a low-rank PCA approximation efficiently, choose oversampling and power iterations, and assess whether the resulting components approximate the data adequately.

Intermediate 10 min read

What You'll Learn

  • Describe how truncated SVD reduces the computation needed for leading principal components.
  • Distinguish the requested output rank from the oversampled search-space dimension.
  • Explain how power iterations can improve randomized subspace approximations.
  • Calculate absolute and relative Frobenius residuals for an approximate component space.
  • Check orthogonality, residuals, and reproducibility when diagnosing approximate components.
  • Compare randomized SVD with iterative truncated methods and full SVD.

Why Compute Only the Leading Components?

For a single-cell expression matrix, the number of cells and genes can make a full singular value decomposition unnecessarily expensive. PCA usually needs only a modest number of leading components, not every singular vector. A truncated SVD aims to compute that leading subspace directly, avoiding the work and storage associated with the rest of the decomposition. This matters especially when a matrix has many rows, many columns, or both.

Let \(A\) be the \(n \times p\) matrix supplied to PCA after the centering and gene-scaling decisions described in the earlier material on gene scaling and PCA. Rows represent cells and columns represent genes. Its singular value decomposition is \(A=U\Sigma V^T\), with singular values in nonincreasing order. The first \(k\) left singular vectors describe the leading cell-space directions, while the corresponding right singular vectors describe gene-space directions. The rank-\(k\) approximation is \(U_k\Sigma_kV_k^T\). The computational task here is to obtain those leading directions efficiently and to check the quality of an approximation.

Definition: A rank-\(k\) truncated SVD retains the first \(k\) singular values and their left and right singular vectors, where \(k\) is smaller than the full matrix rank. For an approximate left singular subspace with orthonormal basis \(Q\), the projection residual is \(R=A-QQ^TA\). Its relative Frobenius residual is \(\|R\|_F/\|A\|_F\). This ratio measures the fraction of the matrix's Frobenius norm left outside the subspace; it is not, by itself, a guarantee that the components are biologically meaningful.

The Frobenius norm satisfies \(\|A\|_F^2=\sum_{i,j} A_{ij}^2=\sum_i \sigma_i^2\). For the exact leading \(k\)-dimensional left singular subspace, the squared residual is \(\sum_{i>k}\sigma_i^2\). An approximate subspace may leave a larger residual. These criteria assess reconstruction of the supplied matrix; they do not decide how many components should later be used for visualization.

Truncated and Randomized Computation

An exact full SVD computes all singular values and vectors. A truncated iterative method, commonly based on Lanczos bidiagonalization, instead builds approximations to the leading singular vectors through repeated matrix-vector products. It can be useful when only a few components are required. A randomized method follows a different route: it uses random projections to find a relatively small space that captures the important directions, then performs a much smaller SVD within that space.

In a randomized range-finding method, choose a target rank \(k\), an oversampling amount \(s\), and let \(\ell=k+s\). Generate a random matrix \(\Omega\) with \(p\) rows and \(\ell\) columns. Form \(Y=A\Omega\), and compute an orthonormal basis \(Q\) for the columns of \(Y\). The range of \(Q\) is a candidate approximation to the leading left singular subspace. Compute a small SVD of \(B=Q^TA\), then retain the leading \(k\) components of that smaller decomposition. The final output has rank \(k\), even though the search space had dimension \(\ell\).

$$ Y=A\Omega,\qquad B=Q^TA,\qquad B=\widetilde U\Sigma V^T,\qquad U_k\approx Q\widetilde U_k $$

Oversampling gives the random search space extra dimensions in which to capture directions that might otherwise be missed. It is a safeguard against an unlucky projection, not an increase in the number of components ultimately retained. A larger \(s\) generally increases the cost of matrix multiplications and the storage for \(Q\), so it should not be increased without reason. Values such as 5 to 20 are common starting points, but the appropriate choice depends on the matrix, target accuracy, and computational constraints.

When the singular values decline slowly, a basic random projection can mix important and less important directions. Power iterations can sharpen the separation. For an integer \(q\geq 0\), construct a basis from

$$ Y=(AA^T)^qA\Omega. $$

A singular direction associated with singular value \(\sigma_i\) is weighted by \(\sigma_i^{2q+1}\) in this transformed matrix. Thus, directions with larger singular values become more dominant relative to smaller ones as \(q\) increases. In practice, orthonormalize during the iterations to avoid numerical instability. Each extra power iteration requires additional passes through the matrix, so it improves subspace separation at a computational cost.

The cost is usually dominated by products involving \(A\) or \(A^T\), rather than the final small SVD. This is one reason randomized methods are useful for large matrices. Centering can make a sparse expression matrix dense if performed by explicitly subtracting gene means. Implementations can often apply the centering operation implicitly in matrix-vector products, preserving the computational advantages of the original sparse representation. The details depend on the software and preprocessing, so verify what matrix operation the implementation actually uses.

Worked Example: Choosing an Oversampled Search Space

Suppose a hypothetical centered matrix has \(n=12{,}000\) cells and \(p=2{,}000\) genes. The goal is to retain \(k=30\) components, with oversampling \(s=10\). For a float64 basis \(Q\), calculate its approximate storage requirement.

The search-space dimension is \(\ell=k+s=30+10=40\), while the final output still contains 30 components. The basis \(Q\) has \(n\ell=12{,}000\times40=480{,}000\) entries. At 8 bytes per entry, it requires \(480{,}000\times8=3{,}840{,}000\) bytes, or about 3.84 MB in decimal units. The compressed matrix \(B=Q^TA\) has \(40\times2{,}000=80{,}000\) entries, about 0.64 MB.

If \(s\) were increased to 30, then \(\ell=60\) and \(Q\) would require \(12{,}000\times60\times8=5{,}760{,}000\) bytes, or about 5.76 MB. More oversampling costs more memory and computation. These storage figures exclude the input matrix and software overhead; the dominant benefit of randomized SVD is that the calculation can focus on a small subspace rather than computing a full decomposition.

Checking Approximation Error

For an orthonormal basis \(Q\), the projection \(QQ^TA\) is the closest matrix to \(A\) with columns in the span of \(Q\), under the Frobenius norm. The residual \(R=A-QQ^TA\) records what that subspace fails to represent. Its relative Frobenius residual is scale-free, making it easier to compare matrices with different overall magnitudes. It is important to distinguish this reconstruction check from a test of whether particular cell groups or biological signals are preserved.

Justification: If \(Q^TQ=I\), then \(QQ^T\) is an orthogonal projection. The matrices \(QQ^TA\) and \((I-QQ^T)A\) are orthogonal under the Frobenius inner product because $$ \langle QQ^TA,(I-QQ^T)A\rangle_F =\operatorname{tr}\!\left(A^TQQ^T(I-QQ^T)A\right)=0, $$ using \(Q^T(I-QQ^T)=0\). Therefore, $$ \|A\|_F^2=\|QQ^TA\|_F^2+\|A-QQ^TA\|_F^2. $$ This identity gives the residual from the total and captured squared norms, provided the reported captured quantity is computed for that orthogonal projection.

Worked Example: Calculating a Relative Frobenius Residual

Consider a hypothetical matrix with \(\|A\|_F^2=141\). A computed orthonormal rank-two subspace captures squared Frobenius norm \(\|Q^TA\|_F^2=134.75\). Find the absolute and relative projection residuals.

Because \(Q\) has orthonormal columns, \(\|QQ^TA\|_F=\|Q^TA\|_F\). The squared residual is \(141-134.75=6.25\), so the absolute residual is \(\|A-QQ^TA\|_F=\sqrt{6.25}=2.5\). The total norm is \(\|A\|_F=\sqrt{141}\). Thus the relative residual is

$$ \frac{\|A-QQ^TA\|_F}{\|A\|_F} =\frac{2.5}{\sqrt{141}} \approx 0.2105. $$

The rank-two projection leaves about 21.05% of the matrix's Frobenius norm outside its subspace. The calculation does not show that each cell is reconstructed to that relative accuracy, and it does not establish that the retained directions capture every scientifically relevant pattern.

Worked Example: What One Power Iteration Changes

Suppose a hypothetical matrix has singular values 10, 5, 2, and 0.5, among others. Compare the relative weighting of the directions with singular values 2 and 5 before and after one power iteration.

With \(q=0\), the ratio of their weights is \(2/5=0.4\). With \(q=1\), the transformed weights are proportional to the cubes of the singular values, so the ratio becomes \((2/5)^3=8/125=0.064\). The smaller direction is therefore only 0.064 times as prominent relative to the direction at 5, rather than 0.4 times as prominent. This can help isolate a leading subspace when singular values are not well separated, but the extra matrix passes cost time. It does not alter the definition of the final rank \(k\) approximation.

Diagnostics, Alternatives, and Limitations

A small residual is useful evidence that a subspace reconstructs much of the input matrix, but it should be considered alongside other checks. Confirm that the reported basis is close to orthonormal by inspecting \(Q^TQ\); its diagonal should be near one and its off-diagonal entries near zero. For individual approximate singular triplets \((u_i,\sigma_i,v_i)\), check both \(\|Av_i-\sigma_i u_i\|\) and \(\|A^Tu_i-\sigma_i v_i\|\). Large values indicate that the vectors do not closely satisfy the singular-vector equations.

When an exact truncated decomposition is feasible, compare its leading singular values or subspace with the approximate result on a representative matrix. Signs of singular vectors are arbitrary, so compare subspaces or reconstructed projections rather than requiring matching signs. If repeated or nearly repeated singular values occur, individual vectors can rotate substantially within their shared subspace even when the subspace itself is accurate. Repeating a randomized computation with different random seeds can reveal sensitivity; materially different results merit investigation of rank, oversampling, power iterations, or numerical stability.

The principal alternatives have different costs and uses:

ApproachStrengthLimitation
Full SVDComputes the complete decomposition and provides a reference when feasible.Can use excessive time and memory when only a few components are needed.
Iterative truncated SVDTargets leading singular vectors without computing every component.May require many iterations, particularly when leading singular values are tightly spaced.
Randomized SVDUses a small number of matrix passes and can scale well to large matrices.Is approximate; quality depends on rank, oversampling, power iterations, and numerical implementation.

A common mistake is to set the search dimension equal to the desired number of components and assume the resulting approximate subspace must be adequate. The correction is to treat \(k\) as the output rank, use oversampling when needed to make subspace capture more reliable, and assess the result with residual and stability checks. Another mistake is to interpret a small global residual as proof that rare cell states or low-variance biological signals have been preserved. The Frobenius norm is dominated by contributions to total squared magnitude; it does not guarantee accuracy for every subset or feature.

Truncated computation also does not settle how many components should be used for a downstream display, nor does it remove the effects of earlier preprocessing choices. It produces a computationally efficient approximation to the PCA of the matrix supplied. The meaning of its components remains tied to that matrix and to the scaling and centering used to construct it.

1
Set the target rank.
Choose \(k\) as the number of components the calculation must return, based on the computational task.
2
Choose the search space.
Set \(\ell=k+s\), ensuring it does not exceed the matrix's possible rank, and use power iterations if a sharper separation is needed.
3
Check the computed result.
Inspect orthogonality and residuals, and assess stability across seeds or against a feasible reference calculation.
Key takeaway: Truncated SVD avoids computing components that are not needed. Randomized SVD adds a random search space, with oversampling and optional power iterations to improve its approximation. Treat residuals as diagnostics of matrix reconstruction, not as proof of biological validity.

Check Your Understanding

Try each problem, then compare your reasoning with the solutions.

  1. A computation requests \(k=25\) components and uses \(s=8\) oversampling dimensions. What are the search-space dimension and output rank?
  2. A matrix has squared Frobenius norm 200, and an orthogonal projection captures squared norm 192. Calculate the absolute and relative residuals.
  3. Why might one power iteration help when singular values decline slowly, and what is its computational cost?
  4. Name two checks, other than a small Frobenius residual, that can help diagnose an approximate SVD.

Solutions. (1) The search-space dimension is \(\ell=25+8=33\); the output rank remains 25. (2) The absolute residual is \(\sqrt{200-192}=\sqrt{8}\approx2.828\). The relative residual is \(\sqrt{8}/\sqrt{200}=\sqrt{0.04}=0.2000\), or 20.00%. (3) A power iteration weights singular directions in proportion to higher powers of their singular values, making dominant directions more prominent. It requires additional passes involving \(A\) and \(A^T\). (4) Check whether the basis is orthonormal, inspect singular-triplet residuals, compare with an exact truncated result when feasible, or assess stability across random seeds.

Clinical Biostats