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: explicitly set post-fit parameter errors to 0 for fixed parameters #346

Merged
merged 2 commits into from
Jul 29, 2022
Merged
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
13 changes: 6 additions & 7 deletions src/cabinetry/fit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ def _fit_model_pyhf(
log.info(f"MINUIT status:\n{result_obj.minuit.fmin}")

bestfit = pyhf.tensorlib.to_numpy(result[:, 0])
uncertainty = pyhf.tensorlib.to_numpy(result[:, 1])
# set errors for fixed parameters to 0 (see iminuit#762)
uncertainty = np.where(
result_obj.minuit.fixed, 0.0, pyhf.tensorlib.to_numpy(result[:, 1])
)
labels = model.config.par_names()
corr_mat = pyhf.tensorlib.to_numpy(corr_mat)
best_twice_nll = float(best_twice_nll) # convert 0-dim np.ndarray to float
Expand Down Expand Up @@ -147,10 +150,6 @@ def _fit_model_custom(

labels = model.config.par_names()

# set initial step size to 0 for fixed parameters
# this will cause the associated parameter uncertainties to be 0 post-fit
step_size = [0.1 if not fix_pars[i_par] else 0.0 for i_par in range(len(init_pars))]

def twice_nll_func(pars: np.ndarray) -> Any:
"""The objective for minimization: twice the negative log-likelihood.

Expand All @@ -167,7 +166,6 @@ def twice_nll_func(pars: np.ndarray) -> Any:
return twice_nll[0]

m = iminuit.Minuit(twice_nll_func, init_pars, name=labels)
m.errors = step_size
m.fixed = fix_pars
m.limits = par_bounds
m.errordef = 1
Expand All @@ -180,7 +178,8 @@ def twice_nll_func(pars: np.ndarray) -> Any:
log.info(f"MINUIT status:\n{m.fmin}")

bestfit = np.asarray(m.values)
uncertainty = np.asarray(m.errors)
# set errors for fixed parameters to 0 (see iminuit#762)
uncertainty = np.where(m.fixed, 0.0, m.errors)
corr_mat = m.covariance.correlation() # iminuit.util.Matrix, subclass of np.ndarray
best_twice_nll = m.fval

Expand Down