#!/usr/bin/env python3 """Job #2493 (explore/discovery, lane formalize, routeless) — a NEW finite statistic with a pre-registered falsifier. QUESTION. At a fold prime p the census machine (served source `docs/research/a3-08-adjacent-pairs.js`, read at source by return #1188 / job #2492) walks the old gap word and labels every gap Z (g = 0 mod p), P (g = +2 mod p), M (g = -2 mod p) or X (none of the three). It closes a component at every label change between P and M and at every X. The retained censuses (L, node/edge counts, the run spectrum) cannot decide WHY the largest component is smaller than the largest X-free block: #1188's C3 measured L(T23,29) = 2 with block support R + 1 = 3, and #1188's C4 refuted the natural repair "L = 1 + longest ALTERNATING span" (formula 3 vs machine 2 at p = 29). So the missing decision is: Is the P/M label sequence inside X-free blocks *memoryless* (i.i.d. given the marginals), or does it carry a correlation long enough to move L? STATISTIC S(p) (countable, finite, one pass over the word): Z-free adjacency is a pair of CONSECUTIVE gaps both non-Z (they are then inside one block). n = #{adjacent non-Z pairs} Y = #{such pairs whose two labels are EQUAL (PP or MM)} Same-label pairs are exactly where a chain must restart, so Y is the coarse carrier of the non-isolation L <= R + 1 - 1 = R that #1188 observed. null value under H0 (i.i.d. labels with the observed pipeline marginals pp, pm): m0 = n * (pp^2 + pm^2), sigma0 = sqrt(n * m0/n * (1 - m0/n)) PRE-REGISTERED FALSIFIER (written before any run, 2026-09-19T08:36Z, this file committed first): H0. Inside blocks the non-Z labels are i.i.d. Bernoulli with the block marginals. FALSIFIER: reject H0 at prime p if |Y - m0| > 4 * sigma0 (a 4-sigma excess/deficit of same-label adjacencies). Rejecting H0 means a memoryless residue model CANNOT produce the observed block structure, so a genuine run statistic (not a marginal correction) is required to predict L; NOT rejecting means the observed defect is fluctuation-sized, the alternating formula failed for a reason the marginals already carry, and no cheap i.i.d. repair exists. CONTROL (matched, repo style): label permutation — permute the non-Z labels among the non-Z positions (Z positions fixed), 500 draws, recompute Y; compare the observed |z| to the permutation null. A statistic whose observed value sits inside the permutation null is not evidence of structure however large its z is. SCALE. T23 (the retained word, D_twin = 7 952 175, built here in seconds) at the real folds p = 29, 31, 37; the effect is visible at T29 (D = 214 708 725) only through the segmented sieve of run_20260919_093308_2xAUdg/work/src2479/ (cost recorded in the report). Controls from published numbers: the served [8] column L(T23,p) at p = 29,31,37 is 2,3,2 (return #1188, C1) -- this script must reproduce them or the word is wrong. """ import json import sys import time import numpy as np ODD = [3, 5, 7, 11, 13, 17, 19, 23] CH = 1 << 24 SERVED_T23 = {29: 2, 31: 3, 37: 2} FALSIFY_SIGMA = 4.0 def t23_word(): """Twin-admissible residue set mod 2*23# (validated in job #2492 / job2479-t29gaps.py).""" P = 2 for q in ODD: P *= q parts = [] for start in range(0, P, CH): end = min(start + CH, P) ok = np.ones(end - start, dtype=bool) ok[(-start) % 2::2] = False for q in ODD: ok[(-start) % q::q] = False ok[(q - 2 - start) % q::q] = False parts.append(np.flatnonzero(ok).astype(np.int64) + start) return np.concatenate(parts), P def gap_word(res, P): g = np.diff(res) return np.append(g, res[0] + P - res[-1]) def machine(gaps, p): """Literal transcription of the served census (job #2492 / return #1188, C1).""" m = gaps % p q = (m == 0) | (m == 2) | (m == p - 2) if bool(q[0]) or not bool(q[-1]): j0 = int(np.flatnonzero(~q)[0]) gaps = np.roll(gaps, -j0) m = gaps % p runs = {} l0 = l1 = 1 edges = 0 for gi in gaps.tolist(): r = gi % p if r == 0: l0 += 1 l1 += 1 edges += 2 elif r == 2: runs[l0] = runs.get(l0, 0) + 1 l0 = l1 + 1 l1 = 1 edges += 1 elif r == p - 2: runs[l1] = runs.get(l1, 0) + 1 l1 = l0 + 1 l0 = 1 edges += 1 else: runs[l0] = runs.get(l0, 0) + 1 runs[l1] = runs.get(l1, 0) + 1 l0 = l1 = 1 nodes = sum(k * c for k, c in runs.items()) return {"L": int(max(runs)), "nodes": int(nodes), "edges": int(edges), "run_spectrum": {str(k): int(v) for k, v in sorted(runs.items())}} def statistic(gaps, p, n_perm=200, seed=2493): """S(p): n, Y, the i.i.d. null, the 4-sigma falsifier and the permutation control.""" m = gaps % p Z = m == 0 P_ = m == 2 M = m == p - 2 qual = Z | P_ | M # largest X-free block (gaps) = #159's R + 1 idx = np.flatnonzero(~qual) if idx.size: qual_rot = np.roll(qual, -int(idx[0])) else: qual_rot = qual best = cur = 0 for v in qual_rot.tolist(): cur = cur + 1 if v else 0 best = max(best, cur) # adjacent non-Z pairs (consecutive in the word; cyclic, so wrap the last->first) nz = P_ | M # P/M labelled gaps only both = nz[:-1] & nz[1:] # consecutive P/M pairs, non-wrapping n = int(both.sum()) lab = np.where(P_, 1, np.where(M, -1, 0)).astype(np.int64) # +1 = P, -1 = M, 0 = Z/X same = both & (lab[:-1] == lab[1:]) & (lab[:-1] != 0) Y = int(same.sum()) nP = int(P_.sum()) nM = int(M.sum()) pp = nP / (nP + nM) pm = nM / (nP + nM) psame = pp * pp + pm * pm m0 = n * psame sigma0 = float(np.sqrt(n * psame * (1.0 - psame))) if 0 < psame < 1 else 0.0 z = (Y - m0) / sigma0 if sigma0 > 0 else 0.0 # matched control: permute the non-Z labels among the non-Z positions. Y depends only on # the labels at non-Z positions, so compress to their rank vector and permute that. rng = np.random.default_rng(seed) pos = np.flatnonzero(nz) nz_lab = lab[pos].copy() rank = np.empty(lab.size, dtype=np.int64) rank[pos] = np.arange(pos.size) idx = np.flatnonzero(both) a = rank[idx] b = rank[idx + 1] perm_Y = np.empty(n_perm, dtype=np.int64) for k in range(n_perm): rng.shuffle(nz_lab) perm_Y[k] = int((nz_lab[a] == nz_lab[b]).sum()) return {"p": int(p), "R_gap_run": int(best), "n_adjacent_PM": n, "Y_same_label": Y, "nP": nP, "nM": nM, "p_same_iid": float(psame), "null_mean": float(m0), "null_sigma": sigma0, "z": float(z), "falsifier_rej": bool(abs(z) > FALSIFY_SIGMA), "perm_mean": float(perm_Y.mean()), "perm_sd": float(perm_Y.std()), "perm_p_two_sided": float((np.abs(perm_Y - perm_Y.mean()) >= abs(Y - perm_Y.mean())).mean())} def main(): t0 = time.time() res, P = t23_word() gaps = gap_word(res, P) checks = [] out = {"word": {"period": int(P), "residues": int(res.size), "gaps": int(gaps.size), "gap_sum": int(gaps.sum())}, "stats": {}, "checks": checks} # A1 the word is the retained T23 word: cyclic identity and the served census reproduction checks.append({"id": "A1_gap_sum_is_period", "pass": int(gaps.sum()) == P, "detail": f"sum(gaps)={int(gaps.sum())} period={P}"}) for p in (29, 31, 37): mach = machine(gaps, p) out["stats"][str(p)] = statistic(gaps, p) out[f"machine_{p}"] = mach checks.append({"id": f"A2_served_L_T23_{p}", "pass": mach["L"] == SERVED_T23[p], "detail": f"machine L={mach['L']} served={SERVED_T23[p]}"}) # A3 the statistic agrees with a slow (loop) count on the smallest fold p = 29 m = gaps % p Z = m == 0 lab = np.where(m == 2, 1, np.where(m == p - 2, -1, 0)) nzq = (m == 2) | (m == p - 2) slow_n = slow_Y = 0 n_g = gaps.size for i in range(n_g - 1): j = i + 1 if nzq[i] and nzq[j]: slow_n += 1 if lab[i] == lab[j] != 0: slow_Y += 1 st = out["stats"][str(p)] checks.append({"id": "A3_vectorised_equals_loop", "pass": slow_n == st["n_adjacent_PM"] and slow_Y == st["Y_same_label"], "detail": f"loop n={slow_n} Y={slow_Y} vs vector n={st['n_adjacent_PM']} Y={st['Y_same_label']}"}) # A6 the block statistic reproduces #1188's C3 (R = 2 at the three real folds) checks.append({"id": "A6_R_equals_1188_C3", "pass": all(out["stats"][str(q)]["R_gap_run"] == 2 for q in (29, 31, 37)), "detail": "R = " + ",".join(str(out["stats"][str(q)]["R_gap_run"]) for q in (29, 31, 37)) + " (return #1188 C3: R = 2)"}) # A4 the permutation control is not degenerate ok4 = all(out["stats"][str(q)]["perm_sd"] > 0 for q in (29, 31, 37)) checks.append({"id": "A4_permutation_control_live", "pass": bool(ok4), "detail": "all three permutation sd > 0" if ok4 else "degenerate control"}) # A5 the pre-registered falsifier verdict is reported for every fold checks.append({"id": "A5_falsifier_reported", "pass": all("falsifier_rej" in out["stats"][str(q)] for q in (29, 31, 37)), "detail": "4-sigma H0 verdict present at p=29,31,37"}) out["all_pass"] = all(c["pass"] for c in checks) out["wall_s"] = round(time.time() - t0, 3) print(json.dumps(out, indent=1, sort_keys=True)) return 0 if out["all_pass"] else 1 if __name__ == "__main__": sys.exit(main())