MATLAB Answers

Problem understanding PCA and eigenvectors of covariance matrix

9 views (last 30 days)
Hello everyone. I am currently working with the function pca, principal component analysis and also with the Karhunen-Loève expansion.
As far as I understand, in pca, the scores are equivalent to the eigenvectors of the covariance matrix.
I am working with a matrix called realizations, which is 144*5, meaning there are 144 observations of 5 random variables.
If I do the pca as
[coeff,score,latent,tsquared,explained,mu] = pca(realizations);
I obtain as score a matrix of size 144*5, as expected. However, if I write
covMat=cov(realizations)
The resulting matrix is a 5*5. Therefore, the eigenvectors (calculated using eig) of covMat are a 5*5 matrix too, instead of a 144*5, like in the case of the socres obtained using pca. As far as I know I am doing this right, since in the documentation of cov it says that rows are observations and columns are random variables.
Can someone please tell me the difference between both methodologies?
How can I get a eigenvector of length 144?
Best regards .
Jaime.

Accepted Answer

David Goodmanson
David Goodmanson on 19 Jul 2021
Edited: David Goodmanson on 19 Jul 2021
Hello Jaime,
Scores are not the eigenvalues of the covariance matrix. Rather, 'latent' have that role. Scores are projections of each observation onto the principal vectors. The code below is an example using eig (in the interest of numerical accuracy, pca actually uses the svd decomposition by default).
In your case the eigenvalues are always for a 5x5.
x = rand(10,3);
n = size(x,1);
mu = mean(x);
xcm = x - mu; % cm values for each column
covx = (xcm'*xcm)/(n-1); % covariance matrix
[v lambda] = eig(covx);
% for consistency with pca, sort the eigenvalues and permute
% the columns of the eigenvector matrix to match.
[lambda, ind] = sort(diag(lambda),'descend');
v = v(:,ind);
coeff = v % each column of coeff describes a principal vector
score = xcm*v % scores are projections of each observation onto the principal vectors
latent = lambda % length of each principal vector
% compare with pca
[coeff1 score1 latent1] = pca(x)
% the principal vectors can differ by a factor of -1 between methods, so
% the coeff ratio below may have either +1 or -1 down columns.
% However, the score ratio bvelow will have matching -1 down its columns, so the desription
% of observations in terms of principal vectors is unchanged.
% latent values, being eigenvalues, always match.
coeff./coeff1
score./score1
latent./latent1

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!