Fit#

class gammapy.modeling.Fit(backend='minuit', optimize_opts=None, covariance_opts=None, confidence_opts=None, store_trace=False)[source]#

Bases: object

Fit class.

The fit class provides a uniform interface to multiple fitting backends. Currently available: “minuit”, “sherpa” and “scipy”

Parameters
backend{“minuit”, “scipy” “sherpa”}

Global backend used for fitting, default : minuit

optimize_optsdict

Keyword arguments passed to the optimizer. For the "minuit" backend see https://iminuit.readthedocs.io/en/stable/reference.html#iminuit.Minuit for a detailed description of the available options. If there is an entry ‘migrad_opts’, those options will be passed to iminuit.Minuit.migrad().

For the "sherpa" backend you can from the options method = {"simplex",  "levmar", "moncar", "gridsearch"} Those methods are described and compared in detail on http://cxc.cfa.harvard.edu/sherpa/methods/index.html. The available options of the optimization methods are described on the following pages in detail:

For the "scipy" backend the available options are described in detail here: https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html

covariance_optsdict

Covariance options passed to the given backend.

confidence_optsdict

Extra arguments passed to the backend. E.g. iminuit.Minuit.minos supports a maxcall option. For the scipy backend confidence_opts are forwarded to brentq. If the confidence estimation fails, the bracketing interval can be adapted by modifying the the upper bound of the interval (b) value.

store_tracebool

Whether to store the trace of the fit

Attributes Summary

minuit

Iminuit object

Methods Summary

confidence(datasets, parameter[, sigma, ...])

Estimate confidence interval.

covariance(datasets)

Estimate the covariance matrix.

optimize(datasets)

Run the optimization.

run(datasets)

Run all fitting steps.

stat_contour(datasets, x, y[, numpoints, sigma])

Compute stat contour.

stat_profile(datasets, parameter[, reoptimize])

Compute fit statistic profile.

stat_surface(datasets, x, y[, reoptimize])

Compute fit statistic surface.

Attributes Documentation

minuit#

Iminuit object

Methods Documentation

confidence(datasets, parameter, sigma=1, reoptimize=True)[source]#

Estimate confidence interval.

Extra kwargs are passed to the backend. E.g. iminuit.Minuit.minos supports a maxcall option.

For the scipy backend kwargs are forwarded to brentq. If the confidence estimation fails, the bracketing interval can be adapted by modifying the the upper bound of the interval (b) value.

Parameters
datasetsDatasets or list of Dataset

Datasets to optimize.

parameterParameter

Parameter of interest

sigmafloat

Number of standard deviations for the confidence level

reoptimizebool

Re-optimize other parameters, when computing the confidence region.

Returns
resultdict

Dictionary with keys “errp”, ‘errn”, “success” and “nfev”.

covariance(datasets)[source]#

Estimate the covariance matrix.

Assumes that the model parameters are already optimised.

Parameters
datasetsDatasets or list of Dataset

Datasets to optimize.

Returns
resultCovarianceResult

Results

optimize(datasets)[source]#

Run the optimization.

Parameters
datasetsDatasets or list of Dataset

Datasets to optimize.

Returns
optimize_resultOptimizeResult

Optimization result

run(datasets)[source]#

Run all fitting steps.

Parameters
datasetsDatasets or list of Dataset

Datasets to optimize.

Returns
fit_resultFitResult

Fit result

stat_contour(datasets, x, y, numpoints=10, sigma=1)[source]#

Compute stat contour.

Calls iminuit.Minuit.mncontour.

This is a contouring algorithm for a 2D function which is not simply the fit statistic function. That 2D function is given at each point (par_1, par_2) by re-optimising all other free parameters, and taking the fit statistic at that point.

Very compute-intensive and slow.

Parameters
datasetsDatasets or list of Dataset

Datasets to optimize.

x, yParameter

Parameters of interest

numpointsint

Number of contour points

sigmafloat

Number of standard deviations for the confidence level

Returns
resultdict

Dictionary containing the parameter values defining the contour, with the boolean flag “success” and the info objects from mncontour.

stat_profile(datasets, parameter, reoptimize=False)[source]#

Compute fit statistic profile.

The method used is to vary one parameter, keeping all others fixed. So this is taking a “slice” or “scan” of the fit statistic.

Parameters
datasetsDatasets or list of Dataset

Datasets to optimize.

parameterParameter

Parameter of interest. The specification for the scan, such as bounds and number of values is taken from the parameter object.

reoptimizebool

Re-optimize other parameters, when computing the confidence region.

Returns
resultsdict

Dictionary with keys “values”, “stat” and “fit_results”. The latter contains an empty list, if reoptimize is set to False

stat_surface(datasets, x, y, reoptimize=False)[source]#

Compute fit statistic surface.

The method used is to vary two parameters, keeping all others fixed. So this is taking a “slice” or “scan” of the fit statistic.

Caveat: This method can be very computationally intensive and slow

See also: Fit.stat_contour

Parameters
datasetsDatasets or list of Dataset

Datasets to optimize.

x, yParameter

Parameters of interest

reoptimizebool

Re-optimize other parameters, when computing the confidence region.

Returns
resultsdict

Dictionary with keys “x_values”, “y_values”, “stat” and “fit_results”. The latter contains an empty list, if reoptimize is set to False