Search code examples
matlabcovariance

Basic Concept of covariance matrix and using it in Matlab


I am now studying PCA using an Eigen matrix. For doing PCA, obtaining covariance matrix is essential, but every time I obtain a covariance matrix by my hand and compare with my Matlab result, they are completely different.

Below is simple code that obtains a covariance matrix.

x=[-4 9 5;3 3 5;1 3 -1;8 1 7];
c=cov(x);
M=[2 4 4];
beforecov=x-repmat(M,4,1);
summat=zeros(3,3,4);
for i=1:4
   summat(:,:,i)= beforecov(i,:)'*beforecov(i,:);
end
cov_onmyown=(summat(:,:,1)+summat(:,:,2)+summat(:,:,3)+summat(:,:,4))/4;

x is the matrix that has 4 samples with 3 features. The result is

c=[24.667 -16   6;
   -16     12   0;
   6       0   12]

Now I obtain the covariance matrix manually. What I tried is by using the definition of the covariance matrix which is written below:

COV[X]=E[(X-u)(X-u)']

The mean matrix is [2 4 4] so I did X-u for each sample (beforecov in code). Then I made a 3x3 matrix for each 4 samples and divided by 4 (the sample number).

But the results c and cov_onmyown in the code are totally different.

cov_onmyown=[18.5,  -12,  4.5;
             -12,     9,  0;
             4.5,     0,  9]

Could anyone tell me what is wrong with my idea?


Solution

  • From the help text:

    cov(X) or cov(X,Y) normalizes by (N-1) if N>1
    

    So to get the same result as cov use

    cov_onmyown=(summat(:,:,1)+summat(:,:,2)+summat(:,:,3)+summat(:,:,4))/(4 - 1);