trust-calibration-benchmark / code /derive_naive.py
carlostoxtli's picture
Upload Trust-Calibration Benchmark (TCB) v1.0.0
f8c91b8 verified
Raw History Blame Contribute Delete
3.84 kB
"""Re-derive the naive-protocol records from the shipped raw responses (offline).
The naive and constrained protocols consumed the SAME cached model generations (a fixed
512-token budget, with the client's empty-content retry at 3000 tokens); they differ only in
the parser. This script applies the HISTORICAL (naive) parser, preserved below verbatim, to
the full raw responses shipped in tcb_v03_trials_constrained.json, and writes
tcb_v03_trials_naive.json. Its per-model accuracies reproduce the naive column of the
harness-validity comparison exactly, so the naive condition is fully auditable offline.
python3 derive_naive.py
"""
import json, re, pathlib
HERE = pathlib.Path(__file__).resolve().parent
SRC = HERE / ".." / "data" / "tcb_v03_trials_constrained.json"
OUT = HERE / ".." / "data" / "tcb_v03_trials_naive.json"
def naive_parse(text):
"""The historical parser, verbatim: last-match regex with no template guard, falling back
to a bare yes/no line, then to ANY lone yes/no token in the text. Both defects the paper
documents live here: a quoted instruction template ('ANSWER: Yes|No') matches as a
confident Yes, and truncated chain-of-thought fragments are force-scored."""
t = text or ""
confs = re.findall(r"CONF\s*[:=]?\s*(\d{1,3})", t, flags=re.IGNORECASE)
conf = max(0, min(100, int(confs[-1]))) if confs else None
tagged = re.findall(r"ANSWER\s*[:=]?\s*\**\s*(Yes|No)\b", t, flags=re.IGNORECASE)
strict = bool(tagged)
if tagged:
ans = tagged[-1].capitalize()
else:
bare = [w for w in (re.sub(r"[^A-Za-z]", "", ln).lower() for ln in t.splitlines())
if w in ("yes", "no")]
if bare:
ans = bare[-1].capitalize()
else:
m = re.search(r"\b(Yes|No)\b", t, flags=re.IGNORECASE)
ans = m.group(1).capitalize() if m else None
if ans is None:
return None, None, strict, 0
return ans, (conf if conf is not None else 50), strict, int(conf is not None)
def main():
rows = json.load(open(SRC))
out = []
for r in rows:
raw = r.get("raw") or ""
ans, conf, strict, conf_stated = naive_parse(raw)
base = {"model": r["model"], "scenario": r["scenario"], "id": r["id"], "truth": r["truth"],
"tier": r["tier"], "seed": r["seed"], "raw": raw, "strict": int(strict),
"conf_stated": conf_stated}
if ans is None:
out.append({**base, "answer": None, "conf": None, "correct": None, "adherent": 0})
else:
out.append({**base, "answer": ans, "conf": conf,
"correct": int(ans == r["truth"]), "adherent": 1})
# Make the "0/540 mismatches" claim a one-command check: diff the freshly re-derived
# records against the shipped reference naive file before (re)writing it.
if OUT.exists():
ref = json.load(open(OUT))
rk = {(r["model"], r["scenario"], r["id"], r["seed"]): r for r in ref}
mism = sum(1 for r in out
if (o := rk.get((r["model"], r["scenario"], r["id"], r["seed"]))) is None
or o.get("answer") != r["answer"] or o.get("correct") != r["correct"])
print(f"re-derived vs shipped naive reference: {mism} mismatches of {len(out)}")
json.dump(out, open(OUT, "w"), indent=1)
from collections import defaultdict
by = defaultdict(list)
for r in out:
by[r["model"]].append(r)
for m, rs in by.items():
ans = [r for r in rs if r["adherent"]]
acc = sum(r["correct"] for r in ans) / len(ans) if ans else float("nan")
yes = sum(1 for r in ans if r["answer"] == "Yes") / len(ans) if ans else float("nan")
print(f" {m:24s} n={len(ans):2d} acc={acc:.3f} yes={yes:.2f}")
print(f"wrote {OUT}")
if __name__ == "__main__":
main()