简体   繁体   English

Scipy / Numpy:Cholesky 在检查是否为正定时

[英]Scipy / Numpy : Cholesky while checking if positive definite

I am translating a script from Matlab to Python where I have a matrix VarCov and I get the Cholesky decomposition of it.我正在将一个脚本从 Matlab 翻译成 Python,其中我有一个矩阵 VarCov,我得到了它的 Cholesky 分解。 Sometimes, due to float approximation, a matrix that should be positive devinite (PD) is not, and i have to add a small number to the diagonal.有时,由于浮点逼近,应该是正定性 (PD) 的矩阵不是,我必须在对角线上添加一个小数。 Here is the Matlab code:这是Matlab代码:

[CholeskyUpper,pd] = chol(VarCov);
while pd
    VarCov = VarCov + 0.0001 * eye(size(VarCov,1));
    [CholeskyUpper,pd] = chol(VarCov);
end

Matlab is convenient because it can return if a matrix is PD while doing the Cholesky decomposition. Matlab 很方便,因为它可以在进行 Cholesky 分解时返回矩阵是否为 PD。 It doesn't raise an error while doing so.这样做时不会引发错误。 It seems that in Python (scipy), it will just return an error.似乎在 Python (scipy) 中,它只会返回一个错误。 Is there a way to do something similar to Matlab without having to compute the eigenvalues first ?有没有办法做类似于 Matlab 的事情而不必先计算特征值?

Edit: Following sascha's tip I tried this:编辑:按照 sascha 的提示,我尝试了这个:

MyMatrix = np.array([[1,2],[1,2]])
PD = False
while PD == False:
    PD = True
    try:
        MyCholDec = sp.linalg.cholesky(MyMatrix)
    except np.linalg.LinAlgError:
        PD = False
        MyMatrix = MyMatrix + np.eye(2) * 0.001
print("done")  

Is this the best way ?这是最好的方法吗?

The fastest and easiest method I finally found is to directly use the scipy's lapack function:我最后找到的最快最简单的方法是直接使用scipy的lapack函数:

MyMatrix = np.array([[1,2],[1,2]])        
(MyCholDec ,pd) = sp.linalg.lapack.dpotrf(MyMatrix )
while pd > 0:
     MyMatrix= MyMatrix  + np.eye(2) * 0.01
     (MyCholDec ,pd) = sp.linalg.lapack.dpotrf( MyMatrix )

This function works like the Matlab version retunring both the decomposition and an int > 0 if the matrix is not positive definite.如果矩阵不是正定矩阵,则此函数的工作方式类似于 Matlab 版本,同时重新调整分解和 int > 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么通过 Cholesky 分解反转正定矩阵比使用 numpy 进行常规反转慢? - Why is inverting a positive definite matrix via Cholesky decomposition slower than regular inversion with numpy? Scipy Gaussian KDE:矩阵不是正定的 - Scipy Gaussian KDE : Matrix is not positive definite numpy 和 scipy 中的 cholesky 有什么区别? - What is the difference between cholesky in numpy and scipy? 所有特征值都是正的,np.linalg.cholesky 仍然给出矩阵不是正定的错误 - All eigenvalues are positive , still np.linalg.cholesky is giving error that matrix is not positive definite 用 numpy 计算的自相关 function 不是正定的? - autocorrelation function calculated with numpy is not positive definite? 找出矩阵是否为正定矩阵 numpy - Find out if matrix is positive definite with numpy numpy positive半明确警告 - numpy positive semi-definite warning scipy.cluster.vq.kmeans2中的“矩阵不是肯定的”错误 - “Matrix is not positive definite” error in scipy.cluster.vq.kmeans2 为什么 numpy 说我的全正矩阵不是半正定矩阵? - Why is numpy saying that my totally positive matrix is not positive semi-definite? scipy.stats.multivariate_normal 错误:输入矩阵必须是对称正定矩阵 - scipy.stats.multivariate_normal error: input matrix must be symmetric positive definite
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM