Skip to content

feat: Allow model_utils.prediction to skip uncertainty calculation #510

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 14 additions & 7 deletions src/cabinetry/model_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ def yield_stdev(
): (total_stdev_per_bin, total_stdev_per_channel)
}
)

return total_stdev_per_bin, total_stdev_per_channel


Expand All @@ -420,6 +419,7 @@ def prediction(
*,
fit_results: Optional[FitResults] = None,
label: Optional[str] = None,
skip_unc: bool = False,
) -> ModelPrediction:
"""Returns model prediction, including model yields and uncertainties.

Expand Down Expand Up @@ -469,12 +469,19 @@ def prediction(
for ch in model.config.channels
]

# calculate the total standard deviation of the model prediction
# indices: (channel, sample, bin) for per-bin uncertainties,
# (channel, sample) for per-channel
total_stdev_model_bins, total_stdev_model_channels = yield_stdev(
model, param_values, param_uncertainty, corr_mat
)
if not skip_unc:
# calculate the total standard deviation of the model prediction
# indices: (channel, sample, bin) for per-bin uncertainties,
# (channel, sample) for per-channel
total_stdev_model_bins, total_stdev_model_channels = yield_stdev(
model, param_values, param_uncertainty, corr_mat
)
else:
n_bins = sum(model.config.channel_nbins.values())
n_channels = len(model.config.channels)
n_samples = len(model.config.samples)
total_stdev_model_channels = np.zeros((n_channels, n_bins + 1)).tolist()
total_stdev_model_bins = np.zeros((n_bins, n_samples + 1, n_channels)).tolist()

return ModelPrediction(
model, model_yields, total_stdev_model_bins, total_stdev_model_channels, label
Expand Down
20 changes: 20 additions & 0 deletions tests/test_model_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,26 @@ def test_prediction(

caplog.clear()

# post-fit prediction, single-channel model, no uncertainty
fit_results = FitResults(
np.asarray([1.1, 1.01]),
np.asarray([0.1, 0.03]),
["Signal strength", "staterror_Signal-Region[0]"],
np.asarray([[1.0, 0.2], [0.2, 1.0]]),
0.0,
)
model_pred = model_utils.prediction(model, fit_results=fit_results, skip_unc=True)
assert model_pred.model == model
assert np.allclose(model_pred.model_yields, [[[57.54980000]]]) # new par value
assert model_pred.total_stdev_model_bins == [[[0.0], [0.0]]] # new par value
assert model_pred.total_stdev_model_channels == [[0.0, 0.0]] # new par value
assert model_pred.label == "post-fit"

assert mock_asimov.call_count == 1 # no new call
assert mock_unc.call_count == 1 # no new call
assert mock_stdev.call_count == 2 # no new call
caplog.clear()

# custom prediction label, mismatch in parameter names
fit_results = FitResults(
np.asarray([1.1, 1.01]),
Expand Down