Skip to main content
edited tags
Link
amoeba
  • 107.3k
  • 36
  • 321
  • 347
Source Link
Charlie Parker
  • 7.1k
  • 14
  • 77
  • 130

Is the matrix coeff from MATLAB's pca the same as the left singular vectors of the centered data?

Consider the SVD of a centered data matrix:

$$ X_{centered} = U \Sigma V^T$$

where a column of $X_{centered}$ is:

$$ X_{centered} = x^{(i)} - \frac{1}{N} \sum^N_{n=1} x^{(n)} $$

is the matrix $ U $ the same as the matrix coeff that the function pca uses?

I could have sworn that they were the same until I wrote the following code:

clear;clc;
%% data
D = 3
N = 5
X = rand(D, N);
%X = magic(N); %% <------ uncomment this line for disaster
%% process data
x_mean = mean(X, 2); %% computes the mean of the data x_mean = sum(x^(i))
X_centered = X - repmat(x_mean, [1,N]);
%% PCA
[coeff, score, latent, ~, ~, mu] = pca(X'); % coeff =  U
[U, S, V] = svd(X_centered); % coeff = U
%% Reconstruct data
% if U = coeff then the following should be an identity I (since U is orthonormal)
U * U'
coeff * coeff'
% if U = coeff then they should be able to perfectly reconstruct the data
X_tilde_U = U * U'*X
X_tilde_coeff = coeff*coeff'*X

but then if one uncomments X = magic(N); and uses magic as the data matrix instead of random vectors, then we get different results from coeff and U. Meaning that either:

  1. They are not the same (i.e. either I have a misunderstanding that the left singular vectors of the centered data is not the principal components)

OR

  1. the matrix magic has some special properties that makes the pca in matlab be broken.