BUG: random: Fix generation of nan by beta.#24267
Merged
charris merged 1 commit intonumpy:mainfrom Jul 26, 2023
Merged
Conversation
The implementation of Johnk's algorithm for beta(a, b) could generate nan values if both a and b were extremely small (i.e. subnormal or a small multiple of the smallest normal double precision float). The fix is to handle variate generation in this case by noting that when both a and b are extremely small, the probability of generating a double precision value that is not either 0 or 1 is also extremely small. In particular, if a and b are less than 3e-103, the probability of generating a (double precision) value that is not 0 or 1 is less than approximately 1e-100. So instead of using Johnk's algorithm in this extreme case, we can generate the values 0 or 1 as Bernoulli trials, with the probability of 1 being a/(a + b). Closes numpygh-24266.
0f7c3c8 to
a0b45c5
Compare
charris
approved these changes
Jul 26, 2023
Member
|
Thanks Warren. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The implementation of Johnk's algorithm for beta(a, b) could generate nan values if both a and b were extremely small (i.e. subnormal or a small multiple of the smallest normal double precision float).
The fix is to handle variate generation in this case by noting that when both a and b are extremely small, the probability of generating a double precision value that is not either 0 or 1 is also extremely small. In particular, if a and b are less than 3e-103, the probability of generating a (double precision) value that is not 0 or 1 is less than approximately 1e-100. So instead of using Johnk's algorithm in this extreme case, we can generate the values 0 or 1 as Bernoulli trials, with the probability of 1 being a/(a + b).
Closes gh-24266.