trust-calibration-benchmark / code /analyze_harness.py
carlostoxtli's picture
Upload Trust-Calibration Benchmark (TCB) v1.0.0
f8c91b8 verified
Raw History Blame Contribute Delete
5.23 kB
"""Harness-validity analysis: the same nine models on the same sixty items under three
qualification protocols.
naive fixed 512-token budget, unaudited parser (last-match regex, no template guard,
truncated fragments force-scored): ../data/tcb_v03_trials_naive.json
constrained fixed 512-token budget, audited parser, no escalation (an unparseable response
makes the model unavailable on that item): ../data/tcb_v03_trials_constrained.json
adequate audited parser plus escalate-on-unparseable to a 4000-token budget (the paper's
headline protocol): ../data/tcb_v03_trials.json
Reports, per model and protocol: availability (parseable items), accuracy over answered items,
and yes-rate; emits the comparison figure and a JSON summary. Offline; stdlib + matplotlib.
python3 analyze_harness.py
"""
import json, pathlib
import matplotlib; matplotlib.use("Agg")
import matplotlib.pyplot as plt
from collections import defaultdict
HERE = pathlib.Path(__file__).resolve().parent
DATA = HERE / ".." / "data"
FIG = HERE / ".." / "figures"
LABEL_FIXES = {"g7": "Yes"}
FILES = {"naive": "tcb_v03_trials_naive.json",
"constrained": "tcb_v03_trials_constrained.json",
"adequate": "tcb_v03_trials.json"}
MODELS = ["glm-5.1-fp8", "deepseek-v4-pro", "gemma-4-31b", "gptoss-120b", "leanstral-2603",
"qwen3-omni-30b-a3b", "qwen3.6-35b-a3b-fp8", "qwen3.6-27b-fp8", "qwen3.5-9b"]
SHORT = {"glm-5.1-fp8": "glm", "deepseek-v4-pro": "deepseek", "gemma-4-31b": "gemma",
"gptoss-120b": "gptoss", "leanstral-2603": "leanstral", "qwen3-omni-30b-a3b": "qwen-omni",
"qwen3.6-35b-a3b-fp8": "qwen3.6-35b", "qwen3.6-27b-fp8": "qwen3.6-27b", "qwen3.5-9b": "qwen3.5-9b"}
def load(fname):
trials = json.load(open(DATA / fname))
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"])
by = defaultdict(list)
for r in trials:
by[r["model"]].append(r)
return by
def main():
out = {"protocols": list(FILES), "per_model": {}}
data = {p: load(f) for p, f in FILES.items()}
for m in MODELS:
row = {}
for p in FILES:
rs = data[p][m]
ans = [r for r in rs if r.get("answer") is not None]
row[p] = {"available": len(ans), "n_items": len(rs),
"accuracy_answered": round(sum(r["correct"] for r in ans)/len(ans), 4) if ans else None,
"yes_rate": round(sum(1 for r in ans if r["answer"] == "Yes")/len(ans), 4) if ans else None}
out["per_model"][m] = row
worst = max(MODELS, key=lambda m: (out["per_model"][m]["adequate"]["accuracy_answered"] or 0)
- (out["per_model"][m]["naive"]["accuracy_answered"] or 1))
out["headline"] = {
"max_apparent_accuracy_error_model": worst,
"max_apparent_accuracy_error_pp": round(100*((out["per_model"][worst]["adequate"]["accuracy_answered"] or 0)
- (out["per_model"][worst]["naive"]["accuracy_answered"] or 0)), 1)}
json.dump(out, open(DATA / "tcb_harness_comparison.json", "w"), indent=2)
plt.rcParams.update({"font.size": 11, "axes.labelsize": 12, "xtick.labelsize": 9, "ytick.labelsize": 10})
fig, axes = plt.subplots(1, 2, figsize=(10.6, 3.9))
xs = list(range(len(MODELS)))
colors = {"naive": "#d95f0e", "constrained": "#756bb1", "adequate": "#2c7fb8"}
w = 0.27
for k, p in enumerate(FILES):
vals = [out["per_model"][m][p]["accuracy_answered"] or 0 for m in MODELS]
axes[0].bar([x + (k-1)*w for x in xs], vals, w, label=p, color=colors[p], edgecolor="black", lw=0.5)
axes[0].set_ylabel("accuracy on answered items"); axes[0].set_ylim(0.5, 1.05)
axes[0].set_xticks(xs); axes[0].set_xticklabels([SHORT[m] for m in MODELS], rotation=40, ha="right")
axes[0].legend(fontsize=9); axes[0].grid(axis="y", alpha=0.25)
for k, p in enumerate(FILES):
vals = [out["per_model"][m][p]["available"]/60 for m in MODELS]
axes[1].bar([x + (k-1)*w for x in xs], vals, w, label=p, color=colors[p], edgecolor="black", lw=0.5)
axes[1].set_ylabel("availability (parseable items / 60)"); axes[1].set_ylim(0.5, 1.05)
axes[1].set_xticks(xs); axes[1].set_xticklabels([SHORT[m] for m in MODELS], rotation=40, ha="right")
axes[1].grid(axis="y", alpha=0.25)
plt.tight_layout()
plt.savefig(FIG / "tcb_harness.pdf", bbox_inches="tight")
plt.savefig(FIG / "tcb_harness.png", dpi=140, bbox_inches="tight"); plt.close()
print(f"{'model':22s} naive_acc(yes) constrained avail/acc adequate acc")
for m in MODELS:
r = out["per_model"][m]
print(f"{m:22s} {r['naive']['accuracy_answered']} ({r['naive']['yes_rate']}) "
f"{r['constrained']['available']}/60 {r['constrained']['accuracy_answered']} "
f"{r['adequate']['accuracy_answered']}")
print("headline:", out["headline"])
print("wrote tcb_harness_comparison.json and figures/tcb_harness.{pdf,png}")
if __name__ == "__main__":
main()