Chandra X-Ray Observatory
Skip to the navigation links
Last modified: December 2010

Jump to: Description · Example · See Also

AHELP for CIAO 4.3 Sherpa v1


Context: statistics


Load a user-defined fit statistic


load_user_stat(statname, calc_stat_func, calc_err_func=None, priors={})


The load_user_stat command loads a user-defined statistic which can be used during fitting. After loading the function, use the set_stat() command ("ahelp set_stat") to set it as the method to be used in fitting.

  • statname - name of the new statistic
  • calc_stat_func - reference to the function that calculates the statistic
  • calc_err_func - reference to the function that will calculate errors used in the statistic calculation; default=None (Note: if the statistic doesn't require errors, such as a likelihood function, calc_err_func should be left at its default.)
  • priors - a dictionary of prior functions for the user statistic. If there are no such functions, leave this as an empty dictionary (i.e., {}).

The statistic and error functions may defined at the Sherpa command line or read in by a script. The format of the calc_stat_func and calc_err_func are:

def calc_stat_func(data, model, staterror=None, syserror=None, weight=None):

def calc_err_func(data):


  • data - array of y-values from the data set
  • model - array of predicted y-values calculated from model
  • staterror - array of statistical errors (if any)
  • syserror - array of systematic errors (if any)
  • weight - array of weights (if any)

The calc_stat_func function must return a two-element tuple with values (stat_value, stat_err_per_bin). stat_value is the statistic value and stat_err_per_bin is a numpy array with the statistic contribution per data bin.

The calc_err_func function must return a numpy array with the statistical error corresponding to each data bin.

The following functions show a simple example of defining a custom statistic to illustrate the basic code and interface requirements:

def custom_staterr_func(data):
    staterror = 10 # Constant one-sigma uncertainty of 10
    return staterror * numpy.ones_like(data)

def custom_stat_func(data, model, staterror=None, syserror=None, weight=None):
    if staterror is None:
        staterror = 1
    chi2 = ((data - model) / staterror)**2 # Chi^2 per bin
    return chi2.sum(), chi2


sherpa> load_user_stat("new_stat", custom_stat_func,
sherpa> set_stat( "new_stat" )
sherpa> load_user_stat("new_stat", custom_stat_func,

Create a new user statistic from the functions "custom_stat_func" and "custom_staterr_func" which have been previously defined. Then set "new_stat" as the current fit statistic.

See Also

dataspace1d, dataspace2d, fake, load_arf, load_arrays, load_ascii, load_bkg, load_bkg_arf, load_bkg_rmf, load_data, load_grouping, load_image, load_multi_arfs, load_multi_rmfs, load_pha, load_quality, load_rmf, load_staterror, load_syserror, load_table, pack_image, pack_pha, pack_table, unpack_arf, unpack_arrays, unpack_ascii, unpack_bkg, unpack_data, unpack_image, unpack_pha, unpack_rmf, unpack_table
get_default_id, list_bkg_ids, list_data_ids
add_model, add_user_pars, load_table_model, load_user_model, save_model, save_source
save_arrays, save_data, save_delchi, save_error, save_filter, save_grouping, save_image, save_pha, save_quality, save_resid, save_staterror, save_syserror, save_table

Last modified: December 2010
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: Smithsonian Institution, Copyright © 1998-2019. All rights reserved.