-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(evaluators): show honest Wilson CI when bootstrap degenerates at 0%/100% #2034
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
feiiiiii5
wants to merge
1
commit into
NVIDIA:main
Choose a base branch
from
feiiiiii5:fix/wilson-ci-degenerate-2033
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+193
−3
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Wilson score interval for binomial proportions. | ||
|
|
||
| The non-parametric bootstrap used by ``bootstrap_ci`` degenerates when every | ||
| observed outcome is identical (a 0% or 100% rate): every resample is identical, | ||
| so the percentile interval has zero width. A zero-width interval is not a | ||
| narrow interval — it is degenerate, and it hides the uncertainty the reader | ||
| most needs to see. The Wilson score interval is an honest alternative at the | ||
| boundary (e.g. 10/10 → [72%, 100%] at 95%). | ||
| """ | ||
|
|
||
| import math | ||
| from statistics import NormalDist | ||
| from typing import Optional, Tuple | ||
|
|
||
|
|
||
| def _z_score(confidence_level: float) -> float: | ||
| """Two-sided standard-normal quantile for ``confidence_level``.""" | ||
| return NormalDist().inv_cdf((1.0 + confidence_level) / 2.0) | ||
|
|
||
|
|
||
| def calculate_wilson_ci( | ||
| successes: int, | ||
| n: int, | ||
| confidence_level: float = 0.95, | ||
| ) -> Optional[Tuple[float, float]]: | ||
| """Return the Wilson score interval for ``successes`` out of ``n``, in percent. | ||
|
|
||
| Returns ``None`` for invalid inputs (non-positive ``n`` or a count outside | ||
| ``[0, n]``). The interval is clamped to ``[0, 100]``. | ||
| """ | ||
| if n <= 0 or successes < 0 or successes > n: | ||
| return None | ||
| if not 0.0 < confidence_level < 1.0: | ||
| return None | ||
|
|
||
| z = _z_score(confidence_level) | ||
| p = successes / n | ||
| z2 = z * z | ||
| denom = 1.0 + z2 / n | ||
| centre = (p + z2 / (2.0 * n)) / denom | ||
| margin = z * math.sqrt((p * (1.0 - p) + z2 / (4.0 * n)) / n) / denom | ||
| return ( | ||
| max(0.0, (centre - margin) * 100.0), | ||
| min(100.0, (centre + margin) * 100.0), | ||
| ) | ||
|
|
||
|
|
||
| def fallback_wilson_if_degenerate( | ||
| ci_lower: Optional[float], | ||
| ci_upper: Optional[float], | ||
| successes: int, | ||
| n: int, | ||
| confidence_level: float = 0.95, | ||
| ) -> Tuple[Optional[float], Optional[float], str]: | ||
| """Return the best CI, falling back to Wilson when bootstrap is degenerate. | ||
|
|
||
| Used by the evaluator: when a bootstrap interval has zero width (all | ||
| resamples identical, i.e. a 0% or 100% observed rate), replace it with a | ||
| Wilson interval so the report shows honest uncertainty instead of nothing. | ||
| Returns ``(lower, upper, method)`` where ``method`` is ``"bootstrap"`` or | ||
| ``"wilson"``. | ||
| """ | ||
| if ci_lower is None or ci_upper is None: | ||
| return (None, None, "bootstrap") | ||
| if not math.isclose(ci_lower, ci_upper, abs_tol=1e-9): | ||
| return (ci_lower, ci_upper, "bootstrap") | ||
| wilson = calculate_wilson_ci(successes, n, confidence_level) | ||
| if wilson is None: | ||
| return (ci_lower, ci_upper, "bootstrap") | ||
| return (wilson[0], wilson[1], "wilson") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import math | ||
|
|
||
| import pytest | ||
|
|
||
| from garak.analyze.wilson_ci import calculate_wilson_ci, fallback_wilson_if_degenerate | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "successes,n,expected_lower_floor,expected_upper_ceil,description", | ||
| [ | ||
| (10, 10, 70.0, 100.0, "perfect score"), | ||
| (0, 10, 0.0, 30.0, "no successes"), | ||
| (15, 30, 30.0, 70.0, "middle rate"), | ||
| ], | ||
| ) | ||
| def test_calculate_wilson_ci_bounds( | ||
| successes, n, expected_lower_floor, expected_upper_ceil, description | ||
| ): | ||
| """Wilson intervals are within [0, 100] and nonzero at the boundary.""" | ||
| result = calculate_wilson_ci(successes, n) | ||
| assert result is not None, description | ||
| ci_lower, ci_upper = result | ||
| assert 0 <= ci_lower <= 100 | ||
| assert 0 <= ci_upper <= 100 | ||
| assert ci_lower <= ci_upper | ||
| assert ci_lower >= expected_lower_floor | ||
| assert ci_upper <= expected_upper_ceil | ||
| assert ci_lower < ci_upper | ||
|
|
||
|
|
||
| def test_calculate_wilson_ci_boundary_values_match_known_intervals(): | ||
| """10/10 and 0/10 give the textbook [72%, 100%] and [0%, 28%] at 95%.""" | ||
| perfect = calculate_wilson_ci(10, 10) | ||
| assert perfect is not None | ||
| assert perfect[0] == pytest.approx(72.2, abs=0.5) | ||
| assert perfect[1] == 100.0 | ||
|
|
||
| none_success = calculate_wilson_ci(0, 10) | ||
| assert none_success is not None | ||
| assert none_success[0] == 0.0 | ||
| assert none_success[1] == pytest.approx(27.8, abs=0.5) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "successes,n", | ||
| [ | ||
| (0, 0), | ||
| (1, 0), | ||
| (-1, 10), | ||
| (11, 10), | ||
| ], | ||
| ) | ||
| def test_calculate_wilson_ci_invalid_inputs(successes, n): | ||
| assert calculate_wilson_ci(successes, n) is None | ||
|
|
||
|
|
||
| def test_calculate_wilson_ci_confidence_level_narrows_interval(): | ||
| wide = calculate_wilson_ci(15, 30, confidence_level=0.95) | ||
| narrow = calculate_wilson_ci(15, 30, confidence_level=0.90) | ||
| assert wide is not None and narrow is not None | ||
| assert (narrow[1] - narrow[0]) < (wide[1] - wide[0]) | ||
|
|
||
|
|
||
| def test_fallback_wilson_if_degenerate(): | ||
| """A zero-width bootstrap interval is replaced by Wilson; others are kept.""" | ||
| ci_lower, ci_upper, method = fallback_wilson_if_degenerate( | ||
| 100.0, 100.0, successes=10, n=10 | ||
| ) | ||
| assert method == "wilson" | ||
| assert ci_lower < ci_upper | ||
| assert ci_lower >= 70.0 | ||
| assert ci_upper == 100.0 | ||
|
|
||
| ci_lower, ci_upper, method = fallback_wilson_if_degenerate( | ||
| 0.0, 0.0, successes=0, n=10 | ||
| ) | ||
| assert method == "wilson" | ||
| assert ci_lower < ci_upper | ||
|
|
||
| ci_lower, ci_upper, method = fallback_wilson_if_degenerate( | ||
| 10.0, 30.0, successes=5, n=10 | ||
| ) | ||
| assert method == "bootstrap" | ||
| assert (ci_lower, ci_upper) == (10.0, 30.0) | ||
|
|
||
|
|
||
| def test_fallback_wilson_if_degenerate_none_inputs(): | ||
| assert fallback_wilson_if_degenerate(None, None, successes=5, n=10) == ( | ||
| None, | ||
| None, | ||
| "bootstrap", | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The substitution is silent: the only trace anywhere is the per-row
confidence_methodinreport.jsonl. Nothing is logged.Could this warn? The
elsebranch just below already warns when the CI is merely unavailable (line 176), and substituting the configured method seems at least as noteworthy:More testing is in progress on my side. I will follow up here if anything else turns up.