Skip to content

Fix for cov() with complex input - #3882

Closed
Nodd wants to merge 9 commits into
numpy:masterfrom
Nodd:cov
Closed

Nodd wants to merge 9 commits into
numpy:masterfrom
Nodd:cov

Conversation

@Nodd

@Nodd Nodd commented Oct 8, 2013

Copy link
Copy Markdown
Contributor

Closes issues #597 and #2680, which are duplicates.

It follows #2716 which was abandoned.

The example for the test was taken from #597.

@seberg

seberg commented Oct 8, 2013

Copy link
Copy Markdown
Member

Nice. There is still the case of y to check (and the 0-d special case). I would somewhat suggest using:

if y is None:
    dtype = np.result_type(X, np.float64)
else:
    dtype = np.result_type(X, y, np.float64)
# continue using dtype everywhere

which would preserve long double precision (could use 0 for np.float64 to preserve less precision types, but since that could reduce precision of working code). But maybe someone else disagrees, since I like result type ;).

Wondering about the 0-d special case. You could probably just remove it. It should just keep working, except creating warnings due to empty mean call, but I am not sure if that special case should not be an error... Anyway, I guess that is not really important to this PR.

@Nodd

Nodd commented Oct 8, 2013

Copy link
Copy Markdown
Contributor Author

Oh, I completely forgot about y !

I didn't know about result_type, this seems useful. In the last lines the dot product is divided by fact, so in the end the result is at least in np.float. Is preserving less precision types important for the dot product ? It may be optimized for int or something, I have no clue...

Comment thread numpy/lib/function_base.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you have a look at this too? I think we should probably just remove the special case. There will be some warnings raised (actually more then just one, but that is also the case in std, etc.) in some cases, but I don't see a problem with it, the current result is plain nonsense in those cases.

If you fix this, there is also the case of ddof being too large. So maybe you could add a warning such as std/var are now giving: RuntimeWarning: Degrees of freedom <= 0 for slice if fact is <= 0 (and replace fact by 0 in that case).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh, I think charris made sure that the warnings for std, about the degrees of freedoms are raised before other warnings about the same thing, so it would be good to move the fact calculation/check before the mean calculation probably.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If I understand correcty, you want me to remove the check for empty arrays and get a RuntimeWarning on the mean computation ?
I can check for ddof but if I set fact = 0 there will be a division by 0 at the end of the function. The correct value should be 1.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

really? I would have thought the most correct value is nan (unless we throw an error) because of that division, just like std gives nan in those cases. If the value was 1, then they would all be 1 and not add up to 1?! I simply care for the warning mean gives, if you prefer to put the mean calculation in a conditional to avoid it, that is fine with me too.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Uh, well, I guess they ar enot supposed to add up, but still, does such a 1 result really make sense, also considering if ddof >= 1 and there actually are values, but just not enough.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right, 1 makes no sense. The division by 0 yields nan only if the value in the array are 0, otherwise it is +/-inf. Is it acceptable ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I tried to remove the special case of X.size == 0, but then test_empty fails (after tweaking to take the warning into account) as the returned value is nan thus not empty: dot([], []) is False and is then divided by 0.0. Returning an empty array the size of the input array seems a better option.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think that is acceptable, i.e. std does the same, and we would add the warning as well.

To be honest, I would guess that the empty special case was just part of many such added when the general support for this was much worse, and the test should just be changed. As far as I can see, the current result is almost always incorrect. Since the old result is plain wrong, I would consider it a bug fix and probably no big warnings are needed. But we could of course add a FutureWarning for now...

Comment thread numpy/lib/function_base.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you said that inf is possible? Could use np.isfinite, or simply c / c.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right, nan was only for empty arrays but it is indeed possible to have a scalar with ddof too high.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can stay as is as far as I am concerned, just in case you like it: np.zeros((0, 0)), is a bit shorter ;).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I prefer to keep np.array([]) as it shows more clearly that the array is empty, compared to np.zeros((0, 0)).

@seberg

seberg commented Oct 9, 2013

Copy link
Copy Markdown
Member

Anyway, great, thanks! Will just wait a bit with merging in case someone thinks changing the 0-d case should be done more gentle, or has some other comment.

@Nodd

Nodd commented Oct 10, 2013

Copy link
Copy Markdown
Contributor Author

Should I merge these commits into a big one ?

@seberg

seberg commented Oct 10, 2013

Copy link
Copy Markdown
Member

Squashing away the back and forth edits and tiny fixups would make sense. Whatever you feel sensible :).

@juliantaylor

Copy link
Copy Markdown
Contributor

note pull #3864 probably conflicts with this one, it would also be great if you can help out reviewing it.

@seberg

seberg commented Oct 15, 2013

Copy link
Copy Markdown
Member

Sorry to bother, but where you planning on squashing it? Otherwise I will just put it in or maybe squash myself. Thanks a lot for all the patience fixing the the 0-size thing too :)!

@Nodd

Nodd commented Oct 15, 2013

Copy link
Copy Markdown
Contributor Author

Oh, sorry, I forgot about that. I think it's better if you squash it yourself or put it as it is (I'm still learning about git rebase...).

I use numpy everyday at work and sometimes at home, I'm glad to have contributed to such a great library !

@seberg

seberg commented Oct 15, 2013

Copy link
Copy Markdown
Member

Actually it is a bit simpler for you, since we cannot edit PRs directly. Rebasing is quite simple. If you just want to squash it together into a single commit, you can do:

  1. git rebase -i HEAD~9. You will be given a text editor to decide what to do with the last 9 commits.
  2. You can replace the pick of the first with r for reword (or just leave pick and ammend it later)
  3. Replace all following pick with f for fixup (means the changes will just be added to the first one and the commit message be discarted)
  4. Safe and close. If you used r for the first, you should get a prompt for the new commit message. If you did not, just use git commit -a --amend to amend it.
  5. git push -f to update here.

(See also https://help.github.com/articles/interactive-rebase)

@rgommers

Copy link
Copy Markdown
Member

@seberg you can squash, rebase and push (with no-ff in case of multiple commits) and then link to this PR in the merge commit message, that works as well.

@seberg

seberg commented Oct 15, 2013

Copy link
Copy Markdown
Member

yes, will do. I should learn that too...

@seberg seberg closed this in 84c52d8 Oct 16, 2013
@seberg

seberg commented Oct 16, 2013

Copy link
Copy Markdown
Member

Thanks again, pushed in 84c52d8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants