Last modified: November 2016

Jump to: Description · Examples · Bugs · See Also

AHELP for CIAO 4.12 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.

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, bkg=None):

def calc_err_func(data):



Argument Description
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)
bkg The background data (if any); this is new in CIAO 4.8

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, bkg=None):
    if staterror is None:
        staterror = 1
    chi2 = (data - model) / staterror  # Chi per bin
    return (chi2**2).sum(), chi2


Example 1

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

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. Note that the call to set_stat uses the variable name, not a string.

Example 2

sherpa> load_user_stat("mystat", my_stat_func)
sherpa> set_stat(mystat)

Create a new user statistic with the definition of chi2 statistic, where my_stat_func is defined as:

def my_stat_func(data, model, staterror,
                 syserror=None, weight=None, bkg=None):
    fvec = (data - model) / staterror
    stat = (fvec**2).sum()
    return (stat, fvec) 


See the bugs pages on the Sherpa website for an up-to-date listing of known bugs.

See Also

dataspace1d, dataspace2d, datastack, 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_template_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