#!/usr/bin/env python3 """job1631-checks.py -- independent re-derivation of return #839's statistic. Subject: run_20260917_110729_ZPWnCg (job #1629 -> return #839), `research/notes/N-1629-01-companion-removed-census-statistic.md`. Source artifact: `.solveathome/twin-primes/repo/research/centered-discrepancy-measurement.json` (retained columns only; no network, no re-measurement, 0 CPU-h). Statistic re-derived (as stated in the note): R(j) = D_y(2^j)/2^j + (T1(2^j)/2^j - C2) ctrl_rms(j) = rms over the 4 seeded random-sign draws of D_seed/2^j rho(j) = |R(j)| / ctrl_rms(j), raw(j) = |D_y|/2^j / ctrl_rms(j) Pass = the note's printed values reproduce from the artifact. """ import json import math import os import sys HERE = os.path.dirname(os.path.abspath(__file__)) ART = os.path.join(HERE, "..", "..", "..", "..", "..", "twin-primes", "repo", "research", "centered-discrepancy-measurement.json") ART = os.path.normpath(ART) fails = [] results = [] def check(name, ok, detail): results.append((name, ok, detail)) if not ok: fails.append(name) d = json.load(open(ART)) C2 = d["config"]["C2"] rows = sorted([r for r in d["rows"] if r["shift"] == 2], key=lambda r: r["j"]) check("artifact loaded, shift-2 rows present", len([r for r in rows if r["j"] <= 38]) >= 20, "rows=%d j=%s" % (len(rows), [r["j"] for r in rows])) # --- 1. the retained identity, uncontrolled --------------------------------- worst_id = 0.0 for r in rows: x = r["x"] lhs = r["D"] / x rhs = ((r["S"] / x - C2) - (r["T1"] / x - C2) - r["P"] / x - r["Epp"] / x - r["Eeven"] / x) worst_id = max(worst_id, abs(lhs - rhs)) check("printed identity D_y/x = (S/x-C2) - (T1/x-C2) - P/x - Epp/x - Eeven/x", worst_id < 1e-10, "max |residual| = %.3g" % worst_id) # --- 2. companion-removed residual and the two normalisations --------------- main = [] for r in rows: if r["j"] < 30: continue x = r["x"] R = r["D"] / x + (r["T1"] / x - C2) seeds = r["seeds"] ctrl_rms = math.sqrt(sum((s["D"] / x) ** 2 for s in seeds) / len(seeds)) rho = abs(R) / ctrl_rms raw = abs(r["D"] / x) / ctrl_rms # The note prints this as 3*ctrl_rms(j)/x, but its ctrl_rms is already the rms # of the normalised draws D_seed/x, so the printed floor is simply 3*ctrl_rms # in D/x units. Recorded as a normalisation reading, not a discrepancy. floor = 3.0 * ctrl_rms main.append({"j": r["j"], "x": x, "R": R, "ctrl_rms": ctrl_rms, "rho": rho, "raw": raw, "floor": floor, "nseed": len(seeds)}) check("four seeded draws on every j >= 30 analysed", all(m["nseed"] == 4 for m in main), "nseed set = %s" % sorted({m["nseed"] for m in main})) rho_max = max(main, key=lambda m: m["rho"]) raw_max = max(main, key=lambda m: m["raw"]) check("max rho(j) over j >= 30 is 1.0514 at j = 35 (note's value)", abs(rho_max["rho"] - 1.0514) < 5e-4 and rho_max["j"] == 35, "rho_max = %.4f at j=%d" % (rho_max["rho"], rho_max["j"])) check("rho(j) <= 1.0514 for every j >= 30 (pre-registered falsifier rho > 3 does not fire)", all(m["rho"] <= 1.0514 + 5e-4 for m in main), "max rho = %.4f" % rho_max["rho"]) check("raw |D_y|/ctrl_rms reaches 21.816 at j = 38 (note's value)", abs(raw_max["raw"] - 21.816) < 5e-3 and raw_max["j"] == 38, "raw_max = %.3f at j=%d" % (raw_max["raw"], raw_max["j"])) at38 = [m for m in main if m["j"] == 38][0] check("factor between raw and detrended AT THE SAME j = 38 is about 469", abs((at38["raw"] / at38["rho"]) - 469) < 5, "raw(38)/rho(38) = %.1f" % (at38["raw"] / at38["rho"])) # --- 3. fitted slopes over j >= 30 ------------------------------------------ def slope(ys, xs): n = len(xs) mx = sum(xs) / n my = sum(ys) / n return sum((xs[i] - mx) * (ys[i] - my) for i in range(n)) / sum((xs[i] - mx) ** 2 for i in range(n)) # The note prints lam(R) = 0.5318 and lam(ctrl) = 0.4448 as positive decay rates. # Fitting ln(value) against j gives the same numbers NEGATED (a decay exponent); # we report the decay exponent d = -d ln(value) / d ln(x) so the comparison is # convention-free and the note's printed sign is recorded as a reading. LN2 = math.log(2.0) xs = [m["j"] for m in main] lam_R = -slope([math.log(abs(m["R"])) for m in main], xs) / LN2 lam_ctrl = -slope([math.log(m["ctrl_rms"]) for m in main], xs) / LN2 raw_vals = [abs(r["D"]) / r["x"] for r in rows if r["j"] >= 30] lam_raw = -slope([math.log(v) for v in raw_vals], xs) / LN2 check("decay exponent of R over j >= 30 is 0.5318 (note's printed lam(R))", abs(lam_R - 0.5318) < 5e-4, "d ln|R| / d ln x = %.4f" % lam_R) check("decay exponent of the control rms over j >= 30 is 0.4448 (note's printed lam(ctrl))", abs(lam_ctrl - 0.4448) < 5e-4, "d ln ctrl / d ln x = %.4f" % lam_ctrl) check("raw |D_y|/x is flat over j >= 30 (|decay| < 0.01; note printed -0.0014 as a j>=30 fit)", abs(lam_raw) < 0.01, "d ln(|D_y|/x) / d ln x = %.4f" % lam_raw) # --- 4. the floors and the 1/788 figure ------------------------------------- check("floor 3*ctrl_rms/x = 2.03e-4 at j = 38 (note's value)", abs(at38["floor"] - 2.03e-4) / 2.03e-4 < 0.02, "floor(38) = %.3e" % at38["floor"]) check("floor(38) is 1/788 of the 4/25 tolerance of (16)", abs((4.0 / 25.0) / at38["floor"] - 788) < 8, "ratio = %.1f" % ((4.0 / 25.0) / at38["floor"])) # --- 5. the sqrt-rounding sanity on the two printed 'reference' rows --------- check("the same floor at j = 34 and j = 37 is 3.27e-4 and 1.42e-4 (note's values)", abs([m for m in main if m["j"] == 34][0]["floor"] - 3.27e-4) < 5e-6 and abs([m for m in main if m["j"] == 37][0]["floor"] - 1.42e-4) < 5e-6, "floors 34/37 = %.3e / %.3e" % ([m for m in main if m["j"] == 34][0]["floor"], [m for m in main if m["j"] == 37][0]["floor"])) # --- 6. NEW (job #1631): the printed floor is NOT monotone in j ---------------- ordered = sorted(main, key=lambda m: m["j"]) steps = [(ordered[i]["j"], ordered[i]["floor"] / ordered[i + 1]["floor"]) for i in range(len(ordered) - 1)] rises = [s for s in steps if s[1] < 1.0] check("NEW: the per-dyadic-step floor ratio falls below 1 at 5 of 8 steps 30..38", len(rises) == 5, "rises at j=%s; ratios=%s" % ([s[0] for s in rises], ["%.2f" % s[1] for s in steps])) avg = (ordered[0]["floor"] / ordered[-1]["floor"]) ** (1.0 / (len(ordered) - 1)) check("NEW: '~x1.5 per dyadic step' is not a stepwise rate: the j>=30 endpoint average is only x1.315", abs(avg - 1.315) < 0.005, "endpoint average over j=30..38 = x%.3f" % avg) check("NEW: single-step ratios span x0.64 (a rise) to x3.75, so the floor is not a per-step contraction", min(s[1] for s in steps) < 0.70 and max(s[1] for s in steps) > 3.70, "min = x%.2f at j=%d, max = x%.2f at j=%d" % (min(s[1] for s in steps), [s[0] for s in steps if s[1] == min(t[1] for t in steps)][0], max(s[1] for s in steps), [s[0] for s in steps if s[1] == max(t[1] for t in steps)][0])) check("counts retained: countActive/countSupport present on every shift-2 row", all(isinstance(r.get("countActive"), int) and isinstance(r.get("countSupport"), int) for r in rows), "ok") width = max(len(n) for n, _, _ in results) for name, ok, detail in results: print("%-4s %-*s %s" % ("PASS" if ok else "FAIL", width, name, detail)) print("") print("rho table (j, x, R, ctrl_rms, rho, raw):") for m in main: print(" j=%-3d x=%-14d R=%-14.6g ctrl_rms=%-14.6g rho=%-8.4f raw=%-8.3f floor=%.3e" % (m["j"], m["x"], m["R"], m["ctrl_rms"], m["rho"], m["raw"], m["floor"])) print("") print("%d/%d passed" % (len(results) - len(fails), len(results))) sys.exit(1 if fails else 0)