Note

You are not reading the most up to date version of Gammapy documentation.
Access the latest stable version v1.3 or the list of Gammapy releases.

FluxPointFit

class gammapy.spectrum.FluxPointFit(model, data, stat='chi2')[source]

Bases: gammapy.utils.fitting.Fit

Fit a set of flux points with a parametric model.

Parameters:

model : SpectralModel

Spectral model (with fit start parameters)

data : FluxPoints

Flux points.

Examples

Load flux points from file and fit with a power-law model:

from astropy import units as u
from gammapy.spectrum import FluxPoints, FluxPointFit
from gammapy.spectrum.models import PowerLaw

filename = '$GAMMAPY_EXTRA/test_datasets/spectrum/flux_points/diff_flux_points.fits'
flux_points = FluxPoints.read(filename)

model = PowerLaw(
    index=2. * u.Unit(''),
    amplitude=1e-12 * u.Unit('cm-2 s-1 TeV-1'),
    reference=1. * u.TeV,
)

fitter = FluxPointFit(model, flux_points)
result = fitter.run()
print(result['best_fit_model'])

Attributes Summary

Methods Summary

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
run([steps, optimize_opts, profile_opts]) Run all fitting steps.
total_stat(parameters) Total likelihood given the current model parameters

Attributes Documentation

stat

Methods Documentation

likelihood_profile(model, parameter, values=None, bounds=2, nvalues=11)

Compute likelihood profile for a single parameter of the model.

Parameters:

model : SpectralModel

Model 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 int is passed the bounds are computed from bounds * sigma from the best fit value of the parameter, where sigma 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 and nvalues are 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 : SpectralModel or SkyModel

Model 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 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:

Returns:

fit_result : dict

Optimize info dict with the best fit model and additional information.

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 : FitResult

Fit result object with the best fit model and additional information.

total_stat(parameters)[source]

Total likelihood given the current model parameters