Skip to content

FEAT: Add opt-in judge calibration for decided outcome statistics - #4617

Open
Nithin (nithin42) wants to merge 4 commits into
microsoft:mainfrom
nithin42:feature/analytics-judge-calibration
Open

Nithin (nithin42) wants to merge 4 commits into
microsoft:mainfrom
nithin42:feature/analytics-judge-calibration

Conversation

@nithin42

Copy link
Copy Markdown
Contributor

Description

Adds an opt-in helper in pyrit.analytics that adjusts a decided attack success rate (OutcomeStatistics.success_rate_decided) for an objective scorer's known false-positive and false-negative rates, while leaving compute_outcome_statistics() completely unchanged (Phase 2 of #3579, building on #3889).

Raw attack success rates treat the judge as 100% accurate. When two cohorts are scored by judges with different sensitivity and specificity, raw numbers can flip which model looks safer. On a 50-sample live gpt-4o-mini run comparing a lenient refusal inverter (Se=0.925, Sp=0.775) against a 0.9 scale threshold scorer (Se=0.625, Sp=0.975):

Cohort True ASR Raw ASR (95% Wilson CI) Calibrated ASR (95% Lang-Reiczigel CI)
Model A (Scale >= 0.9) 30.0% 18.0% ([9.8%, 30.8%]) 25.8% ([9.1%, 50.0%])
Model B (Refusal Inverter) 14.0% 34.0% ([22.4%, 47.8%]) 16.4% ([0.0%, 38.8%])

Raw scores made the 2x safer model look almost twice as vulnerable, and its Wilson interval missed the true 14.0% rate. Calibration restores the true ranking and widens the interval honestly to include uncertainty from both the attack cohort and the judge evaluation set.

What this PR adds:

  • JudgeCalibrationProfile in pyrit/analytics/calibration.py to hold a judge's confusion counts (true_positive, false_positive, true_negative, false_negative) alongside dataset and scorer provenance (eval_hash, scorer_hash, scorer_class_name, dataset_name, dataset_version, num_scorer_trials, and counts_source). It reads stored counts from FEAT: Persist scorer specificity and add Wilson CI to outcome statistics #3889 directly and can also recover exact verified integer counts for existing objective_achieved_metrics.jsonl entries that have not been re-run yet.
  • calibrate_outcome_statistics() returning CalibratedOutcomeStatistics with the clipped Rogan-Gladen point estimate, closed-form Lang-Reiczigel (2014) confidence bounds in an isolated Decimal context (matching the subnormal outward-rounding behavior from FEAT: Persist scorer specificity and add Wilson CI to outcome statistics #3889), and the original unadjusted rate and Wilson bounds side by side.
  • Built-in reliability and scope guards: if Youden's index (sensitivity + specificity - 1) is at or below min_youden_index (0.20 by default) or its MOVER-Wilson lower confidence bound is at or below 0.0, calibrated outputs are suppressed (None, is_reliable=False) so weak or tiny-sample judges cannot produce misleading numbers. It also surfaces structured warnings for low decided_share, multi-trial majority-vote calibration, and clipped boundary estimates, and supports an optional expected_eval_hash check to block wrong-judge mix-ups.
  • Lazy exports in pyrit/analytics/__init__.py and opt-in usage notes in doc/code/analytics/0_attack_results.md.

Related to #3579 and #3889. CC Roman Lutz (@romanlutz) Richard Lundeen (@richlundeen) Qin Dai (@Allan-dq)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Registry-built profiles lose the persisted evaluation hash, breaking matching live-scorer binding, while fixed-precision calculations produce incorrect probabilities and interval bounds for very large cohorts.

2 open issues
What changed in this PR

Adds opt-in judge calibration to PyRIT analytics, building on #3889’s stored scorer metrics and Wilson intervals. Callers can adjust decided success rates for measured judge errors without changing compute_outcome_statistics().

Changes:

  • Adds calibration profiles using stored or verified recovered confusion counts, with scorer and dataset provenance.
  • Reports calibrated estimates and confidence bounds alongside observed rates, with reliability checks and structured warnings.
  • Adds lazy public exports, usage documentation, and numerical and edge-case tests.
File Description
tests/​unit/​analytics/​test_calibration.py Tests calibration formulas, profile recovery, validation, warnings, and numerical boundaries.
pyrit/​analytics/​calibration.py Implements calibration profiles, estimates, confidence intervals, and reliability checks.
pyrit/​analytics/​__init__.py Lazily exports the calibration API.
doc/​code/​analytics/​0_attack_results.md Documents opt-in usage, assumptions, and reliability behavior.

Review effort: Balanced

Comment thread pyrit/analytics/calibration.py Resolved
Comment thread pyrit/analytics/calibration.py Resolved
@nithin42

Copy link
Copy Markdown
Contributor Author

Addressed both Copilot review findings:

  1. Preserved persisted eval_hash in _load_metrics_from_file() via ComponentIdentifier.with_eval_hash() so registry-loaded ScorerMetricsWithIdentity profiles retain their evaluation hash and bind cleanly against live scorers (expected_eval_hash), with round-trip and non-null shipped-registry tests.
  2. Replaced the fixed 50-digit context in pyrit/analytics/calibration.py with bit-length-scaled Decimal precision and cancellation-free complement terms across all calibration helpers, adding regression tests for 10**100 (1 success out of 10**100, near-1, and tiny Youden's J) as well as 10**5000.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The confidence-level transformation mishandles valid extreme inputs, either raising StatisticsError or collapsing intervals to zero and removing the Youden guard’s uncertainty penalty.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Handle confidence levels that round to 0.5 or 1.0

pyrit/​analytics/​calibration.py:485

The validated range includes values that this floating-point transformation cannot handle. For confidence_level=0.9999999999999999, the argument rounds to 1.0 and inv_cdf raises StatisticsError; for 1e-20, it rounds to 0.5 and returns zero, collapsing the intervals and removing the uncertainty penalty from the Youden guard. Compute the quantile from the lower tail to avoid rounding to 1.0, and explicitly reject levels too small to produce a positive quantile.

1 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The Lang–Reiczigel correction and MOVER reliability guard require independent statistical review because the reference tests repeat the implemented equations and do not independently establish confidence-interval coverage.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants