Why I get two different covariance matrix?

Hi all,
Prof. Andrew Ng in his ML class says that we can calculate covariance matrix as: 1/m*X'*X .
Where;
examples are in rows of X,
X' is transpose of X,
and, m is number of examples.
For example:
X=randi(12,[6,2]);
cov1=1/size(X,1)*X'*X
cov1 = 2×2
92.5000 42.0000 42.0000 21.8333
And, covariance with cov function is:
cov2=cov(X)
cov2 = 2×2
2.7000 -0.9000 -0.9000 1.9000
As you can see, cov1 is different from cov2 !!!
What is the reasan for that? Do you have any idea?
Thanks

 Réponse acceptée

Hi Ali,
Perhaps Prof. Ng has some additional assumptions about the data that aren't included in your question. To compute the covariance we have to subtract off the mean. As for whether or not the outer product should be scaled by 1/m or 1/(m-1) depends on assumptions about the underlying data. IIRC, if we know the data is drawn from a Normal distribution then divide by m (perhaps also for other distributions as well?), but typically we don't assume that and so divide by m-1 for unbiased estimation. As can be seen below, cov subtracts the mean and divides by m-1.
rng(100);
X=randi(12,[6,2])
X = 6×2
7 9 4 10 6 2 11 7 1 11 2 3
cov1=1/(size(X,1)-1)*(X-mean(X))'*(X - mean(X))
cov1 = 2×2
13.3667 -1.6000 -1.6000 14.0000
cov(X)
ans = 2×2
13.3667 -1.6000 -1.6000 14.0000
As for the second question
sigma=[6 2;2 3]; % cov matrix
[a1,v] = eig(sigma)
a1 = 2×2
0.4472 -0.8944 -0.8944 -0.4472
v = 2×2
2.0000 0 0 7.0000
[a2,s,~] = svd(sigma)
a2 = 2×2
-0.8944 -0.4472 -0.4472 0.8944
s = 2×2
7 0 0 2
we see that eig and svd just have a different order for the results.

3 commentaires

ali yaman
ali yaman le 14 Juil 2022
Hi @Paul thanks, very appreciated for your help.
You are right. I checked the data set, and I saw that, as you indicated, all feature has 0 mean and 1 standard deviation. And for that type of data set COV and 1/m*X'*X gives same result. Thanks.
Probably, Prof. NG forgot to say that assumption or maybe he said but ı did not catch.
About 2nd part:
Yes, they just have diffrent order. So, how can i know which one should i use? or does not matter, both of them give me same result if i use?
Paul
Paul le 14 Juil 2022
Which one of eig() or svd() to use? it would never occur to me to use svd() to get the eigenvectors of a symmetric matrix. Don't know if eig() or svd() is better for that special case. Asking that as a new question is more likely to get the attention of knowledgeable people that can answer.
ali yaman
ali yaman le 14 Juil 2022
@Paul Ok, Thanks Bro.

Connectez-vous pour commenter.

Plus de réponses (1)

What about the second question!!
Just like covarince matrixe we can get two different eigenvectors.
For example;
sigma=[6 2;2 3]; % cov matrix
[a,~]=eig(sigma)
a = 2×2
0.4472 -0.8944 -0.8944 -0.4472
And, lets calculate eigenvectors with svd, singular value decomposition function:
[a,~,~]=svd(sigma)
a = 2×2
-0.8944 -0.4472 -0.4472 0.8944
As you can see we are getting two different eigenvectors value.
What is the reason?
Thanks, in advance.

4 commentaires

John D'Errico
John D'Errico le 14 Juil 2022
Modifié(e) : John D'Errico le 14 Juil 2022
PLEASE DON"T ASK a new question as an answer to your own question! In fact, I won't answer a question posed as you have done, and I could probably do so.
ali yaman
ali yaman le 14 Juil 2022
@John D'Errico Ok. sorry for that. I did not know it. I will be more careful from now on
John D'Errico
John D'Errico le 14 Juil 2022
Modifié(e) : John D'Errico le 14 Juil 2022
Since this is probably of some general interest, I'll actually post a question of my own, then answer it myself, discussing the relative issues between eig and svd.
ali yaman
ali yaman le 14 Juil 2022
OK. that would be nice. thanks

Connectez-vous pour commenter.

Catégories

En savoir plus sur Linear Algebra dans Centre d'aide et File Exchange

Produits

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by