trust-calibration-benchmark / code /analyze_decouple.py
carlostoxtli's picture
Upload Trust-Calibration Benchmark (TCB) v1.0.0
f8c91b8 verified
Raw History Blame Contribute Delete
2.99 kB
"""Offline re-analyzer for the Q3 confidence-elicitation manipulation (Section 6).
Recomputes, from the shipped per-trial file with no endpoint, the per-model per-variant
accuracy, mean confidence, five-bin ECE, and answer-agreement-with-default that the paper
reports, and checks them against the shipped summary tcb_decouple.json.
python3 analyze_decouple.py
The manipulation re-elicits three top-accuracy models under prompt variants (default, hedged,
bold, flat) that change only how confidence is reported, not what is answered; the flat variant
is a mechanical worst case (forced mid-scale confidence) that bounds the manipulation.
"""
import json, pathlib
from collections import defaultdict
HERE = pathlib.Path(__file__).resolve().parent
TRIALS = HERE / ".." / "data" / "tcb_decouple_trials.json"
SUMMARY = HERE / ".." / "data" / "tcb_decouple.json"
def ece5(items):
"""items: list of (conf_prob in [0,1], correct in {0,1}); five equal-width bins."""
bins = defaultdict(list)
for cp, corr in items:
bins[min(4, int(cp * 5))].append((cp, corr))
n = len(items)
e = 0.0
for xs in bins.values():
mc = sum(x[0] for x in xs) / len(xs)
ma = sum(x[1] for x in xs) / len(xs)
e += len(xs) / n * abs(mc - ma)
return e
def main():
trials = json.load(open(TRIALS))
summary = json.load(open(SUMMARY))
# default-elicitation answers per (model, id) to compute answer agreement.
default_ans = {}
for t in trials:
if t["variant"] == "default":
default_ans[(t["model"], t["id"])] = t["answer"]
by = defaultdict(list)
for t in trials:
by[(t["model"], t["variant"])].append(t)
mismatches = 0
print(f"{'model':16} {'variant':8} {'acc':>6} {'mean_conf':>9} {'ece5':>7} {'agree_default':>13}")
for (model, variant), rows in sorted(by.items()):
n = len(rows)
acc = sum(r["correct"] for r in rows) / n
confs = [r["conf"] for r in rows if r["conf"] is not None]
mean_conf = sum(confs) / len(confs) if confs else float("nan")
items = [((0.5 if r["conf"] is None else r["conf"] / 100.0), r["correct"]) for r in rows]
e = ece5(items)
agree = sum(1 for r in rows if default_ans.get((model, r["id"])) == r["answer"]) / n
print(f"{model:16} {variant:8} {acc:6.3f} {mean_conf:9.1f} {e:7.4f} {agree:13.2f}")
ref = summary["per"][model][variant]
# mean_conf is reported to one decimal in the summary, so allow that rounding.
for label, got, want, tol in [("acc", acc, ref["accuracy"], 5e-3), ("ece5", e, ref["ece5"], 5e-3),
("mean_conf", mean_conf, ref["mean_conf"], 0.1)]:
if abs(got - want) > tol:
print(f" MISMATCH {label}: recomputed {got:.4f} vs shipped {want:.4f}")
mismatches += 1
print("DECOUPLE OK" if mismatches == 0 else f"{mismatches} MISMATCHES")
if __name__ == "__main__":
main()