2
votes

Given a data matrix data of M dimensions and N samples, say,

data = randn(N, M);

I could compute the covariance matrix with

data_mu = data - ones(N, 1)*mean(data);
cov_matrix = (data_mu'*data_mu)./N

If I use the native MATLAB function

cov_matrix2 = cov(data)

this will always be equal to

cov_matrix = (data_mu'*data_mu)./(N-1)

That is, the denominator is (N - 1) is one less.

Why?? Can you reproduce it? Is this a bug??

I use MATLAB version 7.6.0.324 (2008).

2
No way Matlab would let a "bug" like that slip by them. There is a reason for N-1, let me find it. newton.dep.anl.gov/newton/askasci/1993/math/MATH014.HTM wiki.answers.com/Q/…'or'n-1' - Hamish Grubijan
It probably depends on whether you're looking at sample (co)variance or population (co)variance. For large N they are pretty close, but you have to be careful with small N. - Paul R

2 Answers

9
votes

That is, the denominator is (N - 1) is one less. Why?? Can you reproduce it? Is this a bug??

See the cov documentation. It has to do with population variance vs. sample variance.

Note also that if you wish to use the denominator N instead of N-1, you can add a trailing 1 argument to the call, i.e. cov(x,y,1) or cov(x,1) as per the documentation.

0
votes

n-1 is the correct denominator to use in computation of variance. It is what's known as Bessel's correction (http://en.wikipedia.org/wiki/Bessel%27s_correction) Simply put, 1/(n-1) produces a more accurate expected estimate of the variance than 1/n.