Numpy Cholesky分解LinAlgError [英] Numpy Cholesky decomposition LinAlgError

查看:879
本文介绍了Numpy Cholesky分解LinAlgError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试对周期边界条件的2D数组的方差-协方差矩阵执行cholesky分解时,在某些参数组合下,我总是得到LinAlgError: Matrix is not positive definite - Cholesky decomposition cannot be computed.由于脚本很简单,因此不确定是numpy.linalg还是实现问题:

In my attempt to perform cholesky decomposition on a variance-covariance matrix for a 2D array of periodic boundary condition, under certain parameter combinations, I always get LinAlgError: Matrix is not positive definite - Cholesky decomposition cannot be computed. Not sure if it's a numpy.linalg or implementation issue, as the script is straightforward:

sigma = 3.
U = 4

def FromListToGrid(l_):
    i = np.floor(l_/U)
    j = l_ - i*U
    return np.array((i,j))

Ulist = range(U**2)

Cov = []
for l in Ulist:
    di = np.array([np.abs(FromListToGrid(l)[0]-FromListToGrid(i)[0]) for i, x in enumerate(Ulist)])
    di = np.minimum(di, U-di)

    dj = np.array([np.abs(FromListToGrid(l)[1]-FromListToGrid(i)[1]) for i, x in enumerate(Ulist)])
    dj = np.minimum(dj, U-dj)

    d = np.sqrt(di**2+dj**2)
    Cov.append(np.exp(-d/sigma))
Cov = np.vstack(Cov)

W = np.linalg.cholesky(Cov)

尝试删除潜在的奇点也未能解决问题.非常感谢您的帮助.

Attempts to remove potential singularies also failed to resolve the problem. Any help is much appreciated.

推荐答案

深入研究问题,我尝试打印Cov矩阵的特征值.

Digging a bit deeper in problem, I tried printing the Eigenvalues of the Cov matrix.

print np.linalg.eigvalsh(Cov)

答案就是这个

[-0.0801339  -0.0801339   0.12653595  0.12653595  0.12653595  0.12653595 0.14847999  0.36269785  0.36269785  0.36269785  0.36269785  1.09439988 1.09439988  1.09439988  1.09439988  9.6772531 ]

啊哈!注意前两个负特征值吗?现在,当且仅当其所有特征值均为正时,矩阵才为正定.因此,矩阵的问题不在于它接近零",而在于它是负".为了扩展@duffymo类比,这是线性代数,等效于尝试取负数的平方根.

Aha! Notice the first two negative eigenvalues? Now, a matrix is positive definite if and only if all its eigenvalues are positive. So, the problem with the matrix is not that it's close to 'zero', but that it's 'negative'. To extend @duffymo analogy, this is linear algebra equivalent of trying to take square root of negative number.

现在,让我们尝试执行相同的操作,但是这次使用scipy.

Now, let's try to perform same operation, but this time with scipy.

scipy.linalg.cholesky(Cov, lower=True)

那还不能说更多话

numpy.linalg.linalg.LinAlgError: 12-th leading minor not positive definite

这说明了更多,(尽管我真的不明白为什么它抱怨第12个未成年人).

That's telling something more, (though I couldn't really understand why it's complaining about 12-th minor).

最重要的是,矩阵不是很接近零",而是更像负".

Bottom line, the matrix is not quite close to 'zero' but is more like 'negative'

这篇关于Numpy Cholesky分解LinAlgError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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