# How to calculate the covariance matrix

I tried searching a lot on the net and got the following sources:

• Source One
• Source Two
• The first source seems to be incorrect cause when I calculate it using matlab it comes to be different from what they have given as the answer. As for the second link I cant understand that cause its not completely explaining as to how to calculate. Could anyone please provide me with a sound link or explain how to calculate a co-variance matrix?

• What covariance matrix? The covariance matrix for the OLS estimator is not the same thing as the covariance matrix for the residuals, for example, if we think of a regression context. You need to be more specific. Do you mean sample (co)variance (whose univariate counterpart is $(n-1)^{-1}\sum_{i=1}^n(x_i-\bar{x})^2$)? Nov 11 '13 at 20:58
• I got my answer finally. Have posted it below. Nov 12 '13 at 4:23
• Where? ${ }$ ${ }$ ${ }$ ${ }$ Nov 12 '13 at 6:23
• @draks... Bah! Internet problem. Now done! Nov 12 '13 at 13:38

I finally understood the concept behind co-variance. Co-variance is different for population data and sample data.

Following is the method I followed:

Let $$A$$ be a $$n \times m$$ matrix where $$n$$ is the number of rows (observations) and $$m$$ represents the number of columns (variables).
Let $$\mathbf e$$ be the $$n \times 1$$ column vector composed entirely of ones. Then, $$X= A - \left(\frac{1}{n}\right)\mathbf e\mathbf e^TA$$ Then, denote $$Y = X^TX.$$

Next is the step that differs for population data and sample data.

In case of population data, the covariance matrix $$\Sigma$$ is given by : $$\Sigma=\left(\frac{1}{n}\right)Y$$
and in case of sample data, the covariance matrix $$\Sigma$$ is given by : $$\Sigma=\left(\frac{1}{n-1}\right)Y$$ Hope it helps anyone stuck on a similar problem.

• I've erroneously accepted an edit that was not quite right; now I've tried to edit to adress the confusion that existed prior to this edit. I hope I didn't make things worse. Apr 25 '19 at 14:27