Skip to content

BUG: Fix bounds checking for random.logseries#22450

Merged
charris merged 1 commit intonumpy:mainfrom
seberg:logseries-bounds
Oct 24, 2022
Merged

BUG: Fix bounds checking for random.logseries#22450
charris merged 1 commit intonumpy:mainfrom
seberg:logseries-bounds

Conversation

@seberg
Copy link
Copy Markdown
Member

@seberg seberg commented Oct 18, 2022

Logseries previously did not enforce bounds to be strictly exclusive. This leads to incorrect behavior for 0 and 1. Add a new path to implement the correct bounds-checking.

The NOT_NAN check is removed, since it was never used: The current bounded version always excludes NaNs.

@seberg seberg added component: numpy.random 09 - Backport-Candidate PRs tagged should be backported labels Oct 18, 2022
@seberg seberg changed the title BUG: Fix boundschecking for logseries BUG: Fix boundschecking for random.logseries Oct 18, 2022
@WarrenWeckesser
Copy link
Copy Markdown
Member

WarrenWeckesser commented Oct 18, 2022

I agree that p=1 should not be allowed, but the current behavior with p=0 looks correct to me. The limit of the distribution is well-defined for p → 0: it is the trivial distribution with support {1}. And it looks like the code currently handles that case correctly. I think it would be OK to continue handling this edge case.

Logseries previously did not enforce bounds to be strictly exclusive
for the upper bound, where it leads to incorrect behavior.

The NOT_NAN check is removed, since it was never used: The current bounded
version always excludes NaNs.
@seberg
Copy link
Copy Markdown
Member Author

seberg commented Oct 18, 2022

Yeah, it does. I was unsure about that, since technically the distribution is not defined. But with float inaccuracies, I guess it is more pragmatic to just allow it in either case.

Anyway changed, will not have an array fast-path function now, but I suspect that this is OK for logseries.

Copy link
Copy Markdown
Member

@WarrenWeckesser WarrenWeckesser left a comment

Choose a reason for hiding this comment

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

Looks good to me, let's see if @bashtage has any suggestions.

Copy link
Copy Markdown
Contributor

@bashtage bashtage left a comment

Choose a reason for hiding this comment

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

I think the mtrand changes are outside of the allowed scope per NEP19.

----------
p : float or array_like of floats
Shape parameter for the distribution. Must be in the range (0, 1).
Shape parameter for the distribution. Must be in the range [0, 1).
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not 100% sure that this change is allowed. I think this would be either an enhancement or a bug fix, and IIRC the NEP says that these should not be applied to mtrand.

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.

Yes, that seems like a reasonable take.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is only a doc update? There is no change in behavior. 0 always worked, as pointed out by Warren, 1 never worked it is just explicit now.

@bashtage
Copy link
Copy Markdown
Contributor

bashtage commented Oct 18, 2022

Does the generator behave sensibly in a neighborhood of 0+? Or is 0 a special case and other tiny numbers are less reliable?

@seberg
Copy link
Copy Markdown
Member Author

seberg commented Oct 19, 2022

I have not spelled it out, but the path for 1 hangs, so 1 was never a valid input. Changing 0 to be excluded would indeed be a change that may be covered by the NEP.
Other tiny numbers work just as well (they always return 1 immediately), I am not sure if there is a loss of precision close to 1, but in that case I still guess this is right and that would be a different enhancement.

@charris charris merged commit 8d61ebc into numpy:main Oct 24, 2022
@charris
Copy link
Copy Markdown
Member

charris commented Oct 24, 2022

Thanks Sebastian.

@seberg seberg deleted the logseries-bounds branch October 25, 2022 06:46
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Nov 15, 2022
@charris charris changed the title BUG: Fix boundschecking for random.logseries BUG: Fix bounds checking for random.logseries Nov 19, 2022
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.

4 participants