File size: 2,455 Bytes
f8c91b8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | """Sensitivity check: do the headline findings survive excluding confidence-imputed trials?
A response with a parseable answer but no stated CONF receives an imputed neutral confidence
of 50 and is flagged conf_stated=0 in the trials file. This script recomputes, on the subset
of trials WITH stated confidence only: (a) the accuracy-gated calibration-error separation,
and (b) the confidence-inversion contrast (accuracy at maximal stated confidence versus below)
for the three poorly calibrated models. Offline; standard library only.
python3 sensitivity.py
"""
import json, math, random, pathlib
from collections import defaultdict
HERE = pathlib.Path(__file__).resolve().parent
trials = json.load(open(HERE / ".." / "data" / "tcb_v03_trials.json"))
LABEL_FIXES = {"g7": "Yes"}
for r in trials:
if r["id"] in LABEL_FIXES:
r["truth"] = LABEL_FIXES[r["id"]]
if r.get("answer") is not None:
r["correct"] = int(r["answer"] == r["truth"])
def ece(rows, nb=5):
bins = defaultdict(list)
for r in rows:
p = r["conf"] / 100; b = min(nb - 1, int(p * nb)); bins[b].append(r)
n = len(rows); e = 0.0
for rs in bins.values():
conf = sum(x["conf"] / 100 for x in rs) / len(rs)
acc = sum(x["correct"] for x in rs) / len(rs)
e += len(rs) / n * abs(conf - acc)
return e
def boot_ci(rows, fn, B=2000, seed=0):
rng = random.Random(seed); vals = []
for _ in range(B):
s = [rows[rng.randrange(len(rows))] for _ in rows]
try: vals.append(fn(s))
except Exception: pass
vals.sort(); return (vals[int(.025 * len(vals))], vals[int(.975 * len(vals))])
by = defaultdict(list)
n_imputed = 0
for r in trials:
if r.get("adherent"):
if not r.get("conf_stated", 1):
n_imputed += 1
continue
by[r["model"]].append(r)
print(f"excluded {n_imputed} confidence-imputed trials; re-analysis on stated-confidence rows only\n")
# Per-model ECE with vs without imputed-confidence rows (stability check).
import collections
full = collections.defaultdict(list)
for r in trials:
if r.get("adherent"):
full[r["model"]].append(r)
print(f"{'model':22s} {'n_imp':>5s} ece(all) ece(stated-only) delta")
for m, rs in full.items():
stated = by[m]
if not stated: continue
e_all, e_st = ece(rs), ece(stated)
print(f"{m:22s} {len(rs)-len(stated):5d} {e_all:.4f} {e_st:.4f} {e_st-e_all:+.4f}")
|