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

Bound on absolute tolerance 'xtol' in 'optimize/zeros.py' is too strict or not properly documented #14059

Closed
miccoli opened this issue May 16, 2021 · 0 comments · Fixed by #17666
Labels
Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org scipy.optimize
Milestone

Comments

@miccoli
Copy link

miccoli commented May 16, 2021

A number of root finding functions do not accept xtol = 0 although the docs clearly state that 'The parameter must be nonnegative.'

I had no time to thoroughly inspect the code, but I suspect that imposing xtol > 0 (instead of xtol >= 0) is overzealous and I would classify this issue as a bug.

Alternatively if erroring when xtol == 0 is strictly intended behaviour, the docs should read "The parameter must be positive."

In both cases I can provide a PR.

Reproducing code example:

import scipy.optimize
f = lambda x: x - 1

scipy.optimize.root_scalar(f, method='bisect', xtol=0, bracket=[0, 2])
scipy.optimize.root_scalar(f, method='ridder', xtol=0, bracket=[0, 2])
scipy.optimize.root_scalar(f, method='brentq', xtol=0, bracket=[0, 2])
scipy.optimize.root_scalar(f, method='brenth', xtol=0, bracket=[0, 2])
scipy.optimize.root_scalar(f, method='toms748', xtol=0, bracket=[0, 2])

Error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../lib/python3.8/site-packages/scipy/optimize/_root_scalar.py", line 249, in root_scalar
    r, sol = methodc(f, a, b, args=args, **kwargs)
  File ".../lib/python3.8/site-packages/scipy/optimize/zeros.py", line 546, in bisect
    raise ValueError("xtol too small (%g <= 0)" % xtol)
ValueError: xtol too small (0 <= 0)

Scipy/Numpy/Python version information:

>>> import sys, scipy, numpy; print(scipy.__version__, numpy.__version__, sys.version_info)
1.6.3 1.18.5 sys.version_info(major=3, minor=8, micro=7, releaselevel='final', serial=0)

Code reference

Culprit for method = bisect

xtol : number, optional
The computed root ``x0`` will satisfy ``np.allclose(x, x0,
atol=xtol, rtol=rtol)``, where ``x`` is the exact root. The
parameter must be nonnegative.

if xtol <= 0:
raise ValueError("xtol too small (%g <= 0)" % xtol)

etc.

@AtsushiSakai AtsushiSakai added scipy.optimize Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org labels May 20, 2021
@tupui tupui added this to the 1.11.0 milestone Dec 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org scipy.optimize
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants