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. An overview of all the available models can be found in the Model gallery.

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.spectrum 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

Tutorials

Tutorials that show examples using gammapy.modeling:

Reference/API

gammapy.modeling Package

Models and fitting.

Functions

plot_corner(sampler, dataset[, nburn])

Corner plot for each parameter explored by the walkers.

plot_trace(sampler, dataset)

Plot the trace of walkers for every steps

run_mcmc(dataset[, nwalkers, nrun, threads])

Run the MCMC sampler.

uniform_prior(value, umin, umax)

Uniform prior distribution.

Classes

Covariance(parameters[, data])

Parameter covariance class

Fit(datasets)

Fit class.

Parameter(name, value[, unit, scale, min, …])

A model parameter.

Parameters([parameters])

Parameters container.

gammapy.modeling.models Package

Built-in models in Gammapy.

Functions

create_crab_spectral_model([reference])

Create a Crab nebula reference spectral model.

create_cosmic_ray_spectral_model([particle])

Cosmic a cosmic ray spectral model at Earth.

Classes

SkyModelBase(**kwargs)

Sky model base class

Models([models])

Sky model collection.

SkyModel(spectral_model[, spatial_model, …])

Sky model component.

SkyDiffuseCube(map[, norm, tilt, reference, …])

Cube sky map template model (3D).

BackgroundModel(map[, norm, tilt, …])

Background model.

Absorption(energy, param, data[, filename, …])

Gamma-ray absorption models.

SpatialModel(**kwargs)

Spatial model base class.

SpectralModel(**kwargs)

Spectral model base class.

TemporalModel(**kwargs)

Temporal model base class.

ConstantSpatialModel(**kwargs)

Spatially constant (isotropic) spatial model.

TemplateSpatialModel(map[, norm, meta, …])

Spatial sky map template model (2D).

DiskSpatialModel(**kwargs)

Constant disk model.

GaussianSpatialModel(**kwargs)

Two-dimensional Gaussian model.

PointSpatialModel(**kwargs)

Point Source.

ShellSpatialModel(**kwargs)

Shell model.

ConstantSpectralModel(**kwargs)

Constant model.

CompoundSpectralModel(model1, model2, operator)

Arithmetic combination of two spectral models.

PowerLawSpectralModel(**kwargs)

Spectral power-law model.

PowerLaw2SpectralModel(**kwargs)

Spectral power-law model with integral as amplitude parameter.

SmoothBrokenPowerLawSpectralModel(**kwargs)

Spectral smooth broken power-law model.

ExpCutoffPowerLawSpectralModel(**kwargs)

Spectral exponential cutoff power-law model.

ExpCutoffPowerLaw3FGLSpectralModel(**kwargs)

Spectral exponential cutoff power-law model used for 3FGL.

SuperExpCutoffPowerLaw3FGLSpectralModel(**kwargs)

Spectral super exponential cutoff power-law model used for 3FGL.

SuperExpCutoffPowerLaw4FGLSpectralModel(**kwargs)

Spectral super exponential cutoff power-law model used for 4FGL.

LogParabolaSpectralModel(**kwargs)

Spectral log parabola model.

TemplateSpectralModel(energy, values[, …])

A model generated from a table of energy and value arrays.

GaussianSpectralModel(**kwargs)

Gaussian spectral model.

AbsorbedSpectralModel(spectral_model, …[, …])

Spectral model with EBL absorption.

NaimaSpectralModel(radiative_model[, …])

A wrapper for Naima models.

ScaleSpectralModel(model[, norm])

Wrapper to scale another spectral model by a norm factor.

ConstantTemporalModel(**kwargs)

Constant temporal model.

LightCurveTemplateTemporalModel(table[, …])

Temporal light curve model.

Variables

SPATIAL_MODELS

Built-in spatial models.

TEMPORAL_MODELS

Built-in temporal models.

SPECTRAL_MODELS

Built-in spectral models.