Skip to content
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

equality used to compare floating point numbers (test_bootstrap_alternative) #20043

Closed
drew-parsons opened this issue Feb 7, 2024 · 2 comments · Fixed by #20047
Closed

equality used to compare floating point numbers (test_bootstrap_alternative) #20043

drew-parsons opened this issue Feb 7, 2024 · 2 comments · Fixed by #20047
Labels
maintenance Items related to regular maintenance tasks scipy.stats task A straightforward change, verification or fix.
Milestone

Comments

@drew-parsons
Copy link
Contributor

Values calculated by the bootstrap method are tested at

assert_equal(l.confidence_interval.high, t.confidence_interval.high)

But the test uses equality to compare the actual and desired values. This inevitably fails for floating point numbers, for instance on i386 (debian build tests on scipy v1.11.1)

______________________ test_bootstrap_alternative[basic] _______________________

method = 'basic'

    @pytest.mark.parametrize("method", ['basic', 'percentile', 'BCa'])
    def test_bootstrap_alternative(method):
        rng = np.random.default_rng(5894822712842015040)
        dist = stats.norm(loc=2, scale=4)
        data = (dist.rvs(size=(100), random_state=rng),)
    
        config = dict(data=data, statistic=np.std, random_state=rng, axis=-1)
        t = stats.bootstrap(**config, confidence_level=0.9)
    
        config.update(dict(n_resamples=0, bootstrap_result=t))
        l = stats.bootstrap(**config, confidence_level=0.95, alternative='less')
        g = stats.bootstrap(**config, confidence_level=0.95, alternative='greater')
    
>       assert_equal(l.confidence_interval.high, t.confidence_interval.high)
E       AssertionError: 
E       Items are not equal:
E        ACTUAL: 4.6006151751157525
E        DESIRED: 4.600615175115755

config     = {'axis': -1, 'bootstrap_result': BootstrapResult(confidence_interval=ConfidenceInterval(low=3.6279404186700406, high=4...382229, -0.1082399 ,
        0.70475099, 10.78761938,  1.05744104,  8.45953715,  3.85913619]),), 'n_resamples': 0, ...}
data       = (array([ 2.62480303,  0.94487726,  3.91730505,  5.3708377 ,  5.43231996,
        9.19056041,  3.97648906,  3.98515237,...5813,  1.05203055, -0.55382229, -0.1082399 ,
        0.70475099, 10.78761938,  1.05744104,  8.45953715,  3.85913619]),)
dist       = <scipy.stats._distn_infrastructure.rv_continuous_frozen object at 0xd1c16300>
g          = BootstrapResult(confidence_interval=ConfidenceInterval(low=3.627940418670041, high=array(inf)), bootstrap_distribution....90581787, 4.42585593, 4.45999832, ..., 4.03392801, 3.98546718,
       4.18018658]), standard_error=0.2937303808414012)
l          = BootstrapResult(confidence_interval=ConfidenceInterval(low=array(-inf), high=4.6006151751157525), bootstrap_distributi....90581787, 4.42585593, 4.45999832, ..., 4.03392801, 3.98546718,
       4.18018658]), standard_error=0.2937303808414012)
method     = 'basic'
rng        = Generator(PCG64) at 0xD1ED3398
t          = BootstrapResult(confidence_interval=ConfidenceInterval(low=3.6279404186700406, high=4.600615175115755), bootstrap_dist....90581787, 4.42585593, 4.45999832, ..., 4.03392801, 3.98546718,
       4.18018658]), standard_error=0.2937303808414012)

scipy/stats/tests/test_resampling.py:538: AssertionError

The same error occurs with other test_bootstrap_alternative methods, [percentile] and [BCa]. Full log at https://buildd.debian.org/status/fetch.php?pkg=scipy&arch=i386&ver=1.11.1-1exp1&stamp=1707086250&raw=0

The values reported in the error log would pass with assert_allclose instead of assert_equal

@j-bowhay j-bowhay added scipy.stats task A straightforward change, verification or fix. maintenance Items related to regular maintenance tasks labels Feb 7, 2024
@mdhaber
Copy link
Contributor

mdhaber commented Feb 8, 2024

Typically we don't make changes to ensure that tests pass on all possible platforms, but since the test already xfails on 32-bit because it is sensitive to numerical issues, it may be reasonable to change this one. If you submit a PR using assert_allclose with a tight tolerance and removing the xfail, I can merge it.

@drew-parsons
Copy link
Contributor Author

The default tolerance for assert_allclose seems to be sufficient. The patched test passes at https://buildd.debian.org/status/fetch.php?pkg=scipy&arch=i386&ver=1.11.1-1exp2&stamp=1707371799&raw=0
I'll prepare a PR.

drew-parsons added a commit to drew-parsons/scipy that referenced this issue Feb 8, 2024
assert_equal is not reliable for comparing floating point numbers
and fails on i386

Closes: scipy#20043
drew-parsons added a commit to drew-parsons/scipy that referenced this issue Feb 8, 2024
assert_equal is not reliable for comparing floating point numbers
and fails on i386

Closes: scipy#20043
@lucascolley lucascolley added this to the 1.13.0 milestone Feb 8, 2024
drew-parsons added a commit to drew-parsons/scipy that referenced this issue Feb 8, 2024
assert_equal is not reliable for comparing floating point numbers
and fails on i386.

Use a tight tolerance rtol=1e-14

Closes: scipy#20043
drew-parsons added a commit to drew-parsons/scipy that referenced this issue Feb 8, 2024
assert_equal is not reliable for comparing floating point numbers
and fails on i386.

Use a tight tolerance rtol=1e-14

Closes: scipy#20043
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Items related to regular maintenance tasks scipy.stats task A straightforward change, verification or fix.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants