# What is the sample variance-covariance matrix?

This is a more succinct question from a previous post, but I have arrived at two different answers, and need help determining which - if either - is correct.

5  4  -1
2  3  -3
3  4  -4
1  3  -2


and I need to compute the variance-covariance matrix. I had originally tried:

• For the non-diagonals, calculate the covariance values using the formula:

$cov(x,y)=\frac{1}{n-1} \sum_{i=1}^n~(x_i- \overline{x} )*(y_i- \overline{y} )$

• For the diagonals, I caculate the variance values using the formula:

$var(x)=\frac{1}{n-1} \sum_{i=1}^n~(x_i- \overline{x} )^2$

2.916  4.042  -1.458
4.042  0.333  -2.917
-1.458 -2.917  1.666


However, I tried to double-check this hand-done work using R, and wrote:

m  = matrix(c(5,2,3,1,4,3,4,3,-1,-3,-4,-2), nrow=4, ncol=3)
covM = cov(m, method = "pearson")


2.916  0.833  0.833
0.833  0.333  0.000
0.833  0.000  1.666


So, it makes me wonder if I am using the wrong R function, or if I am using the wrong equations and calculations. Any ideas are greatly appreciated!!

Edit: What I am asking is, notice that the diagonal is the same for my hand calculation and R calculation. Only the off-diagonal is incorrect. For my hand calculation of the off-diagonals, I used the covariance equation I listed above (for my hand calculation of the diagonals, I used the varaince equation I listed above). The off-diagonal use of the covariance equation is what differs with the R output. So, am I using the correct equation? Many thanks!

Here is an example of how I got an off-diagonal value, and it was different than the same cell of the R output:

cov(1,2)=((5-2.75)(4-3.5)+(2-2.75)(3-3.5)+(3-2.75)(4-3.5)+(1-2.75)(3-3.5))/(4-1)=4.042


• @StellaJ Your term is correct. Only the calculation must be incorrect. I copied your term in here:wolframalpha.com/input/… The result is the result of R. Maybe the following rules help to get the correct result: $$\text{1. Terms in brackets have to be calculated first}$$ $$\text{2. Multiplication and division first, then addition and subtraction}$$ – callculus Sep 8 '14 at 20:52