3

I have the following dataframe:

   A  B
0  1  5
1  2  6
2  3  7
3  4  8

I wish to calculate the covariance

a = df.iloc[:,0].values

b = df.iloc[:,1].values

Using numpy for cov as :

numpy.cov(a,b)

I get:

array([[ 1.66666667,  1.66666667],
   [ 1.66666667,  1.66666667]])

Shouldn't the diagonal elements be 1? How do I get the diagonal elements to 1?

Prgmr
  • 129
  • 4
  • 11

3 Answers3

3

No they shouldn't. I think you might be confusing it with Correlation. Correlation and Covariance are different.

What you see in the diagonals is simply the variance of the variables! Wiki screenshot for the formulas -

enter image description here

Wiki Link

Vivek Kalyanarangan
  • 8,951
  • 1
  • 23
  • 42
1

Use pd.DataFrame.corr
Also, no need to use Numpy here when the built in Pandas method does the job well for you. Correlations will be one because you've normalized the different series by their respective standard deviations.

df.corr() 

     A    B
A  1.0  1.0
B  1.0  1.0

While pd.DataFrame.cov gets you

df.cov()

          A         B
A  1.666667  1.666667
B  1.666667  1.666667

The other posters are correct. We can see that performing the maths correctly, we get

df.cov().div(df.std()).div(df.std(), 0)

     A    B
A  1.0  1.0
B  1.0  1.0
piRSquared
  • 285,575
  • 57
  • 475
  • 624
0

I believe the function that you are looking for should be numpy.corrcoef rather than numpy.cov .

The relationship between correlation matrix and covariance matris is as follows:

R[i,j] = C[i,j]/sqrt(C[i,i]*C[j,k])
Siong Thye Goh
  • 3,518
  • 10
  • 23
  • 31