Optimization commands

Once the data have been read in and the model has been set, the model parameters can be optimized to fit the data. The methods in this section are used for the optimization and determination of the uncertainties in the fit. Fit

The SPEX command to fit is simply fit and this has been implemented as fit in PYSPEX as well:


Fit command.


niter (int) – (Optional) Maximum number of iterations.

This method can be called without any arguments. Optionally, the number of iteration steps can be given, but this is only useful in some particular cases. When done fitting, the values of the fit statistics can be found in the object s.opt_fit. See the Fit() class for details.


>>> s.fit()
>>> s.fit(niter=50) Fit results

The resulting C-statistics/Chi-square value and degrees of freedom can be obtained with separate commands:


Get the current C-statistics from the model.


Tuple containing the C-stat value and the degrees of freedom.

Return type



Get the current Chi2-statistics from the model.


Tuple containing the \chi^2 value and the degrees of freedom.

Return type


These commands return a tuple with the latest statistics value and the degrees of freedom.


>>> cstat = s.fit_cstat()
>>> print(cstat)
(2979.7792968, 3000)
>>> chisq = s.fit_chisq()
>>> print(chisq)
(2997.3247823, 3000) Fit statistics

Although C-statistics is recommended for Poisson distributed data like X-ray spectra, sometimes it may be better to switch to chi^2 statistics. This can be done using the fit_stat method:


Set the fit statistics.


stat (str) – Fit statistics, for example: ‘csta’, ‘chi2’, ‘wsta’

The statistics (stat) can be either csta (C-statistics), chi2 (Chi-square statistics), or wsta (W-statistics). The W-statistics is added to SPEX for reference, but is not recommended for use in analysis.


>>> s.fit_stat('chi2') Fit verbosity

The intermediate results from the fit iterations can be shown in the terminal (and in a pgplot window). In PYSPEX, this feature is on by default. If you want to turn it off, then call


Set the fit output verbosity. Default is true.


status (bool) – Fit output verbosity setting.

with False as status. The intermediate steps will not be printed anymore.


>>> s.fit_print(False) Error

The command for calculating errors on fitted parameters is error and has a direct equivalent in PYSPEX:

Session.error(isect, icomp, name, dchi=None)[source]

Calculate the error for a parameter (name) in sector (isect) and component (icomp).

  • isect (int) – Sector number of the component to calculate the error for.

  • icomp (int) – Component number to calculate the error for.

  • name (str) – Parameter name to calculate the error for.

  • dchi (float) – (Optional) \Delta\chi^2 value to optimize for (Default: 1.0, 68% errors)


An Error object from pyspex.

Return type


The method calculates the lower and upper error value for the parameter with name name in sector isect and component icomp. Optionally, the target delta-c-stat value can be set. By default, dchi is set to 1.0, which results in 68% (1 sigma) errors.

The error command returns an object with the results of the error calculation. See the Error() class definition elsewhere in this manual.


>>> err_si = s.error(1,1,'14')
>>> err_fe = s.error(1,1,'26',dchi=2.71)
>>> print(err_fe.value, err_fe.lerr, err_fe.uerr)
1.023902 -0.119324 0.109223