Sign difference between coeff=pca(X) and [~,~,v] = svd(X)
Show older comments
I am experiencing sign differences between my computations for principal component analysis using the pca function and the svd function. Consider this matrix:
X = magic(4)
X =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
coeff = pca(X) %this will automatically save only the first few principal components
coeff =
0.5000 0.6708 0.4458
-0.5000 -0.2236 0.3573
-0.5000 0.2236 0.6229
0.5000 -0.6708 0.5344
Now when I do svd, I get the following results.
x0 = bsxfun(@minus,X,mean(X,1)); %first center the data
[~,~,v] = svd(x0)
v =
-0.5000 0.6708 -0.4458 -0.3182
0.5000 -0.2236 -0.3573 -0.7565
0.5000 0.2236 -0.6229 0.5586
-0.5000 -0.6708 -0.5344 0.1202
So I get essentially negatives of the first and third principal component. Does anyone know why this is?
Accepted Answer
More Answers (2)
Christine Tobler
on 1 Sep 2020
1 vote
I'm not sure why this happens. As mentioned by others, it doesn't matter what sign these outputs have. You could try stepping through the code of pca.m in the debugger, to see what they do differently before / after the call to the SVD.
Note that the signs returned by the SVD can also change based on which machine MATLAB runs on / which MATLAB version is used (and some other machine configuration-related settings can influence this, e.g. number of threads).
John D'Errico
on 26 Aug 2016
0 votes
SO? Eigenvectors are not unique down to a sign difference. WTP?
2 Comments
njj1
on 26 Aug 2016
John D'Errico
on 27 Aug 2016
Probably correct? I know I am correct in this respect. (You are not even close to the first person to trip over this issue. Not even the 1000'th person.)
There may still be many subtle differences in codes, even if both of them use svd in the core. If the matrix may be subtly different, even down in the least significant bits, then you may get different results in those signs.
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!