我在尝试使用scipy.stats.multivariate_normal
时遇到了麻烦,希望你们中的一个人可以提供帮助。
我有一个2x2矩阵,可以找到使用numpy.linalg.inv()
的倒数,但是当我尝试将它用作multivariate_normal
中的协方差矩阵时,我收到LinAlgError
表示它是一个奇异的矩阵:
In [89]: cov = np.array([[3.2e5**2, 3.2e5*0.103*-0.459],[3.2e5*0.103*-0.459, 0.103**2]])
In [90]: np.linalg.inv(cov)
Out[90]:
array([[ 1.23722158e-11, 1.76430200e-05],
[ 1.76430200e-05, 1.19418880e+02]])
In [91]: multivariate_normal([0,0], cov)
---------------------------------------------------------------------------
LinAlgError Traceback (most recent call last)
<ipython-input-91-44a6625beda5> in <module>()
----> 1 multivariate_normal([0,0], cov)
/mnt/ssd/Enthought_jli199/Canopy_64bit/User/lib/python2.7/site-packages/scipy/stats/_multivariate.pyc in __call__(self, mean, cov, allow_singular, seed)
421 return multivariate_normal_frozen(mean, cov,
422 allow_singular=allow_singular,
--> 423 seed=seed)
424
425 def _logpdf(self, x, mean, prec_U, log_det_cov, rank):
/mnt/ssd/Enthought_jli199/Canopy_64bit/User/lib/python2.7/site-packages/scipy/stats/_multivariate.pyc in __init__(self, mean, cov, allow_singular, seed)
591 """
592 self.dim, self.mean, self.cov = _process_parameters(None, mean, cov)
--> 593 self.cov_info = _PSD(self.cov, allow_singular=allow_singular)
594 self._dist = multivariate_normal_gen(seed)
595
/mnt/ssd/Enthought_jli199/Canopy_64bit/User/lib/python2.7/site-packages/scipy/stats/_multivariate.pyc in __init__(self, M, cond, rcond, lower, check_finite, allow_singular)
217 d = s[s > eps]
218 if len(d) < len(s) and not allow_singular:
--> 219 raise np.linalg.LinAlgError('singular matrix')
220 s_pinv = _pinv_1d(s, eps)
221 U = np.multiply(u, np.sqrt(s_pinv))
LinAlgError: singular matrix
答案 0 :(得分:12)
默认情况下multivariate_normal
检查协方差矩阵的任何特征值是否小于根据其dtype和最大特征值的大小选择的某个容差(请查看{{3}的源代码}和scipy.stats._multivariate._PSD
获取完整的详细信息。)
正如上面提到的@kazemakase,虽然您的协方差矩阵可能会根据np.linalg.inv
使用的标准进行反转,但它仍然非常恶劣,并且无法通过multivariate_normal
使用的更严格的测试。< / p>
您可以将allow_singular=True
传递给multivariate_normal
以跳过此测试,但一般情况下,最好重新调整数据,以避免首先传递这种病态的协方差矩阵。