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:
- 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
- the matrix magic has some special properties that makes the pca in matlab be broken.
X=magic()
you have $N$ points in $N$ dimensions. This can only give you 4 PCs and that's what you get withpca()
. Butsvd()
returns 5 axes, 5th being arbitrary. Unless you usesvd()
with'econ'
parameter. (CC to @usεr11852) $\endgroup$svds
withk
smaller than $N$. For a square matrix, theecon
option won't change anything. Anyway to put this question to sleep: CharlieParker, check yourV
,coeff
are the right singular vectors. $\endgroup$econ
won't change anything in this case, I withdraw the last sentence of my previous comment. Thanks! But you are wrong about right singular vectors: again, X here is DxN, so PCA eigenvectors are left singular vectors. Specifically,coeff
is 4 first columns ofU
. $\endgroup$