Beta random number generator can produce values outside its domain
See original GitHub issueThe numpy.random.beta
function can generate samples that are either 0
or 1
, which is outside of the support of the Beta distribution.
We came across this issue over at pymc3. I started to dig into it and found that numpy’s legacy implementation can in fact produce zeros and ones if the a
and b
parameters are both smaller than 1.
I read a bit of Johnk’s method here, and it seems acceptable to discard the generated sample if it is either 0 or 1. That way, you would always end with generated random number in the open (0, 1) domain.
Reproducing code example:
>>> import numpy as np
>>> a = np.random.beta(0.01, 0.01, size=1000000)
>>> print(np.any(a == 0), np.any(a == 1))
True, True
Numpy/Python version information:
1.18.1 3.7.6 (default, Jan 8 2020, 19:59:22) [GCC 7.3.0]
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
c - Generate a random number outside of a range without a loop
This would work: int r = rand() % (maxval+1 - rangesize); if(r>=rangemin) r+=rangesize;. From your example, rangesize would be 3 since there ...
Read more >Reference - 1.79.0 - Boost C++ Libraries
This type of random-number generator is useful for security applications, where it is important to prevent an outside attacker from guessing the numbers...
Read more >Random Number Generation
When the domain is specified in terms of xmin and xmax, RandomReal and RandomInteger generate uniformly distributed numbers over the specified range.
Read more >Beta random numbers - MATLAB betarnd - MathWorks
R = betarnd(A,B) generates random numbers from the beta distribution with parameters specified by A and B . A and B can be...
Read more >Random Numbers from Functions - McCarl GAMS User Guide
The parameter RNG identifies the random number generator to use with possible values of -1 (free), 0 (system), 1 (lindo1), 2 (lindo2), 3...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Could that be because there are more floating point numbers near 0 than there are near 1?
@VikrantReddy, that’s what the PR I opened, #16231 did, it discarded the samples that were equal to zero or one, and resampled them. Unfortunately, that leads to a huge bias because there are many more values equal to one due to rounding errors than values equal to zero.