Skip to content

Commit

Permalink
rename distributions
Browse files Browse the repository at this point in the history
  • Loading branch information
kratsg committed Jan 16, 2021
1 parent 59ded49 commit 310829e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 44 deletions.
63 changes: 29 additions & 34 deletions src/pyhf/infer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,58 +144,53 @@ def hypotest(
null_mu = null_mu or (1.0 if is_q0 else 0.0)

teststat = calc.teststatistic(alt_mu, null_mu)
sig_plus_bkg_distribution, b_only_distribution = calc.distributions(alt_mu, null_mu)
alt_distribution, null_distribution = calc.distributions(alt_mu, null_mu)

CLsb = sig_plus_bkg_distribution.pvalue(teststat)
CLb = b_only_distribution.pvalue(teststat)
CLs = CLsb / CLb
pvalue_alt = alt_distribution.pvalue(teststat)
pvalue_null = null_distribution.pvalue(teststat)
pvalue_mod_alt = pvalue_alt / pvalue_null

tensorlib, _ = get_backend()
# Ensure that all CL values are 0-d tensors
CLsb, CLb, CLs = (
tensorlib.astensor(CLsb),
tensorlib.astensor(CLb),
tensorlib.astensor(CLs),
# Ensure that all p-values are 0-d tensors
pvalue_alt, pvalue_null, pvalue_mod_alt = (
tensorlib.astensor(pvalue_alt),
tensorlib.astensor(pvalue_null),
tensorlib.astensor(pvalue_mod_alt),
)

_returns = [CLsb if is_q0 else CLs]
_returns = [pvalue_alt if is_q0 else pvalue_mod_alt]
if return_tail_probs:
if is_q0:
_returns.append([CLb])
_returns.append([pvalue_null])
else:
_returns.append([CLsb, CLb])
_returns.append([pvalue_alt, pvalue_null])
if return_expected_set:
CLs_exp = []
pvalue_mod_alt_exp = []
for n_sigma in [2, 1, 0, -1, -2]:

expected_bonly_teststat = b_only_distribution.expected_value(n_sigma)
expected_null_teststat = null_distribution.expected_value(n_sigma)

if is_q0:
# despite the name in this case this is the discovery p value
CLs = sig_plus_bkg_distribution.pvalue(expected_bonly_teststat)
else:
CLs = sig_plus_bkg_distribution.pvalue(
expected_bonly_teststat
) / b_only_distribution.pvalue(expected_bonly_teststat)
CLs_exp.append(tensorlib.astensor(CLs))
pvalue_mod_alt_exp_value = alt_distribution.pvalue(expected_null_teststat)
if not is_q0:
pvalue_mod_alt_exp_value /= null_distribution.pvalue(
expected_null_teststat
)

pvalue_mod_alt_exp.append(tensorlib.astensor(pvalue_mod_alt_exp_value))
if return_expected:
_returns.append(CLs_exp[2])
_returns.append(CLs_exp)
_returns.append(pvalue_mod_alt_exp[2])
_returns.append(pvalue_mod_alt_exp)
elif return_expected:
n_sigma = 0
expected_bonly_teststat = b_only_distribution.expected_value(n_sigma)
expected_null_teststat = null_distribution.expected_value(n_sigma)

if is_q0:
# despite the name in this case this is the discovery p value
CLs = sig_plus_bkg_distribution.pvalue(expected_bonly_teststat)
else:
CLs = sig_plus_bkg_distribution.pvalue(
expected_bonly_teststat
) / b_only_distribution.pvalue(expected_bonly_teststat)
pvalue_mod_alt_exp_value = alt_distribution.pvalue(expected_null_teststat)
if not is_q0:
pvalue_mod_alt_exp_value /= null_distribution.pvalue(expected_null_teststat)

_returns.append(tensorlib.astensor(CLs))
_returns.append(tensorlib.astensor(pvalue_mod_alt_exp_value))

# Enforce a consistent return type of the observed CLs
# Enforce a consistent return type of the observed pvalue_mod_alt
return tuple(_returns) if len(_returns) > 1 else _returns[0]


Expand Down
20 changes: 10 additions & 10 deletions src/pyhf/infer/calculators.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,11 @@ def distributions(self, alt_mu, null_mu):
"""
if self.sqrtqmuA_v is None:
raise RuntimeError('need to call .teststatistic first')
sb_dist = AsymptoticTestStatDistribution(-self.sqrtqmuA_v)
b_dist = AsymptoticTestStatDistribution(
distribution_alt = AsymptoticTestStatDistribution(-self.sqrtqmuA_v)
distribution_null = AsymptoticTestStatDistribution(
0.0
) # TODO is this asimov_mu / null_mu?
return sb_dist, b_dist
return distribution_alt, distribution_null

def teststatistic(self, alt_mu, null_mu):
"""
Expand Down Expand Up @@ -550,9 +550,9 @@ def distributions(self, alt_mu, null_mu, 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(
alt_mu,
sample,
Expand All @@ -563,9 +563,9 @@ def distributions(self, alt_mu, null_mu, 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(
alt_mu,
sample,
Expand All @@ -576,9 +576,9 @@ def distributions(self, alt_mu, null_mu, 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, alt_mu, null_mu):
"""
Expand Down

0 comments on commit 310829e

Please sign in to comment.