Skip to content

BUG: np.random.zipf hangs the interpreter on pathological input #9829

Closed

Description

@eric-wieser
>>> np.random.zipf(np.nan) 
# hangs forever, doesn't respond to ctrl C
>>> np.random.zipf(1.0000000000001)
# hangs for a possibly finite but very long time, also not responding to Ctrl+C

The former should probably just throw an exception. The latter is sort of inevitable with a rejection algorithm, but if we called PyErr_CheckSignals() then at least it could be cancelled.

Activity

  1. eric-wieser commented on Nov 20, 2017

    @eric-wieser
    MemberAuthor

    It was fixed for Nan, but the problem remains for numbers close to 1

  2. toslunar commented on Sep 12, 2018

    @toslunar
    Contributor

    While the nan case has been fixed, the problem remains for large a.

    >>> np.__version__
    '1.16.0.dev0+99f7ad5'
    >>> np.random.zipf(10000)
    # hangs
    

    This is probably because the following code overflows.

        am1 = a - 1.0;
        b = pow(2.0, am1);
    
  3. bashtage commented on Apr 10, 2019

    @bashtage
    Contributor
    >>> np.random.zipf(1.0000000000001)
    # hangs for a possibly finite but very long time, also not responding to Ctrl+C
    

    This doesn't hand forever since there is a positive probability of drawing a value of 0.0, so that 1.0-draw is 1 and the rejection sampler will always exit. It may take an extraordinarily long time (1 in 2**53), but not a bug.

  4. charris commented on Apr 11, 2019

    @charris
    Member

    I think it is a bug. I think we decided that it reasonable to raise an error for parameters excessively close to 1. Note that this problem can be fixed by using a truncated part of the distribution and rewriting the majorizing distribution so that it succeeds more often, easy speedups of 1000x or more follow. See discussion at #9824. @bashtage The new randomgen should be fixed to handle this better.

  5. mattip commented on Apr 11, 2019

    @mattip
    Member

    xref #13164 and #13163

  6. vks commented on Aug 2, 2021

    @vks

    I think the biggest problem is that small a are very likely to result in an infinite X, which is always rejected. Returning RAND_INT_MAX instead would fix this issue at the cost of introducing a bias.

  7. charris commented on Aug 2, 2021

    @charris
    Member

    which is always rejected

    Yes. I note that other versions allow setting the maximum via an argument, which is probably the best way to do it.

  8. added a commit that references this issue on Jul 26, 2024
    f5a5e04
  9. added a commit that references this issue on Nov 1, 2024
    3abed14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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