-
Notifications
You must be signed in to change notification settings - Fork 81
/
test_backend_consistency.py
152 lines (126 loc) · 4.28 KB
/
test_backend_consistency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import pyhf
import numpy as np
import pytest
def generate_source_static(n_bins):
"""
Create the source structure for the given number of bins.
Args:
n_bins: `list` of number of bins
Returns:
source
"""
binning = [n_bins, -0.5, n_bins + 0.5]
data = [120.0] * n_bins
bkg = [100.0] * n_bins
bkgerr = [10.0] * n_bins
sig = [30.0] * n_bins
source = {
'binning': binning,
'bindata': {'data': data, 'bkg': bkg, 'bkgerr': bkgerr, 'sig': sig},
}
return source
def generate_source_poisson(n_bins):
"""
Create the source structure for the given number of bins.
Sample from a Poisson distribution
Args:
n_bins: `list` of number of bins
Returns:
source
"""
np.random.seed(0) # Fix seed for reproducibility
binning = [n_bins, -0.5, n_bins + 0.5]
data = np.random.poisson(120.0, n_bins).tolist()
bkg = np.random.poisson(100.0, n_bins).tolist()
bkgerr = np.random.poisson(10.0, n_bins).tolist()
sig = np.random.poisson(30.0, n_bins).tolist()
source = {
'binning': binning,
'bindata': {'data': data, 'bkg': bkg, 'bkgerr': bkgerr, 'sig': sig},
}
return source
# bins = [1, 10, 50, 100, 200, 500, 800, 1000]
bins = [50, 500]
bin_ids = ['{}_bins'.format(n_bins) for n_bins in bins]
@pytest.mark.parametrize('n_bins', bins, ids=bin_ids)
@pytest.mark.parametrize('invert_order', [False, True], ids=['normal', 'inverted'])
def test_hypotest_qmu_tilde(
n_bins, invert_order, tolerance={'numpy': 1e-02, 'tensors': 5e-03}
):
"""
Check that the different backends all compute a test statistic
that is within a specific tolerance of each other.
Args:
n_bins: `list` of number of bins given by pytest parameterization
tolerance: `dict` of the maximum differences the test statistics
can differ relative to each other
Returns:
None
"""
source = generate_source_static(n_bins)
signal_sample = {
'name': 'signal',
'data': source['bindata']['sig'],
'modifiers': [{'name': 'mu', 'type': 'normfactor', 'data': None}],
}
background_sample = {
'name': 'background',
'data': source['bindata']['bkg'],
'modifiers': [
{
'name': 'uncorr_bkguncrt',
'type': 'shapesys',
'data': source['bindata']['bkgerr'],
}
],
}
samples = (
[background_sample, signal_sample]
if invert_order
else [signal_sample, background_sample]
)
spec = {'channels': [{'name': 'singlechannel', 'samples': samples}]}
pdf = pyhf.Model(spec)
data = source['bindata']['data'] + pdf.config.auxdata
backends = [
pyhf.tensor.numpy_backend(precision='64b'),
pyhf.tensor.tensorflow_backend(precision='64b'),
pyhf.tensor.pytorch_backend(precision='64b'),
pyhf.tensor.jax_backend(precision='64b'),
]
test_statistic = []
for backend in backends:
pyhf.set_backend(backend)
qmu_tilde = pyhf.infer.test_statistics.qmu_tilde(
1.0,
data,
pdf,
pdf.config.suggested_init(),
pdf.config.suggested_bounds(),
)
test_statistic.append(qmu_tilde)
# compare to NumPy/SciPy
test_statistic = np.array(test_statistic)
numpy_ratio = np.divide(test_statistic, test_statistic[0])
numpy_ratio_delta_unity = np.absolute(np.subtract(numpy_ratio, 1))
# compare tensor libraries to each other
tensors_ratio = np.divide(test_statistic[1], test_statistic[2])
tensors_ratio_delta_unity = np.absolute(np.subtract(tensors_ratio, 1))
try:
assert (numpy_ratio_delta_unity < tolerance['numpy']).all()
except AssertionError:
print(
'Ratio to NumPy+SciPy exceeded tolerance of {}: {}'.format(
tolerance['numpy'], numpy_ratio_delta_unity.tolist()
)
)
assert False
try:
assert (tensors_ratio_delta_unity < tolerance['tensors']).all()
except AssertionError:
print(
'Ratio between tensor backends exceeded tolerance of {}: {}'.format(
tolerance['tensors'], tensors_ratio_delta_unity.tolist()
)
)
assert False