Status `~/Documents/github.com/ucla-biostat-257/2023spring/slides/20-eigsvd/Project.toml`
[7d9fca2a] Arpack v0.5.4
[6e4b80f9] BenchmarkTools v1.3.2
[b51810bb] MatrixDepot v1.0.10
[b8865327] UnicodePlots v3.5.3
[37e2e46d] LinearAlgebra
[9a3f8284] Random
[2f01184e] SparseArrays
1 Introduction
Our last topic on numerical linear algebra is eigen-decomposition and singular value decomposition (SVD). We already saw the wide applications of QR decomposition in least squares problem and solving square and under-determined linear equations. Eigen-decomposition and SVD can be deemed as more thorough orthogonalization of a matrix. We start with a brief review of the related linear algebra.
Eigenvalues are defined as roots of the characteristic equation \(\det(\lambda \mathbf{I}_n - \mathbf{A})=0\).
If \(\lambda\) is an eigenvalue of \(\mathbf{A}\), then there exist non-zero \(\mathbf{x}, \mathbf{y} \in \mathbb{R}^n\) such that \(\mathbf{A} \mathbf{x} = \lambda \mathbf{x}\) and \(\mathbf{y}^T \mathbf{A} = \lambda \mathbf{y}^T\). \(\mathbf{x}\) and \(\mathbf{y}\) are called the (column) eigenvector and row eigenvector of \(\mathbf{A}\) associated with the eigenvalue \(\lambda\).
\(\mathbf{A}\) is singular if and only if it has at least one 0 eigenvalue.
Eigenvectors associated with distinct eigenvalues are linearly independent.
Eigenvalues of an upper or lower triangular matrix are its diagonal entries: \(\lambda_i = a_{ii}\).
Eigenvalues of an idempotent matrix are either 0 or 1.
Eigenvalues of an orthogonal matrix have complex modulus 1.
In most statistical applications, we deal with eigenvalues/eigenvectors of symmetric matrices. The eigenvalues and eigenvectors of a real symmetric matrix are real.
Eigenvectors associated with distinct eigenvalues of a symmetry matrix are orthogonal.
Eigen-decompostion of a symmetric matrix: \(\mathbf{A} = \mathbf{U} \Lambda \mathbf{U}^T\), where
\(\mathbf{A} \in \mathbb{R}^{n \times n}\) a square matrix (not required to be symmetric), then \(\text{tr}(\mathbf{A}) = \sum_i \lambda_i\) and \(\det(\mathbf{A}) = \prod_i \lambda_i\).
1.2 Linear algebra review: singular value decomposition (SVD)
Singular value decomposition (SVD): For a rectangular matrix \(\mathbf{A} \in \mathbb{R}^{m \times n}\), let \(p = \min\{m,n\}\), then we have the SVD \[
\mathbf{A} = \mathbf{U} \Sigma \mathbf{V}^T,
\] where
\(\mathbf{U} = (\mathbf{u}_1,\ldots,\mathbf{u}_m) \in \mathbb{R}^{m \times m}\) is orthogonal
\(\mathbf{V} = (\mathbf{v}_1,\ldots,\mathbf{v}_n) \in \mathbb{R}^{n \times n}\) is orthogonal
\(\Sigma = \text{diag}(\sigma_1, \ldots, \sigma_p) \in \mathbb{R}^{m \times n}\), \(\sigma_1 \ge \sigma_2 \ge \cdots \ge \sigma_p \ge 0\). \(\sigma_i\) are called the singular values, \(\mathbf{u}_i\) are the left singular vectors, and \(\mathbf{v}_i\) are the right singular vectors.
Thin/Skinny SVD. Assume \(m \ge n\). \(\mathbf{A}\) can be factored as \[
\mathbf{A} = \mathbf{U}_n \Sigma_n \mathbf{V}^T = \sum_{i=1}^n \sigma_i \mathbf{u}_i \mathbf{v}_i^T,
\] where
\(\mathbf{U}_r \mathbf{U}_r^T\) is the orthogonal projection onto \({\cal C}(\mathbf{A})\)
\(\tilde{\mathbf{U}}_r \tilde{\mathbf{U}}_r^T\) is the orthogonal projection onto \({\cal N}(\mathbf{A}^T)\)
\(\mathbf{V}_r \mathbf{V}_r^T\) is the orthogonal projection onto \({\cal C}(\mathbf{A}^T)\)
\(\tilde{\mathbf{V}}_r \tilde{\mathbf{V}}_r^T\) is the orthogonal projection onto \({\cal N}(\mathbf{A})\)
Relation to eigen-decomposition. Using thin SVD, \[
\begin{eqnarray*}
\mathbf{A}^T \mathbf{A} &=& \mathbf{V} \Sigma \mathbf{U}^T \mathbf{U} \Sigma \mathbf{V}^T = \mathbf{V} \Sigma^2 \mathbf{V}^T \\
\mathbf{A} \mathbf{A}^T &=& \mathbf{U} \Sigma \mathbf{V}^T \mathbf{V} \Sigma \mathbf{U}^T = \mathbf{U} \Sigma^2 \mathbf{U}^T.
\end{eqnarray*}
\] In principle we can obtain singular triplets of \(\mathbf{A}\) by doing two eigen-decompositions.
Another relation to eigen-decomposition. Using thin SVD, \[
\begin{eqnarray*}
\begin{pmatrix} \mathbf{0}_{n \times n} & \mathbf{A}^T \\ \mathbf{A} & \mathbf{0}_{m \times m} \end{pmatrix} = \frac{1}{\sqrt 2} \begin{pmatrix} \mathbf{V} & \mathbf{V} \\ \mathbf{U} & -\mathbf{U} \end{pmatrix} \begin{pmatrix} \Sigma & \mathbf{0}_{n \times n} \\ \mathbf{0}_{n \times n} & - \Sigma \end{pmatrix} \frac{1}{\sqrt 2} \begin{pmatrix} \mathbf{V}^T & \mathbf{U}^T \\ \mathbf{V}^T & - \mathbf{U}^T \end{pmatrix}.
\end{eqnarray*}
\] Hence any symmetric eigen-solver can produce the SVD of a matrix \(\mathbf{A}\) without forming \(\mathbf{A} \mathbf{A}^T\) or \(\mathbf{A}^T \mathbf{A}\).
Yet another relation to eigen-decomposition: If the eigen-decomposition of a real symmetric matrix is \(\mathbf{A} = \mathbf{W} \Lambda \mathbf{W}^T = \mathbf{W} \text{diag}(\lambda_1, \ldots, \lambda_n) \mathbf{W}^T\), then \[
\begin{eqnarray*}
\mathbf{A} = \mathbf{W} \Lambda \mathbf{W}^T = \mathbf{W} \begin{pmatrix}
|\lambda_1| & & \\
& \ddots & \\
& & |\lambda_n|
\end{pmatrix} \begin{pmatrix}
\text{sgn}(\lambda_1) & & \\
& \ddots & \\
& & \text{sgn}(\lambda_n)
\end{pmatrix} \mathbf{W}^T
\end{eqnarray*}
\] is the SVD of \(\mathbf{A}\).
1.3 Applications of eigen-decomposition and SVD
1.3.1 Principal components analysis (PCA).
\(\mathbf{X} \in \mathbb{R}^{n \times p}\) is a centered data matrix. Perform SVD \(\mathbf{X} = \mathbf{U} \Sigma \mathbf{V}^T\) or equivalently eigendecomposition \(\mathbf{X}^T \mathbf{X} = \mathbf{V} \Sigma^2 \mathbf{V}^T\). The linear combinations \(\tilde{\mathbf{x}}_i = \mathbf{X} \mathbf{v}_i\) are the principal components (PC) and have variance \(\sigma_i^2\).
Dimension reduction: reduce dimensionality \(p\) to \(q \ll p\). Use top PCs \(\tilde{\mathbf{x}}_1, \ldots, \tilde{\mathbf{x}}_q\) in visualization and downstream analysis.
For example, image/data compression. Find a low rank approximation of data matrix \(\mathbf{x}\). Eckart-Young theorem: \[
\min_{\text{rank}(\mathbf{Y})=r} \|\mathbf{X} - \mathbf{Y} \|_{\text{F}}^2
\] is achieved by \(\mathbf{Y} = \sum_{i=1}^r \sigma_i \mathbf{u}_i \mathbf{v}_i^T\) with optimal value \(\sum_{i=r}^{p} \sigma_i^2\), where \((\sigma_i, \mathbf{u}_i, \mathbf{v}_i)\) are singular values and vectors of \(\mathbf{X}\).
Using thin SVD, \[
\mathbf{A}^+ = \mathbf{V} \Sigma^+ \mathbf{U}^T,
\] where \(\Sigma^+ = \text{diag}(\sigma_1^{-1}, \ldots, \sigma_r^{-1}, 0, \ldots, 0)\), \(r= \text{rank}(\mathbf{A})\). This is how the pinv function is implemented in Julia.
Given thin SVD \(\mathbf{X} = \mathbf{U} \Sigma \mathbf{V}^T\), \[
\begin{eqnarray*}
\widehat \beta &=& (\mathbf{X}^T \mathbf{X})^- \mathbf{X}^T \mathbf{y} \\
&=& (\mathbf{V} \Sigma^2 \mathbf{V}^T)^+ \mathbf{V} \Sigma \mathbf{U}^T \mathbf{y} \\
&=& \mathbf{V} (\Sigma^{2})^+ \mathbf{V}^T \mathbf{V} \Sigma \mathbf{U}^T \mathbf{y} \\
&=& \mathbf{V}_r \Sigma_r^{-1} \mathbf{U}_r^T \mathbf{y} \\
&=& \sum_{i=1}^r \left( \frac{\mathbf{u}_i^T \mathbf{y}}{\sigma_i} \right) \mathbf{v}_i
\end{eqnarray*}
\] and \[
\begin{eqnarray*}
\widehat{\mathbf{y}} &=& \mathbf{X} \widehat \beta = \mathbf{U}_r \mathbf{U}_r^T \mathbf{y}.
\end{eqnarray*}
\] In general, SVD is more expensive than other approaches (Cholesky, Sweep, QR) we learnt. In some applications, SVD is computed for other purposes then we get least squares solution for free.
1.3.5 Ridge regression
In ridge regression, we minimize \[
\|\mathbf{y} - \mathbf{X} \beta\|_2^2 + \lambda \|\beta\|_2^2,
\] where \(\lambda\) is a tuning parameter.
Ridge regression by augmented linear regression. Ridge regression problem is equivalent to \[
\left\| \begin{pmatrix} \mathbf{y} \\ \mathbf{0}_p \end{pmatrix} - \begin{pmatrix}
\mathbf{X} \\ \sqrt \lambda \mathbf{I}_p
\end{pmatrix} \beta \right\|_2^2.
\] Therefore any methods for linear regression can be applied.
Ridge regression by method of normal equation. The normal equation for the ridge problem is \[
(\mathbf{X}^T \mathbf{X} + \lambda \mathbf{I}_p) \beta = \mathbf{X}^T \mathbf{y}.
\] Therefore Cholesky or sweep operator can be used.
Ridge regression by SVD. If we obtain the (thin) SVD of \(\mathbf{X}\)\[
\mathbf{X} = \mathbf{U} \Sigma_{p \times p} \mathbf{V}^T.
\] Then the normal equation reads \[
(\Sigma^2 + \lambda \mathbf{I}_p) \mathbf{V}^T \beta = \Sigma \mathbf{U}^T \mathbf{y}
\] and we get \[
\widehat \beta (\lambda) = \sum_{i=1}^p \frac{\sigma_i \mathbf{u}_i^T \mathbf{y}}{\sigma_i^2 + \lambda} \mathbf{v}_i = \sum_{i=1}^r \frac{\sigma_i \mathbf{u}_i^T \mathbf{y}}{\sigma_i^2 + \lambda} \mathbf{v}_i, \quad r = \text{rank}(\mathbf{X}).
\] It is clear that \[
\begin{eqnarray*}
\lim_{\lambda \to 0} \widehat \beta (\lambda) = \widehat \beta_{\text{OLS}}
\end{eqnarray*}
\] and \(\|\widehat \beta (\lambda)\|_2\) is monotone decreasing as \(\lambda\) increases.
Only one SVD is needed for all \(\lambda\) (!), in contrast to the method of augmented linear regression, Cholesky, or sweep.
Power method iterates according to \[
\begin{eqnarray*}
\mathbf{x}^{(t)} &\gets& \frac{1}{\|\mathbf{A} \mathbf{x}^{(t-1)}\|_2} \mathbf{A} \mathbf{x}^{(t-1)}
\end{eqnarray*}
\] from an initial guess \(\mathbf{x}^{(0)}\) of unit norm.
\(\lambda_1^{(t)} = \mathbf{x}^{(t)T} \mathbf{A} \mathbf{x}^{(t)}\) converges to \(\lambda_1\).
Inverse power method for finding the eigenvalue of smallest absolute value: Substitute \(\mathbf{A}\) by \(\mathbf{A}^{-1}\) in the power method. (E.g., pre-compute LU or Cholesky of \(\mathbf{A}\)).
Shifted inverse power: Substitute \((\mathbf{A} - \mu \mathbf{I})^{-1}\) in the power method. It converges to an eigenvalue close to the given \(\mu\).
Rayleigh quotient iteration: Substitute \((\mathbf{A} - \mu^{(t-1)} \mathbf{I})^{-1}\), where \(\mu^{(t-1)} = \mathbf{x}^{(t-1)T} \mathbf{A} \mathbf{x}^{(t-1)}\) in the shifted inverse method. Faster convergence.
Example: PageRank problem seeks top left eigenvector of transition matrix \(\mathbf{P}\) and costs \(O(n)\) per iteration.
1.4.2 Top \(r\) eigen-pairs: orthogonal iteration
Generalization of power method to higher dimensional invariant subspace.
\(\mathbf{Z}^{(t)}\) converges to the eigenspace of the largest \(r\) eigenvalues if they are real and separated from remaining spectrum. The convergence rate is \(|\lambda_{r+1}|/|\lambda_r|\).
1.4.3 (Impractical) full eigen-decomposition: QR iteration
Take \(r=n\) in the orthogonal iteration. Then \(\mathbf{Q}^{(t)}\) converges to the eigenspace \(\mathbf{U}\) of \(\mathbf{A}\). This implies that \[
\mathbf{T}^{(t)} := \mathbf{Q}^{(t)T} \mathbf{A} \mathbf{Q}^{(t)}
\] converges to a diagonal form \(\Lambda = \text{diag}(\lambda_1, \ldots, \lambda_n)\).
This is the algorithm implemented in LAPACK: used by Julia, Matlab, R.
Idea: Tri-diagonalization (by Householder) + QR iteration on the tri-diagonal system with implicit shift.
Step 1: Householder tri-diagonalization: \(4n^3/3\) for eigenvalues only, \(8n^3/3\) for both eigenvalues and eigenvectors. (Why can’t we apply Householder to make it diagonal directly?)
Step 2: QR iteration on the tridiagonal matrix. Implicit shift accelerates convergence rate. On average 1.3-1.6 QR iteration per eigenvalue, \(\sim 20n\) flops per QR iteration. So total operation count is about \(30n^2\). Eigenvectors need an extra of about \(6n^3\) flops.
Stage
Eigenvalue
Eigenvector
Householder reduction
\(4n^3/3\)
\(4n^3/3\)
QR with implicit shift
\(\sim 30n^2\)
\(\sim 6n^3\)
Message: Don’t request eigenvectors unless necessary. Use eigvals in Julia to request only eigenvalues.
The unsymmetric QR algorithm obtains the real Schur decomposition of an asymmetric matrix \(\mathbf{A}\).
Random.seed!(123)n, p =1000, 500A =randn(n, p)@benchmarksvdvals(A)
BenchmarkTools.Trial: 134 samples with 1 evaluation.
Range (min … max): 33.891 ms … 44.077 ms┊ GC (min … max): 0.00% … 8.95%
Time (median): 37.404 ms ┊ GC (median): 0.00%
Time (mean ± σ): 37.482 ms ± 1.671 ms┊ GC (mean ± σ): 0.70% ± 2.20%
▂ █▃█▂▅ ▅▅
▅▃▃▅▅▃▁▃▅▁█▃▃█▁▆▃▇█████▇███▇▇█▃▃▃▁▃▃▅▃▃▅▁▁▃▁▃▁▁▁▁▃▁▃▁▁▁▁▁▁▃ ▃
33.9 ms Histogram: frequency by time 43.4 ms <
Memory estimate: 4.11 MiB, allocs estimate: 9.
@benchmarksvd(A)
BenchmarkTools.Trial: 83 samples with 1 evaluation.
Range (min … max): 55.027 ms … 67.249 ms┊ GC (min … max): 0.00% … 4.11%
Time (median): 60.447 ms ┊ GC (median): 0.00%
Time (mean ± σ): 60.422 ms ± 2.342 ms┊ GC (mean ± σ): 1.21% ± 1.74%
▂ ▂ █ ▂▂ ▂ ▂▅ ▂ ▅
▅▁▁▅▁▅▅▁▁▁▅▁▅█▅█▅▁█▅▅██▅▅███▅███▅█████▅▅███▅▁██▁▁▁▅█▁▅█▅▁▅▅ ▁
55 ms Histogram: frequency by time 64.7 ms <
Memory estimate: 17.23 MiB, allocs estimate: 12.
1.6 Lanczos/Arnoldi iterative method for top eigen-pairs
Consider the Google PageRank problem. We want to find the top left eigenvector of the transition matrix \(\mathbf{P}\). Direct methods such as (unsymmetric) QR or SVD takes forever. Iterative methods such as power method is feasible. However power method may take a large number of iterations.
Krylov subspace methods are the state-of-art iterative methods for obtaining the top eigen-values/vectors or singular values/vectors of large sparse or structured matrices.
Lanczos method: top eigen-pairs of a large symmetric matrix.
Arnoldi method: top eigen-pairs of a large asymmetric matrix.
Both methods are also adapted to obtain top singular values/vectors of large sparse or structured matrices.
eigs and svds in Julia Arpack.jl package and Matlab are wrappers of the ARPACK package, which implements Lanczos and Arnoldi methods.
usingMatrixDepot, SparseArrays# Download the Boeing/bcsstk38 matrix (sparse, pd, 8032-by-8032) from SuiteSparse collection# https://www.cise.ufl.edu/research/sparse/matrices/Boeing/bcsstk38.htmlA =matrixdepot("Boeing/bcsstk38")# Change type of A from Symmetric{Float64,SparseMatrixCSC{Float64,Int64}} to SparseMatrixCSCA =sparse(A)@showtypeof(A)Afull =Matrix(A)@showtypeof(Afull)# actual sparsity levelcount(!iszero, A) /length(A)
[ Info: verify download of index files...
[ Info: reading database
[ Info: adding metadata...
[ Info: adding svd data...
[ Info: writing database
[ Info: used remote sites are sparse.tamu.edu with MAT index and math.nist.gov with HTML index
Since orthogonal transform preserves Frobenius norm, we have \[
b_{pp}^2 + b_{qq}^2 = a_{pp}^2 + a_{qq}^2 + 2a_{pq}^2.
\] (Just check the 2-by-2 block)
Since \(\|\mathbf{A}\|_{\text{F}} = \|\mathbf{B}\|_{\text{F}}\), this implies that the off-diagonal part \[
\text{off}(\mathbf{B}) = \text{off}(\mathbf{A}) - 2a_{pq}^2
\] is decreased whenever \(a_{pq} \ne 0\).
One Jacobi rotation costs \(O(n)\) flops.
Classical Jacobi: search for the largest \(|a_{ij}|\) at each iteration. \(O(n^2)\) efforts!
\(\text{off}(\mathbf{A}) \le n(n-1) a_{ij}^2\) and \(\text{off}(\mathbf{B}) = \text{off}(\mathbf{A}) - 2 a_{ij}^2\) together implies \[
\text{off}(\mathbf{B}) \le \left( 1 - \frac{2}{n(n-1)} \right) \text{off}(\mathbf{A}).
\] Thus Jacobi method converges in \(O(n^2)\) iterations.
In practice, cyclic-by-row implementation, to avoid the costly \(O(n^2)\) search in the classical Jacobi.
Jacobi method attracts a lot recent attention because of its rich inherent parallelism.
Parallel Jacobi: “merry-go-round” to generate parallel ordering.