#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Job #1403 — the lag-h disagreement rate of the singleton-fibre sign field, against a matched Möbius-randomized control. QUESTION (frozen in artifacts/prereg-1403.md before this was run) ------------------------------------------------------------------------------ Is the fibre sign field's cancellation LOCAL? For dyadic x, with U = V = floor(x^(6/25)), Y = Z = floor(x^(1/20)) and the grouped coefficient c(n) = C_{U,V}(n) . C_{Y,Z}(n-2), C_{A,B}(m) = sum_{d|m, d>A} mu(d) beta_B(m/d), beta_B(t) = sum_{p^j || t, p^j > B} log p, S = {n in (x/2, x] : c(n) != 0}, form the lag-h disagreement rate T_h(x) = #{n : n, n+h in S, sign(c(n)) sign(c(n+h)) < 0} / #{n : n, n+h in S} for h = 2, 4, 6, and compare T_h against a MATCHED control that re-randomizes the Möbius INPUT (an independent mu~(d) = +-1 for every squarefree d; cutoffs, beta weights, support and interval geometry held exactly fixed). The statistic is z_h = (T_h - mean_null) / sd_null. Pre-registered falsifier: H1 is falsified at scale x if |z_2(x)| <= 3.0; z_2 <= -3.0 is a distinct outcome (sign persistence), not support. Instrument gates, run before any measurement (they validate the code, not the claim): G1 every prime power multiplies back to m (factorization); G2 the EXACT support restriction of the served audit: c(n) = 0 whenever n or n-2 is prime; G3 an independent code path for beta_B (brute force over all divisors with Lambda from a sieve, no shared helper) agrees on small m; G4 the control's own null: the identity assignment mu~ = mu reproduces c(n) exactly. Usage: python fibre-sign-lag.py --x 16384 --draws 200 [--out out.json] [--selftest] """ import argparse import hashlib import json import math import os import sys import time import numpy as np SEED = 1403 def primes_upto(n): sieve = bytearray([1]) * (n + 1) sieve[0:2] = b"\x00\x00" for p in range(2, int(n ** 0.5) + 1): if sieve[p]: sieve[p * p:: p] = bytearray(len(sieve[p * p:: p])) return [i for i in range(2, n + 1) if sieve[i]] def factor(m, primes): """[(p, e)] with prod p**e == m, by trial division over primes <= sqrt(m).""" out, t = [], m for p in primes: if p * p > t: break if t % p == 0: e = 0 while t % p == 0: t //= p e += 1 out.append((p, e)) if t > 1: out.append((t, 1)) return out def beta_of(fact, B): """beta_B(t) = sum_{p^j || t, p^j > B} log p, from the factorization [(p, e)] of t.""" s = 0.0 for p, e in fact: pk = p for _ in range(e): if pk > B: s += math.log(p) pk *= p return s def coeff_terms(fact, A, B): """The (mu(d), beta_B(m/d), d) terms of C_{A,B}(m) = sum_{d|m, d>A} mu(d) beta_B(m/d).""" terms, k = [], len(fact) for mask in range(1 << k): d = 1 for i in range(k): if mask >> i & 1: d *= fact[i][0] if d <= A: continue rest = [(p, e - (1 if mask >> i & 1 else 0)) for i, (p, e) in enumerate(fact)] w = beta_of([(p, e) for p, e in rest if e > 0], B) if w: terms.append(((1 if bin(mask).count("1") % 2 == 0 else -1), w, d)) return terms def is_prime(n, primes): if n < 2: return False for p in primes: if p * p > n: return True if n % p == 0: return False return True def mu_array(nmax): """mu(d) for d <= nmax by a linear sieve; mu(1) = 1.""" mu = np.zeros(nmax + 1, dtype=np.int8) mu[1] = 1 spf = np.zeros(nmax + 1, dtype=np.int64) primes = [] for i in range(2, nmax + 1): if spf[i] == 0: spf[i] = i primes.append(i) mu[i] = -1 for p in primes: if p > spf[i] or i * p > nmax: break spf[i * p] = p mu[i * p] = 0 if p == spf[i] else -mu[i] return mu def lambda_table(nmax): """Lambda(k) for k <= nmax, by a sieve over the prime powers themselves (independent of beta_of). Lambda(p^j) = log p and Lambda(k) = 0 otherwise; adding log p to every MULTIPLE of p^j instead would build the additive function log k, which is what the first version of this gate did, and the gate then disagreed with beta_of on every m with more than one prime factor.""" lam = [0.0] * (nmax + 1) marked = bytearray(nmax + 1) for p in range(2, nmax + 1): if not marked[p]: for k in range(p * p, nmax + 1, p): marked[k] = 1 pk = p while pk <= nmax: lam[pk] += math.log(p) pk *= p return lam def build(x): """Cutoffs, the factorization of every m in [x/2-2, x], and the flat coefficient term arrays.""" U = V = int(math.floor(x ** (6 / 25))) Y = Z = int(math.floor(x ** (1 / 20))) lo, hi = x // 2 - 2, x primes = primes_upto(int(math.isqrt(hi)) + 2) facts = {m: factor(m, primes) for m in range(lo, hi + 1)} idx = {m: i for i, m in enumerate(range(lo, hi + 1))} spec = {"L": (U, V), "R": (Y, Z)} # left coefficient of n, right coefficient of n-2 flat = {} for wname, (A, B) in spec.items(): mi, di, ww, sg = [], [], [], [] for m in range(lo, hi + 1): for sgn, w, d in coeff_terms(facts[m], A, B): mi.append(idx[m]); di.append(d); ww.append(w); sg.append(sgn) flat[wname] = (np.array(mi), np.array(di), np.array(ww, dtype=float), np.array(sg, dtype=float)) return {"x": x, "U": U, "V": V, "Y": Y, "Z": Z, "lo": lo, "hi": hi, "n_range": len(idx), "idx": idx, "facts": facts, "primes": primes, "flat": flat, "spec": spec, "ns": np.arange(x // 2 + 1, x + 1), "li": np.array([idx[n] for n in range(x // 2 + 1, x + 1)]), "ri": np.array([idx[n - 2] for n in range(x // 2 + 1, x + 1)])} def coeff_from(b, wname, eps): """C_{A,B}(m) over the built range with mu(d) replaced by eps[d].""" mi, di, ww, mu = b["flat"][wname] return np.bincount(mi, weights=eps[di] * ww, minlength=b["n_range"]) def c_field(b, CL, CR): return CL[b["li"]] * CR[b["ri"]] def pairs_of(ns, support, h): """Index pairs (i, j) with ns[i] in S, ns[i]+h in S, ns[j] = ns[i]+h.""" where = {int(n): i for i, n in enumerate(ns)} ii, jj = [], [] for i, n in enumerate(ns): n = int(n) if n in support and (n + h) in support: ii.append(i); jj.append(where[n + h]) return np.array(ii), np.array(jj) def rate(sgn, ii, jj): return float(np.mean(sgn[ii] * sgn[jj] < 0)) if len(ii) else float("nan") def measure(x, draws): t0 = time.time() b = build(x) mu = mu_array(b["hi"]).astype(float) CL, CR = coeff_from(b, "L", mu), coeff_from(b, "R", mu) c = c_field(b, CL, CR) ns = b["ns"] support = set(int(n) for n, v in zip(ns, c) if v != 0) sgn = np.sign(c) gates = {} gates["G1_factorization_ok"] = all(math.prod(p ** e for p, e in f) == m for m, f in b["facts"].items()) gates["G1_m_checked"] = len(b["facts"]) viol = [int(n) for n, v in zip(ns, c) if v != 0 and (is_prime(int(n), b["primes"]) or is_prime(int(n) - 2, b["primes"]))] gates["G2_prime_member_zero_ok"] = not viol gates["G2_violations"] = viol[:5] gates["G2_support_size"] = len(support) gates["G2_interval_size"] = int(len(ns)) gates["G2_support_density"] = len(support) / len(ns) # G3: independent beta, brute force over ALL divisors with Lambda from a sieve. lam = lambda_table(320) g3bad, g3n = [], 0 for m in range(200, 301): fm = factor(m, b["primes"]) for (A, B) in ((b["U"], b["V"]), (b["Y"], b["Z"])): tot = 0.0 for d in range(1, m + 1): if m % d or d <= A or mu[d] == 0: continue t = m // d s = 0.0 for r in range(B + 1, t + 1): # the restricted Lambda sum: r | t, r > B if t % r == 0: s += lam[r] tot += float(mu[d]) * s via = sum(sg * w for sg, w, _d in coeff_terms(fm, A, B)) g3n += 1 if abs(tot - via) > 1e-9: g3bad.append([m, A, tot, via]) gates["G3_independent_beta_ok"] = not g3bad gates["G3_cases_checked"] = g3n gates["G3_violations"] = g3bad[:3] CLm, CRm = coeff_from(b, "L", mu), coeff_from(b, "R", mu) gates["G4_control_null_reproduces_true_ok"] = bool(np.allclose(c_field(b, CLm, CRm), c)) pair_idx, observed = {}, {} for h in (2, 4, 6): ii, jj = pairs_of(ns, support, h) pair_idx[h] = (ii, jj) observed[str(h)] = {"N": int(len(ii)), "T": rate(sgn, ii, jj)} rng = np.random.default_rng(SEED) sqfree = np.abs(mu) == 1 s_mask = np.isin(ns, list(support)) # the support is fixed; only the signs are randomized null = {h: [] for h in (2, 4, 6)} for _ in range(draws): eps = (rng.integers(0, 2, size=b["hi"] + 1) * 2 - 1).astype(float) eps[np.logical_not(sqfree)] = 0.0 # a draw is a sign per SQUAREFREE d eps[1] = 1.0 cd = c_field(b, coeff_from(b, "L", eps), coeff_from(b, "R", eps)) sd = np.sign(np.where(s_mask, cd, 0.0)) for h in (2, 4, 6): null[h].append(rate(sd, pair_idx[h][0], pair_idx[h][1])) out = {"x": x, "cutoffs": {"U": b["U"], "V": b["V"], "Y": b["Y"], "Z": b["Z"]}, "support": {"size": len(support), "interval": int(len(ns)), "density": len(support) / len(ns)}, "observed": observed, "control": {"kind": "mu-randomized (an independent sign per squarefree d)", "draws": draws, "seed": SEED}, "null": {}, "z": {}, "gates": gates, "runtime_s": round(time.time() - t0, 2)} for h in (2, 4, 6): v = np.array(null[h], dtype=float) v = v[np.isfinite(v)] mu_n = float(v.mean()) if len(v) else None sd_n = float(v.std(ddof=1)) if len(v) > 1 else None out["null"][str(h)] = {"n_draws": int(len(v)), "mean": mu_n, "sd": sd_n, "p_two_sided": (float(np.mean(np.abs(v - mu_n) >= abs(observed[str(h)]["T"] - mu_n))) if len(v) else None)} out["z"][str(h)] = None if not sd_n else (observed[str(h)]["T"] - mu_n) / sd_n return out def selftest(): r = measure(2 ** 10, draws=20) keys = ("G1_factorization_ok", "G2_prime_member_zero_ok", "G3_independent_beta_ok", "G4_control_null_reproduces_true_ok") ok = all(bool(r["gates"][k]) for k in keys) print(json.dumps({"selftest": "fibre-sign-lag instrument", "ok": ok, "gates": r["gates"], "support": r["support"], "observed": r["observed"]}, indent=1)) return 0 if ok else 1 def main(): ap = argparse.ArgumentParser() ap.add_argument("--x", type=int) ap.add_argument("--draws", type=int, default=200) ap.add_argument("--out", default=None) ap.add_argument("--selftest", action="store_true") a = ap.parse_args() if a.selftest: return selftest() r = measure(a.x, a.draws) r["producer"] = {"file": os.path.basename(__file__), "sha256": hashlib.sha256(open(__file__, "rb").read()).hexdigest()} txt = json.dumps(r, indent=1, sort_keys=True) if a.out: with open(a.out, "w", encoding="utf-8", newline="\n") as fh: fh.write(txt + "\n") print(txt) return 0 if __name__ == "__main__": sys.exit(main())