Last modified: November 2016

Jump to: Description · Examples · Bugs · See Also

AHELP for CIAO 4.11 Sherpa v1


Context: fitting


Perform a fit using current optimization method and fit statistic.


fit(id=None, otherids=None, bkg_only=False, outfile="fit.out",
fit_bkg(id=None, otherids=None, outfile="fit.out", clobber=True)


The fit() function fits the defined model(s) to the data set(s) by data ID. It may be called with no arguments, in which case a fit is done simultaneously on all data sets ready for fitting, i.e., all data sets which have been assigned source models. If the 'bkg_only' argument is changed from the default False to True, only the background data sets ready for fitting will be fit; source data sets ready for fitting will be ignored in this case.

If a background file and background responses are defined for the data set which is being fit, the background is automatically fit as well.

The fit_bkg() function fits the defined model(s) to the background data set(s) by ID. It may be called with no arguments, in which case a fit is done simultaneously on all data sets for which the user has defined a model to be fit.

The fit is done with the specified optimization method and statistic; the default values are levmar and chi2gehrels, respectively. The fit proceeds until it converges to within the set tolerance or hits the maximum number of iterations. The resulting best fit parameters and final statistic value are displayed to the user. The best-fit parameters are accessible with get_fit_results().

If one of the available iterative fitting methods in Sherpa is set for fitting (see "ahelp set_iter_method"), the chosen fit optimization method will be called several times when the fit command is run, until the fit can no longer be improved. If the iterative fitting method is set to "none" (default), then no iterative fitting is done when the fit command is called; the optimization method is called once, and Sherpa otherwise operates as expected.


Example 1

sherpa> fit("src")

A single data set can be fit using its data set ID number or string.

Example 2

sherpa> fit()

The fit function called with no arguments will simultaneously fit all data sets to which models have been assigned.

Example 3

sherpa> fit(bkg_only=True)

The fit function called with the 'bkg_only' argument set to True and no data set ids specified will simultaneously fit all background data sets to which models have been assigned; no source models ready for fitting will be included in the fit.

Example 4

sherpa> fit(2, bkg_only=True)

In this case, the fit function fits only the background data set(s) associated with data set 2.

Example 5

sherpa> load_data("src.pha")
sherpa> load_bkg("bkg.pha")
sherpa> load_bkg_rmf("rmf.fits")
sherpa> load_bkg_arf("arf.fits")
sherpa> set_bkg_model(powlaw1d.p1)
sherpa> fit_bkg()

Load source and background spectra. Define response files and a model for the background data, then fit it.

Example 6

sherpa> fit(3, outfile="fit_info.out", clobber=True)

In this example, data set 3 will be fit, and all the parameter values and the fit statistic used by the optimization method will be stored in the file "fit_info.out". This file will be overwritten if it already exists because clobber=True.

Example 7

sherpa> fit("leg_m1","leg_p1")

When multiple data sets are indicated, a simultaneous fit is performed and the statistics value is summed over all the data sets. For example, one may wish to simultaneously fit a source and background data set, multiple spectral orders of PHA grating data, etc. In this example, the LEG +1 and -1 spectral orders of an HRC-S/LETG grating data set are fit simultaneously by data ID:

sherpa> fit("leg_m1","leg_p1")
Datasets              = 'leg_m1', 'leg_p1'
Method                = levmar
Statistic             = chi2gehrels
Initial fit statistic = 48024.6
Final fit statistic   = 26310.8 at function evaluation 62
Data points           = 32768
Degrees of freedom    = 32765
Probability [Q-value] = 1
Reduced statistic     = 0.803016
Change in statistic   = 21713.8
   bpow.ampl      0.636989    
   bpow.index1    0.52646     
   bpow.index2    2.16595     

sherpa> print(get_fit_results())
datasets       = ('leg_m1', 'leg_p1')
itermethodname = none
methodname     = levmar
statname       = chi2gehrels
succeeded      = True
parnames       = ('bpow.ampl', 'bpow.index1', 'bpow.index2')
parvals        = (0.63698867502154433, 0.52646030609918448, 2.1659465149789296)
statval        = 26310.806281
istatval       = 48024.6491653
dstatval       = 21713.8428843
numpoints      = 32768
dof            = 32765
qval           = 1.0
rstat          = 0.803015604488
message        = successful termination
nfev           = 62


See the bugs pages on the Sherpa website for an up-to-date listing of known bugs.

See Also

get_iter_method_name, get_iter_method_opt, list_iter_methods, set_iter_method, set_iter_method_opt
calc_chisqr, calc_stat
contour_resid, image_fit