#!/usr/bin/env python3 """Side analyses for the realistic-feature experiment (see README). 1. Is the label predictable from the SHOOTER-side belief features? (ridge, CV) 2. LEAKAGE AUDIT: re-extract with the belief built ONLY from scans strictly BEFORE the fire tick (samples_realistic_prev.ndjson) -- if the gain survives without the same-tick scan, it is robust; if it vanishes, the whole effect is the shooter's fresh same-tick scan. 3. Which feature subsets carry the signal. run: python3 analyse_realistic.py """ import json, math, sys, os HERE = os.path.dirname(os.path.abspath(__file__)) F64 = ["dS", "lat", "clo", "tofS", "sp", "bs"] ALL20 = ["dS", "invD", "beliefSinRelBearing", "beliefCosRelBearing", "sp", "bs", "age", "lat", "clo", "beliefLosRate", "tofS", "beliefSinPredRelBearing", "beliefCosPredRelBearing", "beliefPredErr", "gunHeat", "power", "ownSpeed", "beliefDisp", "round", "tick"] def prep(path): d = [json.loads(l) for l in open(path)] for r in d: r["lat"] = r["beliefTgtLatVel"] / 10 r["clo"] = r["beliefClosing"] / 10 r["tofS"] = r["beliefTof"] / 30 r["bs"] = r["bulletSpeed"] / 20 r["dS"] = r["beliefDist"] / 1000 r["invD"] = 1.0 / (1.0 + r["beliefDist"] / 100.0) r["sp"] = r["beliefTgtSpeed"] / 10 r["age"] = min(r["beliefScanAge"], 30) / 30 return d def fit(rows, feats): p = len(feats) + 1 A = [[0.0] * p for _ in range(p)] B = [0.0] * p for r in rows: x = [r[f] for f in feats] + [1.0] for a in range(p): B[a] += x[a] * r["label"] for b in range(p): A[a][b] += x[a] * x[b] for a in range(p): A[a][a] += 1e-8 for c in range(p): piv = max(range(c, p), key=lambda i: abs(A[i][c])) A[c], A[piv] = A[piv], A[c] B[c], B[piv] = B[piv], B[c] dd = A[c][c] for j in range(c, p): A[c][j] /= dd B[c] /= dd for i in range(p): if i != c and A[i][c]: f = A[i][c] for j in range(c, p): A[i][j] -= f * A[c][j] B[i] -= f * B[c] return B def pred(B, r, feats): return sum(w * v for w, v in zip(B, [r[f] for f in feats] + [1.0])) def cv(d, feats): groups = [] for r in d: if r["group"] not in groups: groups.append(r["group"]) fold = {g: i % 5 for i, g in enumerate(groups)} se = n = 0 per = [] for f in range(5): tr = [r for r in d if fold[r["group"]] != f] te = [r for r in d if fold[r["group"]] == f] B = fit(tr, feats) s = 0.0 for r in te: s += (pred(B, r, feats) - r["label"]) ** 2 per.append(math.sqrt(s / len(te))) se += s n += len(te) return math.sqrt(se / n), per def const(d): m = sum(r["label"] for r in d) / len(d) return math.sqrt(sum((r["label"] - m) ** 2 for r in d) / len(d)) def main(): d = prep(os.path.join(HERE, "samples_realistic.ndjson")) p = os.path.join(HERE, "samples_realistic_prev.ndjson") print("== 5-fold-by-round-group ridge, realistic features (pooled held-out RMSE) ==") print(f" constant mean {const(d):.4f}") for name, fs in [("predErr alone", ["beliefPredErr"]), ("our-state only (dist,lat,closing,tof,spd)", F64), ("our-state + predErr", F64 + ["beliefPredErr"]), ("ALL realistic (20)", ALL20)]: r, per = cv(d, fs) print(f" {name:38s} {r:.4f} per-fold " + " ".join(f"{x:.4f}" for x in per)) if os.path.exists(p): dp = prep(p) print("\n== LEAKAGE AUDIT: belief built ONLY from scans strictly before the fire tick ==") n = sum(1 for r in dp if r.get("prevOnly") == 1.0) print(f" rows with a strictly-prior scan: {n}/{len(dp)}") print(f" constant mean {const(dp):.4f}") for name, fs in [("predErr alone", ["beliefPredErr"]), ("our-state only", F64), ("our-state + predErr", F64 + ["beliefPredErr"]), ("ALL realistic (20)", ALL20)]: r, _ = cv(dp, fs) print(f" {name:38s} {r:.4f}") print("\n== law check: label ~ a*predErr + b ==") B = fit(d, ["beliefPredErr"]) print(f" slope {B[0]:.4f} intercept {B[1]:.4f}" " -> DrussGT aims at (current bearing + ~%.0f%% of the correct lead)" % (100 * (1 - B[0]))) ages = {} for r in d: ages[r["beliefScanAge"]] = ages.get(r["beliefScanAge"], 0) + 1 print(" scan-age histogram (ticks since the shooter last scanned us):", dict(sorted(ages.items()))) if __name__ == "__main__": main()