class xspec.FitManager

Xspec fitting class.

PyXspec automatically creates a single object of this class, named Fit.


  • error
  • ftest
  • goodness
  • improve
  • perform
  • renorm
  • show
  • steppar
  • stepparResults

Attributes ((*) = get-only)

  • bayes
  • covariance*
  • criticalDelta
  • delta
  • dof*
  • method
  • nIterations
  • nullhyp*
  • nVarPars*
  • query
  • statMethod
  • statTest
  • statistic*
  • testStatistic*
  • weight

Initialize self. See help(type(self)) for accurate signature.

error(argString, respPar=False)

Determine confidence intervals of a fit.


A string with identical syntax to the standard interactive XSPEC error command.

"[[stopat <ntrial> <toler>] [maximum <redchi>] [<delta fit statistic>] [<model param range>...]]"

where <model param range> =::[<modelName>:]<first param> -
<last param>

See the XSPEC manual for a more detailed description.

respPar: Optional flag. Set this to True if the parameters
are response parameters. [bool]

The results of the error command are stored in the error attributes of the individual Parameter objects.


# Estimate the 90% confidence ranges for parameters 1-3
# Repeat but with delta fit statistic = 9.0, equivalent to the
# 3 sigma range.
# Estimate for parameter 3 after setting the number of trials to 20.
# Note that the tolerance field has to be included (or skipped over).
Fit.error("stop 20,,3")
# Perform an error calculation on response parameter 1
ftest(chisq2, dof2, chisq1, dof1)

Calculate the F-statistic and its probability given new and old values of chisq and number of degrees of freedom (DOF).


chisq2: [float]

dof2: [int]

chisq1: [float]

dof1: [int]

chisq2 and dof2 should come from a new fit, in which an extra model component was added to (or a frozen parameter thawed from) the model which gave chisq1 and dof1. If the F-test probability is low then it is reasonable to add the extra model component.


It is not correct to use the F-test statistic to test for the presence of a line (see Protassov et al 2002, ApJ 571, 545).

Returns: The F-test probability [float].

goodness(nRealizations=100, sim=False)

Perform a Monte Carlo calculation of the goodness-of-fit.


nRealizations: Number of spectra to simulate [int].

sim: flag [bool]

If False (default), all simulations are drawn from the best fit model parameter values. If True, parameters will be drawn from a Gaussian centered on the best fit.

Try to find a new minimum.

When Fit.method is set to one of the MINUIT algorithms, this will run the MINUIT 'improve' command. This does nothing when Fit.method is set to Levenberg-Marquardt.


Perform a fit.


Renormalize the model to minimize statistic with current parameters.


setting: [string]

If None, this will perform an explicit immediate renormalization. Other options determine when renormalization will be performed automatically. They are the following strings:

  • 'auto': Renormalize after a model command or parameter change, and at the beginning of a fit.
  • 'prefit': Renormalize only at the beginning of a fit.
  • 'none': Perform no automatic renormalizations.

Show fit information.


Perform a steppar run.

Generate the statistic "surface" for 1 or more parameters.


argString: [string]

This uses identical syntax to the standard interactive XSPEC steppar command. "<step spec> [<step spec> ...]" where:

<step spec> ::= [<log|nolog>] [<current|best>]
[<modName>:]<param index> <low value> <high value> <# steps>

See the XSPEC manual for a more detailed description of specs.


# Step parameter 3 from 1.5 to 2.5 in 10 linear steps
Fit.steppar("3 1.5 2.5 10")
# Repeat the above but with logarithmic steps
# Step parameter 2 linearly from -.2 to .2 in steps of .02
Fit.steppar("nolog 2 -.2 .2 20")

Retrieve values from the most recent steppar run.



argument should either be 'statistic', 'delstat', or a parameter specifier. A parameter specifier should be a string of the form:


or simply an integer <parNum>.

Returns the requested values as a list of floats.


Turn Bayesian inference on or off [string].

Valid settings are 'on', 'off' (default), or 'cons'. 'cons' turns Bayesian inference on AND gives ALL parameters a constant prior. Priors can be set for parameters individually through the Parameter object's prior attribute.


The covariance matrix from the most recent fit [tuple of floats] (GET only).

As with standard XSPEC's "tclout covar", this only returns the diagonal and below-diagonal matrix elements.


Critical delta for fit statistic convergence [float].

The absolute change in the fit statistic between iterations, less than which the fit is deemed to have converged.


Set fit delta values to be proportional to the parameter value [float].

Returns the current proportional setting, or 0.0 if currently using the fixed fit delta values.
Enter the constant factor which will multiply the parameter value to produce a fit delta. A constant factor of 0.0 or negative will turn off the use of proportional fit deltas.

The degrees of freedom for the fit [int] (GET only).


The fitting algorithm to use [string].

Choices are: 'leven', 'migrad', 'minimize', 'monte', 'simplex'. The default is 'leven'.

When setting the method, additional arguments for <nFitIterations> and <fit critical delta> may also be entered. Valid formats for entering multiple arguments are:

# Single string
Fit.method = "migrad  100 .05"
# List of strings
Fit.method = ["migrad","100",".05"]
# List of strings and numbers
Fit.method = ["migrad", 100, .05]

The maximum number of fit iterations prior to query [int].


The number of variable parameters for the fit [int] (GET only).


The null hypothesis probability for the chi-sq fit (GET only).


The fit query setting [string].

  • 'yes': Fit will continue through query.
  • 'no' : Fit will end at query.
  • 'on' : User will be prompted for "y/n" response.

The type of fit statistic in use [string].

Valid names: 'chi' | 'cstat' | 'lstat' | 'pgstat' | 'pstat' | 'whittle'. To set for individual spectra, add a spectrum number (or range) to the string: ie.

>>> Fit.statMethod = "cstat 2"

The type of test statistic in use [string].

Valid names: 'ad' | 'chi' | 'cvm' | 'ks' | 'pchi' | 'runs'. To set for individual spectra, add a spectrum number (or range) to the string: ie.

>>> Fit.statTest = "ad 2"

Fit statistic value from the most recent fit [float] (GET only).

This returns the total fit statistic. An individual spectrum's contribution to the total is stored in the Spectrum object's statistic attribute.


Test statistic value from the most recent fit [float] (GET only).


Change the weighting function used in the calculation of chi-sq [string].

Available functions: 'standard', 'gehrels', 'churazov', 'model'