Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT: Disallow complex covariances in multivariate_normal #14733

Closed

Conversation

hameerabbasi
Copy link
Contributor

This commit disallows complex covariances in multivariate_normal
as passing them can silently lead to incorrect results.

@bashtage
Copy link
Contributor

Looks good, except I would remove the word "yet" since there is no promise that this function will ever support complex gaussian (and IMO it should not, a complex gaussian should have its own method, but this is another discussion).

@bashtage
Copy link
Contributor

Of course, needs a test to make sure that it is triggered.

@hameerabbasi
Copy link
Contributor Author

this is another discussion

Let's have the discussion somewhere, because it's the difference between a ValueError and a NotImplementedError.

This commit disallows complex covariances in multivariate_normal
as passing them can silently lead to incorrect results.
@hameerabbasi
Copy link
Contributor Author

I have no clue why this is failing. It fails locally too.

@zoj613
Copy link
Contributor

zoj613 commented Oct 17, 2019

I have no clue why this is failing. It fails locally too.

seems like the line added to the tests gives this warning: numpy.ComplexWarning: Casting complex values to real discards the imaginary part, causing the test to fail.

Comment on lines +3442 to +3443
if np.issubdtype(cov.dtype, np.complexfloating):
raise NotImplementedError("Complex gaussians are not supported.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only checks the covariance and not the mean - is that deliberate? If so, I think the message could be clearer

@eric-wieser
Copy link
Member

@hameerabbasi
Copy link
Contributor Author

seems like the line added to the tests gives this warning: numpy.ComplexWarning: Casting complex values to real discards the imaginary part, causing the test to fail.

Worth being aware of these lines:
https://github.com/numpy/numpy/pull/14733/files#diff-6599d3132920de62ea7e896a318d8803R3481-R3482

The check is above that, and should raise an exception before the warning ever happens.

@charris
Copy link
Member

charris commented Nov 15, 2019

Needs rebase.

Base automatically changed from master to main March 4, 2021 02:04
@WarrenWeckesser
Copy link
Member

This looks like a pretty straightforward fix; can we revive it and get it merged?

I agree with @bashtage's comment--there is no plan to make this method accept complex inputs--so the error should be a TypeError. See, for example, what happens when you pass a complex array to multinomial:

In [8]: rng.multinomial(3, [0.25, 0.5, 0.25+0j])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-d28a44019b56> in <module>
----> 1 rng.multinomial(3, [0.25, 0.5, 0.25+0j])

_generator.pyx in numpy.random._generator.Generator.multinomial()

TypeError: can't convert complex to float

Also @eric-wieser's comment should be followed up with a change that also raises an exception if mean is complex.

@charris
Copy link
Member

charris commented Jun 16, 2022

Somewhat related, there was a PR to introduce a complex multivariate implementation. I don't recall what the replacement for the ordinary covariance matrix was, but apparently there is a standard approach somewhere out there in the numerical world.

@charris
Copy link
Member

charris commented Jun 16, 2022

@WarrenWeckesser Feel free to take this over.

@WarrenWeckesser
Copy link
Member

This PR was continued in #21779, which has been merged.

@hameerabbasi hameerabbasi deleted the disallow-complex-gaussian branch June 16, 2022 21:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants