#!/usr/bin/env python3 """job2461-cluster.py -- matched-permutation null for the kill-graph's qualifying-run clustering on the T_x gap word (job #2461, run run_20260919_081234_1sLl5w). QUESTION The served census (docs/research/a3-08-adjacent-pairs.js, sections [5]/[5b]/[6]) reports the kill-graph component spectrum at T_x folded by p; N-2456-01 and N-2459-01 report the longest qualifying-gap run R at T23 and T29. Nothing on record gives a NULL for those counts, so the record cannot decide whether the large components come from an arithmetic clustering of qualifying gaps or are what ANY arrangement of the same gap multiset at the same qualifying density produces. This script supplies that null. STATISTIC S_k(x,p) = number of maximal CYCLIC runs of length >= k in the qualifying mask q_i = [ g_i mod p in {0, 2, p-2} ] of the T_x gap word. S_2 counts the non-trivial kill-graph components (a qualifying run of length >= 2 is exactly the condition for L >= 2); S_3/S_4 count the spectrum tail the census prints. MATCHED CONTROL A uniform random permutation of the gap word preserves its length and its gap MULTISET, and qualifying depends only on the gap VALUE, so a permutation preserves the number K of qualifying gaps EXACTLY and makes the qualifying mask a uniform random K-subset of the D positions. Permutations therefore destroy only the ARRANGEMENT and match length, gap multiset and qualifying count. Sanity controls: the identity permutation reproduces the observed S_k, and D, G2 must equal the published T23 values (7 952 175, 204). PRE-REGISTERED FALSIFIER (written before any run) H_cluster: the arithmetic word clusters qualifying gaps, so the observed S_2 at T23 exceeds a central 95% band of the permutation null. FALSIFIER: if the observed S_2 lies inside [q_2.5, q_97.5] of B = 500 permutations, H_cluster is REFUTED -- the spectrum tail is what the density alone produces. SCALE T23 (D = 7 952 175 slots); the effect, if present, moves S_2 by a factor ~ D*d^2 vs the clustered value, i.e. many sigma -- visible with B = 500. """ import sys, time import numpy as np PRIMES = [3, 5, 7, 11, 13, 17, 19, 23, 29] CHUNK = 1 << 24 B = 500 def gap_word(ps): """Exact T_x gap word over one period W = 2*prod(ps), cyclic (wrap gap included).""" W = 2 for q in ps: W *= q expect_D = 1 for q in ps: expect_D *= (q - 2) gaps = []; first_pos = None; prev_last = None; D = 0; G = 0 for start in range(0, W, CHUNK): end = min(start + CHUNK, W); n = end - start ok = np.ones(n, dtype=bool) ok[0::2] = False for q in ps: f = (0 - start) % q if f < n: ok[f::q] = False f = ((q - 2) - start) % q if f < n: ok[f::q] = False pos = start + np.flatnonzero(ok) if pos.size == 0: continue if first_pos is None: first_pos = int(pos[0]) if prev_last is None: g = np.diff(pos) else: g = np.empty(pos.size, dtype=np.int64) g[0] = pos[0] - prev_last g[1:] = np.diff(pos) prev_last = int(pos[-1]); D += g.size gaps.append(g) G = max(G, int(g.max())) wrap = first_pos + W - prev_last gaps.append(np.array([wrap], dtype=np.int64)) G = max(G, wrap); D += 1 word = np.concatenate(gaps) assert word.size == D == expect_D, (word.size, D, expect_D) return word, W, G def runs_ge(q): """Maximal cyclic run lengths of a boolean mask, and the counts >= 2, >= 3, >= 4.""" n = q.size pad = np.empty(n + 2, dtype=bool) pad[0] = False; pad[-1] = False; pad[1:-1] = q d = np.diff(pad.astype(np.int8)) starts = np.flatnonzero(d == 1) ends = np.flatnonzero(d == -1) lengths = (ends - starts).astype(np.int64) if n and bool(q[0]) and bool(q[-1]) and lengths.size > 1: lengths[0] += lengths[-1] lengths = lengths[:-1] return lengths def mask_of(word, p): m = word % p return (m == 0) | (m == 2) | (m == (p - 2)) def main(): ps = [q for q in PRIMES if q <= 23] t0 = time.time() word, W, G2 = gap_word(ps) D = word.size print("T23 word: W=%d D=%d G2=%d (published D=7952175 G2=204)" % (W, D, G2)) print("build=%.2fs" % (time.time() - t0), file=sys.stderr) assert D == 7952175 and G2 == 204, (D, G2) sys.stdout.flush() rng = np.random.default_rng(2461) for p in (29, 31, 37): q = mask_of(word, p) K = int(q.sum()) obs = runs_ge(q) s2 = int((obs >= 2).sum()); s3 = int((obs >= 3).sum()); s4 = int((obs >= 4).sum()) # identity-permutation control: recompute through the same path ident = runs_ge(q.copy()) assert int((ident >= 2).sum()) == s2 d = K / D null = [] t1 = time.time() for b in range(B): idx = rng.choice(D, K, replace=False) m = np.zeros(D, dtype=bool); m[idx] = True null.append(int((runs_ge(m) >= 2).sum())) null = np.array(null, dtype=np.int64) lo, hi = np.percentile(null, [2.5, 97.5]) pv = (1 + int((null >= s2).sum())) / (B + 1) print("fold p=%-3d K=%-8d d=%.6f S2_obs=%-7d S3_obs=%-4d S4_obs=%-3d | null mean=%.1f sd=%.1f " "q2.5=%.0f q97.5=%.0f p_upper=%.4f %s" % (p, K, d, s2, s3, s4, null.mean(), null.std(), lo, hi, pv, "OUTSIDE (clustered)" if s2 > hi or s2 < lo else "INSIDE (refuted)")) print(" p=%d perms=%.1fs" % (p, time.time() - t1), file=sys.stderr) sys.stdout.flush() if __name__ == "__main__": main()