Chandra X-Ray Observatory
Skip to the navigation links
Last modified: December 2010

Jump to: Description · Examples · Bugs · See Also

AHELP for CIAO 4.3 Sherpa v1


Context: fitting


Perform a fit using current optimization method and fit statistic.


fit( [ id, [otherids], bkg_only=False, outfile="fit.out", clobber=True])
fit_bkg( [ id, [otherids], 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.

  • id, otherids - the id(s) of the data set(s) to use; default is to fit all datasets for which a model has been defined
  • bkg_only - a Boolean switch to indicate if only background data sets should be fit; default=False
  • outfile - name of the file in which to store all parameter values and fit statistic obtained during the fitting process (e.g., "fit.out")
  • clobber - a Boolean switch which indicates whether or not the 'outfile' should be overwritten, if the file already exists; default=True

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")
	     Initial fit statistic = 21857.8
	     Final fit statistic   = 6662.04 at function evaluation 52
	     Data points           = 3162
	     Degrees of freedom    = 3158
	     Probability [Q-value] = 9.26899e-252
	     Reduced statistic     = 2.10958
	     Change in statistic   = 15195.8
	     bpow.phoindx1   1           
	     bpow.breake    0.763347    
	     bpow.phoindx2   1.79799     
	     bpow.norm      0.0294637
        sherpa> print get_fit_results()
	     methodname = LevMar
	     succeeded  = True
	     parnames   = ('bpow.phoindx1', 'bpow.breake', 'bpow.phoindx2', 'bpow.norm')
	     parvals    = (1.00000005713, 0.763346621728, 1.79799457922, 0.0294636807334)
	     covarerr   = None
	     statval    = 6662.04434909
	     istatval   = 21857.8439964
	     dstatval   = 15195.7996473
	     numpoints  = 3162
	     dof        = 3158
	     qval       = 9.26899264507e-252
	     rstat      = 2.10957705798
	     message    = relative error between two consecutive
	     iterates is at most xtol=1.19209e-07
	     nfev       = 52         


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

Last modified: December 2010
Smithsonian Institute Smithsonian Institute

The Chandra X-Ray Center (CXC) is operated for NASA by the Smithsonian Astrophysical Observatory. 60 Garden Street, Cambridge, MA 02138 USA.   Email: Smithsonian Institution, Copyright © 1998-2019. All rights reserved.