def calc_wstat_gammapy(): data, model, staterr, off_vec, alpha = get_test_data() from gammapy.stats import wstat from gammapy.stats.fit_statistics import ( _get_wstat_background, _get_wstat_extra_terms, ) # background estimate bkg = _get_wstat_background(data, off_vec, alpha, model) print("Gammapy mu_bkg: {}".format(bkg)) statsvec = wstat(n_on=data, mu_signal=model, n_off=off_vec, alpha=alpha) print("Gammapy stat: {}".format(np.sum(statsvec))) print("Gammapy statsvec: {}".format(statsvec)) print("---> with extra terms") extra_terms = _get_wstat_extra_terms(data, off_vec) print("Gammapy extra terms: {}".format(extra_terms)) statsvec = wstat(n_on=data, mu_signal=model, n_off=off_vec, alpha=alpha, extra_terms=True) print("Gammapy stat: {}".format(np.sum(statsvec))) print("Gammapy statsvec: {}".format(statsvec))
def _excess_matching_significance_fcn(self, excess, significance, index): TS0 = wstat(excess + self.background[index], self.n_off[index], self.alpha[index], 0) TS1 = wstat(excess + self.background[index], self.n_off[index], self.alpha[index], excess) return np.sign(excess) * np.sqrt(np.clip(TS0 - TS1, 0, None)) - significance
def test_wstat_corner_cases(): """test WSTAT formulae for corner cases""" n_on = 0 n_off = 5 mu_sig = 2.3 alpha = 0.5 actual = stats.wstat(n_on=n_on, mu_sig=mu_sig, n_off=n_off, alpha=alpha) desired = 2 * (mu_sig + n_off * np.log(1 + alpha)) assert_allclose(actual, desired) actual = stats.get_wstat_mu_bkg(n_on=n_on, mu_sig=mu_sig, n_off=n_off, alpha=alpha) desired = n_off / (alpha + 1) assert_allclose(actual, desired) # n_off = 0 and mu_sig < n_on * (alpha / alpha + 1) n_on = 9 n_off = 0 mu_sig = 2.3 alpha = 0.5 actual = stats.wstat(n_on=n_on, mu_sig=mu_sig, n_off=n_off, alpha=alpha) desired = -2 * (mu_sig * (1.0 / alpha) + n_on * np.log(alpha / (1 + alpha))) assert_allclose(actual, desired) actual = stats.get_wstat_mu_bkg(n_on=n_on, mu_sig=mu_sig, n_off=n_off, alpha=alpha) desired = n_on / (1 + alpha) - (mu_sig / alpha) assert_allclose(actual, desired) # n_off = 0 and mu_sig > n_on * (alpha / alpha + 1) n_on = 5 n_off = 0 mu_sig = 5.3 alpha = 0.5 actual = stats.wstat(n_on=n_on, mu_sig=mu_sig, n_off=n_off, alpha=alpha) desired = 2 * (mu_sig + n_on * (np.log(n_on) - np.log(mu_sig) - 1)) assert_allclose(actual, desired) actual = stats.get_wstat_mu_bkg(n_on=n_on, mu_sig=mu_sig, n_off=n_off, alpha=alpha) assert_allclose(actual, 0)
def test_wstat(test_data, reference_values): statsvec = stats.wstat( n_on=test_data["n_on"], mu_sig=test_data["mu_sig"], n_off=test_data["n_off"], alpha=test_data["alpha"], extra_terms=True, ) assert_allclose(statsvec, reference_values["wstat"])
def stat_array(self): """Likelihood per bin given the current model parameters""" mu_sig = self.npred_sig().data on_stat_ = wstat( n_on=self.counts.data, n_off=self.counts_off.data, alpha=self.alpha, mu_sig=mu_sig, ) return np.nan_to_num(on_stat_)
def likelihood_per_bin(self): """Likelihood per bin given the current model parameters""" mu_sig = self.npred().data on_stat_ = wstat( n_on=self.counts.data, n_off=self.counts_off.data, alpha=list(self.alpha.data), mu_sig=mu_sig, ) return np.nan_to_num(on_stat_)
def calc_wstat_xspec(): data, model, staterr, off_vec, alpha = get_test_data() from xspec_stats import xspec_wstat as wstat from xspec_stats import xspec_wstat_f, xspec_wstat_d # alpha = t_s / t_b t_b = 1. / alpha t_s = 1 d = xspec_wstat_d(t_s, t_b, model, data, off_vec) f = xspec_wstat_f(data, off_vec, t_s, t_b, model, d) bkg = f * t_b stat = wstat(t_s, t_b, model, data, off_vec) print("XSPEC mu_bkg (f * t_b): {}".format(bkg)) print("XSPEC stat: {}".format(stat))
def calc_wstat_xspec(mu_sig, n_on, n_off, alpha): from xspec_stats import xspec_wstat as wstat from xspec_stats import xspec_wstat_f, xspec_wstat_d td = test_data() # alpha = t_s / t_b t_b = 1. / alpha t_s = 1 d = xspec_wstat_d(t_s, t_b, mu_sig, n_on, n_off) f = xspec_wstat_f(n_on, n_off, t_s, t_b, mu_sig, d) bkg = f * t_b stat = wstat(t_s, t_b, mu_sig, n_on, n_off) print("XSPEC mu_bkg (f * t_b): {}".format(bkg)) print("XSPEC stat: {}".format(stat))
def calc_wstat_gammapy(mu_sig, n_on, n_off, alpha): from gammapy.stats import wstat, get_wstat_mu_bkg, get_wstat_gof_terms # background estimate bkg = get_wstat_mu_bkg(mu_sig=mu_sig, n_on=n_on, n_off=n_off, alpha=alpha) print("Gammapy mu_bkg: {}".format(bkg)) # # without extra terms # statsvec = wstat(extra_terms=False, mu_sig=mu_sig, n_on=n_on, n_off=n_off, # alpha = alpha) # # print("Gammapy stat: {}".format(np.sum(statsvec))) # print("Gammapy statsvec: {}".format(statsvec)) # # print("---> with extra terms") # extra_terms = _get_wstat_extra_terms(n_on = n_on, # n_off = n_off) # print("Gammapy extra terms: {}".format(extra_terms)) statsvec = wstat(extra_terms=True, mu_sig=mu_sig, n_on=n_on, n_off=n_off, alpha=alpha) print("Gammapy stat: {}".format(np.sum(statsvec))) print("Gammapy statsvec: {}".format(statsvec))
def _stat_fcn(self, mu, delta=0, index=None): return wstat(self.n_on[index], self.n_off[index], self.alpha[index], mu) - delta
def TS_max(self): """Stat value for best fit hypothesis, i.e. expected signal mu = n_on - alpha * n_off""" return wstat(self.n_on, self.n_off, self.alpha, self.excess)
def TS_null(self): """Stat value for null hypothesis, i.e. 0 expected signal counts""" return wstat(self.n_on, self.n_off, self.alpha, 0)