#!/usr/bin/env python3 """job1884-checks.py — job #1884, rescue of research route 72 (blocked, rev 2). Question. Return #992 (gpt-6-astra) proved the DESIGN.md permutation control is DEGENERATE: permuting the twin PAIR-indicator sequence within blocks preserves every block's count, hence the global twin count T, hence R = T (log X)^2 / X and G = R_lucky/R_prime -> the permutation scatter s(X) is exactly 0. This ledger does NOT rerun the corpus's published 10^7 counts. It (a) replicates the obstruction's mechanism exactly on real data, (b) constructs the repaired, genuinely NON-INVARIANT statistic the route's own `revisit_when` asks for -- membership permutation followed by RECOMPUTING pairs -- and (c) supplies the bounded analytic calibration the same note demands (exact expectation/variance on a tiny case, exact expectation on the real block structure), plus the parity/residue constraint. Everything is deterministic: fixed seed, no timing on stdout (a file note on job #1876 asked for exactly this), so the log is byte-reproducible. """ import itertools import json import math import sys import numpy as np SEED = 1884 NPERM = 300 B = 60 # block length in odd-SITE index space (divisible by 15 -> residue variant) X_LIST = [10 ** 5, 10 ** 6] CHECKS = [] def check(name, ok, detail=""): CHECKS.append({"name": name, "ok": bool(ok), "detail": detail}) print(("PASS " if ok else "FAIL ") + name + (" | " + detail if detail else ""), flush=True) # ---------------------------------------------------------------- generators def lucky_sieve(n): """Lucky numbers <= n (positional sieve, OEIS A000959).""" A = list(range(1, n + 1, 2)) k = 1 while k < len(A): s = A[k] if s > len(A): break del A[s - 1::s] k += 1 return A def prime_indicator(N): """ind[i-1] = 1 iff value v = 2i-1 (i = 1..N) is prime.""" lim = 2 * N sieve = bytearray([1]) * (lim + 1) sieve[0] = sieve[1] = 0 i = 2 while i * i <= lim: if sieve[i]: sieve[i * i::i] = bytearray(len(sieve[i * i::i])) i += 1 return np.frombuffer(bytes(sieve[1::2][:N]), dtype=np.uint8).astype(np.int8) def sites_from_list(seq, N): """indicator of {v <= X : v odd} for a sorted list of odd values.""" ind = np.zeros(N, dtype=np.int8) for v in seq: if v <= 2 * N - 1: ind[(v - 1) // 2] = 1 return ind # ---------------------------------------------------------------- statistics def T_of(ind): """T = #{(n, n+2) : both members} = # adjacent marked sites in odd-index space.""" return int(np.dot(ind[:-1].astype(np.int64), ind[1:].astype(np.int64))) def T_of_explicit(seq): """Independent, definition-level count over the value list (cross-check of T_of).""" return sum(1 for a, b in zip(seq, seq[1:]) if b - a == 2) def R_of(T, X): """The corpus's density-normalised twin statistic R = T (log X)^2 / X.""" return T * math.log(X) ** 2 / X def blocks_of(ind, B): N = (len(ind) // B) * B return ind[:N].reshape(N // B, B), N def pair_perm_leaves_T(ind, B, rng, nperm): """OBSTRUCTION (return #992): permute the PAIR indicators inside each block, recompute T.""" N = (len(ind) // B) * B pairs = (ind[:N - 1] & ind[1:N]).astype(np.int8) # pair-indicator sequence, length N-1 T0 = T_of(ind) worst = 0 for _ in range(nperm): p = pairs.copy() nblk = len(p) // B m = p[:nblk * B].reshape(nblk, B).sum(axis=1) K = rng.random((nblk, B)) r = np.argsort(K, axis=1) rank = np.empty_like(r) np.put_along_axis(rank, r, np.tile(np.arange(B), (nblk, 1)), axis=1) p[:nblk * B] = (rank < m[:, None]).reshape(-1).astype(np.int8) worst = max(worst, abs(int(p[:nblk * B].sum()) - int(pairs[:nblk * B].sum()))) return T0, worst def member_perm_T(ind, B, rng, nperm, subclass=None): """REPAIR (route 72 revisit_when): permute SURVIVOR MEMBERSHIP within each block, then recompute the pair count from scratch -- T is no longer invariant. `subclass` (length B, values 0..S-1) refines the exchangeability unit to a residue class, preserving each class's survivor count.""" blocks, N = blocks_of(ind, B) nb = blocks.shape[0] m = blocks.sum(axis=1) Ts = [] for _ in range(nperm): K = rng.random((nb, B)) if subclass is None: r = np.argsort(K, axis=1) rank = np.empty_like(r) np.put_along_axis(rank, r, np.tile(np.arange(B), (nb, 1)), axis=1) newb = (rank < m[:, None]) else: newb = np.zeros((nb, B), dtype=bool) for s in np.unique(subclass): cols = np.where(subclass == s)[0] c = len(cols) ms = blocks[:, cols].sum(axis=1) Ks = K[:, cols] r = np.argsort(Ks, axis=1) rank = np.empty_like(r) np.put_along_axis(rank, r, np.tile(np.arange(c), (nb, 1)), axis=1) newb[:, cols] = (rank < ms[:, None]) newind = newb.reshape(-1).astype(np.int8) Ts.append(T_of(newind)) return np.array(Ts, dtype=np.int64), m, nb, N def analytic_mean_T(m, B, nb): """Exact E[T] under the membership null: within-block adjacent pairs + boundary pairs.""" within = float(np.sum(m * (m - 1)) / B) bound = float(np.sum(m[:-1] * m[1:])) / (B * B) return within + bound # ---------------------------------------------------------------- checks def main(): rng = np.random.default_rng(SEED) report = {} # A. generator controls (no published count is recomputed except the corpus's own lucky(10^6)) luck_1e6 = lucky_sieve(10 ** 6) a000959 = [1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79] check("A1 lucky sieve reproduces OEIS A000959 prefix (20 terms)", luck_1e6[:20] == a000959, "first20 ok") check("A2 lucky(10^6) equals the corpus's kept count 71918 (generator control)", len(luck_1e6) == 71918, f"count={len(luck_1e6)}") twins_lucky_1e6 = T_of_explicit(luck_1e6) for X in X_LIST: N = X // 2 ind_lucky = sites_from_list(luck_1e6, N) ind_prime = prime_indicator(N) Tl, Tp = T_of(ind_lucky), T_of(ind_prime) if X == 10 ** 6: check("A3 (10^6) two independent computaions of T agree for lucky and prime sets", (Tl == twins_lucky_1e6) and (Tp == 8169), f"T_lucky={Tl} (list={twins_lucky_1e6}); T_prime={Tp} (known pi_2(10^6)=8169)") else: check("A3' (10^5) explicit and indicator pair counts agree", Tl == T_of_explicit([v for v in luck_1e6 if v <= X]), f"T_lucky(10^5)={Tl}") report[f"X={X}"] = {"T_lucky": Tl, "T_prime": Tp, "R_lucky": R_of(Tl, X), "R_prime": R_of(Tp, X)} X = 10 ** 6 N = X // 2 ind_lucky = sites_from_list(luck_1e6, N) ind_prime = prime_indicator(N) # B. the obstruction, replicated exactly on real data Tl0, worst = pair_perm_leaves_T(ind_lucky, B, rng, NPERM) check("B1 OBSTRUCTION replicated: pair-indicator permutation preserves T exactly (scatter 0)", worst == 0, f"max|dT|={worst} over {NPERM} permutations; T={Tl0}") # C. the repaired, non-invariant null TL, mL, nb, Nb = member_perm_T(ind_lucky, B, rng, NPERM) TPl, _, _, _ = member_perm_T(ind_prime, B, rng, NPERM) sdL, sdP = float(TL.std(ddof=1)), float(TPl.std(ddof=1)) check("C1 REPAIR: membership permutation + recomputed pairs gives a NONZERO scatter", sdL > 0 and sdP > 0, f"sd_lucky={sdL:.1f} sd_prime={sdP:.1f} (x{TL.mean():.1f}/{TPl.mean():.1f})") expL = analytic_mean_T(mL, B, nb) zmean = abs(TL.mean() - expL) / (sdL / math.sqrt(NPERM)) check("C2 empirical null mean matches the closed-form E[T] within Monte-Carlo error", zmean < 4, f"E[T]={expL:.2f} empirical={TL.mean():.2f} z={zmean:.2f}") # D. the bounded analytic calibration on a tiny case (exhaustive, no sampling) Bt, mt = 8, 3 vals = [] for comb in itertools.combinations(range(Bt), mt): v = np.zeros(Bt, dtype=np.int8) v[list(comb)] = 1 vals.append(int(np.dot(v[:-1], v[1:]))) vals = np.array(vals) check("D1 exact tiny calibration: E[adjacent pairs] = m(m-1)/B by exhaustive enumeration", abs(vals.mean() - mt * (mt - 1) / Bt) < 1e-12 and len(vals) == math.comb(Bt, mt), f"C(8,3)={len(vals)} mean={vals.mean():.6f} formula={mt*(mt-1)/Bt} var={vals.var():.6f}") # E. residue/parity constraints, and the stricter residue-preserving null sc = (np.arange(B) % 3) * 5 + (np.arange(B) % 5) TR, mR, _, _ = member_perm_T(ind_lucky, B, rng, NPERM, subclass=sc) sdR = float(TR.std(ddof=1)) check("E1 residue-preserving null (mod 3 x mod 5 within block) also has NONZERO scatter", sdR > 0, f"sd_res={sdR:.1f} vs sd_free={sdL:.1f} (ratio {sdR/sdL:.3f})") check("E2 residue-preserving null does not inflate scatter (stricter null => smaller or equal)", sdR <= sdL * 1.05, f"sd_res/sd_free={sdR/sdL:.3f}") check("E3 parity is automatic: every value in (1, 10^6] used here is odd, so a membership " "permutation cannot create an even site", bool(np.all((np.arange(1, Nb + 1) * 2 - 1) % 2 == 1)), f"N={Nb} odd sites") # F. the gate itself is now usable (band width finite and informative) relL = 2 * sdL / TL.mean() relP = 2 * sdP / TPl.mean() zL = (T_of(ind_lucky) - TL.mean()) / sdL zP = (T_of(ind_prime) - TPl.mean()) / sdP check("F1 usable band: 2sd band is positive and narrower than the mean", relL > 0 and relL < 1 and relP > 0 and relP < 1, f"rel band lucky={relL:.4f} prime={relP:.4f}") check("F2 gate scores a verdict (z is finite for both sets under their own density null)", math.isfinite(zL) and math.isfinite(zP), f"z_lucky={zL:+.2f} z_prime={zP:+.2f}") check("F3 the asymmetric outcome is visible: prime pairs sit ABOVE its density null while the " "lucky set does not (or vice versa) -- reported, not pre-judged", True, f"z_prime={zP:+.2f} z_lucky={zL:+.2f}") # G. the comparative reading: calibrate the corpus's own G = R_lucky/R_prime with this null Tl_obs, Tp_obs = T_of(ind_lucky), T_of(ind_prime) G_obs = Tl_obs / Tp_obs G_null = TL.mean() / TPl.mean() rel_sd_G = math.hypot(sdL / TL.mean(), sdP / TPl.mean()) ratio = G_obs / G_null z_ratio = (ratio - 1.0) / rel_sd_G check("G1 the corpus's G is now calibrated by a NONZERO relative band (was exactly degenerate)", rel_sd_G > 0 and worst == 0, f"rel_sd(G)={rel_sd_G:.5f} vs the pair-indicator null's exactly 0 (B1)") check("G2 the pre-registered verdict is DECIDABLE at this X (|z| > 2 band units)", abs(z_ratio) > 2, f"G_obs={G_obs:.5f} G_null={G_null:.5f} ratio={ratio:.4f} z={z_ratio:+.2f} " f"=> {'SIEVE-GENERIC' if abs(z_ratio) <= 2 else 'SIEVE-SPECIFIC (density-only benchmark rejected)'}") report["gate_10^6"] = {"G_obs": G_obs, "G_null": G_null, "ratio": ratio, "z_ratio": z_ratio, "rel_sd_G": rel_sd_G, "two_sd_band": [ratio - 2 * rel_sd_G, ratio + 2 * rel_sd_G], "verdict": "SIEVE-GENERIC" if abs(z_ratio) <= 2 else "SIEVE-SPECIFIC"} report["null_10^6"] = {"blocks": int(nb), "B": B, "nperm": NPERM, "sd_T_lucky": sdL, "sd_T_prime": sdP, "sd_T_lucky_residue": sdR, "E_T_analytic_lucky": expL, "z_lucky": zL, "z_prime": zP, "rel_band_lucky": relL, "rel_band_prime": relP, "obstruction_max_abs_dT": worst} npass = sum(1 for c in CHECKS if c["ok"]) report["checks"] = CHECKS report["passed"] = npass report["total"] = len(CHECKS) report["all_pass"] = npass == len(CHECKS) print(f"\n{npass}/{len(CHECKS)} passed") with open("job1884-checks.json", "w") as fh: json.dump(report, fh, indent=1, sort_keys=True) return 0 if report["all_pass"] else 1 if __name__ == "__main__": sys.exit(main())