Open Lab/Mathematical Foundations · Concept
Singular Value Decomposition
SVD
A matrix factorization that expresses a real matrix through orthonormal singular vectors and nonnegative singular values, and that supplies optimal Frobenius low rank approximations.
What is SVD?
Singular Value Decomposition is a factorization of a real matrix into three factors .
The columns of are left singular vectors. The columns of are right singular vectors. is a rectangular diagonal matrix whose diagonal entries are the singular values of . Those singular values are nonnegative and are conventionally ordered from largest to smallest.
Golub and Reinsch give this factorization for a real rectangular matrix and identify the singular values as the nonnegative square roots of the eigenvalues of . The relevant singular vectors form orthonormal sets. They are not, by themselves, chemical factors or latent physical components.
Why use it?
SVD applies to rectangular matrices as well as to square matrices. It exposes rank through the number of nonzero singular values, and it supplies an ordered sequence of rank one contributions that rebuild .
Truncating that sequence gives a lower rank approximation. Eckart and Young treat this as a least squares problem: among matrices of rank at most , the truncated SVD minimizes the sum of squared entrywise differences from , equivalently the Frobenius norm .
SVD is also a numerically standard way to obtain principal components when it is applied to a column-centered data matrix. That use is PCA. SVD itself is the matrix factorization, not the statistical method.
How does SVD work?
Let . A reduced, or economy, SVD can be written
with , , and . In this reduced form
and.
Those identities say that the retained singular vectors are orthonormal. They do not imply when is rectangular and . A full SVD uses square orthogonal factors of orders and , with then the same size as .
The singular values satisfy . If is a right singular vector and is a left singular vector for , then
and.
The number of nonzero singular values equals the exact rank of . In floating point arithmetic, deciding whether a computed singular value is zero requires a tolerance. No single tolerance is appropriate for every matrix.
Equivalently,
.
Each term is a rank one matrix. Keeping the first terms gives the rank approximation
.
The residual is the part of not represented by that rank truncation. It is not automatically noise.
If and are replaced together by and , the rank one term is unchanged. Different implementations can therefore return opposite singular vector signs while representing the same decomposition.
SVD does not center the matrix. Principal Component Analysis is obtained from SVD when the factorization is applied to a column-centered data matrix . In the notation of that page, and . For sample covariance PCA with denominator ,
.
SVD and PCA are not the same concept. SVD is a matrix decomposition. PCA is a data analysis method. SVD is one way to compute PCA when the input has been centered appropriately.
Mathematics & algorithm
The identities below use the reduced factors unless a full square orthogonal factor is stated. .
Reduced SVD
- real matrix being factored
- min(m, n)
- left singular vectors
- right singular vectors
- diagonal matrix of singular values
Interpretation
Equation 2 is orthonormality of the reduced singular vectors. It does not imply that equals the by identity when is smaller than .
Singular values
- singular value , a nonnegative square root of an eigenvalue of
- column of
- column of
Interpretation
Equations 4 and 5 are the reduced reconstructions of and . The possibly nonzero eigenvalues of those Gram matrices are the squares of the singular values.
Rank one expansion
Interpretation
Each summand is a rank one matrix. The factorization rebuilds A from those ordered contributions.
Rank k approximation
- retained rank, with k at most the rank of A
- residual after the rank k truncation, not automatically noise
Interpretation
Equation 11 is the Eckart and Young least squares result, stated here with the Frobenius norm. Among matrices B of rank at most k, the truncated SVD minimizes that criterion. The page does not claim optimality in other matrix norms.
PCA from a centered SVD
- column-centered data matrix, observations as rows
- PCA loadings, as on the PCA page
- PCA scores, as on the PCA page
- sample covariance eigenvalue with denominator n minus 1
Interpretation
These conversions hold for covariance PCA obtained from the SVD of . They do not make SVD and PCA the same method. Centering belongs to PCA, not to SVD.
Singular Value Decomposition workflow
InputA ∈ ℝᵐˣⁿ
Output, , , and optionally
- 01
- 02
- 03choose retained rank k if a truncated representation is required
- 04
- 05
- 06
- 07
- 08return and optionally
This is a SPARKS representation of the decomposition and truncation workflow. Step 01 delegates the numerical SVD to a validated linear algebra implementation. These steps are not the Golub and Reinsch Householder bidiagonalization and QR procedure. The third factor is , the right singular vectors, as in MATLAB svd. NumPy np.linalg.svd returns as its third factor.
Visual example
An explicit 4 by 3 matrix is decomposed with a reduced SVD. A1 and A2 are the rank 1 and rank 2 reconstructions. The bar plot is the Frobenius residual versus rank.
A
A1 rank 1
A2 rank 2
Code
Python uses the official NumPy routine numpy.linalg.svd. With full_matrices=False, the returned factors use reduced shapes based on . contains left singular vectors. is a one dimensional array of singular values in descending order. Vt is . For a real matrix that is . It is not .
Reconstruction follows the NumPy identity (U * s) @ Vt. Rank truncation keeps the first singular components of those reduced factors.
MATLAB uses the official MathWorks routine svd. The three-output form satisfies A = U*S*V'. Economy size decomposition is requested with "econ". In that form is square of order . MathWorks documents that corresponding columns of and may change sign without changing U*S*V'.
This page does not use sklearn.decomposition.TruncatedSVD. That estimator does not center its input, so it is not a substitute for covariance PCA.
import numpy as np def reduced_svd(A): A = np.asarray(A, dtype=float) if A.ndim != 2: raise ValueError("A must be a 2D array.") if not np.all(np.isfinite(A)): raise ValueError("A must contain only finite values.") if min(A.shape) == 0: raise ValueError("A must have at least one row and one column.") U, s, Vt = np.linalg.svd(A, full_matrices=False) return U, s, Vt def reconstruct_svd(U, s, Vt): U = np.asarray(U, dtype=float) s = np.asarray(s, dtype=float) Vt = np.asarray(Vt, dtype=float) return (U * s) @ Vt def rank_k_approx(U, s, Vt, k): U = np.asarray(U, dtype=float) s = np.asarray(s, dtype=float) Vt = np.asarray(Vt, dtype=float) k = int(k) if k < 1: raise ValueError("k must be a positive integer.") k = min(k, s.size) return (U[:, :k] * s[:k]) @ Vt[:k, :] Practical notes
- SVD applies to rectangular matrices as well as to square matrices.
- Singular values are nonnegative and are returned in nonincreasing order by NumPy and MathWorks svd.
- The number of nonzero singular values equals the exact rank of the matrix. Numerical rank requires a tolerance.
- Truncating the decomposition provides a lower rank approximation in the Frobenius sense of Eckart and Young.
- Singular vectors have a joint sign ambiguity. Opposite signs in a paired left and right vector do not change the corresponding rank one term.
- SVD does not center the matrix. Covariance PCA uses SVD of appropriately centered data.
- Singular vectors are not automatically physical or chemical components.
- A small singular value is not automatically noise, and a truncated SVD does not automatically preserve chemically relevant structure.
References
- 1.
Golub, G. H., & Reinsch, C. (1970). Singular value decomposition and least squares solutions. Numerische Mathematik, 14, 403-420.
doi:10.1007/BF02163027 - 2.
Eckart, C., & Young, G. (1936). The approximation of one matrix by another of lower rank. Psychometrika, 1(3), 211-218.
doi:10.1007/BF02288367 - 3.
Jolliffe, I. T., & Cadima, J. (2016). Principal component analysis: a review and recent developments. Philosophical Transactions of the Royal Society A, 374, 20150202.
doi:10.1098/rsta.2015.0202 - 4.
NumPy Developers (n.d.). numpy.linalg.svd. NumPy documentation.
- 5.
The MathWorks, Inc. (n.d.). svd. MATLAB documentation.
