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

feat: Configurable constraints per-modifier #1829

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
33 changes: 26 additions & 7 deletions src/pyhf/modifiers/staterror.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@
from pyhf.exceptions import InvalidModifier
from pyhf.parameters import ParamViewer
from pyhf.tensor.manager import get_backend
from typing import Optional

log = logging.getLogger(__name__)


def required_parset(sigmas, fixed: List[bool]):
def required_parset(sigmas, fixed: List[bool], constraint: Optional[str] = "gaussian"):
n_parameters = len(sigmas)
return {
'paramset_type': 'constrained_by_normal',
'paramset_type': 'constrained_by_normal'
if constraint == "gaussian"
else 'constrained_by_poisson',
'n_parameters': n_parameters,
'is_scalar': False,
'inits': (1.0,) * n_parameters,
'bounds': ((1e-10, 10.0),) * n_parameters,
'fixed': tuple(fixed),
'auxdata': (1.0,) * n_parameters,
'sigmas': tuple(sigmas),
'auxdata': (1.0,) * n_parameters if constraint == "gaussian" else tuple(sigmas),
'sigmas' if constraint == "gaussian" else 'factors': tuple(sigmas),
}


Expand All @@ -37,11 +40,12 @@ def __init__(self, config):
def collect(self, thismod, nom):
uncrt = thismod['data'] if thismod else [0.0] * len(nom)
mask = [True if thismod else False] * len(nom)
return {'mask': mask, 'nom_data': nom, 'uncrt': uncrt}
constraint = thismod.get('constraint', 'gaussian') if thismod else 'gaussian'
return {'mask': mask, 'nom_data': nom, 'uncrt': uncrt, 'constraint': constraint}

def append(self, key, channel, sample, thismod, defined_samp):
self.builder_data.setdefault(key, {}).setdefault(sample, {}).setdefault(
'data', {'uncrt': [], 'nom_data': [], 'mask': []}
'data', {'uncrt': [], 'nom_data': [], 'mask': [], 'constraint': []}
)
nom = (
defined_samp['data']
Expand All @@ -52,6 +56,9 @@ def append(self, key, channel, sample, thismod, defined_samp):
self.builder_data[key][sample]['data']['mask'].append(moddata['mask'])
self.builder_data[key][sample]['data']['uncrt'].append(moddata['uncrt'])
self.builder_data[key][sample]['data']['nom_data'].append(moddata['nom_data'])
self.builder_data[key][sample]['data']['constraint'].append(
moddata['constraint']
)

def finalize(self):
default_backend = pyhf.default_backend
Expand Down Expand Up @@ -118,6 +125,9 @@ def finalize(self):
else:
assert (mask_this_sample == masks[modname]).all()

for modifier_data in self.builder_data[modname].values():
modifier_data['data']['mask'] = masks[modname]

# extract sigmas using this modifiers mask
sigmas = relerrs[masks[modname]]

Expand All @@ -127,7 +137,16 @@ def finalize(self):
# non-Nan constraint term, but in a future PR need to remove constraints
# for these
sigmas[fixed] = 1.0
self.required_parsets.setdefault(parname, [required_parset(sigmas, fixed)])

constraint = [
i
for i, v in zip(modifier_data['data']['constraint'], masks[modname])
if v
]
assert all(constraint[0] == element for element in constraint)
self.required_parsets.setdefault(
parname, [required_parset(sigmas, fixed, constraint[0])]
)
return self.builder_data


Expand Down
1 change: 1 addition & 0 deletions src/pyhf/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def _finalize_parameters_specs(user_parameters, _paramsets_requirements):
f"Multiple parameter configurations for {parameter['name']} were found."
)
_paramsets_user_configs[parameter.get('name')] = parameter

_reqs = reduce_paramsets_requirements(
_paramsets_requirements, _paramsets_user_configs
)
Expand Down
15 changes: 10 additions & 5 deletions src/pyhf/schemas/1.0.0/defs.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@
},
"required": ["lo_data", "hi_data"],
"additionalProperties": false
}
},
"constraint": { "type": "string", "enum": [ "gaussian", "poisson" ] }
},
"required": ["name", "type", "data"],
"additionalProperties": false
Expand All @@ -119,7 +120,8 @@
"properties": {
"name": { "const": "lumi" },
"type": { "const": "lumi" },
"data": { "type": "null" }
"data": { "type": "null" },
"constraint": { "type": "string", "enum": [ "gaussian", "poisson" ] }
},
"required": ["name", "type", "data"],
"additionalProperties": false
Expand Down Expand Up @@ -147,7 +149,8 @@
},
"required": ["lo", "hi"],
"additionalProperties": false
}
},
"constraint": { "type": "string", "enum": [ "gaussian", "poisson" ] }
},
"required": ["name", "type", "data"],
"additionalProperties": false
Expand All @@ -167,7 +170,8 @@
"properties": {
"name": { "type": "string" },
"type": { "const": "shapesys" },
"data": { "type": "array", "items": {"type": "number"}, "minItems": 1 }
"data": { "type": "array", "items": {"type": "number"}, "minItems": 1 },
"constraint": { "type": "string", "enum": [ "gaussian", "poisson" ] }
},
"required": ["name", "type", "data"],
"additionalProperties": false
Expand All @@ -177,7 +181,8 @@
"properties": {
"name": { "type": "string" },
"type": { "const": "staterror" },
"data": { "type": "array", "items": {"type": "number"}, "minItems": 1 }
"data": { "type": "array", "items": {"type": "number"}, "minItems": 1 },
"constraint": { "type": "string", "enum": [ "gaussian", "poisson" ] }
},
"required": ["name", "type", "data"],
"additionalProperties": false
Expand Down