Spectral decomposition (a.k.a., eigen decomposition) is used primarily in principal components analysis (PCA). This method decomposes a square matrix, A, into the product of three matrices:
where, P is a n-dimensional square matrix whose ith column is the ith eigenvector of A, and D is a n-dimensional diagonal matrix whose diagonal elements are composed of the eigenvalues of A. That is, the spectral decomposition is based on the eigenstructure of A.
24.0.1 An Example
Recall that in a previous chapter we used the following \(2 \times 2\) matrix as an example:
Recall also that the eigen() function provided the eigenvalues and eigenvectors for an inputted square matrix. The eigenvectors were outputted as columns in a matrix, so, the $vector output from the function is, in fact, outputting the matrix P. The eigen() function is actually carrying out the spectral decomposition! We can use this output to verify the decomposition by computing whether \(\mathbf{PDP}^{-1}=\mathbf{A}\).
# Create AA =matrix(data =c(-3, 4, 5, -2), nrow =2 )# Compute eigenvalues and eigenvectorsspec_decomp =eigen(A)# Create PP = spec_decomp$vectors# Create DD =diag(spec_decomp$values)# Verify the decompositionP %*% D %*%solve(P)
[,1] [,2]
[1,] -3 5
[2,] 4 -2
Of note, when A is symmetric, then the P matrix will be orthogonal; \(\mathbf{P}^{-1}=\mathbf{P}^\intercal\). We can illustrate this by an example:
# Create symmetric matrix AA =matrix(data =c(1, 4, 2, 4, 1, 3, 2, 3, 1), nrow =3 )# Compute eigenvalues and eigenvectorsspec_decomp =eigen(A)# Create PP = spec_decomp$vectors# Inverse of Psolve(P)
The orthogonal P matrix makes this computationally easier to solve. Moreover, since D is a diagonal matrix, \(\mathbf{D}^{-1}\) is also easy to compute. Namely, \(\mathbf{D}^{-1}\) is also diagonal with elements on the diagonal equal to \(\frac{1}{\lambda_i}\).
Consider our ongoing simulated data:
# Number of casesn =50# Create 50 x-values evenly spread b/w 1 and 500 x =seq(from =1, to =500, len = n)# Create X matrixX =cbind(1, x, x^2, x^3)# Create b matrixb =matrix(data =c(1, 1, 1, 1), nrow =4 )# Create vector of y-valuesset.seed(1)y = X %*% b +rnorm(n, mean =0, sd =1)
We start by using spectral decomposition to decompose \(\mathbf{X}^\intercal\mathbf{X}\).
# Spectral decomposition of (X^T)Xspec_decomp =eigen(t(X) %*% X)# Create P and D^{-1}P = spec_decomp$vectorsD_inv =diag(1/spec_decomp$values)
Now we can carry out the matrix algebra to compute b.