#!/usr/bin/env python3 """Per-opponent generalisation stats: the label~lead law, the ridge baselines and the strictly-prior-scan leakage audit, for every corpus passed on the command line. python3 gen_analysis.py gen/DrussGT.ndjson gen/Dookious.ndjson ... Reuses analyse_realistic.py's prep/fit/cv (same labels, same 5-fold round-group split, same ridge) and additionally reports the fitted slope on beliefPredErr, i.e. (1 - slope) = fraction of the correct lead the opponent actually applies. """ import math, os, sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from analyse_realistic import prep, fit, cv, const, F64, ALL20 def law(d): B = fit(d, ["beliefPredErr"]) n = len(d) mx = sum(r["beliefPredErr"] for r in d) / n my = sum(r["label"] for r in d) / n sxy = sum((r["beliefPredErr"] - mx) * (r["label"] - my) for r in d) sxx = sum((r["beliefPredErr"] - mx) ** 2 for r in d) syy = sum((r["label"] - my) ** 2 for r in d) corr = sxy / math.sqrt(sxx * syy) if sxx > 0 and syy > 0 else 0.0 return B[0], B[1], corr def scan_stats(d): h = {} for r in d: a = r["beliefScanAge"] h[a] = h.get(a, 0) + 1 same = h.get(0.0, 0) + h.get(1.0, 0) * 0 # exact 0 return h, same / len(d) def main(): print("== per-opponent: constant, predErr law, ridge baselines, leakage audit ==") hdr = (f"{'opponent':<14}{'n':>6}{'const':>8}{'predErr':>9}{'corr':>7}" f"{'slope':>8}{'intcpt':>8}{'lead%':>7}{'ALL20':>8}" f"{'|prev|':>7}{'prevConst':>10}{'prevPredErr':>12}{'prevALL20':>10}" f"{'sameTick%':>11}{'everScanned%':>14}") print(hdr) for p in sys.argv[1:]: d = prep(p) s, b, corr = law(d) r_pred = cv(d, ["beliefPredErr"])[0] r_all = cv(d, ALL20)[0] pp = p.replace(".ndjson", "_prev.ndjson") if os.path.exists(pp): dp = prep(pp) pc, ppd, pa = const(dp), cv(dp, ["beliefPredErr"])[0], cv(dp, ALL20)[0] else: pc = ppd = pa = float("nan") h, samefrac = scan_stats(d) ever = sum(1 for r in d if r["beliefEverScanned"] > 0) / len(d) print(f"{os.path.basename(p).replace('.ndjson',''):<14}{len(d):>6}" f"{const(d):>8.4f}{r_pred:>9.4f}{corr:>7.3f}{s:>8.3f}{b:>8.4f}" f"{100*(1-s):>7.1f}{r_all:>8.4f}" f"{sum(1 for r in prep(pp) if r.get('prevOnly')==1.0) if os.path.exists(pp) else 0:>7}" f"{pc:>10.4f}{ppd:>12.4f}{pa:>10.4f}" f"{100*samefrac:>11.1f}{100*ever:>14.1f}") print(f"{'':<14}scan-age histogram: " + ", ".join(f"{int(k)}:{v}" for k, v in sorted(h.items())[:6]) + (" ..." if len(h) > 6 else "")) if __name__ == "__main__": main()