Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use MLEs of NPs to create sampling distributions in ToyCalculator #1610

Merged
merged 8 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/contributors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ Contributors include:
- Henry Schreiner
- Saransh Chopra
- Sviatoslav Sydorenko
- Mason Proffitt
30 changes: 21 additions & 9 deletions src/pyhf/infer/calculators.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ def distributions(self, poi_test, track_progress=None):
... )
>>> sig_plus_bkg_dist, bkg_dist = toy_calculator.distributions(mu_test)
>>> sig_plus_bkg_dist.pvalue(mu_test), bkg_dist.pvalue(mu_test)
(array(0.14), array(0.76))
(array(0.14), array(0.79))

Args:
poi_test (:obj:`float` or :obj:`tensor`): The value for the parameter of interest.
Expand All @@ -699,14 +699,26 @@ def distributions(self, poi_test, track_progress=None):
tensorlib, _ = get_backend()
sample_shape = (self.ntoys,)

signal_pars = self.pdf.config.suggested_init()
signal_pars[self.pdf.config.poi_index] = poi_test
signal_pdf = self.pdf.make_pdf(tensorlib.astensor(signal_pars))
signal_pars = fixed_poi_fit(
poi_test,
self.data,
self.pdf,
self.init_pars,
self.par_bounds,
self.fixed_params,
)
signal_pdf = self.pdf.make_pdf(signal_pars)
signal_sample = signal_pdf.sample(sample_shape)

bkg_pars = self.pdf.config.suggested_init()
bkg_pars[self.pdf.config.poi_index] = 1.0 if self.test_stat == 'q0' else 0.0
bkg_pdf = self.pdf.make_pdf(tensorlib.astensor(bkg_pars))
bkg_pars = fixed_poi_fit(
1.0 if self.test_stat == 'q0' else 0.0,
self.data,
self.pdf,
self.init_pars,
self.par_bounds,
self.fixed_params,
)
bkg_pdf = self.pdf.make_pdf(bkg_pars)
bkg_sample = bkg_pdf.sample(sample_shape)
matthewfeickert marked this conversation as resolved.
Show resolved Hide resolved

teststat_func = utils.get_test_stat(self.test_stat)
Expand Down Expand Up @@ -774,7 +786,7 @@ def pvalues(self, teststat, sig_plus_bkg_distribution, bkg_only_distribution):
>>> sig_plus_bkg_dist, bkg_dist = toy_calculator.distributions(mu_test)
>>> CLsb, CLb, CLs = toy_calculator.pvalues(q_tilde, sig_plus_bkg_dist, bkg_dist)
>>> CLsb, CLb, CLs
(array(0.01), array(0.41), array(0.02439024))
(array(0.03), array(0.37), array(0.08108108))

Args:
teststat (:obj:`tensor`): The test statistic.
Expand Down Expand Up @@ -820,7 +832,7 @@ def expected_pvalues(self, sig_plus_bkg_distribution, bkg_only_distribution):
>>> sig_plus_bkg_dist, bkg_dist = toy_calculator.distributions(mu_test)
>>> CLsb_exp_band, CLb_exp_band, CLs_exp_band = toy_calculator.expected_pvalues(sig_plus_bkg_dist, bkg_dist)
>>> CLs_exp_band
[array(0.), array(0.), array(0.06186224), array(0.28450033), array(1.)]
[array(0.), array(0.), array(0.08403955), array(0.21892596), array(0.86072977)]

Args:
sig_plus_bkg_distribution (~pyhf.infer.calculators.EmpiricalDistribution):
Expand Down
2 changes: 1 addition & 1 deletion src/pyhf/infer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def create_calculator(calctype, *args, **kwargs):
... )
>>> qmu_sig, qmu_bkg = toy_calculator.distributions(mu_test)
>>> qmu_sig.pvalue(mu_test), qmu_bkg.pvalue(mu_test)
(array(0.14), array(0.76))
(array(0.14), array(0.79))

Args:
calctype (:obj:`str`): The calculator to create. Choose either
Expand Down
32 changes: 16 additions & 16 deletions tests/test_infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,30 +365,30 @@ def test_toy_calculator(tmpdir, hypotest_args):
assert qtilde_mu_sig.samples.tolist() == pytest.approx(
[
0.0,
0.13298492825293806,
0.0,
0.7718560148925349,
1.814884694401428,
0.017350013494649374,
0.0,
0.2338008822475217,
0.020328779776718875,
0.8911134903562186,
0.04408274703718007,
0.0,
0.03977591672014569,
0.0,
0.0,
0.06586643485326249,
],
1e-07,
)
assert qtilde_mu_bkg.samples.tolist() == pytest.approx(
[
2.2664625749100082,
1.081660887453154,
2.7570218408936853,
1.3835691388297846,
0.4707467005909507,
0.0,
3.7166483705294127,
3.8021896732709592,
5.114135391143066,
1.3511153731000718,
5.642956861215396,
0.37581364290284114,
4.875367689039649,
3.4299006094989295,
1.0161021805475343,
0.03345317321810626,
0.21984803001140563,
1.274869119189077,
9.368264062021098,
3.0716486684082156,
],
1e-07,
)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ def expected_result_1bin_shapesys_q0():
@pytest.fixture(scope='module')
def expected_result_1bin_shapesys_q0_toys():
expected_result = {
"exp": [0.0, 0.0005, 0.0145, 0.1205, 0.402761],
"obs": 0.005,
"exp": [0.0, 0.0, 0.0135, 0.1365, 0.39854497],
"obs": 0.004,
}
return expected_result

Expand Down