SpectrumFit

class gammapy.spectrum.SpectrumFit(obs_list, model, stat='wstat', forward_folded=True, fit_range=None, background_model=None, method='sherpa', err_method='sherpa')[source]

Bases: object

Spectral Fit

For usage examples see Spectral Fitting

Parameters:

obs_list : SpectrumObservationList, SpectrumObservation

Observation(s) to fit

model : SpectralModel

Source model. 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 in obs_list

fit_range : tuple of Quantity

Fit range, will be convolved with observation thresholds. If you want to control which bins are taken into account in the fit for each observations, use qualitiy()

background_model : SpectralModel, optional

Background model to be used in cash fits

method : {‘sherpa’}

Optimization backend for the fit

err_method : {‘sherpa’}

Optimization backend for error estimation

Attributes Summary

bins_in_fit_range Bins participating in the fit for each observation
fit_range Fit range
predicted_counts Current value of predicted counts
statval Current value of statval
total_stat Statistic summed over all bins and all observations
true_fit_range True fit range for each observation

Methods Summary

calc_statval() Calc statistic for all observations
est_errors() Estimate errors
fit() Run the fit
likelihood_1d(model, parname, parvals) Compute likelihood profile
plot_likelihood_1d([ax]) Plot 1D likelihood profile
predict_counts() Predict counts for all observations
run([outdir]) Run all steps and write result to disk

Attributes Documentation

bins_in_fit_range

Bins participating in the fit for each observation

fit_range

Fit range

predicted_counts

Current value of predicted counts

For each observation a tuple to counts for the on and off region is returned.

statval

Current value of statval

For each observation the statval per bin is returned

total_stat

Statistic summed over all bins and all observations

This is what is used for the fit

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.

est_errors()[source]

Estimate errors

fit()[source]

Run the fit

likelihood_1d(model, parname, parvals)[source]

Compute likelihood profile

Parameters:

model : SpectralModel

Model to draw likelihood profile for

parname : str

Parameter to calculate profile for

parvals : Quantity

Parameter values

plot_likelihood_1d(ax=None, **kwargs)[source]

Plot 1D likelihood profile

see likelihood_1d()

predict_counts()[source]

Predict counts for all observations

The result is stored as predicted_counts attribute

run(outdir=None)[source]

Run all steps and write result to disk

Parameters:

outdir : Path, str

directory to write results files to (if given)