Chandra X-Ray Observatory
	(CXC)
Skip to the navigation links
Last modified: 26 October 2016

URL: http://cxc.harvard.edu/sherpa/statistics/index.html

Sherpa Statistics


The following fit statistics are available in Sherpa:

For a detailed explanation of the fitting concepts behind X-ray spectral analysis in Sherpa, see the Sherpa documents Spectral Fitting and Statistics: a chapter for "X-ray Astronomy Handbook" on the Sherpa References page.


cash

A Poisson log-likelihood function.

Counts are sampled from the Poisson distribution, and so the best way to assess the quality of model fits is to use the product of individual Poisson probabilities computed in each bin i, or the likelihood ℒ:

L = (product)_i [ M(i)^N(i)/N(i)! ] * exp[-M(i)]

where M=S+B is the sum of source and background model amplitudes, and D is the number of observed counts, in bin i.

The Cash statistic (Cash 1979, ApJ 228, 939) is derived by (1) taking the logarithm of the likelihood function, (2) changing its sign, (3) dropping the factorial term (which remains constant during fits to the same dataset), and (4) multiplying by two:

C = 2 * (sum)_i [ M(i) - D(i) log M(i) ]

The factor of two exists so that the change in Cash statistic from one model fit to the next, ΔC, is distributed approximately as Δχ2 when the number of counts in each bin is high (> 5). One can then in principle use ΔC instead of Δχ2 in certain model comparison tests. However, unlike χ2, the Cash statistic may be used regardless of the number of counts in each bin.

The magnitude of the Cash statistic depends upon the number of bins included in the fit and the values of the data themselves. Hence one cannot analytically assign a goodness-of-fit measure to a given value of the Cash statistic. Such a measure can, in principle, be computed by performing Monte Carlo simulations. One would repeatedly sample new datasets from the best-fit model, and fit them, and note where the observed Cash statistic lies within the derived distribution of Cash statistics.

Note on Background Subtraction

The background should not be subtracted from the data when this statistic is used. It should be modeled simultaneously with the source.

Examples

  1. Specify the fitting statistic and then confirm it has been set.

    sherpa> set_stat('cash')
    sherpa> print(get_stat_name())
    cash
    

cstat

A Poisson log-likelihood function.

The CSTAT statistic is equivalent to the XSPEC implementation of the Cash statistic when the data set has no associated background or a model is to be fit to the background. The wstat statistic, added in CIAO 4.8, should be used if the background is to be included as a model component, rather than fit separately.

The following is based on the Poisson data (cstat) discussion from XSPEC.

Counts are sampled from the Poisson distribution, and so the best way to assess the quality of model fits is to use the product of individual Poisson probabilities computed in each bin i, or the likelihood ℒ:

L = (product)_i [ M(i)^N(i)/N(i)! ] * exp[-M(i)]

where M=S+B is the sum of source and background model amplitudes, and D is the number of observed counts, in bin i.

The CSTAT statistic is derived by (1) taking the natural logarithm of the likelihood function, (2) changing its sign, (3) approximating the factorial term by log (D_i!) = D_i log(D_i), (4) adding an extra data-dependent term, and (4) multiplying by two:

C = 2 * (sum)_i [ M(i) - D(i) + D(i)*[log D(i) - log M(i)] ]

The factor of two exists so that the change in CSTAT statistic from one model fit to the next, ΔC, is distributed approximately as Δχ2 when the number of counts in each bin is high (> 5). One can then in principle use ΔC instead of Δχ2 in certain model comparison tests. However, unlike χ2, the CSTAT statistic may be used regardless of the number of counts in each bin.

The advantage of CSTAT over Sherpa's implementation of CASH is that one can assign an approximate goodness-of-fit measure to a given value of the CSTAT statistic, i.e., the observed statistic, divided by the number of degrees of freedom, should be of order 1 for good fits.

Note on Background Subtraction

The background should not be subtracted from the data when this statistic is used. It should be modeled simultaneously with the source. An alternative is to use the wstat statistic, added in CIAO 4.8.

Examples

  1. Specify the fitting statistic and then confirm it has been set.

    sherpa> set_stat("cstat")
    sherpa> print(get_stat_name())
    cstat
    

wstat

A Poisson log-likelihood function with Poisson background.

The WSTAT statistic is equivalent to the XSPEC implementation of the Cash statistic when the data set has an associated background component. The cstat statistic should be used instead if no background is present, or a model is to be fit to the background.

The statistic is described in the Poisson data with Poisson background (cstat) section of the XSPEC documentation.

Note on Background Subtraction

The background should not be subtracted from the data when this statistic is used. It will be automatically included in the fit, but note that care should be taken when interpreting the results, since the behavior of the statistic with low counts is not guaranteed.

An alternative is to use the cstat statistic, and fit a model to the background.

[WARNING]
Warning

The background is included when fitting the data, and calculating statistics, but commands like plot_model and plot_fit do not . This means that the displayed fit will be lower than the data (the difference depends on the contribution of the background to the source region).

Examples

  1. Specify the fitting statistic and then confirm it has been set.

    sherpa> set_stat("wstat")
    sherpa> print(get_stat_name())
    wstat
    

χ2 statistics:

A Gaussian log-likelihood function.

Counts are sampled from the Gaussian (Normal) distribution, and so the best way to assess the quality of model fits is to use the product of individual Gaussian probabilities computed in each bin i, or the likelihood ℒ:

L = (product)_i [ 1 / sigma^2 (2*pi)^0.5] * exp[(N(i)-M(i))^2/sigma^2]

where M=S+B is the sum of source and background model amplitudes, and N is the number of observed counts, in bin i.

The χ2 statistic can be derived by taking the natural log of the likelihood function, (2) multiplying by two, and (3) ignoring the terms which depend only on the data.

chi^2 = (sum)_i [ [ N(i,S) - B(i,x,pB) - S(i,x,pS) ]^2 / sigma(i)^2 ]

where Ni,S  is the total number of observed counts in bin i of the on-source region; B(x, p) is the number of predicted background model counts in bin i of the on-source region (zero for background-subtracted data), rescaled from bin i of the off-source region, and computed as a function of the model argument x (e.g., energy or time) and set of background model parameter values p; S(x, p) is the number of predicted source model counts in bin i, as a function of the model argument x and set of source model parameter values p; and σi  is the error in bin i.

The options for assigning σi  are described in the documentation for chi2datavar, chi2gehrels, chi2modvar, and the Primini fit statistic. In each of these descriptions, Ni,B  is the total number of observed counts in bin i of the off-source region; A is the off-source "area," which could be the size of the region from which the background is extracted, or the length of a background time segment, or a product of the two, etc.; and A is the on-source "area."

In the analysis of PHA data, A is the product of the BACKSCAL and EXPTIME FITS header keyword values, provided in the file containing the background data. A is computed similarly, from keyword values in the source data file.

Note that in the current version of Sherpa, it is assumed that there is a one-to-one mapping between a given background region bin and a given source region bin. For instance, in the analysis of PHA data, it is assumed that the input background counts spectrum is binned in exactly the same way as the input source counts spectrum, and any filter applied to the source spectrum automatically applied to the background spectrum. This means that currently, the user cannot, e.g., specify arbitrary background and source regions in two dimensions and get correct results. This will be changed in a future version of Sherpa.

(However, this limitation only applies when analyzing background data that have been entered with the load_bkg command. One can always enter the background as a separate dataset and jointly fit the source and background regions.)


leastsq

The leastsq statistic is equivalent to chi2constvar, except the variance is always set to 1.

Examples

  1. Set the fitting statistic and then confirm the new value:

    sherpa> set_stat("leastsq")
    sherpa> print(get_stat_name())
    leastsq
    

chi2constvar

χ2 statistic with constant variance computed from the counts data. (CHI PARENT | CHI CVAR in CIAO 3.4)

In some applications, the variance can be assumed to be constant and for this statistics the variance is calculated as the mean number of counts, or

sigma(i)^2 = (1/N) * (sum)_(j=1)^N N(j,S) + [A(S)/A(B)]^2 N(j,B) ,

where N is the number of on-source (and off-source) bins included in the fit. The background term appears only if a background region is specified and background subtraction is done.

See the section on χ2 statistics for more information, including definitions of the additional quantities shown in the equation.

Examples

  1. Specify the fitting statistic and then confirm it has been set.

    sherpa> set_stat('chi2constvar')
    sherpa> print(get_stat_name())
    chi2constvar
    

chi2datavar

χ2 statistic with variance computed from the data. (CHI DVAR in CIAO 3.4)

If the number of counts in each bin is large (> 5), then the shape of the Poisson distribution from which the counts are sampled tends asymptotically towards that of a Gaussian distribution, with variance

sigma(i)^2 = N(i,S) + [A(S)/A(B)]^2 N(i,B) .

The background term appears only if a background region is specified and background subtraction is done. See the section on χ2 statistics for more information, including definitions of the additional quantities shown in the equation.

Examples

  1. Specify the fitting statistic and then confirm it has been set.

    sherpa> set_stat('chi2datavar')
    sherpa> print(get_stat_name())
    chi2datavar
    

chi2gehrels

χ2 statistic with the Gehrels variance function.

This is the Sherpa default statistic.

If the number of counts in each bin is small (< 5), then we cannot assume that the Poisson distribution from which the counts are sampled has a nearly Gaussian shape. The standard deviation (i.e., the square-root of the variance) for this low-count case has been derived by Gehrels (1986):

sigma(i,S) = 1 + (sqrt)[N(i,S)+0.75]
sigma(i,B) = 1 + (sqrt)[N(i,B)+0.75]

Higher-order terms have been dropped from the expression; it is accurate to approximately one percent. If one does not perform background subtraction, then σi,S ; otherwise, one may use standard error propagation to estimate that

sigma(i)^2 = sigma(i,S)^2 + [A(S)/A(B)]^2 sigma(i,B)^2 .

The background term appears only if a background region is specified and background subtraction is done. See the section on χ2 statistics for more information, including definitions of the additional quantities shown in the equation.

Note on Background Subtraction

We have not determined the accuracy of the latter expression, thus the user should proceed with caution when subtracting background from the raw data when using this statistic. An approach preferable to background subtraction is to model the background and data simultaneously.

Examples

  1. Specify the fitting statistic and then confirm it has been set.

    sherpa> set_stat("chi2gehrels")
    sherpa> print(get_stat_name())
    chi2gehrels
    

chi2modvar

χ2 statistic with variance computed from model amplitudes. (CHI MVAR in CIAO 3.4)

This statistic is equivalent to chi2datavar , except that the variance is estimated using the background and source model amplitudes rather than the observed counts data:

sigma(i)^2 = S(i) + [A(S)/A(B)]^2 B(i,off) ,

where Bi,off  is the background model amplitude in bin i of the off-source region. See the section on χ2 statistics for more information, including definitions of the additional quantities shown in the equation.

Note on Background Subtraction

The background should not be subtracted from the data when this statistic is used. chi2modvar underestimates the variance when fitting background-subtracted data.

Examples

  1. Specify the fitting statistic and then confirm it has been set.

    sherpa> set_stat("chi2modvar")
    sherpa> print(get_stat_name())
    chi2modvar
    

chi2xspecvar

χ2 statistic with variance computed from data amplitudes.

This statistic is equivalent to chi2datavar , except that when the number of counts in a channel bin is less than 1 the variance is set to 1.


Primini

This is a χ2 statistic with variance computed from model amplitudes derived in the iterative process.

Iterative fitting with the Primini method takes effect when the fit function is called, after changing the current iterative fitting method from the default value of 'none' to 'primini' with 'set_iter_method("primini").' This is identical to running 'fit()' in Sherpa in the usual way, except that it modifies the chosen χ2 fit statistic to use the Primini variance function. This 'Iterative Weighting' (IW; see Wheaton et al. 1995, ApJ 438, 322) attempts to remove biased estimates of model parameters which is inherent in χ2 statistics (see Kearns, Primini, & Alexander, 1995, ADASS IV, 331).

The variance in bin i is estimated to be:

sigma(i)^2 = S(i,pS^(j-1)) + [A(S)/A(B)]^2 B_off(i,x_i,pB^(j-1)) ,

where j is the number of iterations that have been carried out in the fitting process, Boff is the background model amplitude in bin i of the off-source region, and θSj-1 and θBj-1 are the set of source and background model parameter values derived during the iteration previous to the current one. The variances are set to an array of ones on the first iteration.

In addition to reducing parameter estimate bias, this statistic can be used even when the number of counts in each bin is small (< 5), although the user should proceed with caution.

Note on Background Subtraction

The background should not be subtracted from the data when this statistic is used. The Primini fit statistic underestimates the variance when fitting background-subtracted data.

Example

  1. To use the Primini iterative fitting method (with χ2 statistics only), set the current iterative fitting method to "primini" with the set_iter_method function:

    sherpa> print(get_iter_method_name())
    none
    sherpa> set_iter_method("primini")
    sherpa> set_stat("chi2gehrels")
    sherpa> fit()
    

User statistic

It is possible for the user to create and implement his or her own fit statistic function within Sherpa. A fit statistic is the measure one uses to compare predicted model data to real measured data. It is by minimizing the fit statistic that one finds a good fit for a model.

The load_user_stat() Sherpa function accommodates user-defined functions for a statistic and statistical errors, in addition to defining a list of model parameters and hyperparameters for prior distributions (if prior desired).

[NOTE]
Changes in CIAO 4.8

The function interface for user statistics has changed, since the bkg parameter has been added (with a default value of None).

Examples

  1. A simple user-defined statistic in Python Sherpa would be defined as follows:

    sherpa> def my_stat_func(data, model, staterror, syserror=None, weight=None, bkg=None): 
        "A simple function to replicate χ2"
        fvec = ((data - model) / staterror)
        stat = (fvec**2).sum()
        return (stat, fvec)
    
    sherpa> def my_staterr_func(data):
        "A simple staterror function"
        return numpy.sqrt(data)
    
    sherpa> load_user_stat("mystat", my_stat_func, my_staterr_func)
    
    sherpa> set_stat(mystat)
    
  2. A more complex user-defined statistic, with prior distributions, would look like this:

    sherpa> def my_stat_func(data, model, staterror=None, syserror=None, weight=None, bkg=None):
        ...
        return (stat, fvec)
    
    sherpa> load_user_stat("mystat", my_stat_func, priors=dict(mugamma=0.017, ..., gamma=abs1.nh, ... ))
    
    sherpa> set_stat(mystat)
    

    In these examples, 'stat' is a scalar statistic value and 'fvec' is an array of the statistic contributions per data bin. This method caters to both types of optimization methods in Sherpa: levmar expects the statistic contribution per bin, whereas simplex and moncar expect a single scalar statistic value.


Last modified: 26 October 2016
Smithsonian Institute Smithsonian Institute

The Chandra X-Ray Center (CXC) is operated for NASA by the Smithsonian Astrophysical Observatory. 60 Garden Street, Cambridge, MA 02138 USA.   Email:   cxcweb@head.cfa.harvard.edu Smithsonian Institution, Copyright © 1998-2016. All rights reserved.