-
Notifications
You must be signed in to change notification settings - Fork 84
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
feat: Cleanup internal names for hypotest #1247
Draft
kratsg
wants to merge
6
commits into
main
Choose a base branch
from
feat/cleanupHypotestInternalNames
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
log = logging.getLogger(__name__) | ||
|
||
|
||
def generate_asimov_data(asimov_mu, data, pdf, init_pars, par_bounds, fixed_params): | ||
def generate_asimov_data(mu, data, pdf, init_pars, par_bounds, fixed_params): | ||
""" | ||
Compute Asimov Dataset (expected yields at best-fit values) for a given POI value. | ||
|
||
|
@@ -35,7 +35,7 @@ def generate_asimov_data(asimov_mu, data, pdf, init_pars, par_bounds, fixed_para | |
array([ 60.61229858, 56.52802479, 270.06832542, 48.31545488]) | ||
|
||
Args: | ||
asimov_mu (:obj:`float`): The value for the parameter of interest to be used. | ||
mu (:obj:`float`): The value for the parameter of interest to be used. | ||
data (:obj:`tensor`): The observed data. | ||
pdf (~pyhf.pdf.Model): The statistical model adhering to the schema ``model.json``. | ||
init_pars (:obj:`tensor`): The initial parameter values to be used for fitting. | ||
|
@@ -47,7 +47,7 @@ def generate_asimov_data(asimov_mu, data, pdf, init_pars, par_bounds, fixed_para | |
|
||
""" | ||
bestfit_nuisance_asimov = fixed_poi_fit( | ||
asimov_mu, data, pdf, init_pars, par_bounds, fixed_params | ||
mu, data, pdf, init_pars, par_bounds, fixed_params | ||
) | ||
return pdf.expected_data(bestfit_nuisance_asimov) | ||
|
||
|
@@ -193,7 +193,7 @@ def __init__( | |
self.test_stat = test_stat | ||
self.sqrtqmuA_v = None | ||
|
||
def distributions(self, poi_test): | ||
def distributions(self, alt_mu, null_mu): | ||
r""" | ||
Probability distributions of the test statistic, as defined in | ||
:math:`\S` 3 of :xref:`arXiv:1007.1727` under the Wald approximation, | ||
|
@@ -209,26 +209,30 @@ def distributions(self, poi_test): | |
>>> observations = [51, 48] | ||
>>> data = observations + model.config.auxdata | ||
>>> mu_test = 1.0 | ||
>>> null_mu = 0.0 | ||
>>> asymptotic_calculator = pyhf.infer.calculators.AsymptoticCalculator(data, model, test_stat="qtilde") | ||
>>> _ = asymptotic_calculator.teststatistic(mu_test) | ||
>>> qmu_sig, qmu_bkg = asymptotic_calculator.distributions(mu_test) | ||
>>> _ = asymptotic_calculator.teststatistic(mu_test, null_mu) | ||
>>> qmu_sig, qmu_bkg = asymptotic_calculator.distributions(mu_test, null_mu) | ||
>>> qmu_sig.pvalue(mu_test), qmu_bkg.pvalue(mu_test) | ||
(0.002192624107163899, 0.15865525393145707) | ||
|
||
Args: | ||
poi_test (:obj:`float` or :obj:`tensor`): The value for the parameter of interest. | ||
alt_mu (:obj:`float` or :obj:`tensor`): The value for the parameter of interest for the alternative hypothesis. | ||
null_mu (:obj:`float` or :obj:`tensor`): The value for the parameter of interest for the null hypothesis. | ||
|
||
Returns: | ||
Tuple (~pyhf.infer.calculators.AsymptoticTestStatDistribution): The distributions under the hypotheses. | ||
|
||
""" | ||
if self.sqrtqmuA_v is None: | ||
raise RuntimeError('need to call .teststatistic(poi_test) first') | ||
sb_dist = AsymptoticTestStatDistribution(-self.sqrtqmuA_v) | ||
b_dist = AsymptoticTestStatDistribution(0.0) | ||
return sb_dist, b_dist | ||
|
||
def teststatistic(self, poi_test): | ||
raise RuntimeError('need to call .teststatistic first') | ||
distribution_alt = AsymptoticTestStatDistribution(-self.sqrtqmuA_v) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. signal + background in exclusion fit is the null |
||
distribution_null = AsymptoticTestStatDistribution( | ||
0.0 | ||
) # TODO is this asimov_mu / null_mu? | ||
return distribution_alt, distribution_null | ||
|
||
def teststatistic(self, alt_mu, null_mu): | ||
""" | ||
Compute the test statistic for the observed data under the studied model. | ||
|
||
|
@@ -242,12 +246,14 @@ def teststatistic(self, poi_test): | |
>>> observations = [51, 48] | ||
>>> data = observations + model.config.auxdata | ||
>>> mu_test = 1.0 | ||
>>> null_mu = 0.0 | ||
>>> asymptotic_calculator = pyhf.infer.calculators.AsymptoticCalculator(data, model, test_stat="qtilde") | ||
>>> asymptotic_calculator.teststatistic(mu_test) | ||
>>> asymptotic_calculator.teststatistic(mu_test, null_mu) | ||
0.14043184405388176 | ||
|
||
Args: | ||
poi_test (:obj:`float` or :obj:`tensor`): The value for the parameter of interest. | ||
alt_mu (:obj:`float` or :obj:`tensor`): The value for the parameter of interest for the alternative hypothesis. | ||
null_mu (:obj:`float` or :obj:`tensor`): The value for the parameter of interest for the null hypothesis. | ||
|
||
Returns: | ||
Float: The value of the test statistic. | ||
|
@@ -258,7 +264,7 @@ def teststatistic(self, poi_test): | |
teststat_func = utils.get_test_stat(self.test_stat) | ||
|
||
qmu_v = teststat_func( | ||
poi_test, | ||
alt_mu, | ||
self.data, | ||
self.pdf, | ||
self.init_pars, | ||
|
@@ -267,18 +273,16 @@ def teststatistic(self, poi_test): | |
) | ||
sqrtqmu_v = tensorlib.sqrt(qmu_v) | ||
|
||
asimov_mu = 1.0 if self.test_stat == 'q0' else 0.0 | ||
|
||
asimov_data = generate_asimov_data( | ||
asimov_mu, | ||
null_mu, | ||
self.data, | ||
self.pdf, | ||
self.init_pars, | ||
self.par_bounds, | ||
self.fixed_params, | ||
) | ||
qmuA_v = teststat_func( | ||
poi_test, | ||
alt_mu, | ||
asimov_data, | ||
self.pdf, | ||
self.init_pars, | ||
|
@@ -489,7 +493,7 @@ def __init__( | |
self.test_stat = test_stat | ||
self.track_progress = track_progress | ||
|
||
def distributions(self, poi_test, track_progress=None): | ||
def distributions(self, alt_mu, null_mu, track_progress=None): | ||
""" | ||
Probability Distributions of the test statistic value under the signal + background and background-only hypothesis. | ||
|
||
|
@@ -505,15 +509,17 @@ def distributions(self, poi_test, track_progress=None): | |
>>> observations = [51, 48] | ||
>>> data = observations + model.config.auxdata | ||
>>> mu_test = 1.0 | ||
>>> null_mu = 0.0 | ||
>>> toy_calculator = pyhf.infer.calculators.ToyCalculator( | ||
... data, model, ntoys=100, track_progress=False | ||
... ) | ||
>>> qmu_sig, qmu_bkg = toy_calculator.distributions(mu_test) | ||
>>> qmu_sig, qmu_bkg = toy_calculator.distributions(mu_test, null_mu) | ||
>>> qmu_sig.pvalue(mu_test), qmu_bkg.pvalue(mu_test) | ||
(0.14, 0.76) | ||
|
||
Args: | ||
poi_test (:obj:`float` or :obj:`tensor`): The value for the parameter of interest. | ||
alt_mu (:obj:`float` or :obj:`tensor`): The value for the parameter of interest for the alternative hypothesis. | ||
null_mu (:obj:`float` or :obj:`tensor`): The value for the parameter of interest for the null hypothesis. | ||
track_progress (:obj:`bool`): Whether to display the `tqdm` progress bar or not (outputs to `stderr`) | ||
|
||
Returns: | ||
|
@@ -524,12 +530,12 @@ def distributions(self, poi_test, track_progress=None): | |
sample_shape = (self.ntoys,) | ||
|
||
signal_pars = self.pdf.config.suggested_init() | ||
signal_pars[self.pdf.config.poi_index] = poi_test | ||
signal_pars[self.pdf.config.poi_index] = alt_mu | ||
signal_pdf = self.pdf.make_pdf(tensorlib.astensor(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_pars[self.pdf.config.poi_index] = null_mu | ||
bkg_pdf = self.pdf.make_pdf(tensorlib.astensor(bkg_pars)) | ||
bkg_sample = bkg_pdf.sample(sample_shape) | ||
|
||
|
@@ -544,11 +550,11 @@ def distributions(self, poi_test, track_progress=None): | |
unit='toy', | ||
) | ||
|
||
signal_teststat = [] | ||
teststat_alt = [] | ||
for sample in tqdm.tqdm(signal_sample, **tqdm_options, desc='Signal-like'): | ||
signal_teststat.append( | ||
teststat_alt.append( | ||
teststat_func( | ||
poi_test, | ||
alt_mu, | ||
sample, | ||
self.pdf, | ||
signal_pars, | ||
|
@@ -557,11 +563,11 @@ def distributions(self, poi_test, track_progress=None): | |
) | ||
) | ||
|
||
bkg_teststat = [] | ||
teststat_null = [] | ||
for sample in tqdm.tqdm(bkg_sample, **tqdm_options, desc='Background-like'): | ||
bkg_teststat.append( | ||
teststat_null.append( | ||
teststat_func( | ||
poi_test, | ||
alt_mu, | ||
sample, | ||
self.pdf, | ||
bkg_pars, | ||
|
@@ -570,11 +576,11 @@ def distributions(self, poi_test, track_progress=None): | |
) | ||
) | ||
|
||
s_plus_b = EmpiricalDistribution(tensorlib.astensor(signal_teststat)) | ||
b_only = EmpiricalDistribution(tensorlib.astensor(bkg_teststat)) | ||
return s_plus_b, b_only | ||
distribution_alt = EmpiricalDistribution(tensorlib.astensor(teststat_alt)) | ||
distribution_null = EmpiricalDistribution(tensorlib.astensor(teststat_null)) | ||
return distribution_alt, distribution_null | ||
|
||
def teststatistic(self, poi_test): | ||
def teststatistic(self, alt_mu, null_mu): | ||
""" | ||
Compute the test statistic for the observed data under the studied model. | ||
|
||
|
@@ -590,22 +596,24 @@ def teststatistic(self, poi_test): | |
>>> observations = [51, 48] | ||
>>> data = observations + model.config.auxdata | ||
>>> mu_test = 1.0 | ||
>>> null_mu = 0.0 | ||
>>> toy_calculator = pyhf.infer.calculators.ToyCalculator( | ||
... data, model, ntoys=100, track_progress=False | ||
... ) | ||
>>> toy_calculator.teststatistic(mu_test) | ||
>>> toy_calculator.teststatistic(mu_test, null_mu) | ||
array(3.93824492) | ||
|
||
Args: | ||
poi_test (:obj:`float` or :obj:`tensor`): The value for the parameter of interest. | ||
alt_mu (:obj:`float` or :obj:`tensor`): The value for the parameter of interest for the alternative hypothesis. | ||
null_mu (:obj:`float` or :obj:`tensor`): The value for the parameter of interest for the null hypothesis. | ||
|
||
Returns: | ||
Float: The value of the test statistic. | ||
|
||
""" | ||
teststat_func = utils.get_test_stat(self.test_stat) | ||
teststat = teststat_func( | ||
poi_test, | ||
alt_mu, | ||
self.data, | ||
self.pdf, | ||
self.init_pars, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s+b is null