Scipy_Minimize

class sherpa.optmethods.optscipy.Scipy_Minimize(name: str | None = None, **kwargs)[source] [edit on github]

Bases: ScipyBase

Optimizer using scipy.optimize.minimize.

See the Scipy User Guide for minimzation for details. for details on how scipy.optimize.minimize works, a short summary is below.

scipy.optimize.minimize implements several different algorithms than can be chosen with the method keyword. Each method has its own advantages and drawbacks, e.g. some only work with finite bounds or require the function to be smooth. Each method has its own set of parameters that controls the optimization process, e.g. for the step size or maximum number of iterations.

See the scipy.optimize.minimize documentation for a full list of methods and their parameters. By default, scipy.optimize.minimize will choose a method that this appropriate for the given input, but since Sherpa does not supply analytic gradients or Hessians, some methods will fail if explicitly selected.

Sherpa will automatically convert statistics functions, input values, parameter limits etc. to the format required by the scipy function. The following attributes of scipy.optimize.minimize can be set as attributes of this class.

method

The optimization method to use. See the scipy.optimize.minimize documentation for a list of available methods.

Type:

str or callable

tol

Tolerance for termination.

Type:

float

options

A dictionary of solver options.

Type:

dict

callback

A callable called after each iteration.

Type:

callable

Example

We begin with a simple example of fitting a Gaussian and a constant to some data, using all the default options for the optimizer.

>>> import numpy as np
>>> from sherpa.data import Data1D
>>> from sherpa.models import Const1D, Gauss1D
>>> from sherpa.stats import Chi2
>>> from sherpa.optmethods import Scipy_Minimize
>>> from sherpa.fit import Fit
>>> data = Data1D('data1', x=np.arange(10),
...              y=[34.5, 23.4, 22.3, 45.6, 56.7, 67.8, 58.9, 43.0, 30.1, 25.2],
...              staterror=5 * np.ones(10))
>>> const = Const1D(name='const')
>>> gauss = Gauss1D(name='gauss')
>>> gauss.pos = 5.0  # start not too far from the data
>>> model = const + gauss
>>> optimizer = Scipy_Minimize()
>>> fit = Fit(data=data, model=model, stat=Chi2(), method=optimizer)
>>> result = fit.fit()
>>> print(result)
datasets       = None
itermethodname = none
methodname     = scipy_minimize
statname       = chi2
succeeded      = True
parnames       = ('const.c0', 'gauss.fwhm', 'gauss.pos', 'gauss.ampl')
...

We can change the method used in the optimization and set some of the options for the optimizer, such as the goal for the precision and the maximum number of function evaluations before the fit stops.

>>> optimizer.method = 'TNC'
>>> optimizer.options = {'maxfun': 200, 'xtol': 1e-4}
>>> model.reset()
>>> print(fit.fit())
datasets       = None
...
message        = Converged (|x_n-x_(n-1)| ~= 0)
...

Attributes Summary

default_config

The default settings for the optimiser.

Methods Summary

fit(statfunc, pars, parmins, parmaxes[, ...])

Run the optimiser.

Attributes Documentation

default_config

The default settings for the optimiser.

Methods Documentation

fit(statfunc: Callable[[Sequence[float] | ndarray], tuple[float, ndarray]], pars: Sequence[float] | ndarray, parmins: Sequence[float] | ndarray, parmaxes: Sequence[float] | ndarray, statargs: Any | None = None, statkwargs: Any | None = None) tuple[bool, ndarray, float, str, dict[str, Any]] [edit on github]

Run the optimiser.

Changed in version 4.18.0: The statargs and statkwargs arguments are now ignored.

Changed in version 4.16.0: The statkwargs argument now defaults to None rather than {}.

Parameters:
  • statfunc (function) – Given a list of parameter values as the first argument and, as the remaining positional arguments, statargs and statkwargs as keyword arguments, return the statistic value.

  • pars (sequence) – The start position of the model parameter values.

  • parmins (sequence) – The minimum allowed values for each model parameter. This must match the length of pars.

  • parmaxes (sequence) – The maximum allowed values for each model parameter. This must match the length of pars.

  • statargs (optional) – This is currently unused.

  • statkwargs (dict, optional) – This is currently unused.

Returns:

newpars – The tuple contains: boolean indicating whether the optimization succeeded or not, the best fit parameters as a NumPy array, the statistic value at the best-fit location, a string message indicating the status, and a dictionary containing information about the optimisation (this depends on the optimiser).

Return type:

tuple