Skip to content

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

@wmatern

Description

@wmatern

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions