| AHELP for CIAO 4.5 Sherpa v1 | load_user_stat |
Context: statistics |
Synopsis
Load a user-defined fit statistic
Syntax
load_user_stat(statname, calc_stat_func, calc_err_func=None, priors={})
Description
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):
where
- 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) # Chi per bin
return (chi2**2).sum(), chi2
Example 1
sherpa> load_user_stat("new_stat", custom_stat_func,
custom_staterr_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.
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):
fvec = ((data - model) / staterror)
stat = (fvec**2).sum()
return (stat, fvec)
See Also
- data
- 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
- filtering
- load_filter
- info
- get_default_id, list_bkg_ids, list_data_ids
- modeling
- add_model, add_user_pars, load_table_model, load_template_model, load_user_model, save_model, save_source
- saving
- 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

![[Sherpa Logo]](../imgs/sherpa_logo_navbar.gif)