modeling - Models and fitting¶
Introduction¶
gammapy.modeling
contains all the functionality related to modeling and fitting
data. This includes spectral, spatial and temporal model classes, as well as the fit
and parameter API.
Assuming you have prepared your gamma-ray data as a set of
Dataset
objects, and
stored one or more datasets in a Datasets
container, you are
all set for modeling and fitting. Either via a YAML config file, or via Python
code, define the Models
to use, which is a list of
SkyModel
objects representing additive emission
components, usually sources or diffuse emission, although a single source can
also be modeled by multiple components if you want. The
SkyModel
is a factorised model with a
SpectralModel
component and a
SpatialModel
component. Most commonly used models in
gamma-ray astronomy are built-in, see the Model gallery.
It is easy to create user-defined models and
datasets, Gammapy is very flexible.
The Fit
class provides methods to fit, i.e. optimise
parameters and estimate parameter errors and correlations. It interfaces with a
Datasets
object, which in turn is connected to a
Models
object, which has a
Parameters
object, which contains the model parameters.
Currently iminuit
is used as modeling and fitting backend, in the future we
plan to support other optimiser and error estimation methods, e.g. from
scipy
. Models can be unique for a given dataset, or contribute to multiple
datasets and thus provide links, allowing e.g. to do a joint fit to multiple
IACT datasets, or to a joint IACT and Fermi-LAT dataset. Many examples are given
in the tutorials.
Getting Started¶
In the following you will see how to fit spectral data in OGIP format. The
format is described at 1D counts spectra. An example dataset is available in the
$GAMMAPY_DATA
repo. For a description of the available fit statstics see
Fit statistics.
The following example shows how to fit a power law simultaneously to two
simulated crab runs using the Fit
class.
from gammapy.datasets import SpectrumDatasetOnOff
from gammapy.modeling import Fit
from gammapy.modeling.models import PowerLawSpectralModel
import matplotlib.pyplot as plt
path = "$GAMMAPY_DATA/joint-crab/spectra/hess/"
obs_1 = SpectrumDatasetOnOff.from_ogip_files(path + "pha_obs23523.fits")
obs_2 = SpectrumDatasetOnOff.from_ogip_files(path + "pha_obs23592.fits")
model = PowerLawSpectralModel(
index=2,
amplitude='1e-12 cm-2 s-1 TeV-1',
reference='1 TeV',
)
obs_1.model = model
obs_2.model = model
fit = Fit([obs_1, obs_2])
result = fit.run()
model.parameters.covariance = result.parameters.covariance
You can check the fit results by looking at the result and model object:
>>> print(result)
OptimizeResult
backend : minuit
method : minuit
success : True
nfev : 115
total stat : 65.36
message : Optimization terminated successfully.
>>> print(model)
PowerLawSpectralModel
Parameters:
name value error unit min max frozen
--------- --------- --------- -------------- --- --- ------
index 2.781e+00 1.120e-01 nan nan False
amplitude 5.201e-11 4.965e-12 cm-2 s-1 TeV-1 nan nan False
reference 1.000e+00 0.000e+00 TeV nan nan True
Covariance:
name index amplitude reference
--------- --------- --------- ---------
index 1.255e-02 3.578e-13 0.000e+00
amplitude 3.578e-13 2.465e-23 0.000e+00
reference 0.000e+00 0.000e+00 0.000e+00
Reference/API¶
gammapy.modeling Package¶
Models and fitting.
Functions¶
|
Corner plot for each parameter explored by the walkers. |
|
Plot the trace of walkers for every steps |
|
Run the MCMC sampler. |
|
Uniform prior distribution. |
Classes¶
|
Parameter covariance class |
|
Fit class. |
|
A model parameter. |
|
Parameters container. |
gammapy.modeling.models Package¶
Built-in models in Gammapy.
Functions¶
|
Create a Crab nebula reference spectral model. |
|
Cosmic a cosmic ray spectral model at Earth. |
Classes¶
|
Sky model collection. |
|
Sky model component. |
|
Spatial model base class. |
|
Spectral model base class. |
|
Temporal model base class. |
|
Spatially constant (isotropic) spatial model. |
|
Spatial sky map template model. |
|
Constant disk model. |
|
Two-dimensional Gaussian model. |
|
Two-dimensional Generealized Gaussian model. |
|
Point Source. |
|
Shell model. |
|
Constant model. |
|
Arithmetic combination of two spectral models. |
|
Spectral power-law model. |
|
Spectral power-law model with integral as amplitude parameter. |
|
Spectral broken power-law model. |
|
Spectral smooth broken power-law model. |
|
Piecewise spectral correction |
|
Spectral exponential cutoff power-law model. |
|
Spectral exponential cutoff power-law model used for 3FGL. |
|
Spectral super exponential cutoff power-law model used for 3FGL. |
|
Spectral super exponential cutoff power-law model used for 4FGL. |
|
Spectral log parabola model. |
|
A model generated from a table of energy and value arrays. |
|
Gaussian spectral model. |
|
Gamma-ray absorption models. |
|
A wrapper for Naima models. |
|
Wrapper to scale another spectral model by a norm factor. |
|
Spectral power-law model with normalized amplitude parameter. |
|
Norm spectral log parabola model. |
|
Norm spectral exponential cutoff power-law model. |
|
Constant temporal model. |
|
Temporal light curve model. |
|
Temporal model with an exponential decay. |
|
A Gaussian temporal profile |
Variables¶
Registry of model classes |
|
Registry of spatial model classes. |
|
Registry of temporal models classes. |
|
Registry of spectral model classes. |