GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BUG: Fix np.quantile([0, 1], 0, method='weibull') by eendebakpt · Pull Request #24710 · numpy/numpy

Conversation

@eendebakpt

@eendebakpt eendebakpt commented Sep 14, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Issue #24592 was opened because the np.quantile method gives incorrect answers for the weibull method. The problem:

import numpy as np
x=np.arange(10)
np.quantile(x, 0., method='weibull') # output 0.0, correct
np.quantile(x, 0, method='weibull') # output 9, incorrect!
np.quantile(x, 1, method='weibull') # raises an error

The value of np.quantile(x, q=0, method='weibull') is incorrect for integer inputs. Two options to address this:

  1. Do not allow integer input for np.quantile. The documentation for the probability argument states that "q : array_like of float", so this seems reasonable. But this would be a behaviour change that might have a large impact and there are unit tests that test for integer input as well.

  2. Automatically cast the input argument q to float. Also this would be a behaviour change and break existing tests.

We rejected these two options because of the issues mentioned. Applying either one of the options only for weibull would make the input handling less uniform.

The problem with weibull (and some other methods) is that inside np.lib._function_base_impl._quantile there are two paths: one for integer virtual indices and one for other types of virtual indices (in the code the statement if np.issubdtype(virtual_indexes.dtype, np.integer):. The integer path is only valid for methods returning integer indices in the range [0, size(arr)>. For the weibull method something like q = [0, .5, 1] is cast to float, which results in float virtual_indices (which contains floats with integer values, but that is okay).

We can either:

i) Modify the code in the integer path to handle integers < 0 and > size(arr) -1
ii) Check whether the methods supports integer output, and if not use the alternative path

We opted for the second option.

Fixes #24592

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

seberg reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@eendebakpt eendebakpt changed the title Draft: BUG: Fix np.quantile([0, 1], 0, method='weibull') BUG: Fix np.quantile([0, 1], 0, method='weibull') Sep 14, 2023
seberg
seberg reviewed Sep 19, 2023
View reviewed changes
Comment thread numpy/lib/_function_base_impl.py Outdated
supports_integers = supports_integers or \
(method == 'linear' and int_virtual_indices)

if supports_integers:

@seberg seberg Sep 19, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I have to think about it a tiny bit more maybe. We end up with integers here due to integer input. I suspect we had discussed this, but I am not sure we got it right: I wonder if we shouldn't just use method_props["fix_gamma"] is None.

That would change the odd exception method == 'linear' with 0 or 1 as input. But I rather prefer the clarity that the result type depends (mainly) on the method and not on some (very special!) values.

(Others may have disagreed though, arguing that for 0 and 1 the answer is clearly exact for the linear method.)

I like the fix_gamma to None refactor, though. And I am not convinced that a / 1 to the virtual index calculation (or so... which would also be plausible) would be a better fix.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@eendebakpt eendebakpt Sep 19, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The type indeed depends on the input values: np.quantile([0,1,2,3,],[0,1]) results in integer type, while np.quantile([0,1,2,3,],[0,.1]) is float.

The reason for making an exception for method == 'linear' is to allow for tf_quant = np.quantile(True, False). That case is in the unit tests, so I added the special case to keep supporting it.

It seems very odd to run quantile on boolean types though....

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@seberg seberg Jan 30, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Ah, I still agree with my above comment: linear interpolation should always interpolate to ensure the right result dtype, IMO. Even if the user passed in 0 or 1 to the quantile.
(Assuming this is what is going on, because I didn't think about it very long now.)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@eendebakpt eendebakpt Jan 31, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

@seberg Always interpolating for method 'linear' indeed seems the right thing to do. I do not expect any performance loss in the general case (where the quantiles argument will have floating values and not just 0 and 1). On the other hand:

  • The original behaviour of the linear method was to sometimes take the integer path. This makes no difference for common types like float64, but might for other input types
  • Even if I modify the code to always do interpolation for method linear, then np.quantile([1,2,1,2,1,2], [0, 1]) results in array([1, 2]) (so integer type), while np.quantile([1,2,1,2,1,2], [0.5]) results in array([1.5]) (floating type). So always running the interpolation is no guarantee we end up with the same dtype (even for the same dtype input). For the linear method this could be solved by replacing 1 with 1. somewhere in the code. But again: that works fine for the normal input types (float32, float64), but might change behavior for other types.
  • Forcing linear to always do the interpolation breaks existing code. In particular np.quantile([True], [False]) fails because TypeError: numpy boolean subtract, the - operator is not supported, use .... How to deal with this? We could just drop support for any input types that do not support subtract (for the linear method). Another option would be to decide whether to take the integer path based on the input dtype. So dtype=boolean: use the integer path, dtype=float64: do not use the input path. The latter option does not seem very elegant though, I would not want to have a big table deciding how to handle different input dtypes.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@eendebakpt
eendebakpt requested a review from seberg October 13, 2023 12:25
@eendebakpt eendebakpt mentioned this pull request Oct 13, 2023
BUG: quantile 1 with 'weibull' gives confusing error #24592
Closed
@anirudh-hegde

anirudh-hegde commented Oct 13, 2023

Copy link
Copy Markdown

This error is raised because you're trying to calculate the 100% quantile (q=1) on an array of size 10. However, this results in a problem when the method tries to find the "10th" element in an array that only has indices 0-9.

np.quantile(x, 1.0, method='weibull') #use1.0 instead of 1
output: 9.0

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eendebakpt

eendebakpt commented Oct 28, 2023

Copy link
Copy Markdown
Contributor Author

This error is raised because you're trying to calculate the 100% quantile (q=1) on an array of size 10. However, this results in a problem when the method tries to find the "10th" element in an array that only has indices 0-9.

np.quantile(x, 1.0, method='weibull') #use1.0 instead of 1 output: 9.0

Your observation is correct, with value 1.0 the result is good. I think we should either disallow integer input (e.g. raise an error on `np.quantile(x, 1, method='weibull') or return the correct result. In this PR we modify the implementation to return the correct result.

anirudh-hegde reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eendebakpt
eendebakpt force-pushed the weibull branch from 316523d to 3512830 Compare December 1, 2023 19:55
seberg
seberg reviewed Jan 30, 2024
View reviewed changes
Comment thread numpy/lib/_function_base_impl.py Outdated
if np.issubdtype(virtual_indexes.dtype, np.integer):
int_virtual_indices = np.issubdtype(virtual_indexes.dtype, np.integer)
supports_integers = method_props["fix_gamma"] is None
supports_integers = supports_integers or \

@seberg seberg Jan 30, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This PR needs a rebase unfortunately. Just commenting here to nitpick that I would prefer no \ continuation :).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@eendebakpt eendebakpt Jan 31, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Did a rebase (with force push). The \ was there to avoid a line too long, I rewrote the style to make it fit.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@seberg seberg Jan 31, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks, the other solution (with the exception of with statements) is to just use parantheses (...) which allow spanning multiple lines :).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@eendebakpt
BUG: Fix np.quantile([0, 1], 0, method='weibull')
675ad95
@eendebakpt
eendebakpt force-pushed the weibull branch from 3512830 to 675ad95 Compare January 31, 2024 12:24
seberg
seberg reviewed Jan 31, 2024
View reviewed changes
Comment thread numpy/lib/_function_base_impl.py Outdated
virtual_indexes = np.asanyarray(virtual_indexes)

if np.issubdtype(virtual_indexes.dtype, np.integer):
int_virtual_indices = np.issubdtype(virtual_indexes.dtype, np.integer)

@seberg seberg Jan 31, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Suggested change
int_virtual_indices = np.issubdtype(virtual_indexes.dtype, np.integer)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@seberg seberg Jan 31, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Sorry, I thought you didn't use it at all?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
eendebakpt added 2 commits January 31, 2024 13:35
@eendebakpt
lint
7187c11
@eendebakpt
lint
f19950e
@charris charris added 00 - Bug 09 - Backport-Candidate PRs tagged should be backported labels Jan 31, 2024
@charris

charris commented Feb 5, 2024

Copy link
Copy Markdown
Member

close/reopen

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@charris charris closed this Feb 5, 2024
@charris charris reopened this Feb 5, 2024
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Feb 5, 2024
@seberg
seberg merged commit 8fe55b0 into numpy:main Feb 11, 2024
@seberg

seberg commented Feb 11, 2024

Copy link
Copy Markdown
Member

Thanks, FWIW, I still don't like the special case, and I would probably prefer to just change the test and disallow act is if q was a raction (i.e. you could say as if q / 1 was calculated).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

Reviewers

@seberg seberg seberg left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

00 - Bug

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

BUG: quantile 1 with 'weibull' gives confusing error

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@eendebakpt @anirudh-hegde @charris @seberg
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.