The sherpa.optmethods.optoptimagic module

Interface to optimagic optimization methods.

This module contains classes that wrap the optimization functions in optimagic to match the calling signature and return values to the Sherpa interface.

If optimagic is installed, classes are created automatically and can be used in the same way as other optimizers in Sherpa.

Optimagic supports well over 50 different algorithms

Optimagic supports several different optimizers from about a dozen different libraries, including scipy, nlopt, pygmo2, and others; most if these dependencies need to be installed separately; they will only be available to you if the respective library is installed in your Python environment.

While optimagic aims to provide a consistent interface to all of these libraries, the options that can be set for those optimizers depends on the algorithm, e.g. in some cases one may set the maximum number of iterations, while in others one may define the direction of the optimization. The optimagic documentation lists all available algorithms and their options.

There is no default algorithm, so you always need to specify the algorithm to use before you can use the optimizer in a fit:

>>> from sherpa.optmethods import Optimagic
>>> opt = Optimagic(name='my_cool_optimizer')
>>> opt.algorithm = 'scipy_ls_dogbox'

Setting additional algorithm options is supported, but not required (all options have defaults):

>>> opt.algo_options = {'stopping.maxfun': 1000}

Most inputs for optimagic are automatically filled by Sherpa

This module wraps the optimagic.minimize function in such a way that all the information that Sherpa already has about the model, the data, and the statistic is passed to the optimizer. Sherpa will automatically pick the correct format of the statistic function and convert the parameter minimum and maximum to the format for optimagic bounds.

Caveats to watch out for

Optimagic contains both local and global optimizers (see Set minimum and maximum values for parameters for a discussion). Global optimizers need all parameters to be bounded between finite values.

Sherpa never provides analytical gradients for any model (because many Sherpa models are not differentiable, e.g. use tabulated values). While optimagic can use numerical gradients, this means that much of the efficiency gain for typical gradient-based optimizers is lost.

Optimagic is designed to be fault tolerant if, e.g. some bins return NaN values, but how exactly that impacts the optimization depends on the algorithm used. If errors occur, the error from optimagic will be passed to the user, which may differ from typical Sherpa error messages.

What statistic functions are supported?

Most statistic functions return a single scalar value that is minimized by the optimizer. However, some optimizers in optimagic are designed to work with explicitly with least-squares or likelihood functions and can converge faster if the optimizer is given the full array of per-bin values. Sherpa will automatically detect if the optimizer is one of those and return the statistic values in the correct format. However, it is up to user to make sure that statistic and the optimizer are compatible. For example, one can feed a LeastSq statistic into the optimagic.algos.bhhh optimizer (which expects a likelihood), but the fit might take much longer or may not converge at all and the results could be wrong.

Further diagnostics

The last element of the optimizer output is a dictionary which holds the output from optimagic in an optimagic.optimization.optimize_result.OptimizeResult object. When used in a fit, this dictionary is stored in the extra_output field of the fit result: result.extra_output['optimagic result']. These output values can be used for example to visualize the optimization history or to benchmark different optimizers as described in the optimagic documentation.

Classes

Optimagic([name])

Optimizer using the optimagic library.

Class Inheritance Diagram

Inheritance diagram of Optimagic