Last modified: December 2024

URL: https://cxc.cfa.harvard.edu/sherpa/ahelp/fit.html
AHELP for CIAO 4.17 Sherpa

fit

Context: fitting

Synopsis

Fit a model to one or more data sets.

Syntax

fit(id=None, *otherids, **kwargs)

Description

The function is also called simulfit().

Use forward fitting to find the best-fit model to one or more data sets, given the chosen statistic and optimization method. The fit proceeds until the results converge or the number of iterations exceeds the maximum value (these values can be changed with `set_method_opt` ). An iterative scheme can be added using `set_iter_method` to try and improve the fit. The final fit results are displayed to the screen and can be retrieved with `get_fit_results` .


Examples

Example 1

Simultaneously fit all data sets with models and then store the results in the variable fres:

>>> fit()
>>> fres = get_fit_results()

Example 2

Fit just the data set 'img':

>>> fit('img')

Example 3

Simultaneously fit data sets 1, 2, and 3:

>>> fit(1, 2, 3)

Example 4

Fit data set 'jet' and write the fit results to the text file 'jet.fit', over-writing it if it already exists:

>>> fit('jet', outfile='jet.fit', clobber=True)

Example 5

Store the per-iteration values in a StringIO object and extract the data into the variable txt (this avoids the need to create a file):

>>> from io import StringIO
>>> out = StringIO()
>>> fit(outfile=out)
>>> txt = out.getvalue()

PARAMETERS

The parameters for this function are:

Parameter Type information Definition
id int or str, optional The data set that provides the data. If not given then all data sets with an associated model are fit simultaneously.
*otherids sequence of int or str, optional Other data sets to use in the calculation.
outfile str, Path, IO object, or None, optional If set, then the fit results will be written to a file with this name. The file contains the per-iteration fit results.
clobber bool, optional This flag controls whether an existing file can be overwritten ( True ) or if it raises an exception ( False , the default setting). This is only used if `outfile` is set to a string or Path object.

Notes

For PHA data sets with background components, the function will fit any background components for which a background model has been created (rather than being subtracted). The `fit_bkg` function can be used to fit models to just the background data.

If outfile is sent a file handle then it is not closed by this routine.

Changes in CIAO

Changed in CIAO 4.17

The outfile parameter can now be sent a Path object or a file handle instead of a string.


Bugs

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

See Also

fitting
fit_bkg, get_fit_results, simulfit
info
show_fit
methods
get_iter_method_name, get_iter_method_opt, list_iter_methods, set_iter_method, set_iter_method_opt
modeling
freeze, set_bkg_full_model, set_bkg_model, set_full_model, set_model, thaw
plotting
plot_fit, plot_fit_delchi, plot_fit_resid
statistics
get_stat, get_stat_name
utilities
calc_chisqr, calc_stat
visualization
contour_resid, image_fit