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 background(self): """Background counts estimated from the marginalized likelihood estimate. See :ref:`wstat` """ mu_bkg = self.alpha.data * get_wstat_mu_bkg( n_on=self.counts.data, n_off=self.counts_off.data, alpha=self.alpha.data, mu_sig=self.npred_sig().data, ) return RegionNDMap.from_geom(geom=self._geom, data=mu_bkg)
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))