question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Beta random number generator can produce values outside its domain

See original GitHub issue

The 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:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
WarrenWeckessercommented, May 14, 2020

There are a lot more exact 1s than exact 0s, but with a==b, the two ends ought to be symmetric.

Could that be because there are more floating point numbers near 0 than there are near 1?

1reaction
lucianopazcommented, May 16, 2020

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found