Chandra X-Ray Observatory
Skip to the navigation links
Last modified: 13 March 2017

How can I define my own fit statistic in Sherpa? [Updated]

The load_user_stat function provides an interface for adding user-defined statistic functions in Sherpa. It 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).

For example, a simple user-defined statistic in Python Sherpa would look like this:

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

sherpa> def my_stat_func(data, model, staterror, syserror=None, weight=None, bkg=None): 
    # A simple function to replicate χ2
    fvec = ((data - model) / staterror)**2
    stat = fvec.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)

And 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 of each 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.

Changes in CIAO 4.8

The CIAO 4.8 release supports the bkg keyword, with a default of None, for the statistic function sent to load_user_stat.

Last modified: 13 March 2017
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-2017. All rights reserved.