cov computation in Matlab
9 views (last 30 days)
Show older comments
If the definition of covariance is (x-mean(x))'*(x-mean(x)), why cov(x) does not return the same result? Thank you.
0 Comments
Accepted Answer
Roger Stafford
on 27 Oct 2017
The 'cov' function normalizes by dividing by N-1 where N is the number of observations, which in this case is the number of rows in your matrix x.
10 Comments
Roger Stafford
on 28 Oct 2017
Edited: Roger Stafford
on 28 Oct 2017
@Bi Bu: No, the two expressions approximating the mean and the covariance are of a different nature. In the case of the mean the expression is a simple sum so that its expected value is simply the sum of the n separate means, and that certainly indicates the need to divide by n, not n-1 (where n is the number of terms). To divide by n-1 would be to give a biased estimate.
On the other hand, the expression for approximating the covariance is the sum of products, which in part depend on an approximation to the means of the two variables. It is this latter source of variation that has the effect of reducing, somewhat, the expected value of this expression, and results in a need to divide by the smaller n-1, not n. There is no such feature in the simple mean computation.
Remember, producing an unbiased estimate is defined as having the expected value of the approximation be precisely equal to the theoretical mean or covariance, so there is no choice in the matter in either case.
By the way, the website demonstration I mentioned above is actually concerned with the variance of one variable rather than the covariance of two variables. However, its argument is very similar to that needed for covariance, so it should serve to show the need for dividing by n-1 for the covariance computation. I would give the proof here, but I’m afraid it would take up quite a lot of space in this supposedly simple “answer”.
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!