You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Could random.hypergeometric() be made to match behavior of random.binomial() when sample or n = 0? #9237
I am trying to implement a multidimensional hypergeometric distribution. My algorithm would be made simpler if np.random.hypergeometric() returned zeros when sample=0, just like the np.random.binomial() does when n = 0 (instead of throwing an ValueError). Note that the binomial is often used as an approximation to the hypergeometric so getting the same behavior when swapping one for the other may be advantageous.
I think it would be a matter of 3 changes to the code base:
change Line 4254-5 of master/numpy/random/mtrand/mtrand.pyx to: if lnsample < 0: raise ValueError("nsample < 0")
Move line 794 of master/numpy/random/mtrand/distributions.c to line 791.
Add if(sample == 0) Z = 0 to rk_hypergeometric_hrua in master/numpy/random/mtrand/distributions.c --- though there might be another way to avoid an if but I don't understand how the code works well enough.
Also, as a new user, please let me know if there is a better way to ask for this change.
Activity
bashtage commentedon Jun 12, 2017
See #8056 -- this is tagged for inclusion in 1.14.
WarrenWeckesser commentedon Jun 16, 2017
This seems reasonable, but I would think so, given that my implementation of
multivariate_hypergeometricin #8056 behaves this way, e.g.:ENH: Enable 0 samples in hypergeometric
bashtage commentedon Apr 11, 2019
xref #13163 . This is fixed in the randomgen branch (subject to pending PR mattip#8)
mattip commentedon May 29, 2019
Closing. Verified that
np.random.Generator().hypergeometric(1,1,0)returns 0. Please reopen if I misunderstood.