#!/usr/bin/env python3 """analyze2783.py -- route 82 step (job 2783): exact Lambda_k spectrum and anchor checks from lagspec JSON. Lambda_k(T_x,p) = K_k / E, E = m(m-1)/(D-1) (exact exchangeable null, the same at every lag k; route 82 / #1394). repayment(p) = (K_2 - E) / (E - K_1): the share of the lag-1 deficit returned as lag-2 excess (#1394's 93%/89%, 67%/63%). plateau(p) = mean of Lambda_k over k = 4..64 (#1394's S4 statistic). R/E_rep with R = sum_g R_g, E_rep = sum_g h_g(h_g-1)/(D-1); forced class g = +-1 (mod 5) (#1355). Anchors: every fold shared with #1355's tN.json (m, K2, lambda), its repeat ratio, D, maxgap; #1394's Lambda_1..3. usage: analyze2783.py [] -> JSON on stdout, exit 1 on any anchor mismatch """ import json, sys from fractions import Fraction as Fr x = int(sys.argv[1]); L = json.load(open(sys.argv[2])) pub = json.load(open(sys.argv[3])) if len(sys.argv) > 3 else None D = L["D"] # #1394's evidence text (T23, T29), Lambda_1, Lambda_2, Lambda_3 rounded to 5 decimals R1394 = {23: {29: (0.03853, 1.89701, 0.69588), 31: (0.07289, 1.82829, 0.68805), 37: (0.05534, 0.38222, 0.84572)}, 29: {29: (0.11333, 1.59715, 0.81085), 31: (0.14836, 1.53408, 0.80657), 37: (0.13850, 0.52322, 0.98826)}} out = {"x": x, "D": D, "G2": L["G2"], "folds": {}, "anchors": []} bad = 0 def anchor(name, ours, theirs, ok): global bad out["anchors"].append({"name": name, "ours": ours, "published": theirs, "ok": ok}) bad += (not ok) for ps, F in L["folds"].items(): p = int(ps); m = F["m"]; K = F["K"] E = Fr(m * (m - 1), D - 1) lam = [Fr(k) / E if E else None for k in K] rec = {"m": m, "E": float(E), "K": K, "Lambda": [float(v) if v is not None else None for v in lam]} if E and E > K[0]: rec["repayment"] = float((K[1] - E) / (E - K[0])) if E: rec["plateau_4_64"] = float(sum(lam[3:64]) / 61) rec["min_4_64"] = float(min(lam[3:64])); rec["max_4_64"] = float(max(lam[3:64])) rec["argmin_4_64"] = 4 + min(range(61), key=lambda i: lam[3 + i]) out["folds"][ps] = rec if x in R1394 and p in R1394[x]: for k in range(3): anchor(f"#1394 Lambda_{k+1}(T{x},{p})", round(float(lam[k]), 5), R1394[x][p][k], abs(float(lam[k]) - R1394[x][p][k]) <= 5e-6) # repeat ratio and forced-zero class h = {int(g): v for g, v in L["hist"].items()}; Rg = {int(g): v for g, v in L["repeats"].items()} R = sum(Rg.values()); Erep = Fr(sum(v * (v - 1) for v in h.values()), D - 1) forced = [g for g in h if g % 5 in (1, 4)] Ef = Fr(sum(h[g] * (h[g] - 1) for g in forced), D - 1) out["repeat"] = {"R": R, "E_rep": float(Erep), "ratio": float(R / Erep), "forced_values": len(forced), "forced_R": sum(Rg[g] for g in forced), "forced_share_of_E_rep": float(Ef / Erep), "core_ratio": float(R / (Erep - Ef))} if pub: anchor("D", D, pub["D"], D == pub["D"]); anchor("max gap", L["G2"], pub["maxgap"], L["G2"] == pub["maxgap"]) anchor("R", R, pub["repeat"]["R"], R == pub["repeat"]["R"]) anchor("R/E_rep", out["repeat"]["ratio"], pub["repeat"]["ratio"], abs(out["repeat"]["ratio"] - pub["repeat"]["ratio"]) < 1e-12) for f in pub["folds"]: ps = str(f["p"]) if ps in L["folds"]: F = L["folds"][ps] ok = F["m"] == f["m"] and F["K"][0] == f["K2"] and abs(out["folds"][ps]["Lambda"][0] - f["lambda"]) < 1e-9 anchor(f"#1355 T{x} p={ps} (m, K2, lambda)", [F["m"], F["K"][0]], [f["m"], f["K2"]], ok) out["anchor_mismatches"] = bad print(json.dumps(out, indent=1)) sys.exit(1 if bad else 0)