Skip to content

BUG: random: Fix generation of nan by beta.#24267

Merged
charris merged 1 commit intonumpy:mainfrom
WarrenWeckesser:beta-tiny-params
Jul 26, 2023
Merged

BUG: random: Fix generation of nan by beta.#24267
charris merged 1 commit intonumpy:mainfrom
WarrenWeckesser:beta-tiny-params

Conversation

@WarrenWeckesser
Copy link
Member

@WarrenWeckesser WarrenWeckesser commented Jul 26, 2023

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.

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.
@charris charris merged commit 7e806a1 into numpy:main Jul 26, 2023
@charris
Copy link
Member

charris commented Jul 26, 2023

Thanks Warren.

@WarrenWeckesser WarrenWeckesser deleted the beta-tiny-params branch July 26, 2023 18:36
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.

BUG: random: beta can generate nan when the parameters are extremely small.

2 participants