question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

It would be great to have multivariate_normal raising exception

See original GitHub issue

instead 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:closed
  • Created 7 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
rgommerscommented, Jan 5, 2017

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 a ValueError if needed.

1reaction
jenishahcommented, Jan 5, 2017

If no one is working, I would like to take this up.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found