#!/usr/bin/env python3 """Job #1464: rho(m, D) mapped exactly on synthetic pairs. Foreground, prints as it goes. rho(m, D) := E_null(W, D)/(m*lnD), the exact mean of the matched-density null (D distinct positions drawn uniformly from the W integers of the cycle) over the corpus's normaliser `pred = m*lnD` (`two-class-lower-bounds.md` section 6). Only (W, D) enters the null, so rho can be mapped on synthetic pairs `W = round(m*D)` and then EVALUATED at the corpus's own (W, D). Pre-registered: M1 rho is a function of m alone if its spread across D is < 1% at fixed m; M2 of D alone if the same holds across m at fixed D; M3 otherwise (the correction above x = 13 stays unmeasured). The routine is the one validated at 230/230 against exhaustive enumeration in `exact-null-normalizer.py`; the anchors are not recomputed here (they are in that script's output). """ import json import math import pathlib import sys import time HERE = pathlib.Path(__file__).resolve().parent T0 = time.time() GAMMA = 0.5772156649015329 WORK_BUDGET = 4.0e9 def log(m): sys.stderr.write("[%7.2fs] %s\n" % (time.time() - T0, m)) sys.stderr.flush() def exact_null_mean(W, D, eps_lo=1e-11, eps_hi=1e-13): if D <= 0 or D >= W: return float(W) m = W / D kmin = -(-W // D) k0, kc = kmin, W - D + 1 for k in range(kmin, kmin + 4 * int(m * (math.log(D) + GAMMA + 8.0)) + 8): p = math.exp(-D * math.exp(-k / m)) if p < eps_lo: k0 = k + 1 elif p > 1.0 - eps_hi: kc = k break terms = sum(min(D, (W - D) // k) + 1 for k in range(k0, kc)) log_cwd = math.lgamma(W + 1) - math.lgamma(D + 1) - math.lgamma(W - D + 1) if terms * log_cwd / math.log(2) > WORK_BUDGET: raise MemoryError("cost too high for (W=%d,D=%d)" % (W, D)) total = float(k0) for k in range(k0, kc): n = 0 for j in range(0, min(D, (W - D) // k) + 1): t = math.comb(D, j) * math.comb(W - j * k - 1, D - 1) n += t if (j % 2 == 0) else -t if n <= 0: raise ArithmeticError("nonpositive count W=%d D=%d k=%d" % (W, D, k)) total += 1.0 - math.exp(math.log(n) + math.log(W) - math.log(D) - log_cwd) return total MS = [3.0, 4.0, 5.0, 6.0, 8.0, 12.0, 17.0, 25.0, 40.0] def main(): rep = {"tool": "job1464/null-correction-map", "grid": [], "verdict": None, "prereg": {"M1": "rho(m) alone if <1% spread across D at fixed m", "M2": "rho(D) alone if <1% spread across m at fixed D", "M3": "neither"}} out = HERE / "null-correction-map.json" for D in (135, 480, 1485): for m in MS: W = int(round(m * D)) try: e = exact_null_mean(W, D) except (MemoryError, ArithmeticError) as exc: log(" (W=%d D=%d) skipped: %s" % (W, D, exc)) continue mm = W / D rec = {"W": W, "D": D, "m": round(mm, 4), "E": round(e, 6), "rho_lnD": round(e / (mm * math.log(D)), 6)} rep["grid"].append(rec) log(" D=%-6d m=%-7.3f W=%-8d rho_lnD=%.4f" % (D, mm, W, rec["rho_lnD"])) out.write_text(json.dumps(rep, indent=1, sort_keys=True), encoding="utf-8", newline="\n") by_m, by_d = {}, {} for r in rep["grid"]: by_m.setdefault(r["m"], []).append(r["rho_lnD"]) by_d.setdefault(r["D"], []).append(r["rho_lnD"]) s_m = {str(k): round(100 * (max(v) - min(v)) / (sum(v) / len(v)), 3) for k, v in by_m.items() if len(v) > 1} s_d = {str(k): round(100 * (max(v) - min(v)) / (sum(v) / len(v)), 3) for k, v in by_d.items() if len(v) > 1} m1 = bool(s_m) and all(v < 1.0 for v in s_m.values()) m2 = bool(s_d) and all(v < 1.0 for v in s_d.values()) rep["spread_across_D_at_fixed_m_pct"] = s_m rep["spread_across_m_at_fixed_D_pct"] = s_d rep["verdict"] = {"M1_rho_is_function_of_m_alone": m1, "M2_rho_is_function_of_D_alone": m2, "M3_both_needed": (not m1) and (not m2)} out.write_text(json.dumps(rep, indent=1, sort_keys=True), encoding="utf-8", newline="\n") log("wrote null-correction-map.json") print(json.dumps({"spread_D_at_fixed_m": s_m, "spread_m_at_fixed_D": s_d, "verdict": rep["verdict"]}, indent=1, sort_keys=True)) return 0 if __name__ == "__main__": sys.exit(main())