From 40d9b13c69c58bd93d73b1b3977fa75ed90c8a1c Mon Sep 17 00:00:00 2001 From: Titus Mombacher Date: Thu, 24 Apr 2025 14:49:23 +0200 Subject: [PATCH 1/2] initial fix for frequentist calculator --- .../hypotests/calculators/frequentist_calculator.py | 2 +- src/hepstats/hypotests/hypotests_object.py | 8 +++----- src/hepstats/hypotests/toyutils.py | 10 ++++++++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/hepstats/hypotests/calculators/frequentist_calculator.py b/src/hepstats/hypotests/calculators/frequentist_calculator.py index a892e6a7..7416829e 100644 --- a/src/hepstats/hypotests/calculators/frequentist_calculator.py +++ b/src/hepstats/hypotests/calculators/frequentist_calculator.py @@ -83,7 +83,7 @@ def qnull( >>> poialt = POI(mean, 1.2) >>> q = calc.qnull(poinull, poialt) """ - toysresults = self.get_toys_null(poinull, poialt, qtilde) + toysresults = self.get_toys_null(poinull, poinull, qtilde) ret = {} for p in poinull: diff --git a/src/hepstats/hypotests/hypotests_object.py b/src/hepstats/hypotests/hypotests_object.py index d922954c..76091683 100644 --- a/src/hepstats/hypotests/hypotests_object.py +++ b/src/hepstats/hypotests/hypotests_object.py @@ -201,7 +201,6 @@ def sampler(self): >>> sampler = calc.sampler() """ - self.set_params_to_bestfit() nevents = [] for m, d in zip(self.loss.model, self.loss.data): nevents_data = get_nevents(d) @@ -251,8 +250,7 @@ def toys_loss(self, parameter_name: str): Example with `zfit`: >>> loss = calc.toys_loss(zfit.Parameter("mean")) """ - if parameter_name not in self._toys_loss: - parameter = self.get_parameter(parameter_name) - sampler = self.sampler() - self._toys_loss[parameter.name] = self.lossbuilder(self.model, sampler) + parameter = self.get_parameter(parameter_name) + sampler = self.sampler() + self._toys_loss[parameter.name] = self.lossbuilder(self.model, sampler) return self._toys_loss[parameter_name] diff --git a/src/hepstats/hypotests/toyutils.py b/src/hepstats/hypotests/toyutils.py index 5ad1fe22..b21eaba0 100644 --- a/src/hepstats/hypotests/toyutils.py +++ b/src/hepstats/hypotests/toyutils.py @@ -232,11 +232,17 @@ def generate_and_fit_toys( # TODO PROFILE THIS poieval: POI values to evaluate the loss function """ - self.set_params_to_bestfit() - minimizer = self.minimizer param = poigen.parameter + # generate toys from best fit at profile point + param.set_value(poigen.value) + is_poi_floating = param.floating + param.floating=False + profile_minimum = minimizer.minimize(loss=self.loss) + if not profile_minimum.valid: print(poigen, "PROBLEM: profile fit not valid, toys won't be either") + param.floating = is_poi_floating + toys_loss = self.toys_loss(poigen.name) sampler = toys_loss.data From 9faf126293a855e42f6660a6e8fbf436d3e1deb4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 24 Apr 2025 13:32:21 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/hepstats/hypotests/toyutils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/hepstats/hypotests/toyutils.py b/src/hepstats/hypotests/toyutils.py index b21eaba0..868c2ea7 100644 --- a/src/hepstats/hypotests/toyutils.py +++ b/src/hepstats/hypotests/toyutils.py @@ -238,9 +238,10 @@ def generate_and_fit_toys( # TODO PROFILE THIS # generate toys from best fit at profile point param.set_value(poigen.value) is_poi_floating = param.floating - param.floating=False + param.floating = False profile_minimum = minimizer.minimize(loss=self.loss) - if not profile_minimum.valid: print(poigen, "PROBLEM: profile fit not valid, toys won't be either") + if not profile_minimum.valid: + print(poigen, "PROBLEM: profile fit not valid, toys won't be either") param.floating = is_poi_floating toys_loss = self.toys_loss(poigen.name)