#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Job #1403 — POST-HOC diagnostic of the fibre lag-h statistic. EXPLORATORY, NOT pre-registered. `prereg-1403.md` fixed the decision and the falsifier before any run and is NOT retuned here. This file answers a question the pilot raised and the pre-registration did not anticipate: the pre-registered control re-randomizes the Möbius INPUT, so it does not hold the observed field's MARGINAL sign balance fixed. A field whose marginals are skewed can show a disagreement rate below/above 1/2 with no local structure at all. Two bounded diagnostics, each a few seconds: D1 MARGINAL-MATCHED CONTROL. A uniformly random permutation of the observed sign multiset across the support. This destroys all local structure and preserves the marginals EXACTLY, so z^perm measures local order beyond what the marginals alone imply. The benchmark a field with no local structure must hit is 2 q (1-q), q = P(sign = +1). D2 SMALL-PRIME STRATA. Persistence at lag h can be a multiplicative RESONANCE and not a new phenomenon: if a prime p divides h then n and n+h have the SAME residue mod p, so they share divisibility by p and their divisor coefficients are structurally alike. For lag 6 that prime is 3 (6 = 2*3) and also 2; for lag 4 it is 2; for lag 2 NO odd prime divides 2 and both members are odd, so lag 2 has no such resonance. Reporting T_h inside each stratum n mod p separates the two explanations. The mu-randomized control has no mod-p structure at all, so its per-stratum rates are the resonance-free reference. Usage: python fibre-lag-diagnostic.py --x 65536 --draws 200 [--out out.json] """ import argparse import hashlib import importlib.util import json import os import sys import time import numpy as np HERE = os.path.dirname(os.path.abspath(__file__)) PRODUCER = os.path.join(HERE, "fibre-sign-lag.py") SEED = 1403 def load_producer(path): spec = importlib.util.spec_from_file_location("fibre_producer", path) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) return mod def stratified(sgn, ns, ii, jj, mod): """{(residue, which_member): (N, T)} for residue classes of n mod `mod`. A pair (n, n+h) is assigned by the residue of its FIRST member; for a resonance prime the second member has the same residue, which is the point of the stratum.""" out = {} res = np.array([int(n) % mod for n in ns]) for r in range(mod): sel = res[ii] == r n = int(sel.sum()) out[str(r)] = {"N": n, "T": (float(np.mean(sgn[ii[sel]] * sgn[jj[sel]] < 0)) if n else None)} return out def run(x, draws): m = load_producer(PRODUCER) t0 = time.time() b = m.build(x) hi = b["hi"] mu = m.mu_array(hi).astype(float) c = m.c_field(b, m.coeff_from(b, "L", mu), m.coeff_from(b, "R", mu)) ns = b["ns"] sgn = np.sign(c) support_bool = c != 0 sup_idx = np.flatnonzero(support_bool) q = float(np.mean(sgn[sup_idx] > 0)) bench = 2.0 * q * (1.0 - q) rng = np.random.default_rng(SEED) res = {"x": x, "support": {"size": int(sup_idx.size), "interval": int(len(ns))}, "marginals": {"P_plus": q, "P_minus": 1.0 - q, "no_local_structure_disagreement_rate": bench}, "control_kind": "marginal-matched permutation of the observed sign multiset", "seed": SEED, "draws": draws, "lag": {}, "gates": {}} # instrument gates for THIS code path res["gates"]["signs_are_minus_one_zero_plus_one"] = bool(np.all(np.isin(sgn, (-1.0, 0.0, 1.0)))) res["gates"]["permutation_preserves_the_sign_multiset"] = bool( np.array_equal(np.sort(sgn[sup_idx]), np.sort(sgn[rng.permutation(sup_idx)]))) res["gates"]["both_signs_present"] = bool(0.0 < q < 1.0) for h in (2, 4, 6): ii, jj = m.pairs_of(ns, set(int(n) for n in ns[support_bool]), h) obs = float(np.mean(sgn[ii] * sgn[jj] < 0)) if len(ii) else float("nan") vals = [] for _ in range(draws): perm = rng.permutation(sup_idx) sd = np.zeros_like(sgn) sd[perm] = sgn[sup_idx] vals.append(float(np.mean(sd[ii] * sd[jj] < 0))) v = np.array(vals, dtype=float) mu_n, sd_n = float(v.mean()), float(v.std(ddof=1)) res["lag"][str(h)] = { "N": int(len(ii)), "T_observed": obs, "perm_mean": mu_n, "perm_sd": sd_n, "z_perm": (obs - mu_n) / sd_n if sd_n else None, "perm_two_sided_p": float(np.mean(np.abs(v - mu_n) >= abs(obs - mu_n))), } # D2: strata by the primes that can resonate with the lag for h, mods in ((2, (3, 5)), (4, (3, 5)), (6, (3, 5))): ii, jj = m.pairs_of(ns, set(int(n) for n in ns[support_bool]), h) strata = {} for mod in mods: strata["mod_%d" % mod] = stratified(sgn, ns, ii, jj, mod) # resonance-free reference: the same strata under the mu-randomized (pre-registered) # control, which carries no mod-p structure by construction eps = (rng.integers(0, 2, size=hi + 1) * 2 - 1).astype(float) eps[np.abs(mu) != 1] = 0.0 eps[1] = 1.0 cd = m.c_field(b, m.coeff_from(b, "L", eps), m.coeff_from(b, "R", eps)) sdr = np.sign(np.where(support_bool, cd, 0.0)) ref = {} for mod in mods: ref["mod_%d" % mod] = stratified(sdr, ns, ii, jj, mod) res.setdefault("strata", {})[str(h)] = {"observed": strata, "mu_randomized_reference": ref} res["runtime_s"] = round(time.time() - t0, 2) res["producer"] = {"file": os.path.basename(__file__), "sha256": hashlib.sha256(open(__file__, "rb").read()).hexdigest()} res["note"] = ("post-hoc and exploratory: reported ALONGSIDE the pre-registered verdict, " "never in place of it; the pre-registered decision is unchanged") return res def main(): ap = argparse.ArgumentParser() ap.add_argument("--x", type=int, required=True) ap.add_argument("--draws", type=int, default=200) ap.add_argument("--out", default=None) a = ap.parse_args() r = run(a.x, a.draws) 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())