필터 지우기
필터 지우기

Covariance

조회 수: 3 (최근 30일)
Benjamin
Benjamin 2012년 3월 13일
댓글: Chinmayee L M 2021년 7월 19일
Hi,
quick question: can someone confirm that Matlab doesn't give back correct results for 'cov ? Example is here: http://www.mathworks.ch/help/techdoc/ref/cov.html
I calculated by hand and excel. I think there's something. Need the covariance for the financial toolbox.
Thanks a lot Ben
  댓글 수: 2
David Young
David Young 2012년 3월 13일
If you think the example has the wrong result, what do you think the correct result should be, according to your hand calculation and excel?
David Young
David Young 2012년 3월 14일
Thanks for the information (though perhaps better to edit the question than to add an answer). I think Oleg has the explanantion.

댓글을 달려면 로그인하십시오.

채택된 답변

nor
nor 2012년 3월 26일
Matrix A in the example has 3 rows which represents 3 observations. In your case,if you use: a) cov(A) this command will divide by 2 i.e (N-1), giving you 10.3333 for the first element in the covariance matrix.
b)cov(A,1) this command will divide by 3 i.e (N), giving you the same result as you obtain in Excel. Hope this helps.

추가 답변 (4개)

Oleg Komarov
Oleg Komarov 2012년 3월 14일
You should read the documentation thoroughly:
cov(A,1)
is the population covariance (divides by N), while
cov(A)
is the sample covariance (divides by N-1)
By default, the functions cov, var and std compute the sample statistic.

Sean Lawson
Sean Lawson 2012년 3월 13일
I used MATLAB cov and don't experience a wrong answer. Maybe you could post your example here if you cannot find anything wrong.
  댓글 수: 1
David Young
David Young 2012년 3월 14일
This strikes me as a comment on the question, rather than an answer to it.

댓글을 달려면 로그인하십시오.


Benjamin
Benjamin 2012년 3월 14일
For the matrix: A = [-1 1 2 ; -2 3 1 ; 4 0 3] Matrix gives result ans =
10.3333 -4.1667 3.0000
-4.1667 2.3333 -1.5000
3.0000 -1.5000 1.0000
just like the Example given in the link. With Excel and its covariance function (data analysis add-in), I receive this result 6.888888889 -2.777777778 2 -2.777777778 1.555555556 -1 2 -1 0.666666667
and by hand just for the very first element: Cov(1,1)=(-1-1/3)^2+(-2-1/3)^2+(4-1/3)^2 = 20.666/3 = 6.88889 But it seems that Matlab devides 20,666/2 = 10.333
What am I doing wrong? Thanks
  댓글 수: 1
Benjamin
Benjamin 2012년 3월 14일
Here again the excel result, hopefully in the right format:
6.8888 -2.77777 2.0000
-2.7777 1.5555 -1.0000
2.0000 -1.0000 0.6666

댓글을 달려면 로그인하십시오.


Chinmayee L M
Chinmayee L M 2021년 7월 19일
I didn't quite understand the terminology used in the documentation. Could someone clarify?
The documentation reads:
"If A is a matrix whose columns represent random variables and whose rows represent observations, C is the covariance matrix with the corresponding column variances along the diagonal."
If I have 10 time series with 400 samples, how should the matrix be organised before taking covariance?
a. 10 x 400
b. 400 x 10
Thanks
  댓글 수: 1
Chinmayee L M
Chinmayee L M 2021년 7월 19일
Figured it out.
Input to cov function should be a 10 x 400 matrix.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by