views:

84

answers:

1

Can someone please either confirm or correct this Wikipedia algorithm for computing the first principal component? I want a simple implementation of PCA in D, which doesn't have any existing libraries for PCA AFAIK. I've tried implementing this, and it doesn't seem like my results on simple examples match stuff I get from R or Octave. I'm pretty sure it's not an implementation bug because I've checked over my implementation several times and even rewritten it from scratch, and it's a pretty simple algorithm.

+3  A: 

Is your data matrix is zero meaned, as required by the pre-conditions?

The code snippet is unclear on this point, but the way that the data matrix is originally described, I think that the column means need to be zero for the power method to work.

qdjm
Yep, that was it. I thought just the matrix as a whole had to have zero mean.
dsimcha
Glad to hear that. I've added the following clarification to the Wikipedia algorithm: "Note that here a zero mean data matrix means that the columns of X^T should each have zero mean." Could you confirm please that this is in fact what you did to fix the algorithm (instead of, say, making the column means of X zero)?
qdjm