alishaexe / LISA_ET

0 stars 0 forks source link

Positive definite warning for covariance matrix #6

Closed alishaexe closed 6 months ago

alishaexe commented 6 months ago

When calculating the samps, and samps2 for the BPL fisher matrices you get a warning saying that the covariance matrix is not positive definite:


114: RuntimeWarning: covariance is not symmetric positive-semidefinite.
  samps2 = np.random.multivariate_normal(meansB, covmB, size=nsamp)
alishaexe commented 6 months ago

I've tested the covariance matrices for each one and they're all positive definite. I've looked around online and it's a problem with numpy that apparently has been patched. It essentially just raises this error when the off-diagonal elements are > 0. But since it is positive definite we can ignore the warning. I've included a tolerance thing in the code now so we shouldn't get the warnings and I'll leave the code that checks if the arrays are positive definite at the bottom of the script hashed out.


import numpy as np

arr = #"covariance matrix array"

# Check if the matrix is symmetric
is_symmetric = np.allclose(arr, arr.T)
print("Is symmetric:", is_symmetric)

# Check if all principal minors are positive
minors_positive = all(np.linalg.det(arr[:i, :i]) > 0 for i in range(1, arr.shape[0] + 1))
print("All principal minors are positive:", minors_positive)