8000 Could random.hypergeometric() be made to match behavior of random.binomial() when sample or n = 0? · Issue #9237 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Could random.hypergeometric() be made to match behavior of random.binomial() when sample or n = 0? #9237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wmatern opened this issue Jun 9, 2017 · 4 comments

Comments

@wmatern
Copy link
wmatern commented Jun 9, 2017

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.

In particular:
np.random.binomial(0,.5)
Returns
0

whereas:
np.random.hypergeometric(1,1,0)
Returns
... ValueError: nsample < 1

I think it would be a matter of 3 changes to the code base:

  1. change Line 4254-5 of master/numpy/random/mtrand/mtrand.pyx to:
    if lnsample < 0: raise ValueError("nsample < 0")

  2. Move line 794 of master/numpy/random/mtrand/distributions.c to line 791.

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

Thank you

-Will

@bashtage
Copy link
Contributor

See #8056 -- this is tagged for inclusion in 1.14.

@WarrenWeckesser
Copy link
Member

This seems reasonable, but I would think so, given that my implementation of multivariate_hypergeometric in #8056 behaves this way, e.g.:

In [5]: np.random.multivariate_hypergeometric([5, 10, 15], 0)
Out[5]: array([0, 0, 0])

bashtage added a commit to bashtage/randomgen that referenced this issue Apr 10, 2019
Enable Hypergeometric to work with 0 samples

xref numpy/numpy#9237
@bashtage
Copy link
Contributor

xref #13163 . This is fixed in the randomgen branch (subject to pending PR mattip#8)

@mattip
Copy link
Member
mattip commented May 29, 2019

Closing. Verified that np.random.Generator().hypergeometric(1,1,0) returns 0. Please reopen if I misunderstood.

@mattip mattip closed this as completed May 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
0