Skip to content

Commit

Permalink
Merge pull request scipy#5939 from andyfaff/gh4484
Browse files Browse the repository at this point in the history
DOC: fix optimize.fmin convergence docstring
  • Loading branch information
dlax committed Mar 21, 2016
2 parents 05dc7b8 + f17a463 commit e1aec61
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
7 changes: 5 additions & 2 deletions scipy/optimize/_minimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,12 @@ def minimize(fun, x0, args=(), method=None, jac=None, hess=None,
# set default tolerances
if tol is not None:
options = dict(options)
if meth in ['nelder-mead', 'newton-cg', 'powell', 'tnc']:
if meth == 'nelder-mead':
options.setdefault('xatol', tol)
options.setdefault('fatol', tol)
if meth in ['newton-cg', 'powell', 'tnc']:
options.setdefault('xtol', tol)
if meth in ['nelder-mead', 'powell', 'l-bfgs-b', 'tnc', 'slsqp']:
if meth in ['powell', 'l-bfgs-b', 'tnc', 'slsqp']:
options.setdefault('ftol', tol)
if meth in ['bfgs', 'cg', 'l-bfgs-b', 'tnc', 'dogleg', 'trust-ncg']:
options.setdefault('gtol', tol)
Expand Down
55 changes: 40 additions & 15 deletions scipy/optimize/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,11 @@ def fmin(func, x0, args=(), xtol=1e-4, ftol=1e-4, maxiter=None, maxfun=None,
args : tuple, optional
Extra arguments passed to func, i.e. ``f(x,*args)``.
xtol : float, optional
Relative error in xopt acceptable for convergence.
Absolute error in xopt between iterations that is acceptable for
convergence.
ftol : number, optional
Relative error in func(xopt) acceptable for convergence.
Absolute error in func(xopt) between iterations that is acceptable for
convergence.
maxiter : int, optional
Maximum number of iterations to perform.
maxfun : number, optional
Expand Down Expand Up @@ -365,7 +367,8 @@ def fmin(func, x0, args=(), xtol=1e-4, ftol=1e-4, maxiter=None, maxfun=None,
performance in high-dimensional problems and is not robust to
minimizing complicated functions. Additionally, there currently is no
complete theory describing when the algorithm will successfully
converge to the minimum, or how fast it will if it does.
converge to the minimum, or how fast it will if it does. Both the ftol and
xtol criteria must be met for convergence.
References
----------
Expand All @@ -379,8 +382,8 @@ def fmin(func, x0, args=(), xtol=1e-4, ftol=1e-4, maxiter=None, maxfun=None,
Harlow, UK, pp. 191-208.
"""
opts = {'xtol': xtol,
'ftol': ftol,
opts = {'xatol': xtol,
'fatol': ftol,
'maxiter': maxiter,
'maxfev': maxfun,
'disp': disp,
Expand All @@ -401,9 +404,9 @@ def fmin(func, x0, args=(), xtol=1e-4, ftol=1e-4, maxiter=None, maxfun=None,


def _minimize_neldermead(func, x0, args=(), callback=None,
xtol=1e-4, ftol=1e-4, maxiter=None, maxfev=None,
disp=False, return_all=False, initial_simplex=None,
**unknown_options):
maxiter=None, maxfev=None, disp=False,
return_all=False, initial_simplex=None,
xatol=1e-4, fatol=1e-4, **unknown_options):
"""
Minimization of scalar function of one or more variables using the
Nelder-Mead algorithm.
Expand All @@ -412,10 +415,6 @@ def _minimize_neldermead(func, x0, args=(), callback=None,
-------
disp : bool
Set to True to print convergence messages.
xtol : float
Relative error in solution `xopt` acceptable for convergence.
ftol : float
Relative error in ``fun(xopt)`` acceptable for convergence.
maxiter : int
Maximum number of iterations to perform.
maxfev : int
Expand All @@ -425,8 +424,34 @@ def _minimize_neldermead(func, x0, args=(), callback=None,
``initial_simplex[j,:]`` should contain the coordinates of
the j-th vertex of the ``N+1`` vertices in the simplex, where
``N`` is the dimension.
xatol : float, optional
Absolute error in xopt between iterations that is acceptable for
convergence.
fatol : number, optional
Absolute error in func(xopt) between iterations that is acceptable for
convergence.
"""
if 'ftol' in unknown_options:
warnings.warn("ftol is deprecated for Nelder-Mead,"
" use fatol instead. If you specified both, only"
" fatol is used.",
DeprecationWarning)
if (np.isclose(fatol, 1e-4) and
not np.isclose(unknown_options['ftol'], 1e-4)):
# only ftol was probably specified, use it.
fatol = unknown_options['ftol']
unknown_options.pop('ftol')
if 'xtol' in unknown_options:
warnings.warn("xtol is deprecated for Nelder-Mead,"
" use xatol instead. If you specified both, only"
" xatol is used.",
DeprecationWarning)
if (np.isclose(xatol, 1e-4) and
not np.isclose(unknown_options['xtol'], 1e-4)):
# only xtol was probably specified, use it.
xatol = unknown_options['xtol']
unknown_options.pop('xtol')

_check_unknown_options(unknown_options)
maxfun = maxfev
retall = return_all
Expand Down Expand Up @@ -484,8 +509,8 @@ def _minimize_neldermead(func, x0, args=(), callback=None,
iterations = 1

while (fcalls[0] < maxfun and iterations < maxiter):
if (numpy.max(numpy.ravel(numpy.abs(sim[1:] - sim[0]))) <= xtol and
numpy.max(numpy.abs(fsim[0] - fsim[1:])) <= ftol):
if (numpy.max(numpy.ravel(numpy.abs(sim[1:] - sim[0]))) <= xatol and
numpy.max(numpy.abs(fsim[0] - fsim[1:])) <= fatol):
break

xbar = numpy.add.reduce(sim[:-1], 0) / N
Expand Down
14 changes: 13 additions & 1 deletion scipy/optimize/tests/test_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import numpy as np
from numpy.testing import (assert_raises, assert_allclose, assert_equal,
assert_, TestCase, run_module_suite, dec,
assert_almost_equal)
assert_almost_equal, assert_warns)

from scipy._lib._testutils import suppressed_stdout
from scipy import optimize
Expand Down Expand Up @@ -420,6 +420,18 @@ def test_ncg_hessp(self):
atol=1e-6, rtol=1e-7)


def test_neldermead_xatol_fatol():
# gh4484
# test we can call with fatol, xatol specified
func = lambda x: x[0]**2 + x[1]**2

optimize._minimize._minimize_neldermead(func, [1, 1], maxiter=2,
xatol=1e-3, fatol=1e-3)
assert_warns(DeprecationWarning,
optimize._minimize._minimize_neldermead,
func, [1, 1], xtol=1e-3, ftol=1e-3, maxiter=2)


class TestOptimizeWrapperDisp(CheckOptimizeParameterized):
use_wrapper = True
disp = True
Expand Down

0 comments on commit e1aec61

Please sign in to comment.