Perform principle component analysis (PCA) using singular value decomposition (SVD)


One method for performing PCA on a matrix and getting its factor loadings and eigenvalues is to use SVD as follows:

Given matrix M:

  1. Compute the correlation matrix of M (i.e., the matrix of all of its columns' correlations to each other).
  2. Perform SVD on the correlation matrix.
  3. Get the loadings and eigenvalues, which are U and the positive non-zero values in \Sigma.

In pseudo-code:

const C = correlationMatrix(M)
const { U, S, VT } = svd(C)
const loadings = U
const eigenvalues = flatten(S).filter(v => v > 0)