SpectrumFit¶
-
class
gammapy.spectrum.SpectrumFit(obs_list, model, stat='wstat', forward_folded=True, fit_range=None)[source]¶ Bases:
gammapy.utils.fitting.FitOrchestrate 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,SpectrumObservationObservation(s) to fit
model :
SpectralModelSource model with initial parameter values. Should return counts if
forward_foldedis False and a flux otherwisestat : {‘wstat’, ‘cash’}
Fit statistic
forward_folded : bool, default: True
Fold
modelwith the IRFs given inobs_listfit_range : tuple of
QuantityThe 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_rangeBins participating in the fit for each observation. fit_rangeFit range. obs_listObservations participating in the fit predicted_countsCurrent value of predicted counts. resultBundle fit results into SpectrumFitResult.statvalCurrent value of statval. true_fit_rangeTrue fit range for each observation. Methods Summary
calc_statval()Calc statistic for all observations. likelihood_profile(model, parameter[, …])Compute likelihood profile for a single parameter of the model. likelihood_profiles(model[, parameters])Compute likelihood profiles for multiple parameters. optimize([backend])Run the optimization predict_counts()Predict counts for all observations. run([steps, optimize_opts, profile_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 :
ParametersBest fit 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
SpectrumFitwith 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.
-
likelihood_profile(model, parameter, values=None, bounds=2, nvalues=11)¶ Compute likelihood profile for a single parameter of the model.
Parameters: model :
SpectralModelModel to compute the likelihood profile for.
parameter : str
Parameter to calculate profile for
values :
Quantity(optional)Parameter values to evaluate the likelihood for.
bounds : int or tuple of float
When an
intis passed the bounds are computed frombounds * sigmafrom the best fit value of the parameter, wheresigmacorresponds to the one sigma error on the parameter. If a tuple of floats is given those are taken as the min and max values andnvaluesare linearly spaced between those.nvalues : int
Number of parameter grid points to use.
Returns: likelihood_profile : dict
Dict of parameter values and likelihood values.
-
likelihood_profiles(model, parameters='all')¶ Compute likelihood profiles for multiple parameters.
Parameters: model :
SpectralModelorSkyModelModel to compute the likelihood profile for.
parameters : list of str or “all”
For which parameters to compute likelihood profiles.
-
optimize(backend='minuit', **kwargs)¶ Run the optimization
Parameters: backend : {“minuit”, “sherpa”}
Which fitting backend to use.
**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. 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:Returns: fit_result :
dictOptimize info dict with the best fit model and additional information.
-
predict_counts()[source]¶ Predict counts for all observations.
The result is stored as
predicted_countsattribute.
-
run(steps='all', optimize_opts=None, profile_opts=None)¶ Run all fitting steps.
Parameters: steps : {“all”, “optimize”, “errors”, “profiles”}
Which fitting steps to run.
optimize_opts : dict
Options passed to
Fit.optimize.profile_opts : dict
Options passed to
Fit.likelihood_profiles.Returns: fit_result :
FitResultFit result object with the best fit model and additional information.
-
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 :
ParametersModel parameters
-