Optimization & Simulation 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:
- Spex.fit(niter=100)¶
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)
Fit results¶
The resulting C-statistics/Chi-square value and degrees of freedom can be obtained with separate commands:
- Spex.fit_cstat()¶
Get the current C-statistics from the model.
- Returns:
Tuple containing the C-stat value and the degrees of freedom.
- Return type:
tuple
- Spex.fit_chisq()¶
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)
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:
- Spex.fit_stat(stat)¶
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:
- Spex.fit_stat_inst(stat, inst, reg)¶
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)
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:
- Spex.fit_method(method)¶
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:
- Spex.fit_set_ann(param, value)¶
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)
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
- Spex.fit_print(status)¶
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)
Error¶
The command for calculating errors on fitted parameters is error and has a direct equivalent
in PYSPEX:
- Spex.error(isect, icomp, name, dchi=None)¶
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
Simulate¶
Based on an instrument response and a model spectrum, SPEX can simulate an observed spectrum
using the simulate commands. In SPEX, the options can be set by giving multiple commands,
but in PYSPEX this is done in a single command with optional parameters:
- Spex.simulate(extime, ssys=None, bsys=None, noise=None, bnoise=None, seed=None)¶
Simulate a spectrum using a loaded response file.
- Parameters:
extime (float) – Exposure time to simulate.
inst (str) – (Optional) Instrument range to simulate (default all)
reg (str) – (Optional) Region range to simulate (default all)
ssys (float) – (Optional) Add a systematic error to the source spectrum (Default 0).
bsys (float) – (Optional) Add a systematic error to the background spectrum (Default 0).
noise (bool) – (Optional) Add Poisson noise to the simulated source spectrum (Default True).
bnoise (bool) – (Optional) Add Poisson noise to the simulated background spectrum (Default False).
seed (int) – (Optional) Set the random seed for the simulation (Default: system clock).
The only required parameter is extime, which is the desired exposure time (float) for the simulation.
The optional parameters are:
instThe instrument number range to simulate (string). Just like in SPEX, the range can be provided by giving two numbers with a:in between, like1:3.regThe region number range to simulate (string). This has the same behaviour asinst.ssysAdds a systematic error to the source spectrum (float, default is 0.).bsysAdds a systematic error to the background spectrum (float, default is 0.).noiseAdds Poisson noise to the simulated spectrum (default is True).bnoiseAdds Poisson noise the the background spectrum (default is False).seedProvide a random seed for the simulation (by default the seed is randomly generated using the system clock).
For example:
>>> s.simulate(1E+5, inst='1:3', bnoise=True, seed=42)