Why do these two codes give different covariance matrices?

1 回表示 (過去 30 日間)
Sadiq Akbar
Sadiq Akbar 2021 年 5 月 5 日
編集済み: David Goodmanson 2021 年 5 月 10 日
1st code:
S =[ 0.6661 - 0.7458i 0.4210 - 0.9071i 1.0000 + 0.0000i;-0.9127 + 0.4086i -0.8429 + 0.5381i 1.0000 + 0.0000i;1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i;-0.9127 - 0.4086i -0.8429 - 0.5381i 1.0000 - 0.0000i;0.6661 + 0.7458i 0.4210 + 0.9071i 1.0000 - 0.0000i];
Rmm=cov(S);
2nd code:
S =[ 0.6661 - 0.7458i 0.4210 - 0.9071i 1.0000 + 0.0000i;-0.9127 + 0.4086i -0.8429 + 0.5381i 1.0000 + 0.0000i;1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i;-0.9127 - 0.4086i -0.8429 - 0.5381i 1.0000 - 0.0000i;0.6661 + 0.7458i 0.4210 + 0.9071i 1.0000 - 0.0000i];
Rmm= S*S'/length(S(1,:));

回答 (1 件)

David Goodmanson
David Goodmanson 2021 年 5 月 5 日
Hi Sadiq,
cov subtracts mean from each column of S, and also divides by (size(S,1) -1). Also S and S' have to be multiplied in the correct order.
S =[ 0.6661 - 0.7458i 0.4210 - 0.9071i 1.0000 + 0.0000i;
-0.9127 + 0.4086i -0.8429 + 0.5381i 1.0000 + 0.0000i;
1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i;
-0.9127 - 0.4086i -0.8429 - 0.5381i 1.0000 - 0.0000i;
0.6661 + 0.7458i 0.4210 + 0.9071i 1.0000 - 0.0000i];
Rmm = cov(S)
S1 = S-mean(S);
Rmm1= S1'*S1/(size(S1,1)-1)
  3 件のコメント
David Goodmanson
David Goodmanson 2021 年 5 月 8 日
編集済み: David Goodmanson 2021 年 5 月 8 日
Well, the second code is actually not correct, and the different size of Rmm compared to cov(S) is one indication.
David Goodmanson
David Goodmanson 2021 年 5 月 9 日
編集済み: David Goodmanson 2021 年 5 月 10 日
Actually it's pretty easy to say the second code is wrong, since it gives totally different results than Matlab cov which is correct.
Besides which, youtube videos are hardly infallabe. Anyone can post one, and a few are wrong, so you have to take them with a grain of salt. This video is not incorrect, but I would say it is very misleading.
In the video, his table is in the standard form with samples down the columns, variables across the rows. For each variable, its samples form a column vector. The key is that the covariance between two variables is the inner product, a scalar quantity. For two columns of sample data g and h, the inner product is g' * h [matrix multiplication of row g x column h].
However, for purposes of illustration he enters the data as row vectors g and h and makes no comment about the distinction. Then the inner product is expressed the other way round, g * h' .
Since S has its samples in columns, you can see which multiplication is required

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by