Skip to content

BUG: weighted quantile for some zero weights #27549

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

Merged
merged 2 commits into from
Oct 14, 2024

Conversation

lorentzenchr
Copy link
Contributor

This PR fixed weighted quantiles (and percentiles) for a corner case:

  • at least one weight is zero
  • q=0 (0-quantile equals minimum)
    Then:
np.quantile(np.arange(3), 0, weights=[0, 0, 1], method="inverted_cdf")

should return 2, the minimum when neglecting zero weight values. Current main returns 0.

@lorentzenchr
Copy link
Contributor Author

lorentzenchr commented Oct 14, 2024

@seberg seberg changed the title FIX: weighted quantile for some zero weights BUG: weighted quantile for some zero weights Oct 14, 2024
@seberg
Copy link
Member

seberg commented Oct 14, 2024

Looks good, but to make sure: Do we have tests for the 0 weights being in a position other than the start? I don't think it is particularly interesting with the current code, but maybe good to add anyway.

@lorentzenchr
Copy link
Contributor Author

Do we have tests for the 0 weights being in a position other than the start?

Yes, e.g. in test_quantile_with_integer_weights.

But this is not the main point. weights is sorted according to the input array. From then on, the leading 0 in weights (and corresponding elements in the sorted input array) should be dropped.

@seberg
Copy link
Member

seberg commented Oct 14, 2024

But this is not the main point

Yes, I realize. But seemed good to cover out of principle. LGTM, then let's put it in, thanks.

@seberg seberg merged commit f35d3bd into numpy:main Oct 14, 2024
65 of 67 checks passed
@lorentzenchr lorentzenchr deleted the fix_w_quantile branch October 14, 2024 12:49
@charris charris added 00 - Bug 09 - Backport-Candidate PRs tagged should be backported labels Oct 14, 2024
charris pushed a commit to charris/numpy that referenced this pull request Oct 15, 2024
This PR fixed weighted quantiles (and percentiles) for a corner case:

* at least one weight is zero
* q=0 (0-quantile equals minimum)
Then:
```
np.quantile(np.arange(3), 0, weights=[0, 0, 1], method="inverted_cdf")
```
should return 2, the minimum when neglecting zero weight values. Current main returns 0.
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Oct 15, 2024
charris added a commit that referenced this pull request Oct 15, 2024
BUG: weighted quantile for some zero weights (#27549)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants