Skip to content

Check sum is 1 using np.isclose instead of == in powerdiscrepancy #8558

Closed

Description

@yoavram

powerdiscrepancy checks if sum of expected equals (==) to n or 1, but should probably check if the sum is np.isclose or np.allclose to avoid throwing an error (see below) when the sum is 0.9999999.

Error thrown is:

ValueError: observed and expected need to have the same number of observations, or e needs to add to 1

from line 153 of statsmodels/stats/gof.py

I changed the code in line 147 to
if np.allclose(np.sum(e, axis=axis), n):
and in line 149 to
elif np.allclose(np.sum(e, axis=axis), 1):
and then it works fine.

Activity

  1. added this to the 0.14 milestone on Dec 6, 2022
  2. josef-pkt commented on Dec 8, 2022

    @josef-pkt
    Member

    scipy uses rtol=1e-8 on powerdiscrepancy/chisquare

            rtol = 1e-8  # to pass existing tests
            with np.errstate(invalid='ignore'):
                f_obs_sum = f_obs_float.sum(axis=axis)
                f_exp_sum = f_exp.sum(axis=axis)
                relative_diff = (np.abs(f_obs_sum - f_exp_sum) /
                                 np.minimum(f_obs_sum, f_exp_sum))
                diff_gt_tol = (relative_diff > rtol).any()
    
  3. josef-pkt commented on Dec 9, 2022

    @josef-pkt
    Member

    fixed in #8567

    @yoavram Thanks for the issue

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions