"""Job #2550 (leads: new statistic with a falsifier). THE STATISTIC. For a level x (tile T_x, period M = x#) and a window length H, over the windows [a, a+H) aligned to multiples of H inside each complete period k of the exposure [M, (P+1)M): A_i = number of tile slots in the window (a census quantity, exact, period-invariant), N_i = number of twin pairs (n, n+2) with n in the window, lambda_k = twins_k / D (the period's occupancy rate, R1 of route 35), R_cond = sum_i (N_i - lambda_k A_i)^2 / sum_i lambda_k A_i (the dispersion of occupancy GIVEN the tile). THE DECISION. Route 35 (#1302, #1309) matched the unconditional dispersion R = Var N / E N to the 4-tuple conjecture; the audit of paper/variance-note.md (#1319) and return #1316 show that part of that dispersion is the tile's own regularity (Var A / E A = 1 - E[A](1 - rho^(<=x)), exact). R_cond removes the tile and asks whether what is left is what the conjecture's LARGE-prime factors say: prediction R_HL_cond = (1 - lambda) - lambda E[A] (rho^(<=x) - rho_H), where rho^(<=x) is the singular-series sum truncated to the tile's primes (exact product formula) and rho_H the full sum (#1302). THE MATCHED CONTROL. Independent thinning of the tile at rate lambda_k (each slot a twin with probability lambda_k, independently): then N_i | A_i ~ Bin(A_i, lambda_k) and R_cond = 1 - lambda exactly in expectation; 400 thinning draws give the null distribution of R_cond (its width sets the scale at which a large-prime effect is visible). PRE-REGISTERED FALSIFIERS (written before the run). F1 (effect present): the measured R_cond lies below the thinning null's 0.01 quantile at x = 29 for both H (the level with the statistics to see it) and has the predicted sign at x = 19, 23. F2 (effect as predicted): |R_cond - R_HL_cond| <= 2 sigma_boot at every level and length (6 cells). Scale: at x = 19, H = 2310, the predicted offset from the control is lambda E[A] (rho^(<=19) - rho_2310) = 0.110 * 90.2 * 0.00736 = 0.073 against a bootstrap sd of about 0.01 over 33,600 windows; at x = 29 the offset is 0.035 against a sd below 0.002. Gates: G1 tile slot count per period = D(T_x); G2 sum of A_i over a period's windows <= D and sum of N_i = twins in the covered range; G3 the control's mean R over draws within 3 sd of 1 - lambda. Stdout: ledger + JSON; stderr: progress. RUN: python rcond2550.py --levels 19,23,29 --out rcond2550.json """ import argparse, json, math, sys, time import numpy as np from importlib.machinery import SourceFileLoader bg = SourceFileLoader("bg", "../job1936/job1936-blockgrain.py").load_module() vr = SourceFileLoader("vr", "../job2656/var2656.py").load_module() C2 = vr.C2; PERIODS = {19: 8, 23: 2, 29: 2}; D_T = {19: 378675, 23: 7952175, 29: 214708725} HS = [2310, 30030]; DRAWS, SEED = 400, 2550; FINE = 128 def block_bool(lo, hi, qs): n = hi - lo; ok = np.ones(n, dtype=bool); ok[(-lo) % 2:: 2] = False for q in qs: for kill in (0, (q - 2) % q): ok[(kill - lo) % q:: q] = False return ok def trunc_defect(H, x): ps = bg.primes_to(x); hs = np.arange(1, H); r = np.ones(H - 1) for p in ps: p = int(p); nu2 = 1 if p == 2 else 2 res = np.stack([np.zeros(H - 1, dtype=np.int64), np.full(H - 1, 2 % p), hs % p, (hs + 2) % p]) nu = np.array([len(set(col)) for col in res.T]); r *= (1 - nu / p) / (1 - nu2 / p) ** 2 ratio = np.where(hs % 2 == 0, r, 0.0) return 1 - 2 * float(((H - hs) * ratio).sum()) / (H * H) def main(): ap = argparse.ArgumentParser(); ap.add_argument("--out", required=True); ap.add_argument("--levels", default="19,23,29"); ap.add_argument("--draws", type=int, default=DRAWS) a = ap.parse_args(); log = sys.stderr; levels = [int(v) for v in a.levels.split(",")]; ledger = [] def check(tag, ok, detail): ledger.append(f"{'PASS' if ok else 'FAIL'} {tag} :: {detail}") primes_all = [int(p) for p in bg.primes_to(10 ** 6)] full = {} for H in HS: s, _ = vr.singular_sum(H, primes_all); full[H] = 1 - s / (H * H * (2 * C2) ** 2) out = {"instrument": "rcond2550.py", "job": 2550, "preregistered": "see header", "full_defect": {str(H): full[H] for H in HS}, "levels": {}, "ledger": ledger} rng = np.random.default_rng(SEED) for x in levels: t0 = time.time(); qs = [int(q) for q in bg.primes_to(x) if q > 2]; M = 2 for q in qs: M *= q P = PERIODS[x]; hi = (P + 1) * M sp = bg.primes_to(int(math.isqrt(hi)) + 1); tpts = bg.twins_in(sp, M, hi, log=log) per_k = {k: int(((tpts >= k * M) & (tpts < (k + 1) * M)).sum()) for k in range(1, P + 1)} print(f"x={x}: twins {sum(per_k.values())} {time.time() - t0:.1f}s", file=log, flush=True) lv = {"M": int(M), "periods": P, "twins_per_k": {str(k): per_k[k] for k in per_k}, "windows": {}} # tile slot counts per aligned window of each H, streamed over the period in FINE blocks (window boundaries lie on # block boundaries when the block length is a multiple of both H; use block length = lcm-friendly: 30030 * c) BL = 30030 * 64 # 1,921,920: a multiple of both window lengths nb = (M + BL - 1) // BL A = {H: [] for H in HS}; slots_total = 0 for b in range(nb): lo = b * BL; hi_b = min(M, lo + BL); ok = block_bool(lo, hi_b, qs); slots_total += int(ok.sum()) pre = np.concatenate([[0], np.cumsum(ok, dtype=np.int64)]) for H in HS: nw = (hi_b - lo) // H # full windows inside this block (the last partial block drops its tail) if nw: A[H].append(pre[H * np.arange(1, nw + 1)] - pre[H * np.arange(nw)]) check(f"G1_slots_x{x}", slots_total == D_T[x], f"streamed {slots_total} vs D(T_{x}) {D_T[x]}") for H in HS: A_H = np.concatenate(A[H]).astype(float); nwin = len(A_H) # windows per period, aligned at multiples of H starts = (np.arange(nwin) * H).astype(np.int64) # twins per window per period N = np.zeros((P, nwin)); lam = np.zeros(P) for k in range(1, P + 1): i0 = np.searchsorted(tpts, k * M + starts); i1 = np.searchsorted(tpts, k * M + starts + H); N[k - 1] = i1 - i0 lam[k - 1] = per_k[k] / D_T[x] check(f"G2_x{x}_H{H}", int(A_H.sum()) <= D_T[x] and all(int(N[k].sum()) <= per_k[k + 1] for k in range(P)), f"sum A {int(A_H.sum())} <= D; sum N per period <= twins") EA = float(A_H.mean()); lam_bar = float(lam.mean()) def rcond(Nmat): num = sum(((Nmat[k] - lam[k] * A_H) ** 2).sum() for k in range(P)); den = sum((lam[k] * A_H).sum() for k in range(P)) return float(num / den) R = rcond(N) # bootstrap over windows (resample window indices, all periods together) boots = [] for _ in range(a.draws): j = rng.integers(0, nwin, nwin) num = sum(((N[k][j] - lam[k] * A_H[j]) ** 2).sum() for k in range(P)); den = sum((lam[k] * A_H[j]).sum() for k in range(P)); boots.append(float(num / den)) boots = np.array(boots) # matched control: independent thinning of the tile at lambda_k ctrl = [] for _ in range(a.draws): Nt = np.stack([rng.binomial(A_H.astype(np.int64), lam[k]) for k in range(P)]).astype(float); ctrl.append(rcond(Nt)) ctrl = np.array(ctrl) trunc = trunc_defect(H, x); offset = lam_bar * EA * (full[H] - trunc); pred = (1 - lam_bar) - offset # rho^(<=x) - rho_H = (1 - trunc) - (1 - full) = full - trunc check(f"G3_control_x{x}_H{H}", abs(ctrl.mean() - (1 - lam_bar)) <= 3 * ctrl.std() / math.sqrt(a.draws) + 3 * ctrl.std(), f"control mean {ctrl.mean():.5f} vs 1-lambda {1 - lam_bar:.5f}") cell = {"windows_per_period": int(nwin), "E_A": EA, "lambda_bar": lam_bar, "lambda_k": lam.tolist(), "R_cond": R, "boot_sd": float(boots.std()), "boot_q05": float(np.quantile(boots, 0.05)), "boot_q95": float(np.quantile(boots, 0.95)), "control_mean": float(ctrl.mean()), "control_sd": float(ctrl.std()), "control_q01": float(np.quantile(ctrl, 0.01)), "control_q99": float(np.quantile(ctrl, 0.99)), "rho_trunc_defect": trunc, "rho_full_defect": full[H], "predicted_R_cond": pred, "predicted_offset_from_control": offset, "z_vs_prediction": (R - pred) / float(boots.std()), "below_control_q01": bool(R < np.quantile(ctrl, 0.01)), "R_uncond_for_reference": float(sum(((N[k] - lam[k] * EA) ** 2).sum() for k in range(P)) / sum((lam[k] * EA * nwin) for k in range(P)))} lv["windows"][str(H)] = cell print(f"x={x} H={H}: n={nwin} E[A]={EA:.3f} lam={lam_bar:.5f} R_cond={R:.5f} sd={boots.std():.5f} | control {ctrl.mean():.5f} (q01 {np.quantile(ctrl, 0.01):.5f}) | predicted {pred:.5f} (offset {offset:.5f}) z={(R - pred) / boots.std():+.2f} {time.time() - t0:.1f}s", file=log, flush=True) out["levels"][str(x)] = lv; json.dump(out, open(a.out, "w"), indent=1) v = {} x29 = out["levels"].get("29", {}).get("windows", {}) v["F1_effect_present"] = all(x29[str(H)]["below_control_q01"] for H in HS) if x29 else None v["F1_sign_at_19_23"] = all(out["levels"][str(x)]["windows"][str(H)]["R_cond"] < out["levels"][str(x)]["windows"][str(H)]["control_mean"] for x in levels if x in (19, 23) for H in HS) v["F2_within_2sigma_all_cells"] = all(abs(out["levels"][str(x)]["windows"][str(H)]["z_vs_prediction"]) <= 2 for x in levels for H in HS) out["verdict"] = v; json.dump(out, open(a.out, "w"), indent=1) print("\n".join(ledger)); print("VERDICT " + json.dumps(v, sort_keys=True)); return 0 if __name__ == "__main__": sys.exit(main())