0

I found a very interesting situation. Could anyone tell me why it happened?

For a matrix, I ran two different pieces of code to calculate its covariance matrix. The 2 results look the same. But there is a huge gap between their determinants. This is the code:

X = np.array([[11,5,3,34,8],[25,9,2,6,345],[96,13,8823,8,4],[92,15,153,9,12],[7,14,345,31,10]])
Xbar, mu, std = normalize(X)

S = (1/Xbar.shape[0]) * np.dot(Xbar.T, Xbar)
print('DIY covariance matrix:\n', S)
print('determinant:\n', np.linalg.det(S))
print('\n')

SbyNp = np.cov(Xbar, rowvar=False, bias=True)
print('numpy.linalg covariance matrix:\n', SbyNp)
print('determinant:\n', np.linalg.det(SbyNp))

np.testing.assert_almost_equal(S, SbyNp)
np.testing.assert_almost_equal(np.linalg.det(S), np.linalg.det(SbyNp))

The result of running:

DIY covariance matrix:
 [[ 1.          0.57159341  0.62932008 -0.7149413  -0.27222462]
 [ 0.57159341  1.          0.26958072 -0.39024794 -0.29147673]
 [ 0.62932008  0.26958072  1.         -0.38189298 -0.28405386]
 [-0.7149413  -0.39024794 -0.38189298  1.         -0.4706952 ]
 [-0.27222462 -0.29147673 -0.28405386 -0.4706952   1.        ]]
determinant:
 -1.4290811771081465e-16


numpy.linalg covariance matrix:
 [[ 1.          0.57159341  0.62932008 -0.7149413  -0.27222462]
 [ 0.57159341  1.          0.26958072 -0.39024794 -0.29147673]
 [ 0.62932008  0.26958072  1.         -0.38189298 -0.28405386]
 [-0.7149413  -0.39024794 -0.38189298  1.         -0.4706952 ]
 [-0.27222462 -0.29147673 -0.28405386 -0.4706952   1.        ]]
determinant:
 5.716324708432541e-17

Why these determinants are different? Is there any error in numpy.linalg.det()?

Cable W
  • 633
  • 1
  • 8
  • 17

0 Answers0