0

I need to calculate a Hessian (matrix of second derivatives), and modify it iff it is not positive definite. Cholesky decomposition fails if a matrix is not symmetric positive (semi)definite, and Hessians are symmetric. Therefore I can use numpy.linalg.cholesky on my matrix, and this seems to be one of the most efficient ways of checking. See this post for example.

Now I am not sure how to condition on the Cholesky decomposition being performed without error. For instance

H=hessian(X)

if np.linalg.cholesky(H) ...??? :
      'modification'
Meep
  • 351
  • 1
  • 4
  • 15

1 Answers1

0

Check the numpy docs. The function np.linalg.cholesky raises an LinAlgError if the decomposition fails. Hence, wrapping it up in a try/except block would be a possible solution.

import numpy as np

X = np.random.randint(0, 10, (5, 5))

# compute hessian of X
# H = hessian(X)

try:
    L = np.linalg.cholesky(H)
except np.linalg.LinAlgError:
    # modifications
MaxPowers
  • 5,235
  • 2
  • 44
  • 69