fitters - Wrappers for various optimization algorithms¶
BFGS quasi-newton optimizer. |
|
Periodically save fit state so that it can be resumed later. |
|
Display fit progress on the console |
|
Classic Storn and Price differential evolution optimizer. |
|
DREAM wrapper for fit problems. |
|
FitBase defines the interface from bumps models to the various fitting engines available within bumps. |
|
Levenberg-Marquardt optimizer. |
|
MPFit optimizer. |
|
Adaptor which allows solvers to accept progress monitors. |
|
Multi-start monte carlo fitter. |
|
Particle swarm optimizer. |
|
Parallel tempering optimizer. |
|
Random lines optimizer. |
|
Nelder-Mead simplex optimizer. |
|
Collect information at every step of the fit and save it to a file. |
|
Simplified fit interface. |
|
Register a new fitter with bumps, if it is not already there. |
|
Run the fit tests to make sure they work. |
Interfaces to various optimizers.
- class bumps.fitters.BFGSFit(problem)[source]¶
Bases:
FitBase
BFGS quasi-newton optimizer.
BFGS estimates Hessian and its Cholesky decomposition, but initial tests give uncertainties quite different from the directly computed Jacobian in Levenburg-Marquardt or the Hessian estimated at the minimum by numerical differentiation.
To use the internal ‘H’ and ‘L’ and save some computation time, then use:
C = lsqerror.chol_cov(fit.result['L']) stderr = lsqerror.stderr(C)
- static h5dump(group: Group, state: Any) None ¶
Store fitter.state into the given HDF5 Group.
This will be restored by the corresponding h5load, then passed to the fitter to resume from its current state. This strategy is particularly useful for MCMC analysis where you may need more iterations for the chains to reach equilibrium. It is also the basis of checkpoint/restore operations for fitters such as de and amoeba which manage a population, though in those cases the best point seen so far may be good enough.
- static h5load(group: Group) Any ¶
Load internal fit state from the group saved by h5dump. Note that this function will be responsible for migrating state from older versions to newer versions of the saved representation.
- id: str = 'newton'¶
Short name for the fit method, used as –id on the command line.
- name: str = 'Quasi-Newton BFGS'¶
Display name for the fit method
- settings: List[Tuple[str, Any]] = [('steps', 3000), ('ftol', 1e-06), ('xtol', 1e-12), ('starts', 1), ('jump', 0.0)]¶
Available fitting options and their default values.
- solve(monitors: MonitorRunner, mapper=None, **options)[source]¶
- state: Any = None¶
Internal fit state. If the state object has a draw method this should return a set of points from the posterior probability distribution for the fit.
- class bumps.fitters.CheckpointMonitor(checkpoint, progress=1800)[source]¶
Bases:
TimedUpdate
Periodically save fit state so that it can be resumed later.
- checkpoint: Callable[None, None] = None¶
Function to call at each checkpoint.
- config_history(history)¶
Indicate which fields are needed by the monitor and for what duration.
- class bumps.fitters.ConsoleMonitor(problem, progress=1, improvement=30)[source]¶
Bases:
TimedUpdate
Display fit progress on the console
- config_history(history)¶
Indicate which fields are needed by the monitor and for what duration.
- class bumps.fitters.DEFit(problem)[source]¶
Bases:
FitBase
Classic Storn and Price differential evolution optimizer.
- static h5dump(group: Group, state: Dict[str, Any])[source]¶
Store fitter.state into the given HDF5 Group.
This will be restored by the corresponding h5load, then passed to the fitter to resume from its current state. This strategy is particularly useful for MCMC analysis where you may need more iterations for the chains to reach equilibrium. It is also the basis of checkpoint/restore operations for fitters such as de and amoeba which manage a population, though in those cases the best point seen so far may be good enough.
- static h5load(group: Group) Any [source]¶
Load internal fit state from the group saved by h5dump. Note that this function will be responsible for migrating state from older versions to newer versions of the saved representation.
- id: str = 'de'¶
Short name for the fit method, used as –id on the command line.
- name: str = 'Differential Evolution'¶
Display name for the fit method
- settings: List[Tuple[str, Any]] = [('steps', 1000), ('pop', 10), ('CR', 0.9), ('F', 2.0), ('ftol', 1e-08), ('xtol', 1e-06)]¶
Available fitting options and their default values.
- solve(monitors: MonitorRunner, mapper=None, **options)[source]¶
- state: Any = None¶
Internal fit state. If the state object has a draw method this should return a set of points from the posterior probability distribution for the fit.
- class bumps.fitters.DreamFit(problem)[source]¶
Bases:
FitBase
- static h5dump(group: Group, state: MCMCDraw)[source]¶
Store fitter.state into the given HDF5 Group.
This will be restored by the corresponding h5load, then passed to the fitter to resume from its current state. This strategy is particularly useful for MCMC analysis where you may need more iterations for the chains to reach equilibrium. It is also the basis of checkpoint/restore operations for fitters such as de and amoeba which manage a population, though in those cases the best point seen so far may be good enough.
- static h5load(group: Group) MCMCDraw [source]¶
Load internal fit state from the group saved by h5dump. Note that this function will be responsible for migrating state from older versions to newer versions of the saved representation.
- id: str = 'dream'¶
Short name for the fit method, used as –id on the command line.
- name: str = 'DREAM'¶
Display name for the fit method
- settings: List[Tuple[str, Any]] = [('samples', 10000), ('burn', 100), ('pop', 10), ('init', 'eps'), ('thin', 1), ('alpha', 0.0), ('outliers', 'none'), ('trim', False), ('steps', 0)]¶
Available fitting options and their default values.
- solve(monitors: MonitorRunner, mapper=None, **options)[source]¶
- state: Any = None¶
Internal fit state. If the state object has a draw method this should return a set of points from the posterior probability distribution for the fit.
- class bumps.fitters.DreamModel(problem=None, mapper=None)[source]¶
Bases:
object
DREAM wrapper for fit problems. Implements dream.core.Model protocol.
- bounds: ndarray[tuple[int, ...], dtype[_ScalarType_co]]¶
- labels: List[str]¶
- class bumps.fitters.FitBase(problem)[source]¶
Bases:
object
FitBase defines the interface from bumps models to the various fitting engines available within bumps.
Each engine is defined in its own class with a specific set of attributes and methods.
The name attribute is the name of the optimizer. This is just a simple string.
The settings attribute is a list of pairs (name, default), where the names are defined as fields in FitOptions. A best attempt should be made to map the fit options for the optimizer to the standard fit options, since each of these becomes a new command line option when running bumps. If that is not possible, then a new option should be added to FitOptions. A plugin architecture might be appropriate here, if there are reasons why specific problem domains might need custom fitters, but this is not yet supported.
Each engine takes a fit problem in its constructor.
The
solve()
method runs the fit. It accepts a monitor to track updates, a mapper to distribute work and key-value pairs defining the settings.There are a number of optional methods for the fitting engines. Basically, all the methods in
FitDriver
first check if they are specialized in the fit engine before performing a default action.The load/save methods load and save the fitter state in a given directory with a specific base file name. The fitter can choose a file extension to add to the base name. Some care is needed to be sure that the extension doesn’t collide with other extensions such as .mon for the fit monitor.
The plot method shows any plots to help understand the performance of the fitter, such as a convergence plot showing the the range of values in the population over time, as well as plots of the parameter uncertainty if available. The plot should work within is given a figure canvas to work with
The stderr/cov methods should provide summary statistics for the parameter uncertainties. Some fitters, such as MCMC, will compute these directly from the population. Others, such as BFGS, will produce an estimate of the uncertainty as they go along. If the fitter does not provide these estimates, then they will be computed from numerical derivatives at the minimum in the FitDriver method.
- static h5dump(group: Group, state: Any) None [source]¶
Store fitter.state into the given HDF5 Group.
This will be restored by the corresponding h5load, then passed to the fitter to resume from its current state. This strategy is particularly useful for MCMC analysis where you may need more iterations for the chains to reach equilibrium. It is also the basis of checkpoint/restore operations for fitters such as de and amoeba which manage a population, though in those cases the best point seen so far may be good enough.
- static h5load(group: Group) Any [source]¶
Load internal fit state from the group saved by h5dump. Note that this function will be responsible for migrating state from older versions to newer versions of the saved representation.
- id: str¶
Short name for the fit method, used as –id on the command line.
- name: str¶
Display name for the fit method
- settings: List[Tuple[str, Any]]¶
Available fitting options and their default values.
- solve(monitors: MonitorRunner, mapper=None, **options)[source]¶
- state: Any = None¶
Internal fit state. If the state object has a draw method this should return a set of points from the posterior probability distribution for the fit.
- class bumps.fitters.FitDriver(fitclass=None, problem=None, monitors=None, abort_test=None, mapper=None, time=0.0, **options)[source]¶
Bases:
object
- clip()[source]¶
Force parameters within bounds so constraints are finite.
The problem is updated with the new parameter values.
Returns a list of parameter names that were clipped.
- cov()[source]¶
Return an estimate of the covariance of the fit.
Depending on the fitter and the problem, this may be computed from existing evaluations within the fitter, or from numerical differentiation around the minimum.
If the problem uses \(\chi^2/2\) as its nllf, then the covariance is derived from the Jacobian:
x = fit.problem.getp() J = bumps.lsqerror.jacobian(fit.problem, x) cov = bumps.lsqerror.jacobian_cov(J)
Otherwise, the numerical differentiation will use the Hessian estimated from nllf:
x = fit.problem.getp() H = bumps.lsqerror.hessian(fit.problem, x) cov = bumps.lsqerror.hessian_cov(H)
- fit(resume=None, fit_state=None)[source]¶
Providing fit_state allows the fit to resume from a previous state. If None then the fit will be started from a clean state.
The fitclass object should provide static methods for h5dump/h5load for saving and loading the internal state of the fitter to a specific group in an hdf5 file. The result of h5load(group) can be passed as fit_state to resume a fit with whatever new options are provided. It is up to the fitter to decide how to interpret this. The state can be retrieved from state=driver.fitter.state at the end of the fit and saved using h5dump(group, state).
resume (= resume_path / problem.name) is used by the pre-1.0 command line interface to provide the base path for the fit state files. For dream this can be replaced by the following:
fn, labels = getattr(problem, "derive_vars", (None, [])) fit_state = load_state(input_path, report=100, derived_vars=len(labels))
- show_err()[source]¶
Display the error approximation from the numerical derivative.
Warning: cost grows as the cube of the number of parameters.
- stderr()[source]¶
Return an estimate of the standard error of the fit.
Depending on the fitter and the problem, this may be computed from existing evaluations within the fitter, or from numerical differentiation around the minimum.
- stderr_from_cov()[source]¶
Return an estimate of standard error of the fit from covariance matrix.
Unlike stderr, which uses the estimate from the underlying fitter (DREAM uses the MCMC sample for this), stderr_from_cov estimates the error from the diagonal of the covariance matrix. Here, the covariance matrix may have been estimated by the fitter instead of the Hessian.
- class bumps.fitters.LevenbergMarquardtFit(problem)[source]¶
Bases:
FitBase
Levenberg-Marquardt optimizer.
- static h5dump(group: Group, state: Any) None ¶
Store fitter.state into the given HDF5 Group.
This will be restored by the corresponding h5load, then passed to the fitter to resume from its current state. This strategy is particularly useful for MCMC analysis where you may need more iterations for the chains to reach equilibrium. It is also the basis of checkpoint/restore operations for fitters such as de and amoeba which manage a population, though in those cases the best point seen so far may be good enough.
- static h5load(group: Group) Any ¶
Load internal fit state from the group saved by h5dump. Note that this function will be responsible for migrating state from older versions to newer versions of the saved representation.
- id: str = 'scipy.leastsq'¶
Short name for the fit method, used as –id on the command line.
- name: str = 'Levenberg-Marquardt (scipy.leastsq)'¶
Display name for the fit method
- problem: bumps.fitproblem.FitProblem¶
- settings: List[Tuple[str, Any]] = [('steps', 200), ('ftol', 1.5e-08), ('xtol', 1.5e-08)]¶
Available fitting options and their default values.
- solve(monitors: MonitorRunner, mapper=None, **options)[source]¶
- state: Any = None¶
Internal fit state. If the state object has a draw method this should return a set of points from the posterior probability distribution for the fit.
- class bumps.fitters.MPFit(problem)[source]¶
Bases:
FitBase
MPFit optimizer.
- static h5dump(group: Group, state: Any) None ¶
Store fitter.state into the given HDF5 Group.
This will be restored by the corresponding h5load, then passed to the fitter to resume from its current state. This strategy is particularly useful for MCMC analysis where you may need more iterations for the chains to reach equilibrium. It is also the basis of checkpoint/restore operations for fitters such as de and amoeba which manage a population, though in those cases the best point seen so far may be good enough.
- static h5load(group: Group) Any ¶
Load internal fit state from the group saved by h5dump. Note that this function will be responsible for migrating state from older versions to newer versions of the saved representation.
- id: str = 'lm'¶
Short name for the fit method, used as –id on the command line.
- name: str = 'Levenberg-Marquardt'¶
Display name for the fit method
- problem: bumps.fitproblem.FitProblem¶
- settings: List[Tuple[str, Any]] = [('steps', 200), ('ftol', 1e-10), ('xtol', 1e-10), ('starts', 1), ('jump', 0.0)]¶
Available fitting options and their default values.
- state: Any = None¶
Internal fit state. If the state object has a draw method this should return a set of points from the posterior probability distribution for the fit.
- class bumps.fitters.MonitorRunner(monitors: List[Monitor], problem, abort_test=None, max_time=0.0)[source]¶
Bases:
object
Adaptor which allows solvers to accept progress monitors.
The stopping() method manages checks for abort and timeout.
- class bumps.fitters.MultiStart(fitter)[source]¶
Bases:
FitBase
Multi-start monte carlo fitter.
This fitter wraps a local optimizer, restarting it a number of times to give it a chance to find a different local minimum. If the jump radius is non-zero, then restart near the best fit, otherwise restart at random.
- static h5dump(group: Group, state: Any) None ¶
Store fitter.state into the given HDF5 Group.
This will be restored by the corresponding h5load, then passed to the fitter to resume from its current state. This strategy is particularly useful for MCMC analysis where you may need more iterations for the chains to reach equilibrium. It is also the basis of checkpoint/restore operations for fitters such as de and amoeba which manage a population, though in those cases the best point seen so far may be good enough.
- static h5load(group: Group) Any ¶
Load internal fit state from the group saved by h5dump. Note that this function will be responsible for migrating state from older versions to newer versions of the saved representation.
- id: str¶
Short name for the fit method, used as –id on the command line.
- name: str = 'Multistart Monte Carlo'¶
Display name for the fit method
- problem: bumps.fitproblem.FitProblem¶
- settings: List[Tuple[str, Any]] = [('starts', 100), ('jump', 0.0)]¶
Available fitting options and their default values.
- solve(monitors: MonitorRunner, mapper=None, **options)[source]¶
- state: Any = None¶
Internal fit state. If the state object has a draw method this should return a set of points from the posterior probability distribution for the fit.
- class bumps.fitters.PSFit(problem)[source]¶
Bases:
FitBase
Particle swarm optimizer.
- static h5dump(group: Group, state: Any) None ¶
Store fitter.state into the given HDF5 Group.
This will be restored by the corresponding h5load, then passed to the fitter to resume from its current state. This strategy is particularly useful for MCMC analysis where you may need more iterations for the chains to reach equilibrium. It is also the basis of checkpoint/restore operations for fitters such as de and amoeba which manage a population, though in those cases the best point seen so far may be good enough.
- static h5load(group: Group) Any ¶
Load internal fit state from the group saved by h5dump. Note that this function will be responsible for migrating state from older versions to newer versions of the saved representation.
- id: str = 'ps'¶
Short name for the fit method, used as –id on the command line.
- name: str = 'Particle Swarm'¶
Display name for the fit method
- problem: bumps.fitproblem.FitProblem¶
- settings: List[Tuple[str, Any]] = [('steps', 3000), ('pop', 1)]¶
Available fitting options and their default values.
- solve(monitors: MonitorRunner, mapper=None, **options)[source]¶
- state: Any = None¶
Internal fit state. If the state object has a draw method this should return a set of points from the posterior probability distribution for the fit.
- class bumps.fitters.PTFit(problem)[source]¶
Bases:
FitBase
Parallel tempering optimizer.
- static h5dump(group: Group, state: Any) None ¶
Store fitter.state into the given HDF5 Group.
This will be restored by the corresponding h5load, then passed to the fitter to resume from its current state. This strategy is particularly useful for MCMC analysis where you may need more iterations for the chains to reach equilibrium. It is also the basis of checkpoint/restore operations for fitters such as de and amoeba which manage a population, though in those cases the best point seen so far may be good enough.
- static h5load(group: Group) Any ¶
Load internal fit state from the group saved by h5dump. Note that this function will be responsible for migrating state from older versions to newer versions of the saved representation.
- id: str = 'pt'¶
Short name for the fit method, used as –id on the command line.
- name: str = 'Parallel Tempering'¶
Display name for the fit method
- problem: bumps.fitproblem.FitProblem¶
- settings: List[Tuple[str, Any]] = [('steps', 400), ('nT', 24), ('CR', 0.9), ('burn', 100), ('Tmin', 0.1), ('Tmax', 10.0)]¶
Available fitting options and their default values.
- solve(monitors: MonitorRunner, mapper=None, **options)[source]¶
- state: Any = None¶
Internal fit state. If the state object has a draw method this should return a set of points from the posterior probability distribution for the fit.
- class bumps.fitters.RLFit(problem)[source]¶
Bases:
FitBase
Random lines optimizer.
- static h5dump(group: Group, state: Any) None ¶
Store fitter.state into the given HDF5 Group.
This will be restored by the corresponding h5load, then passed to the fitter to resume from its current state. This strategy is particularly useful for MCMC analysis where you may need more iterations for the chains to reach equilibrium. It is also the basis of checkpoint/restore operations for fitters such as de and amoeba which manage a population, though in those cases the best point seen so far may be good enough.
- static h5load(group: Group) Any ¶
Load internal fit state from the group saved by h5dump. Note that this function will be responsible for migrating state from older versions to newer versions of the saved representation.
- id: str = 'rl'¶
Short name for the fit method, used as –id on the command line.
- name: str = 'Random Lines'¶
Display name for the fit method
- problem: bumps.fitproblem.FitProblem¶
- settings: List[Tuple[str, Any]] = [('steps', 3000), ('pop', 0.5), ('CR', 0.9), ('starts', 20), ('jump', 0.0)]¶
Available fitting options and their default values.
- solve(monitors: MonitorRunner, mapper=None, **options)[source]¶
- state: Any = None¶
Internal fit state. If the state object has a draw method this should return a set of points from the posterior probability distribution for the fit.
- class bumps.fitters.Resampler(fitter)[source]¶
Bases:
FitBase
- static h5dump(group: Group, state: Any) None ¶
Store fitter.state into the given HDF5 Group.
This will be restored by the corresponding h5load, then passed to the fitter to resume from its current state. This strategy is particularly useful for MCMC analysis where you may need more iterations for the chains to reach equilibrium. It is also the basis of checkpoint/restore operations for fitters such as de and amoeba which manage a population, though in those cases the best point seen so far may be good enough.
- static h5load(group: Group) Any ¶
Load internal fit state from the group saved by h5dump. Note that this function will be responsible for migrating state from older versions to newer versions of the saved representation.
- id: str¶
Short name for the fit method, used as –id on the command line.
- name: str¶
Display name for the fit method
- problem: bumps.fitproblem.FitProblem¶
- settings: List[Tuple[str, Any]]¶
Available fitting options and their default values.
- state: Any = None¶
Internal fit state. If the state object has a draw method this should return a set of points from the posterior probability distribution for the fit.
- class bumps.fitters.SimplexFit(problem)[source]¶
Bases:
FitBase
Nelder-Mead simplex optimizer.
- static h5dump(group: Group, state: Any) None ¶
Store fitter.state into the given HDF5 Group.
This will be restored by the corresponding h5load, then passed to the fitter to resume from its current state. This strategy is particularly useful for MCMC analysis where you may need more iterations for the chains to reach equilibrium. It is also the basis of checkpoint/restore operations for fitters such as de and amoeba which manage a population, though in those cases the best point seen so far may be good enough.
- static h5load(group: Group) Any ¶
Load internal fit state from the group saved by h5dump. Note that this function will be responsible for migrating state from older versions to newer versions of the saved representation.
- id: str = 'amoeba'¶
Short name for the fit method, used as –id on the command line.
- name: str = 'Nelder-Mead Simplex'¶
Display name for the fit method
- problem: bumps.fitproblem.FitProblem¶
- settings: List[Tuple[str, Any]] = [('steps', 1000), ('radius', 0.15), ('xtol', 1e-06), ('ftol', 1e-08), ('starts', 1), ('jump', 0.01)]¶
Available fitting options and their default values.
- solve(monitors: MonitorRunner, mapper=None, **options)[source]¶
- state: Any = None¶
Internal fit state. If the state object has a draw method this should return a set of points from the posterior probability distribution for the fit.
- class bumps.fitters.SnobFit(problem)[source]¶
Bases:
FitBase
- static h5dump(group: Group, state: Any) None ¶
Store fitter.state into the given HDF5 Group.
This will be restored by the corresponding h5load, then passed to the fitter to resume from its current state. This strategy is particularly useful for MCMC analysis where you may need more iterations for the chains to reach equilibrium. It is also the basis of checkpoint/restore operations for fitters such as de and amoeba which manage a population, though in those cases the best point seen so far may be good enough.
- static h5load(group: Group) Any ¶
Load internal fit state from the group saved by h5dump. Note that this function will be responsible for migrating state from older versions to newer versions of the saved representation.
- id: str = 'snobfit'¶
Short name for the fit method, used as –id on the command line.
- name: str = 'SNOBFIT'¶
Display name for the fit method
- problem: bumps.fitproblem.FitProblem¶
- settings: List[Tuple[str, Any]] = [('steps', 200)]¶
Available fitting options and their default values.
- solve(monitors: MonitorRunner, mapper=None, **options)[source]¶
- state: Any = None¶
Internal fit state. If the state object has a draw method this should return a set of points from the posterior probability distribution for the fit.
- class bumps.fitters.StepMonitor(problem, fid, fields=['step', 'time', 'value', 'point'])[source]¶
Bases:
Monitor
Collect information at every step of the fit and save it to a file.
fid is the file to save the information to fields is the list of “step|time|value|point” fields to save
The point field should be last in the list.
- FIELDS = ['step', 'time', 'value', 'point']¶
- bumps.fitters.fit(problem, method='amoeba', export=None, resume=None, store=None, name=None, verbose=False, parallel=1, **options)[source]¶
Simplified fit interface.
Given a fit problem, the name of a fitter and the fitter options, it will run the fit and return the best value and standard error of the parameters. If verbose is true, then the console monitor will be enabled, showing progress through the fit and showing the parameter standard error at the end of the fit, otherwise it is completely silent.
Returns a scipy OptimizeResult object containing “x” and “dx”. Some fitters also include a “state” object. For dream this can be used in the call bumps.dream.views.plot_all(result.state) to generate the uncertainty plots. Note: success=True and status=0 for now since the stopping condition is not yet available from the fitters.
If resume=result is provided, then attempt to resume the fit from the previous result.
If export=path is provided, generate the standard plots and export files to the specified directory. This uses name as the basename for the output files, or problem.name if name is not provided. Name defaults to “problem”.
If parallel=n is provided, then run on n separate cpus. By default parallel=1 to run on a single cpu. For slow functions set parallel=0 to run on all cpus. You want to run on a single cpu if your function is already parallel (for example using multiprocessing or using gpu code), or if your function is so fast that the overhead of transfering data is higher than cost of n function calls.