SpectrumFit¶
-
class
gammapy.spectrum.
SpectrumFit
(obs_list, model, stat='wstat', forward_folded=True, fit_range=None)[source]¶ Bases:
gammapy.utils.fitting.Fit
Orchestrate a 1D counts spectrum fit.
After running the
run()
method, the fit results are available inresult()
. For usage examples see Spectral FittingParameters: - obs_list :
SpectrumObservationList
,SpectrumObservation
Observation(s) to fit
- model :
SpectralModel
Source model with initial parameter values. Should return counts if
forward_folded
is False and a flux otherwise- stat : {‘wstat’, ‘cash’}
Fit statistic
- forward_folded : bool, default: True
Fold
model
with the IRFs given inobs_list
- fit_range : tuple of
Quantity
The intersection between the fit range and the observation thresholds will be used. If you want to control which bins are taken into account in the fit for each observation, use
quality()
Attributes Summary
bins_in_fit_range
Bins participating in the fit for each observation. fit_range
Fit range. obs_list
Observations participating in the fit predicted_counts
Current value of predicted counts. result
Bundle fit results into SpectrumFitResult
.statval
Current value of statval. true_fit_range
True fit range for each observation. Methods Summary
calc_statval
()Calc statistic for all observations. confidence
(parameter[, backend, sigma])Estimate confidence interval. covariance
([backend])Estimate the covariance matrix. likelihood_contour
()Compute likelihood contour. likelihood_profile
(parameter[, values, …])Compute likelihood profile. minos_contour
(x, y[, numpoints, sigma])Compute MINOS contour. optimize
([backend])Run the optimization. predict_counts
()Predict counts for all observations. run
([optimize_opts, covariance_opts])Run all fitting steps. total_stat
(parameters)Statistic summed over all bins and all observations. Attributes Documentation
-
bins_in_fit_range
¶ Bins participating in the fit for each observation.
-
fit_range
¶ Fit range.
-
obs_list
¶ Observations participating in the fit
-
predicted_counts
¶ Current value of predicted counts.
For each observation a tuple to counts for the on and off region is returned.
-
result
¶ Bundle fit results into
SpectrumFitResult
.Parameters: - parameters :
Parameters
Best fit parameters
- parameters :
-
statval
¶ Current value of statval.
For each observation the statval per bin is returned.
-
true_fit_range
¶ True fit range for each observation.
True fit range is the fit range set in the
SpectrumFit
with observation threshold taken into account.
Methods Documentation
-
calc_statval
()[source]¶ Calc statistic for all observations.
The result is stored as attribute
statval
, bin outside the fit range are set to 0.
-
confidence
(parameter, backend='minuit', sigma=1, **kwargs)¶ Estimate confidence interval.
Extra
kwargs
are passed to the backend. E.g.iminuit.Minuit.minos
supports amaxcall
option.Parameters: - backend : str
Which backend to use (see
gammapy.utils.fitting.registry
)- parameter :
Parameter
Parameter of interest
- sigma : float
Number of standard deviations for the confidence level
Returns: - result : dict
Dictionary with keys “errp”, ‘errn”, “success” and “nfev”.
-
covariance
(backend='minuit')¶ Estimate the covariance matrix.
Assumes that the model parameters are already optimised.
Parameters: - backend : str
Which backend to use (see
gammapy.utils.fitting.registry
)
Returns: - result :
CovarianceResult
Results
-
likelihood_contour
()¶ Compute likelihood contour.
The method used is to vary two parameters, keeping all others fixed. So this is taking a “slice” or “scan” of the likelihood.
See also:
Fit.minos_contour
Parameters: - TODO
Returns: - TODO
-
likelihood_profile
(parameter, values=None, bounds=2, nvalues=11, reoptimize=False, optimize_opts=None)¶ Compute likelihood profile.
The method used is to vary one parameter, keeping all others fixed. So this is taking a “slice” or “scan” of the likelihood.
See also:
Fit.minos_profile
.Parameters: - parameter :
Parameter
Parameter of interest
- values :
Quantity
(optional) Parameter values to evaluate the likelihood for.
- bounds : int or tuple of float
When an
int
is passed the bounds are computed frombounds * sigma
from the best fit value of the parameter, wheresigma
corresponds to the one sigma error on the parameter. If a tuple of floats is given those are taken as the min and max values andnvalues
are linearly spaced between those.- nvalues : int
Number of parameter grid points to use.
- reoptimize : bool
Re-optimize other parameters, when computing the likelihood profile.
Returns: - results : dict
Dictionary with keys “values” and “likelihood”.
- parameter :
-
minos_contour
(x, y, numpoints=10, sigma=1.0)¶ Compute MINOS contour.
Calls
iminuit.Minuit.mncontour
.This is a contouring algorithm for a 2D function which is not simply the likelihood function. That 2D function is given at each point
(par_1, par_2)
by re-optimising all other free parameters, and taking the likelihood at that point.Very compute-intensive and slow.
Parameters: - x, y :
Parameter
Parameters of interest
- numpoints : int
Number of contour points
- sigma : float
Number of standard deviations for the confidence level
Returns: - result : dict
Dictionary with keys “x”, “y” (Numpy arrays with contour points) and a boolean flag “success”. The result objects from
mncontour
are in the additional keys “x_info” and “y_info”.
- x, y :
-
optimize
(backend='minuit', **kwargs)¶ Run the optimization.
Parameters: - backend : str
Which backend to use (see
gammapy.utils.fitting.registry
)- **kwargs : dict
Keyword arguments passed to the optimizer. For the
"minuit"
backend see https://iminuit.readthedocs.io/en/latest/api.html#iminuit.Minuit for a detailed description of the available options. If there is an entry ‘migrad_opts’, those options will be passed toiminuit.Minuit.migrad()
.For the
"sherpa"
backend you can from the optionsmethod = {"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 desribed in detail here: https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html
Returns: - fit_result :
FitResult
Results
-
predict_counts
()[source]¶ Predict counts for all observations.
The result is stored as
predicted_counts
attribute.
-
run
(optimize_opts=None, covariance_opts=None)¶ Run all fitting steps.
Parameters: - optimize_opts : dict
Options passed to
Fit.optimize
.- covariance_opts : dict
Options passed to
Fit.covariance
.
Returns: - fit_result :
FitResult
Results
-
total_stat
(parameters)[source]¶ Statistic summed over all bins and all observations.
This is the likelihood function that is passed to the optimizers
Parameters: - parameters :
Parameters
Model parameters
- parameters :
- obs_list :