有人可以建议一种方法来获得奇异协方差矩阵的 Cholesky 分解吗?我需要它来匹配满秩矩阵上的 Cholesky,即应该保留坐标顺序。我在下面的尝试是在 scipy 中使用 ldl 例程,但这让我对不同的排序进行分解,有什么想法吗?
import numpy as np
from scipy import linalg
def modified_cholesky(arr):
"""Use Cholesky to produce LDL' factorization of arr."""
chol = linalg.cholesky(arr, lower=True)
d1 = np.diag(np.diag(chol))
L = chol@linalg.inv(d1)
return L, d1
def modified_ldl(arr):
lu, d, perm=linalg.ldl(arr, lower=True)
lu2 = lu[perm,:]
return lu2, np.sqrt(d), perm
# sanity checks
arr=np.array([[ 3., 5.], [ 5., 11.]])
mchol, d = modified_cholesky(arr)
np.testing.assert_allclose(mchol @ d @ d @ mchol.T, arr, rtol=1e-6, atol=1e-7)
mchol2, d2, perm = modified_ldl(arr)
np.testing.assert_allclose(mchol2 @ d2 @ d2 @ mchol2.T, arr[perm,:][:,perm], rtol=1e-6, atol=1e-7)
np.testing.assert_allclose(mchol, mchol2) # fails because linalg.ldl is permuted
modified_cholesky(np.array([[1,1],[1,1]])) # fails with 2-th leading minor of the array is not positive definite