BUG: np.random.multivariate_normal gives "RuntimeWarning: covariance is not positive-semidefinite" when it is
· Fixed by
#22171
Describe the issue:
np.random.multivariate_normal when run with a non-symmetric PD / PSD covariance matrix gives "RuntimeWarning: covariance is not positive-semidefinite". Even though it is PSD. It should warn "RuntimeWarning: covariance is not symmetric".
Reproduce the code example:
import numpy as np
cov=np.eye(2)
cov[0,1]=.5
print(cov)
print("symmetric?", (cov==cov.T).all())
print("PSD if eigvals all non-negative:", np.linalg.eig(cov)[0])
np.random.multivariate_normal(np.zeros(cov.shape[0]), cov, size=1)
Error message:
RuntimeWarning: covariance is not positive-semidefinite.
np.random.multivariate_normal(np.zeros(cov.shape[0]), cov, size=1)
NumPy/Python version information:
('1.23.0', '3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)]')
Positive-definite matrices must be symmetric. It isn't enough to have only positive eigenvalues.
Absolutely. But the warning should state that the problem is that is isn't symmetric, not that it isn't PSD
While the current error message is strictly correct, it would be helpful to users who are not as familiar with the mathematical terminology to explicitly use the word "symmetric" somewhere in there. I don't think we should try to test explicitly for symmetry and output a message that
only
mentions symmetry; we can tolerate a little numerical asymmetry to gracefully deal with outputs from other computations, so testing for symmetry would require applying arbitrary tolerances. But we could just say "covariance is not symmetric positive-semidefinite" to give the user more hints as to what they should check for.
But it isn't pd since it isn't symmetric. Symmetry is necessary but not sufficient for PD. I don't think one can meaningfully discuss non symmetric PD matrix because they don't exist.
Sorry, I didn't read your original reply carefully enough. But is that accepted that PD requires symmetry? it does sound like that on
wikipedia
, but not
here
:
"Positive definite matrices do not have to be symmetric, it is just rather common to add this restriction for examples and worksheet questions."
Indeed, my copy of
Matrix Computations
defines positive definiteness separately from symmetry and gives special attention to unsymmetric positive definite systems. So there's a reasonably authoritative reference that not all mathematical usage defines "positive definite" in a way that implies symmetry. It's just very, very common to do so.
…e_normal more descriptive (closes numpy#22140)
Issue numpy#22140 says that numpy.random.multivariate_normal incorrectly warns that a non-symmetric positive-semidefinite matrix isn't positive-semidefinite. In the replies, there was some ambiguity over whether it was possible for a positive-semidefinite matrix to be non-symmetric, with reliable sources saying that symmetry is a common condition to add but not actually necessary. To solve this problem, two different members of the Numpy organization decided that the warning and error message "covariance is not positive-semidefinite" should be changed to "covariance is not symmetric positive-semidefinite". However, this change was never actually made yet.
Since this change only required me to change a few strings instead of actually changing the code, I've decided to skip the CI jobs.
[skip ci]