@@ -47,9 +47,9 @@ def twice_nll(pars, data, pdf):
47
47
return - 2 * pdf .logpdf (pars , data )
48
48
49
49
50
- def fit (data , pdf , init_pars = None , par_bounds = None , ** kwargs ):
50
+ def fit (data , pdf , init_pars = None , par_bounds = None , fixed_params = None , ** kwargs ):
51
51
r"""
52
- Run a unconstrained maximum likelihood fit.
52
+ Run a maximum likelihood fit.
53
53
This is done by minimizing the objective function :func:`~pyhf.infer.mle.twice_nll`
54
54
of the model parameters given the observed data.
55
55
This is used to produce the maximal likelihood :math:`L\left(\hat{\mu}, \hat{\boldsymbol{\theta}}\right)`
@@ -87,6 +87,7 @@ def fit(data, pdf, init_pars=None, par_bounds=None, **kwargs):
87
87
pdf (~pyhf.pdf.Model): The statistical model adhering to the schema model.json
88
88
init_pars (`list`): Values to initialize the model parameters at for the fit
89
89
par_bounds (`list` of `list`\s or `tuple`\s): The extrema of values the model parameters are allowed to reach in the fit
90
+ fixed_params (`list`): Parameters to be held constant in the fit.
90
91
kwargs: Keyword arguments passed through to the optimizer API
91
92
92
93
Returns:
@@ -96,10 +97,23 @@ def fit(data, pdf, init_pars=None, par_bounds=None, **kwargs):
96
97
_ , opt = get_backend ()
97
98
init_pars = init_pars or pdf .config .suggested_init ()
98
99
par_bounds = par_bounds or pdf .config .suggested_bounds ()
99
- return opt . minimize ( twice_nll , data , pdf , init_pars , par_bounds , ** kwargs )
100
+ fixed_params = fixed_params or pdf . config . suggested_fixed ( )
100
101
102
+ # get fixed vals from the model
103
+ fixed_vals = [
104
+ (index , init )
105
+ for index , (init , is_fixed ) in enumerate (zip (init_pars , fixed_params ))
106
+ if is_fixed
107
+ ]
101
108
102
- def fixed_poi_fit (poi_val , data , pdf , init_pars = None , par_bounds = None , ** kwargs ):
109
+ return opt .minimize (
110
+ twice_nll , data , pdf , init_pars , par_bounds , fixed_vals , ** kwargs
111
+ )
112
+
113
+
114
+ def fixed_poi_fit (
115
+ poi_val , data , pdf , init_pars = None , par_bounds = None , fixed_params = None , ** kwargs
116
+ ):
103
117
r"""
104
118
Run a maximum likelihood fit with the POI value fixed.
105
119
This is done by minimizing the objective function of :func:`~pyhf.infer.mle.twice_nll`
@@ -142,6 +156,7 @@ def fixed_poi_fit(poi_val, data, pdf, init_pars=None, par_bounds=None, **kwargs)
142
156
pdf (~pyhf.pdf.Model): The statistical model adhering to the schema model.json
143
157
init_pars (`list`): Values to initialize the model parameters at for the fit
144
158
par_bounds (`list` of `list`\s or `tuple`\s): The extrema of values the model parameters are allowed to reach in the fit
159
+ fixed_params (`list`): Parameters to be held constant in the fit.
145
160
kwargs: Keyword arguments passed through to the optimizer API
146
161
147
162
Returns:
@@ -152,15 +167,11 @@ def fixed_poi_fit(poi_val, data, pdf, init_pars=None, par_bounds=None, **kwargs)
152
167
raise UnspecifiedPOI (
153
168
'No POI is defined. A POI is required to fit with a fixed POI.'
154
169
)
155
- _ , opt = get_backend ()
156
- init_pars = init_pars or pdf .config .suggested_init ()
157
- par_bounds = par_bounds or pdf .config .suggested_bounds ()
158
- return opt .minimize (
159
- twice_nll ,
160
- data ,
161
- pdf ,
162
- init_pars ,
163
- par_bounds ,
164
- [(pdf .config .poi_index , poi_val )],
165
- ** kwargs ,
166
- )
170
+
171
+ init_pars = [* (init_pars or pdf .config .suggested_init ())]
172
+ fixed_params = [* (fixed_params or pdf .config .suggested_fixed ())]
173
+
174
+ init_pars [pdf .config .poi_index ] = poi_val
175
+ fixed_params [pdf .config .poi_index ] = True
176
+
177
+ return fit (data , pdf , init_pars , par_bounds , fixed_params , ** kwargs )
0 commit comments