6.1.2.4. 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.

6.1.2.4.1. Fit

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

Session.fit(niter=100)[source]

Fit command.

Parameters:

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.

Examples:

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

6.1.2.4.1.1. Fit results

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

Session.fit_cstat()[source]

Get the current C-statistics from the model.

Returns:

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

Return type:

tuple

Session.fit_chisq()[source]

Get the current Chi2-statistics from the model.

Returns:

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

Return type:

tuple

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

Examples:

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

6.1.2.4.1.2. 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:

Session.fit_stat(stat)[source]

Set the fit statistics.

Parameters:

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.

Example:

>>> s.fit_stat('chi2')

If you have more instruments or regions which require a different fit statistic (optical or UV spectra for example), then it is possible to set a different statistic for specific regions with the fit_stat_inst command:

Session.fit_stat_inst(stat, inst, reg)[source]

Set the fit statistics for a particular instrument and region.

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

  • inst (int) – Instrument number.

  • reg (int) – Region number.

One needs to specify the instrument and region number for which the new statistics apply.

Example:

>>> s.fit_stat_inst('chi2',2,1)

6.1.2.4.1.3. Fit method

Next to the default Levenberg-Marquardt algorithm, SPEX also offers Simplex and Simulated annealing methods (see Fit: spectral fitting). The method can be selected using the following command:

Session.fit_method(method)[source]

Set the fit statistics for a particular instrument and region.

Parameters:

method – Fit method: classical, simplex or anneal

The simulated annealing method has a number of parameters that can be altered. This can be done using the fit_set_ann method:

Session.fit_set_ann(param, value)[source]

Set the simulating annealing method parameters.

Parameters:
  • param (str) – Type of annealing parameter (rt, t, eps, vm, ns, max, or print).

  • value (float) – Value of the parameter (will be converted to the nearest int if necessary).

The current parameters can also be found in the s.opt_fit object. See Fitting spectra for more information.

Examples:

>>> s.fit_method('ann')
>>> s.fit_set_ann('rt', 0.85)

6.1.2.4.1.4. 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

Session.fit_print(status)[source]

Set the fit output verbosity. Default is true.

Parameters:

status (bool) – Fit output verbosity setting.

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

Example:

>>> s.fit_print(False)

6.1.2.4.2. 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).

Parameters:
  • 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)

Returns:

An Error object from pyspex.

Return type:

pyspex.optimize.Error

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.

Examples:

>>> 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