Skip to content

Commit 268f9d6

Browse files
authored
Merge pull request #14 from SpeysideHEP/simplify-assym-unc
Extend Simplify module with Effective Sigma
2 parents e60f7ac + 524e1ce commit 268f9d6

File tree

4 files changed

+56
-20
lines changed

4 files changed

+56
-20
lines changed

.zenodo.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"description": "pyhf plug-in for spey package",
33
"license": "MIT",
4-
"title": "SpeysideHEP/spey-pyhf: v0.1.6",
5-
"version": "v0.1.6",
4+
"title": "SpeysideHEP/spey-pyhf: v0.1.7",
5+
"version": "v0.1.7",
66
"upload_type": "software",
77
"creators": [
88
{
9-
"affiliation": "Thomas Jefferson National Accelerator Facility",
9+
"affiliation": "Stony Brook University",
1010
"name": "Araz, Jack Y.",
1111
"orcid": "0000-0001-8721-8042"
1212
}
@@ -29,7 +29,7 @@
2929
},
3030
{
3131
"scheme": "url",
32-
"identifier": "https://github.com/SpeysideHEP/spey-pyhf/tree/v0.1.6",
32+
"identifier": "https://github.com/SpeysideHEP/spey-pyhf/tree/v0.1.7",
3333
"relation": "isSupplementTo"
3434
},
3535
{

docs/releases/changelog-v0.1.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
and usage can be found in the online documentation.
88
([#2](https://github.com/SpeysideHEP/spey-pyhf/pull/2))
99

10+
* Full statistical model mapping to effective sigma model have been implemented.
11+
([#14](https://github.com/SpeysideHEP/spey-pyhf/pull/14))
12+
1013
## Improvements
1114

1215
* Sampler functionality has been extended to isolate auxiliary data.

src/spey_pyhf/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Version of the spey - pyhf plugin"""
22

3-
__version__ = "0.1.6"
3+
__version__ = "0.1.7"

src/spey_pyhf/simplify.py

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
import logging
44
import warnings
55
from contextlib import contextmanager
6+
from pathlib import Path
67
from typing import Callable, List, Literal, Optional, Text, Union
78

89
import numpy as np
910
import spey
1011
import tqdm
11-
from scipy.stats import moment, multivariate_normal
12-
from spey.backends.default_pdf import CorrelatedBackground, ThirdMomentExpansion
12+
from scipy.stats import moment, multivariate_normal, norm
13+
from spey.backends.default_pdf import (
14+
CorrelatedBackground,
15+
EffectiveSigma,
16+
ThirdMomentExpansion,
17+
)
18+
from spey.helper_functions import covariance_to_correlation
1319
from spey.optimizer.core import fit
1420

1521
from . import WorkspaceInterpreter
@@ -78,7 +84,8 @@ class Simplify(spey.ConverterBase):
7884
fittype (``Text``, default ``"postfit"``): what type of fitting should be performed ``"postfit"``
7985
or ``"prefit"``.
8086
convert_to (``Text``, default ``"default_pdf.correlated_background"``): conversion type. Should
81-
be either ``"default_pdf.correlated_background"`` or ``"default_pdf.third_moment_expansion"``.
87+
be either ``"default_pdf.correlated_background"``, ``"default_pdf.third_moment_expansion"``
88+
or ``"default_pdf.effective_sigma"``.
8289
number_of_samples (``int``, default ``1000``): number of samples to be generated in order to estimate
8390
contract the uncertainties into a single value.
8491
control_region_indices (``List[int]`` or ``List[Text]``, default ``None``): indices or names of the control and
@@ -171,13 +178,15 @@ def __call__(
171178
statistical_model: spey.StatisticalModel,
172179
fittype: Literal["postfit", "prefit"] = "postfit",
173180
convert_to: Literal[
174-
"default_pdf.correlated_background", "default_pdf.third_moment_expansion"
181+
"default_pdf.correlated_background",
182+
"default_pdf.third_moment_expansion",
183+
"default_pdf.effective_sigma",
175184
] = "default_pdf.correlated_background",
176185
number_of_samples: int = 1000,
177186
control_region_indices: Optional[Union[List[int], List[Text]]] = None,
178187
include_modifiers_in_control_model: bool = False,
179188
save_model: Optional[Text] = None,
180-
) -> Union[CorrelatedBackground, ThirdMomentExpansion]:
189+
) -> Union[CorrelatedBackground, ThirdMomentExpansion, EffectiveSigma]:
181190

182191
assert statistical_model.backend_type == "pyhf", (
183192
"This method is currently only available for `pyhf` full statistical models."
@@ -390,6 +399,13 @@ def __call__(
390399
# in the full statistical model!
391400
background_yields = np.mean(samples, axis=0)
392401

402+
save_kwargs = {
403+
"covariance_matrix": covariance_matrix,
404+
"background_yields": background_yields,
405+
"data": data,
406+
"channel_order": stat_model_pyhf.config.channels,
407+
}
408+
393409
third_moments = []
394410
if convert_to == "default_pdf.correlated_background":
395411
backend = CorrelatedBackground(
@@ -400,6 +416,7 @@ def __call__(
400416
)
401417
elif convert_to == "default_pdf.third_moment_expansion":
402418
third_moments = moment(samples, moment=3, axis=0)
419+
save_kwargs.update({"third_moments": third_moments})
403420

404421
backend = ThirdMomentExpansion(
405422
signal_yields=signal_yields,
@@ -408,21 +425,37 @@ def __call__(
408425
covariance_matrix=covariance_matrix,
409426
third_moment=third_moments,
410427
)
428+
elif convert_to == "default_pdf.effective_sigma":
429+
# Get 68% quantiles
430+
q = (1.0 - (norm.cdf(1.0) - norm.cdf(-1.0))) / 2.0
431+
absolute_uncertainty_envelops = np.stack(
432+
[np.quantile(samples, q, axis=0), np.quantile(samples, 1 - q, axis=0)],
433+
axis=1,
434+
)
435+
save_kwargs.update(
436+
{"absolute_uncertainty_envelops": absolute_uncertainty_envelops}
437+
)
438+
439+
backend = EffectiveSigma(
440+
signal_yields=signal_yields,
441+
background_yields=background_yields,
442+
data=data,
443+
correlation_matrix=covariance_to_correlation(
444+
covariance_matrix=covariance_matrix
445+
),
446+
absolute_uncertainty_envelops=absolute_uncertainty_envelops,
447+
)
411448
else:
412449
raise ConversionError(
413450
"Currently available conversion methods are "
414-
+ "'default_pdf.correlated_background', 'default_pdf.third_moment_expansion'"
451+
+ "'default_pdf.correlated_background', 'default_pdf.third_moment_expansion',"
452+
+ " 'default_pdf.effective_sigma'"
415453
)
416454

417455
if save_model is not None:
418-
if save_model.endswith(".npz"):
419-
np.savez_compressed(
420-
save_model,
421-
covariance_matrix=covariance_matrix,
422-
background_yields=background_yields,
423-
third_moments=third_moments,
424-
data=data,
425-
channel_order=stat_model_pyhf.config.channels,
426-
)
456+
save_path = Path(save_model)
457+
if save_path.suffix != ".npz":
458+
save_path = save_path.with_suffix(".npz")
459+
np.savez_compressed(str(save_path), **save_kwargs)
427460

428461
return backend

0 commit comments

Comments
 (0)