/
parameter.py
329 lines (262 loc) · 10.1 KB
/
parameter.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import logging
import numpy as np
from gammapy.datasets import Datasets
from gammapy.datasets.actors import DatasetsActor
from gammapy.modeling import Fit
from .core import Estimator
log = logging.getLogger(__name__)
class ParameterEstimator(Estimator):
"""Model parameter estimator.
Estimates a model parameter for a group of datasets. Compute best fit value,
symmetric and delta(TS) for a given null value. Additionally asymmetric errors
as well as parameter upper limit and fit statistic profile can be estimated.
Parameters
----------
n_sigma : int
Sigma to use for asymmetric error computation. Default is 1.
n_sigma_ul : int
Sigma to use for upper limit computation. Default is 2.
null_value : float
Which null value to use for the parameter.
selection_optional : list of str, optional
Which additional quantities to estimate. Available options are:
* "all": all the optional steps are executed.
* "errn-errp": estimate asymmetric errors on parameter best fit value.
* "ul": estimate upper limits.
* "scan": estimate fit statistic profiles.
Default is None so the optional steps are not executed.
fit : `Fit`
Fit instance specifying the backend and fit options.
reoptimize : bool
Re-optimize other free model parameters. Default is True.
"""
tag = "ParameterEstimator"
_available_selection_optional = ["errn-errp", "ul", "scan"]
def __init__(
self,
n_sigma=1,
n_sigma_ul=2,
null_value=1e-150,
selection_optional=None,
fit=None,
reoptimize=True,
):
self.n_sigma = n_sigma
self.n_sigma_ul = n_sigma_ul
self.null_value = null_value
self.selection_optional = selection_optional
if fit is None:
fit = Fit()
self.fit = fit
self.reoptimize = reoptimize
def estimate_best_fit(self, datasets, parameter):
"""Estimate parameter asymmetric errors.
Parameters
----------
datasets : `~gammapy.datasets.Datasets`
Datasets.
parameter : `Parameter`
For which parameter to get the value.
Returns
-------
result : dict
Dictionary with the various parameter estimation values. Entries are:
* parameter.name: best fit parameter value.
* "stat": best fit total stat.
* "success": boolean flag for fit success.
* parameter.name_err: covariance-based error estimate on parameter value.
"""
value, total_stat, success, error = np.nan, 0.0, False, np.nan
if np.any(datasets.contributes_to_stat):
result = self.fit.run(datasets=datasets)
value, error = parameter.value, parameter.error
total_stat = result.optimize_result.total_stat
success = result.success
return {
f"{parameter.name}": value,
"stat": total_stat,
"success": success,
f"{parameter.name}_err": error * self.n_sigma,
}
def estimate_ts(self, datasets, parameter):
"""Estimate parameter ts.
Parameters
----------
datasets : `~gammapy.datasets.Datasets`
Datasets.
parameter : `Parameter`
For which parameter to get the value.
Returns
-------
result : dict
Dictionary with the test statistic of the best fit value compared to the null hypothesis. Entries are:
* "ts" : fit statistic difference with null hypothesis.
* "npred" : predicted number of counts per dataset.
* "stat_null" : total stat corresponding to the null hypothesis
"""
npred = self.estimate_npred(datasets=datasets)
if not np.any(datasets.contributes_to_stat):
stat = np.nan
npred["npred"][...] = np.nan
else:
stat = datasets.stat_sum()
with datasets.parameters.restore_status():
# compute ts value
parameter.value = self.null_value
if self.reoptimize:
parameter.frozen = True
_ = self.fit.optimize(datasets=datasets)
ts = datasets.stat_sum() - stat
stat_null = datasets.stat_sum()
return {"ts": ts, "npred": npred["npred"], "stat_null": stat_null}
def estimate_errn_errp(self, datasets, parameter):
"""Estimate parameter asymmetric errors.
Parameters
----------
datasets : `~gammapy.datasets.Datasets`
Datasets.
parameter : `Parameter`
For which parameter to get the value.
Returns
-------
result : dict
Dictionary with the parameter asymmetric errors. Entries are:
* {parameter.name}_errp : positive error on parameter value.
* {parameter.name}_errn : negative error on parameter value.
"""
if not np.any(datasets.contributes_to_stat):
return {
f"{parameter.name}_errp": np.nan,
f"{parameter.name}_errn": np.nan,
}
self.fit.optimize(datasets=datasets)
res = self.fit.confidence(
datasets=datasets,
parameter=parameter,
sigma=self.n_sigma,
reoptimize=self.reoptimize,
)
return {
f"{parameter.name}_errp": res["errp"],
f"{parameter.name}_errn": res["errn"],
}
def estimate_scan(self, datasets, parameter):
"""Estimate parameter statistic scan.
Parameters
----------
datasets : `~gammapy.datasets.Datasets`
The datasets used to estimate the model parameter.
parameter : `~gammapy.modeling.Parameter`
For which parameter to get the value.
Returns
-------
result : dict
Dictionary with the parameter fit scan values. Entries are:
* parameter.name_scan : parameter values scan.
* "stat_scan" : fit statistic values scan.
"""
scan_values = parameter.scan_values
if not np.any(datasets.contributes_to_stat):
return {
f"{parameter.name}_scan": scan_values,
"stat_scan": scan_values * np.nan,
}
self.fit.optimize(datasets=datasets)
profile = self.fit.stat_profile(
datasets=datasets, parameter=parameter, reoptimize=self.reoptimize
)
return {
f"{parameter.name}_scan": scan_values,
"stat_scan": profile["stat_scan"],
}
def estimate_ul(self, datasets, parameter):
"""Estimate parameter ul.
Parameters
----------
datasets : `~gammapy.datasets.Datasets`
The datasets used to estimate the model parameter.
parameter : `~gammapy.modeling.Parameter`
For which parameter to get the value.
Returns
-------
result : dict
Dictionary with the parameter upper limits. Entries are:
* parameter.name_ul : upper limit on parameter value.
"""
if not np.any(datasets.contributes_to_stat):
return {f"{parameter.name}_ul": np.nan}
self.fit.optimize(datasets=datasets)
res = self.fit.confidence(
datasets=datasets,
parameter=parameter,
sigma=self.n_sigma_ul,
reoptimize=self.reoptimize,
)
return {f"{parameter.name}_ul": res["errp"] + parameter.value}
@staticmethod
def estimate_counts(datasets):
"""Estimate counts for the flux point.
Parameters
----------
datasets : Datasets
Datasets.
Returns
-------
result : dict
Dictionary with an array with one entry per dataset with the sum of the
masked counts.
"""
counts = []
for dataset in datasets:
mask = dataset.mask
counts.append(dataset.counts.data[mask].sum())
return {"counts": np.array(counts, dtype=int), "datasets": datasets.names}
@staticmethod
def estimate_npred(datasets):
"""Estimate npred for the flux point.
Parameters
----------
datasets : `~gammapy.datasets.Datasets`
Datasets.
Returns
-------
result : dict
Dictionary with an array with one entry per dataset with the sum of the
masked npred.
"""
npred = []
for dataset in datasets:
mask = dataset.mask
npred.append(dataset.npred().data[mask].sum())
return {"npred": np.array(npred), "datasets": datasets.names}
def run(self, datasets, parameter):
"""Run the parameter estimator.
Parameters
----------
datasets : `~gammapy.datasets.Datasets`
The datasets used to estimate the model parameter.
parameter : `str` or `~gammapy.modeling.Parameter`
For which parameter to run the estimator.
Returns
-------
result : dict
Dictionary with the various parameter estimation values.
"""
if not isinstance(datasets, DatasetsActor):
datasets = Datasets(datasets)
parameter = datasets.parameters[parameter]
with datasets.parameters.restore_status():
if not self.reoptimize:
datasets.parameters.freeze_all()
parameter.frozen = False
result = self.estimate_best_fit(datasets, parameter)
result.update(self.estimate_ts(datasets, parameter))
if "errn-errp" in self.selection_optional:
result.update(self.estimate_errn_errp(datasets, parameter))
if "ul" in self.selection_optional:
result.update(self.estimate_ul(datasets, parameter))
if "scan" in self.selection_optional:
result.update(self.estimate_scan(datasets, parameter))
result.update(self.estimate_counts(datasets))
return result