It would be great to have multivariate_normal raising exception
See original GitHub issueinstead of undefined behaviour as the NumPy doc says:
Note that the covariance matrix must be positive semidefinite (a.k.a. nonnegative-definite). Otherwise, the behavior of this method is undefined and backwards compatibility is not guaranteed.
For example, the following
from numpy import errstate
from numpy.random import RandomState
from numpy.linalg import eigh
from numpy import dot
rs = RandomState(3)
m = rs.randn(10)
G = rs.randn(10, 5)
K = dot(G, G.T)
with errstate(all='raise'):
try:
rs.multivariate_normal(m, K)
except Exception as e:
print("Exception raised!")
else:
print("Exception not raised!")
prints
bug.py:13: RuntimeWarning: covariance is not positive-semidefinite.
rs.multivariate_normal(m, K)
Exception not raised!
As you know, K
happens to not be positive-semidefinite in the above example because of floating point arithmetic. This can be easily fixed by adding a small value to its diagonal. If NumPy raised an Exception, I could perform this hack only if necessary. As it is now, I would need to check for positive-semidefiniteness beforehand, or add a small value to its diagonal before hand. Both solutions are not ideal in my opinion. Raising an exception would solve that.
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Raising exceptions when an exception is already present in ...
Answering to question 3, you can use: raise B('second') from None. Which will remove the exception A traceback. Traceback (most recent call ...
Read more >Raising Exceptions | Engineering Education (EngEd) Program
This article will go over how to manually throw exceptions in Python and use assertions for better debugging. Raising exceptions allows us ...
Read more >8. Errors and Exceptions — Python 3.11.1 documentation
Raising and Handling Multiple Unrelated Exceptions¶. There are situations where it is necessary to report several exceptions that have occurred. This is often ......
Read more >Descriptive error message for distribution constraint violation
Motivation. When I see this error, I have to identify the source of the problematic parameter, add logging code, run the program again ......
Read more >How to Throw Exceptions in Python - Rollbar
You can do this by checking a condition and raising the exception, if the condition is True. The raised exception typically warns the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You said you wanted to use
errstate
.errstate
is for floating point errors (as in IEEE 754). This is different - you basically want a check for whether the covariance matrix provided by a user is valid or not. The normal way to deal with those kinds of user parameter checks is: do a specific check, then raise aValueError
if needed.If no one is working, I would like to take this up.