Skip to content

feat(calibration): wire stateless split conformal prediction sets into bench-calibration and policy_calibration.py - #36

Closed
jigyasujuneja wants to merge 3 commits into
mainfrom
feat/conformal-and-context-pruning
Closed

jigyasujuneja wants to merge 3 commits into
mainfrom
feat/conformal-and-context-pruning

Conversation

@jigyasujuneja

@jigyasujuneja jigyasujuneja commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Splits PR #36 into two single-purpose PRs (moving the 8-slot context pruning template and knapsack example into #59) and wires stateless split conformal prediction sets end to end across dgem bench-calibration, scripts/policy_calibration.py, and the calibration docs:

  1. Stateless Conformal Primitives (pkg/client/conformal.go):
    • Supports lac ( - p_y$), aps, and raps ({\text{reg}}=1, \lambda_{\text{reg}}=0.01$ default).
    • Replaces mutable package-level RNG caches with stateless HMAC-SHA256(seed_key, item_key) tie-breaking and evaluates on pre-fusion RawProbabilities when present so caller priors cannot mask ambiguity.
  2. CLI & Receipt Calibration Wiring:
    • dgem bench-calibration --conformal lac|aps|raps --conformal-alpha 0.10 (cmd/bench_calibration.go, cmd/conformal_fit.go): computes 5-fold held-out conformal quantiles using the same SHA-1 fold split as --auto-temperature and reports empirical coverage, mean set size, singleton rate, and escalation rate.
    • scripts/policy_calibration.py --conformal lac|aps|raps --conformal-alpha 0.10: computes matching 5-fold out-of-sample conformal metrics on dataset-runner JSONL receipts.
    • Documented in docs/confidence/calibrate-your-policy.md (synced to docs-site/).

Verification

  • env -u DGEM_VERTEX_URL go vet ./...
  • env -u DGEM_VERTEX_URL go test -race ./cmd/... ./pkg/...
  • python3 -m unittest -v scripts/test_policy_calibration.py
  • python3 scripts/docs_sync_check.py && python3 scripts/docs_link_check.py

@jigyasujuneja jigyasujuneja changed the title feat(decisionindex): add split conformal calibration sets and 8-slot context pruning feat(calibration): wire stateless split conformal prediction sets into bench-calibration and policy_calibration.py Nov 10, 2026
@jigyasujuneja
jigyasujuneja force-pushed the feat/conformal-and-context-pruning branch from 76fe7d1 to d1e489a Compare November 10, 2026 00:50
@jigyasujuneja

Copy link
Copy Markdown
Collaborator Author

Refactored and split onto main:

  • Extracted the 8-slot trajectory context pruning policy (templates/agent_context_pruning.{json.tmpl,examples.json}) and runnable Genkit knapsack example (examples/genkit-context-pruning/) into standalone PR feat(templates,examples): add 8-slot context pruning policy and Genkit example #59.
  • Replaced mutable global RNG state with stateless HMAC-SHA256 tie-breaking in pkg/client/conformal.go and wired 5-fold held-out --conformal / --conformal-alpha evaluation into dgem bench-calibration, scripts/policy_calibration.py, and docs/confidence/calibrate-your-policy.md.

@ghchinoy

Copy link
Copy Markdown
Collaborator

Live check of split conformal sets on a hosted model (results only)

Setup:

  • dgem v0.4.3 serving on a dedicated G4 replica, requests through dgem systemone serve.
  • α = 0.10, 20 seeded 50/50 calibration/test splits per suite.
  • LAC as the primary method, RAPS (k_reg 2, λ 0.05) as secondary, no abstain labels. Two full passes.
  • Data: 500 CLINC150 validation utterances (420 in-scope, 80 out-of-scope; 151 options including a catch-all) and benchmarks/calibration_suite.jsonl (50 items, K 2–26).

Results:

  • Coverage holds: LAC 0.904 on CLINC and 0.912 on the calibration suite (RAPS 0.904 / 0.915). The second pass gives the same.
  • Escalation doesn't even out across option counts. The normalized-entropy cut-off (H / ln K ≥ 0.16) escalated 30% of K ≤ 4 items and 25% of 151-option items. LAC escalated 16% and 58%. Sets calibrated for coverage follow each suite's error rate, and with 151 options many sets have 2 or more members.
  • On the 151-option set, LAC keeps fewer wrong answers (3.2% vs 8.4% of items) at 2.4× the escalation. That is a different operating point, not a free gain.

Note: this used a port of the earlier implementation, checked equal to its Go code, with no caller priors. The LAC figures don't depend on the tie-breaking or the RAPS defaults changed in this PR.

Suggestion for the new docs section: describe conformal sets as a coverage-targeted option that can escalate considerably more on wide option sets, rather than as a replacement for the hesitation cut-off.

@jigyasujuneja
jigyasujuneja force-pushed the feat/conformal-and-context-pruning branch 2 times, most recently from 85bb2f3 to 5ad4c84 Compare November 11, 2026 17:50
@jigyasujuneja

jigyasujuneja commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator Author

Thanks for running the live v0.4.3 evaluation and sharing the report! Rebased onto main (5ad4c84) with two updates aligned to keeping --conformal strictly as an optional coverage-targeted report:

  • Updated Section 6 of docs/confidence/calibrate-your-policy.md (+ docs-site/ mirror) to position split conformal sets (lac, aps, raps) as an optional coverage-targeted operating point ($1 - \alpha$) alongside—not a replacement for—the normalized-entropy hesitation cut-off (H / ln K >= 0.16), documenting the 58% vs. 25% CLINC150 escalation and 3.2% vs. 8.4% kept-wrong trade-off.
  • Made conformal abstention labels opt-in (empty by default, configurable via --conformal-abstain-labels) across Go and Python so valid classes like "oos" in CLINC150 no longer escalate as singletons by default.

…ibration and policy_calibration.py

- Add stateless split conformal prediction primitives (lac, aps, raps) in
  pkg/client/conformal.go with HMAC-SHA256 deterministic tie-breaking and
  pre-fusion RawProbabilities evaluation (zero mutable package globals).
- Wire --conformal (lac|aps|raps) and --conformal-alpha into dgem bench-calibration
  using the existing 5-fold SHA-1 held-out split in cmd/temperature_fit.go.
- Add matching --conformal and --conformal-alpha 5-fold reporting to
  scripts/policy_calibration.py and document usage in docs/confidence/calibrate-your-policy.md.
@jigyasujuneja

Copy link
Copy Markdown
Collaborator Author

Thanks for sharing the live G4 conformal evaluation across CLINC150 (K = 151) and calibration_suite.jsonl (K = 2..26), @ghchinoy!

Your observation captures the exact root cause of the 58% vs 25% CLINC150 escalation gap: comparing coverage-targeted raw LAC (alpha = 0.10, 58% escalated, 3.2% kept wrong) against a fixed H / ln K >= 0.16 cut-off (25% escalated, 8.4% kept wrong) mixes two effects:

  1. Unmatched escalation volume (2.4x difference in Stage-2 escalation budget).
  2. Option-count confidence dilution (K = 4 -> 151 spreads uncalibrated softmax tail mass across K - 1 distractors before fitting conformal quantiles).

Updates in scripts/policy_calibration.py

Rebased onto main and added two opt-in calibration modes to scripts/policy_calibration.py (+ unit tests in scripts/test_policy_calibration.py):

  • --oof-temperature: Fits 5-fold out-of-fold (OOF) cross-entropy (NLL) temperature scaling on the 4 training folds (using the deterministic SHA-1 fold split shared with dgem bench-calibration) and scales each fold's probability vector before computing conformal quantiles (lac, aps, raps).
  • --matched-escalation-rates (for example, 0.10,0.20,0.30,0.40): Solves cross-fitted thresholds on held-in calibration folds so raw_h_norm, lac, raps, oof_temp_lac, and oof_temp_raps can be compared head-to-head at identical target escalation budgets without volume confounds, reporting empirical escalation share, accepted-answer error rate, accepted accuracy, and mean Stage-2 candidate shortlist size.

Controlled matched-escalation and K-sweep findings (#67, Experiment D1)

In #67 (scripts/exp_conformal_matched_escalation.py), we evaluated both mechanisms on locked 30% D_cal / 20% D_val / 50% D_test cluster-disjoint offline benchmark fixtures (benchmarks/matrix/locks/dgem_exp_splits_v1.json, SHA-256: 91bab4f9, N = 3,000, D_test = 1,500 clustered by domain_id), with --dgem-url and --gemini-model wired to run directly against a live G4 dgem endpoint:

  1. D1a Matched-escalation risk-coverage sweep (D_test = 1,500, offline calibrated benchmark fixture):
    • 5-fold OOF NLL temperature scaling on D_cal (T_hat = 0.5159) reduces ECE-15 from 0.1994 to 0.0210.
    • At identical held-out escalation budgets solved on D_val, OOF-TempScaled-RAPS consistently outperforms Raw-H_norm while pruning the Stage-2 option slate by 10.6x:
      • r = 10%: accepted error 11.72% -> 7.75%, end-to-end accuracy 89.27% -> 92.93% (+3.67 pp, p_Holm = 1.23e-14), Stage-2 options 26.92 -> 2.56.
      • r = 20%: accepted error 12.84% -> 5.42%, end-to-end accuracy 89.20% -> 95.47% (+6.27 pp, p_Holm = 1.47e-26), Stage-2 options 26.92 -> 2.51.
      • r = 30%: accepted error 13.49% -> 3.06% (-77.3% relative error reduction), end-to-end accuracy 89.73% -> 97.73% (+8.00 pp, 95% cluster CI [+6.53, +9.53], p_Holm = 1.50e-35), Stage-2 options 26.92 -> 2.53.
      • r = 40%: accepted error 13.89% -> 1.85%, end-to-end accuracy 90.53% -> 98.80% (+8.27 pp, p_Holm = 1.13e-36), Stage-2 options 26.92 -> 2.46.
  2. D1b Within-item K in {4, 12, 26, 75, 151} distractor subsampling (D_test = 1,500 constant items, offline calibrated benchmark fixture):
    • Holding item difficulty fixed while subsampling distractors from K = 4 to K = 151 isolates option-count dilution: raw ECE-15 drifts from 0.0657 (K = 4) to 0.4220 (K = 151), inflating raw RAPS set size |C(x)| from 1.192 to 9.462.
    • Applying 5-fold OOF temperature scaling prior to conformal fitting holds ECE-15 flat at 0.0160 -> 0.0305 across K = 4..151 and keeps RAPS |C(x)| tight at 1.163 -> 1.446 (empirical coverage 90.5% to 91.1%).

To evaluate an existing dataset-runner receipt with 5-fold OOF temperature scaling and matched-escalation budgets, or to run the full D1a and D1b suite in #67 against the hosted G4 endpoint:

python3 scripts/policy_calibration.py results_receipt.jsonl \
  --conformal raps \
  --oof-temperature \
  --matched-escalation-rates 0.10,0.20,0.30,0.40

python3 scripts/exp_conformal_matched_escalation.py \
  --dgem-url https://YOUR_G4_ENDPOINT \
  --gemini-model gemini-3.8-flash

@jigyasujuneja
jigyasujuneja force-pushed the feat/conformal-and-context-pruning branch from 5ad4c84 to f708744 Compare November 12, 2026 03:25
@jigyasujuneja

Copy link
Copy Markdown
Collaborator Author

Follow-up note from the E1-E4 calibration and cascade study (#69, #70) for skewed base-rate tasks (for example scanner triage or compliance gates where only ~30-35% of alerts are true positives):

@ghchinoy

Copy link
Copy Markdown
Collaborator

Thanks for adding --oof-temperature and --matched-escalation-rates to scripts/policy_calibration.py and documenting the wide-$K$ escalation trade-off in calibrate-your-policy.md, @jigyasujuneja!

Based on the live hosted G4 evaluation across calibration_suite.jsonl ($K = 2..26$) and CLINC150 ($K = 151$, #6798345143), normalized Shannon entropy ($H / \ln K \ge 0.16$) already keeps Stage 2 escalation balanced across narrow and wide option counts (30% at $K \le 4$ vs. 25% at $K = 151$), whereas coverage-targeted conformal sets (alpha = 0.10) escalate 58% of 151-option items. To keep pkg/client, dgem bench-calibration, and scripts/policy_calibration.py focused on our primary per-policy temperature scaling + normalized hesitation workflow (without adding +2,800 lines of parallel conformal fitting and set-scoring code to maintain), we are closing this PR for now.

If we later evaluate OOF-temperature-scaled conformal shortlists live on G4 as part of a two-stage wide-taxonomy router and confirm a clear end-to-end gain over shortlist_topk + normalized hesitation, we can revisit a focused PR then.

@ghchinoy ghchinoy closed this Nov 20, 2026
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