Skip to content

BUG: Better np.inf behavior for quantile and percentile calcs - #22005

Closed
evinism wants to merge 1 commit into
numpy:mainfrom
evinism:gh21932
Closed

evinism wants to merge 1 commit into
numpy:mainfrom
evinism:gh21932

Conversation

@evinism

@evinism evinism commented Jul 17, 2022

Copy link
Copy Markdown

Addresses #21932

Copies equal values directly across in _lerp

@seberg

seberg commented Jul 18, 2022

Copy link
Copy Markdown
Member

I don't think this changes behavior of np.quantile(np.array([0, np.inf]), 0.5). Is that intentional?
An interesting question for that one may be to check prior art. E.g. what does the R code do for that case? Returning inf is fairly straight forward with many interpolation formulas, I think. Just our monoticity correct interpolation does not.

@evinism

evinism commented Jul 18, 2022 •

Copy link
Copy Markdown
Author

I don't think this changes behavior of np.quantile(np.array([0, np.inf]), 0.5). Is that intentional?

Good catch! That's an oversight. I think you'd proposed it as a possible approach in #21343, but now it's clear that that doesn't quite work as anticipated.

If I look at prior art in R or Matlab, and it suggests np.quantile(np.array([0, np.inf]), 0.5) == np.inf, do you have any suggestions on approach?

@seberg

seberg commented Aug 22, 2022

Copy link
Copy Markdown
Member

Hmmm, I am not sure. I suspect that the only way is to actually check for infinite/non-finite values (and maybe a fast-path when no such value exists, since that is likely).

@seberg

seberg commented Oct 26, 2022

Copy link
Copy Markdown
Member

@evinism just a gentle ping in case you have time to look at it again (just because I stumbled on it). I suspect the overhead of adding the check is very small, so adding isfinite/isinf checks seems like the best bet to me.

An interesting angle is that C++20 actually has lerp now with compatible behavior (guarantees endpoints and monotonic behavior). I do think that is an argument for adding a lerp ufunc that we can reuse here.
However, that is of course more work and also a somewhat bigger decision.

@jorenham

jorenham commented Jan 8, 2026

Copy link
Copy Markdown
Member

This seems to have stranded, so I'm going to close it. We can re-open if you still want to work on this @evinism

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

Labels

Projects

Development

Successfully merging this pull request may close these issues.

3 participants