scipy.linalg.eig返回协方差矩阵的复特征值? [英] scipy.linalg.eig return complex eigenvalues for covariance matrix?

查看:290
本文介绍了scipy.linalg.eig返回协方差矩阵的复特征值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为协方差矩阵是对称的且是半正定的,所以协方差矩阵的特征值应为实数且为非负数.

The eigenvalues of a covariance matrix should be real and non-negative because covariance matrices are symmetric and semi positive definite.

但是,请看下面的scipy实验:

However, take a look at the following experiment with scipy:

>>> a=np.random.random(5)
>>> b=np.random.random(5)
>>> ab = np.vstack((a,b)).T
>>> C=np.cov(ab)
>>> eig(C)
7.90174997e-01 +0.00000000e+00j,
2.38344473e-17 +6.15983679e-17j,
2.38344473e-17 -6.15983679e-17j,
-1.76100435e-17 +0.00000000e+00j,   
5.42658040e-33 +0.00000000e+00j

但是,在Matlab中重现上面的示例可以正常工作:

However, reproducing the above example in Matlab works correctly:

a = [0.6271, 0.4314, 0.3453, 0.8073, 0.9739]
b = [0.1924, 0.3680, 0.0568, 0.1831, 0.0176]
C=cov([a;b])
eig(C)
-0.0000
-0.0000
 0.0000
 0.0000
 0.7902

推荐答案

您提出了两个问题:

  1. scipy.linalg.eig返回的特征值不是真实的.
  2. 某些特征值是负的.
  1. The eigenvalues returned by scipy.linalg.eig are not real.
  2. Some of the eigenvalues are negative.

这两个问题都是由截断和舍入错误引入的错误的结果,这些错误总是在使用浮点算术的迭代算法中发生.请注意,Matlab结果也产生了负的特征值.

Both of these issues are the result of errors introduced by truncation and rounding errors, which always happen with iterative algorithms using floating-point arithmetic. Note that the Matlab results also produced negative eigenvalues.

现在,对于问题的一个更有趣的方面:​​为什么Matlab的结果是真实的,而SciPy的结果却包含一些复杂的成分?

Now, for a more interesting aspect of the issue: why is Matlab's result real, whereas SciPy's result has some complex components?

Matlab的eig检测输入矩阵是实对称的还是Hermitian的,并使用Cholesky因式分解.请参阅 eig文档中对chol参数的描述. . SciPy不会自动完成此操作.

Matlab's eig detects if the input matrix is real symmetric or Hermitian and uses Cholesky factorization when it is. See the description of the chol argument in the eig documentation. This is not done automatically in SciPy.

如果要使用利用实对称矩阵或Hermitian矩阵结构的算法,请使用

If you want to use an algorithm that exploits the structure of a real symmetric or Hermitian matrix, use scipy.linalg.eigh. For the example in the question:

>>> eigh(C, eigvals_only=True)
array([ -3.73825923e-17,  -1.60154836e-17,   8.11704449e-19,
         3.65055777e-17,   7.90175615e-01])

如果四舍五入到与Matlab打印的精度相同的位数,则此结果与Matlab的结果相同.

This result is the same as Matlab's, if you round to the same number of digits of precision that Matlab printed.

这篇关于scipy.linalg.eig返回协方差矩阵的复特征值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆