Skip to content

BUG: random: np.random.weibull(a=0) sometimes returns inf. #12371

Closed

Description

@WarrenWeckesser

Over in #8056, there was a test failure that appears to be a numpy bug. The test that is failing is TestRandomDist.test_weibull_0, which is:

def test_weibull_0(self):
    assert_equal(np.random.weibull(a=0), 0)
    assert_raises(ValueError, np.random.weibull, a=-0.)

The assertion assert_equal(np.random.weibull(a=0), 0) is failing, because sometimes np.random.weibull(a=0) returns inf:

In [36]: np.__version__
Out[36]: '1.15.4'

In [37]: np.random.seed(123)

In [38]: np.random.weibull(a=0, size=10)
Out[38]: array([inf,  0.,  0.,  0., inf,  0., inf, inf,  0.,  0.])

Those inf values should not be there. I get the same result with the current master branch, '1.16.0.dev0+e34f5bb'.

Activity

  1. WarrenWeckesser commented on Nov 12, 2018

    @WarrenWeckesser
    MemberAuthor

    Here's the code to generate Weibull variates:

    double rk_weibull(rk_state *state, double a)
    {
        return pow(rk_standard_exponential(state), 1./a);
    }
    

    If a is 0, then 1/a is inf. If 0 < r < 1, then r**inf is 0, but if r == 1 then r**inf is 1, and if r > 1 then r**inf is inf. Those are not correct values for this degenerate case of the Weibull distribution. The only value that should be returned by np.random.weibll(a=0) is 0.

    This could be fixed by handling a = 0 explicitly, e.g.

    double rk_weibull(rk_state *state, double a)
    {
        if (a == 0) {
            return 0.0;
        }
        return pow(rk_standard_exponential(state), 1./a);
    }
    

    (The even edgier edge case of a = -0 is handled before this C code is called.)

  2. added a commit that references this issue on Nov 12, 2018
    360de1b
  3. WarrenWeckesser commented on Nov 12, 2018

    @WarrenWeckesser
    MemberAuthor

    Pull request to fix this: #12372

  4. added a commit that references this issue on Nov 18, 2018
    fd521af
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions