Skip to content

Commit

Permalink
Merge pull request scipy#5784 from nmayorov/ls_tol
Browse files Browse the repository at this point in the history
MAINT: Change default tolerances of least_squares to 1e-8
  • Loading branch information
dlax committed Jan 31, 2016
2 parents ec7cd5b + 5aa2a46 commit a6d44c1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
4 changes: 2 additions & 2 deletions doc/source/tutorial/optimize.rst
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,9 @@ finally plots the original data and the fitted model function:
>>> x0 = np.array([2.5, 3.9, 4.15, 3.9])
>>> res = least_squares(fun, x0, jac=jac, bounds=(0, 100), args=(u, y), verbose=1)
`ftol` termination condition is satisfied.
Function evaluations 129, initial cost 4.4383e+00, final cost 1.5375e-04, first-order optimality 1.41e-05.
Function evaluations 130, initial cost 4.4383e+00, final cost 1.5375e-04, first-order optimality 4.92e-08.
>>> res.x
array([ 0.19280849, 0.19124746, 0.12304994, 0.13604619])
array([ 0.19280596, 0.19130423, 0.12306063, 0.13607247])

>>> import matplotlib.pyplot as plt
>>> u_test = np.linspace(0, 5)
Expand Down
29 changes: 13 additions & 16 deletions scipy/optimize/_lsq/least_squares.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,9 @@ def loss_function(f, cost_only=False):

def least_squares(
fun, x0, jac='2-point', bounds=(-np.inf, np.inf), method='trf',
ftol=EPS**0.5, xtol=EPS**0.5, gtol=EPS**0.5, x_scale=1.0,
loss='linear', f_scale=1.0, diff_step=None, tr_solver=None,
tr_options={}, jac_sparsity=None, max_nfev=None, verbose=0, args=(),
kwargs={}):
ftol=1e-8, xtol=1e-8, gtol=1e-8, x_scale=1.0, loss='linear',
f_scale=1.0, diff_step=None, tr_solver=None, tr_options={},
jac_sparsity=None, max_nfev=None, verbose=0, args=(), kwargs={}):
"""Solve a nonlinear least-squares problem with bounds on the variables.
Given the residuals f(x) (an m-dimensional function of n variables) and
Expand Down Expand Up @@ -292,24 +291,22 @@ def least_squares(
Default is 'trf'. See Notes for more information.
ftol : float, optional
Tolerance for termination by the change of the cost function.
Default is the square root of machine epsilon. The optimization process
is stopped when ``dF < ftol * F``, and there was an adequate agreement
between a local quadratic model and the true model in the last step.
Tolerance for termination by the change of the cost function. Default
is 1e-8. The optimization process is stopped when ``dF < ftol * F``,
and there was an adequate agreement between a local quadratic model and
the true model in the last step.
xtol : float, optional
Tolerance for termination by the change of the independent variables.
Default is the square root of machine epsilon. The exact condition
checked depends on the `method` used:
Default is 1e-8. The exact condition depends on the `method` used:
* For 'trf' and 'dogbox' : ``norm(dx) < xtol * (xtol + norm(x))``
* For 'lm' : ``Delta < xtol * norm(xs)``, where ``Delta`` is
a trust-region radius and ``xs`` is the value of ``x``
scaled according to `x_scale` parameter (see below).
gtol : float, optional
Tolerance for termination by the norm of the gradient. Default is
the square root of machine epsilon. The exact condition depends
on a `method` used:
Tolerance for termination by the norm of the gradient. Default is 1e-8.
The exact condition depends on a `method` used:
* For 'trf' : ``norm(g_scaled, ord=np.inf) < gtol``, where
``g_scaled`` is the value of the gradient scaled to account for
Expand Down Expand Up @@ -578,9 +575,9 @@ def least_squares(
>>> res_1.x
array([ 1., 1.])
>>> res_1.cost
2.4651903288156619e-30
9.8669242910846867e-30
>>> res_1.optimality
4.4408921315878507e-14
8.8928864934219529e-14
We now constrain the variables, in such a way that the previous solution
becomes infeasible. Specifically, we require that ``x[1] >= 1.5``, and
Expand Down Expand Up @@ -636,7 +633,7 @@ def least_squares(
>>> res_3 = least_squares(fun_broyden, x0_broyden,
... jac_sparsity=sparsity_broyden(n))
>>> res_3.cost
4.5687161966109073e-23
4.5687069299604613e-23
>>> res_3.optimality
1.1650454296851518e-11
Expand Down

0 comments on commit a6d44c1

Please sign in to comment.