13

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.

2
  • What did you do to remove singularities? Does something like Cov = Cov + numpy.diag(numpy.repeat(delta, k)) work? (Basically adding a small diagonal matrix to Cov. Here delta is a small float and k is dimension of Cov) Feb 6, 2014 at 13:41
  • I simply had Cov = Cov + d*np.identity(k). But looking at the original matrix, no value seems to be that close to zero.. Feb 6, 2014 at 13:50

2 Answers 2

17

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

print np.linalg.eigvalsh(Cov)

And the answer turns out to be this

[-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 ]

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.

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

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

And that fails saying something more

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

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'

1
  • 2
    Very good, well done. Cholesky requires positive definite. I think LU decomposition can handle it. Try changing to ludecomp; Cholesky is the positive definite special case: en.wikipedia.org/wiki/LU_decomposition
    – duffymo
    Feb 6, 2014 at 18:19
4

The problem is the data you're feeding to it. The matrix is singular, according to the solver. That means a zero or near-zero diagonal element, so inversion is impossible.

It'd be easier to diagnose if you could provide a small version of the matrix.

Zero diagonals aren't the only way to create a singularity. If two rows are proportional to each other then you don't need both in the solution; they're redundant. It's more complex than just looking for zeroes on the diagonal.

If your matrix is correct, you have a non-empty null space. You'll need to change algorithms to something like SVD.

See my comment below.

2
  • Hm. When calling np.diagonal(Cov) of the above matrix, it outputs an array of 1's. On a separate note, the 16x16 matrix generated by the above script is the smallest I've found to return the error message, but maybe that's still too big to paste here? Feb 6, 2014 at 13:47
  • 3
    I don't know. You understand the mathematical significance of what the error is telling you, right? It's the linear algebra equivalent of dividing by zero. It has to do with your matrix, not NumPy or your coding. I'm suggesting that you need to check carefully to make sure that you're populating that matrix correctly. If you're certain of it, and you still get the error, I'd say that you should change algorithms to something like SVD, which will cope with a singular matrix if you tell it how.
    – duffymo
    Feb 6, 2014 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.