FitManager

class xspec.FitManager

Xspec fitting class.

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

Methods

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

Attributes ((*) = get-only)

  • bayes
  • covariance*
  • criticalDelta
  • delta
  • dof*
  • method
  • nIterations
  • nullhyp*
  • query
  • statMethod
  • statTest
  • statistic*
  • testStatistic*
  • weight
error(argString)

Determine confidence intervals of a fit.

Args:
argString:

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.

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

Examples:

# Estimate the 90% confidence ranges for parameters 1-3
Fit.error("1-3")
# Repeat but with delta fit statistic = 9.0, equivalent to the
# 3 sigma range.
Fit.error("9.0")
# 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")
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).

Args:

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.

Warning

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.

Args:

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.
improve()

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

Perform a fit.

renorm(setting=None)

Renormalize the model to minimize statistic with current parameters.

Args:

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

Show fit information.

steppar(argString)

Perform a steppar run.

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

Args:

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.

Examples:

# 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
Fit.steppar("log")
# Step parameter 2 linearly from -.2 to .2 in steps of .02
Fit.steppar("nolog 2 -.2 .2 20")
stepparResults(arg)

Retrieve values from the most recent steppar run.

Args:

arg:

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

'[<modName>:]<parNum>'

or simply an integer <parNum>.

Returns the requested values as a list of floats.

bayes

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.

covariance

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.

criticalDelta

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.

delta

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

Get:
Returns the current proportional setting, or 0.0 if currently using the fixed fit delta values.
Set:
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.
dof

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

method

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

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

nullhyp

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

query

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

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

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

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

testStatistic

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

weight

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

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