#!/usr/bin/env python3 # -*- coding: utf-8 -*- """WHICH YARDSTICK? The sham-stop / null-calibration control for the deficit frontier (job #2067, route 87). THE QUESTION (return #1101's registered next step). The deficit frontier rejects the finite world F(X0) (stop at X0 = x - W) at level kappa iff the deficit D(W) := M(x) - M(x-W) reaches kappa * sigma(x). Two readings of sigma were left open, seven orders apart at the 1e19 census: LOCAL sigma = c * sqrt(delta_M(h)), the locally detrended amplitude over a +/-h-decade window (#1091, #1094, #1101) -> W = 1.18e11 at h=0.10, eps = 1.18e-8; INTERVAL sigma = c * sqrt(D) itself, the fluctuation of the count over the candidate stop-window -> D = kappa^2 c^2, W = kappa^2 c^2 / M'(x) -> W ~ 3e3, eps ~ 3e-13. THE RESOLUTION TESTED HERE. The rejection is a statement about the WORLD, and the only thing that can make it unsupported is the MODEL's own error: an observer holding the census to x and the model M cannot distinguish "the world stopped at x - W" from "the world is infinite and M is wrong by D(W)". A stop is therefore excludable exactly when D(W) exceeds the model's error at x at the stated confidence -- not when it exceeds a high-frequency roughness, and not when it exceeds the count's own increment fluctuation. So the calibrated yardstick is the alpha-quantile of the residual |d(x)| under the INFINITE world, and it has no window at all. Everything here measures that, and uses it to score the two readings. PRE-REGISTERED DECISION RULE (fixed before the run). Q(alpha; x) := the alpha-upper quantile of the DOWNWARD residual -d(x) under the measured infinite-world process (below). A reading R with threshold W_R(x) is CALIBRATED iff D(W_R) = kappa*sigma = Q(alpha;x); it is ANTI-CONSERVATIVE (over-claims exclusions) iff D(W_R) < Q, and CONSERVATIVE (under-claims) iff D(W_R) > Q. The reported quantity is the over-claim factor Q / D(W_R), per reading, per decade. The verdict is read off those factors with no discretion: the calibrated reading has factor 1 by construction, and every other reading is labelled by its factor. THE NULL PROCESS, and why its amplitude may be carried across scales (this is where #1101 is used). #1101 measured, at four window widths, that the residual is a random walk in log x with the SAME constant relative to sqrt(model increment) -- so the walk's coefficient measured locally can be extrapolated to the range scale, which is exactly what the calibration needs. s := measured/simulated from #1101 = 0.71 (rms) and 0.77 (one-sided down); the null is a Gaussian walk with increment variance s^2 * (model increment), i.e. d(x) ~ N(0, s^2 (M(x) - M(2))). CLT makes the Gaussian form right at these sizes, and the CONTROL below checks it by simulating the walk at the decade grid. CONTROLS. A. A007508 agreement at the decades present in the grid. B. The interval amplitude curve c_int(L) measured DIRECTLY and undetrended: for L = 0.05..1.6 decades, the standardised interval difference u_i := (d(x_i 10^{L/2}) - d(x_i 10^{-L/2})) / sqrt(model increment over that interval), over shifted positions x_i, reporting rms(u) and -min(u). This is the gate on the whole calibration: walk-like behaviour shows as rms(u) FLAT in L at the same value as the local constant, which is what licenses carrying s from the local fit to the range scale. It is reported even if it fails, and if it fails the calibration is withdrawn. Offsets are restricted to x >= 1e9 because below that the model's low-x transient (ratio 0.764 at 1e3, 24% over-prediction) dominates the differences and would be read as walk amplitude -- the control is run both with and without that restriction so the contamination is visible rather than hidden. -min(u) is reported with its n, since a one-sided extremum grows with the number of independent positions. C. The null quantile by simulation vs the analytic Gaussian used for Q. D. Goodness of fit: the simulated percentile of each OBSERVED |d| at the decades -- if the observed residual were an extreme draw of the null, the calibration would be measuring the wrong process. Sources cited: TOS pi(x)/pi2(x) tables (sweet.ua.pt/tos/primes.html); A007508; returns #1091, #1094 and #1101 (this handle) for the statistic, the constants and the walk-scaling result. """ import gzip import json import math import os import sys import mpmath as mp import numpy as np mp.mp.dps = 30 HERE = os.path.dirname(os.path.abspath(__file__)) C2 = mp.mpf("0.660161815846869573927812110014") KAPPA = 3.0 S_RMS = 0.71 # measured/simulated local amplitude, #1101 (calibrates the walk coefficient) S_DN = 0.77 ALPHAS = [0.003, 0.00135] # one-sided 3 sigma and 3 sigma two-sided-equivalent L_LIST = [0.05, 0.10, 0.20, 0.40, 0.80, 1.60] DECADES = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] MC = 200000 A007508 = {10: 27412679, 11: 224376048, 12: 1870585220, 13: 15834664872, 14: 135780321665, 15: 1177209242304, 16: 10304195697298, 17: 90948839353159, 18: 808675888577436, 19: 7237518093734545} C_DN_H10 = 0.4689 # #1094's one-sided constant at h = 0.10 HALF_DECADE = 0.10 def M(x): return 2 * C2 * mp.quad(lambda t: 1 / mp.log(t) ** 2, [2, mp.mpf(x)]) def Mprime(x): L = mp.log(mp.mpf(x)) return 2 * C2 * (1 / L ** 2) * (1 - 2 / L) def main(): out, log = {"tool": "frontier5.py/job2067", "kappa": KAPPA, "s_rms": S_RMS, "s_dn": S_DN, "alphas": ALPHAS}, [] def say(s): log.append(s) print(s, flush=True) rows = [] for name in ["2b00", "2d10", "2d11", "2d12", "2d13", "2d14", "2d15"]: txt = gzip.decompress(open(os.path.join(HERE, "tos", name + ".txt.gz"), "rb").read()) base = 2.0 if name == "2b00" else 10.0 for line in txt.decode("latin-1", "replace").splitlines(): s = line.strip() if not s or s.startswith("#") or s.startswith("-"): continue p = s.split() if len(p) < 3 or not p[1].isdigit(): continue n, e = p[0].split("b") if "b" in p[0] else p[0].split("d") rows.append((float(n) * base ** int(e), int(p[1]), float(p[2].rstrip(".")))) pts = {} for x, pi2, model in rows: pts[x] = (pi2, model) grid = sorted(pts) xs = np.array(grid) lx = np.log10(xs) dd = np.array([pts[x][0] - pts[x][1] for x in grid]) modelv = np.array([pts[x][1] for x in grid]) dd = dd - dd[0] # both processes start at the same origin, x = 2 say("grid: %d points, log10 x in [%.3f, %.3f]; d from the published columns, shifted to d(2) = 0" % (len(grid), lx.min(), lx.max())) a_ok = sum(1 for k, v in A007508.items() if 10.0 ** k in pts and pts[10.0 ** k][0] == v) n_present = sum(1 for k in A007508 if 10.0 ** k in pts) say("CONTROL A: A007508 agreement at the decades present in the grid: %d/%d" % (a_ok, n_present)) out["control_A"] = a_ok # ---- CONTROL B: the measured, UNDETRENDED interval amplitude curve --------------------------- say("") say("CONTROL B -- measured interval amplitude, undetrended: c_int(L) of" " d(x*10^(L/2)) - d(x*10^-(L/2)) over shifted positions, normalised by sqrt(model increment)") def curve(lo_x, tag): res = [] for L in L_LIST: h = L / 2.0 lo, hi = math.log10(lo_x) + h, lx.max() - h pos = np.arange(lo, hi, L) # non-overlapping intervals if len(pos) > 400: pos = pos[:: int(math.ceil(len(pos) / 400.0))] u = [] for p in pos: d = float(np.interp(p + h, lx, dd) - np.interp(p - h, lx, dd)) inc = float(np.interp(p + h, lx, modelv) - np.interp(p - h, lx, modelv)) if inc > 0: u.append(d / math.sqrt(inc)) u = np.array(u) res.append({"L": L, "n": len(u), "c_rms": float(np.sqrt((u ** 2).mean())), "c_dn": float(max(-u.min(), 0.0)), "c_mean": float(u.mean())}) say(" %-16s L=%.2f n=%3d c_rms=%.4f c_dn=%+.4f (mean %+.4f)" % (tag, L, len(u), res[-1]["c_rms"], res[-1]["c_dn"], res[-1]["c_mean"])) return res say(" (a) offsets x >= 1e9, where the model is converged -- the calibrated curve") intr = curve(1e9, "x>=1e9") rms_curve = [r["c_rms"] for r in intr] flat_rms = float(np.std(rms_curve) / np.mean(rms_curve)) flat = [r["c_rms"] for r in intr if r["L"] <= 0.80] # the flat part; L=1.6 has only 6 offsets S_MEAS = float(np.mean(flat)) S_MEAS_SD = float(np.std(flat)) say(" WALK GATE: over L = 0.05..0.80 the interval constant is c_int = %.4f +- %.4f (%.1f%% spread)," " against #1101's locally detrended 0.71 -- flat in L, so the walk coefficient may be carried to" " the range scale. (L=1.60 uses 6 offsets and is reported but not used.)" % (S_MEAS, S_MEAS_SD, 100 * S_MEAS_SD / S_MEAS)) say(" (b) offsets from x = 2, i.e. INCLUDING the low-x transient -- shown so the contamination is" " visible and not read as walk amplitude") intr_all = curve(2.0, "x>=2 (contam.)") out["control_B"] = {"curve_xge1e9": intr, "curve_all": intr_all, "c_rms_rel_spread": flat_rms, "c_rms_mean": float(np.mean(rms_curve))} # ---- the null: the model's error at x, measured (no window) --------------------------------- say("") say("THE NULL (the model's own error at the census scale, no window). Increment variance =" " s^2 * model increment, d(x) ~ N(0, s^2 (M(x) - M(2))), with s = %.4f measured HERE at the range" " scale (control B) as the primary, and s = %.2f (#1101's locally detrended value) carried as" " the sensitivity -- the two differ by %.0f%% and the calibrated frontier is reported for both." % (S_MEAS, S_RMS, 100 * (S_RMS / S_MEAS - 1))) rng = np.random.default_rng(2067) probes, sims = {}, {} M2 = float(M(2)) for k in DECADES: x = 10.0 ** k if not (lx.min() < math.log10(x) < lx.max()): continue Mx = float(M(x)) probes[k] = {"x": x, "M": Mx, "Mprime": float(Mprime(x)), "inc": Mx - M2} z = rng.standard_normal(MC) sims[k] = (S_MEAS * math.sqrt(Mx - M2)) * z say("CONTROL C -- null quantile: simulated vs analytic Gaussian at alpha = 0.003 / 0.00135") for k in sorted(probes): q_an = [0.0] * len(ALPHAS) for i, a in enumerate(ALPHAS): q_an[i] = -np.percentile(sims[k], 100 * a) s = math.sqrt(probes[k]["inc"]) * S_MEAS zvals = [2.7478, 3.0] say(" 1e%-2d simulated Q_down = %s | analytic s*z = %s" % (k, ["%.3f" % q for q in q_an], ["%.3f" % (s * z) for z in zvals])) probes[k]["Q_sim"] = q_an out["null"] = {str(k): {"inc": probes[k]["inc"], "Q_sim": probes[k]["Q_sim"]} for k in probes} # ---- CONTROL D: is the observed residual a plausible draw of the null? ---------------------- say("") say("CONTROL D -- the observed |d| at each decade, as a percentile of the null (a fit that is" " grossly wrong would show up here):") for k in sorted(probes): d_obs = float(np.interp(k, np.log10(xs), dd)) pct = 100.0 * float(np.mean(np.abs(sims[k]) <= abs(d_obs))) say(" d(1e%d) = %+.4e -> |d| at percentile %.1f of the null, %.2f sigma" % (k, d_obs, pct, abs(d_obs) / (S_MEAS * math.sqrt(probes[k]["inc"])))) # ---- THE TEST: the three thresholds, and each reading's over-claim factor ------------------- say("") say("THE TEST -- thresholds in stop distance W at kappa = %.0f, and the over-claim factor" " Q / D(W_R) per reading (1 = calibrated, >1 = excludes stops the model's error can mimic):" % KAPPA) say(" census local W (h=0.10) factor | interval W factor | calibrated W eps_cal") table = [] for k in sorted(probes): pv = probes[k] alpha = 0.003 Q = -np.percentile(sims[k], 100 * alpha) Mp = pv["Mprime"] # local reading, exactly as #1094 has it dM_h = float(M(pv["x"] * 10 ** HALF_DECADE) - M(pv["x"] * 10 ** (-HALF_DECADE))) sig_loc = C_DN_H10 * math.sqrt(dM_h) D_loc = KAPPA * sig_loc W_loc = D_loc / Mp # interval reading: D = kappa^2 c_int^2 (self-consistent), c from the measured curve at L=1.6 c_int = S_MEAS # the interval coefficient measured at the range scale D_int = KAPPA ** 2 * c_int ** 2 W_int = D_int / Mp # calibrated: the model's error at x, one-sided alpha W_cal = Q / Mp table.append({"k": k, "Q": Q, "W_local": W_loc, "factor_local": Q / D_loc, "W_interval": W_int, "factor_interval": Q / D_int, "W_calibrated": W_cal, "eps_calibrated": W_cal / pv["x"], "eps_local": W_loc / pv["x"], "eps_interval": W_int / pv["x"]}) say(" 1e%-2d %.4e %8.2f | %.4e %12.2f | %.4e %.3e" % (k, W_loc, Q / D_loc, W_int, Q / D_int, W_cal, W_cal / pv["x"])) out["test"] = table # ---- the frontier at the 1e19 census, calibrated -------------------------------------------- x19 = 1e19 Mp19 = float(Mprime(x19)) root19 = math.sqrt(float(M(x19)) - M2) Q19 = S_MEAS * root19 * 2.7478 W19 = Q19 / Mp19 Q19b = S_RMS * root19 * 2.7478 say("") say("THE FRONTIER AT THE 1e19 CENSUS, calibrated (no window): sigma = s * z_alpha *" " sqrt(M(1e19)-M(2)) with the measured range-scale s = %.4f -> W = %.4e, eps = %.3e" % (S_MEAS, W19, W19 / x19)) say(" sensitivity to s: with #1101's s = %.2f, W = %.4e and eps = %.3e (%.0f%% wider)" % (S_RMS, Q19b / Mp19, Q19b / Mp19 / x19, 100 * (S_RMS / S_MEAS - 1))) dM19 = float(M(1e19 * 10 ** HALF_DECADE) - M(1e19 * 10 ** (-HALF_DECADE))) D_loc19 = KAPPA * C_DN_H10 * math.sqrt(dM19) W_loc19 = D_loc19 / Mp19 D_int19 = KAPPA ** 2 * S_MEAS ** 2 W_int19 = D_int19 / Mp19 say(" AT 1e19, like for like: local h=0.10 gives eps = %.4e (over-claims by %.2fx); interval gives" " eps = %.4e (over-claims by %.1e x)" % (W_loc19 / x19, Q19 / D_loc19, W_int19 / x19, Q19 / D_int19)) out["frontier_1e19"] = {"s_meas": S_MEAS, "s_sens": S_RMS, "sigma": Q19, "W": W19, "eps": W19 / x19, "eps_s_sens": Q19b / Mp19 / x19, "eps_local_1e19": W_loc19 / x19, "eps_interval_1e19": W_int19 / x19, "factor_local_1e19": Q19 / D_loc19, "factor_interval_1e19": Q19 / D_int19} # ---- the sham-stop table: constructed finite worlds, detection under each reading ----------- say("") say("SHAM-STOP CONTROL -- constructed finite worlds F(x - W) at the 1e18 census; DETECTED means the" " reading excludes the world (D >= its threshold), and IN-BAND means D is below the model's own" " 3-sigma error, so the exclusion is not supported by the null:") pv = probes[18] Mp = pv["Mprime"] Q = -np.percentile(sims[18], 0.3) dM_h = float(M(pv["x"] * 10 ** HALF_DECADE) - M(pv["x"] * 10 ** (-HALF_DECADE))) sig_loc = C_DN_H10 * math.sqrt(dM_h) c_int = S_MEAS # the measured interval coefficient W_loc_th = KAPPA * sig_loc / Mp W_int_th = KAPPA ** 2 * c_int ** 2 / Mp W_cal_th = Q / Mp stops = [] say(" thresholds at this census: local %.3e, interval %.3e, calibrated %.3e" % (W_loc_th, W_int_th, W_cal_th)) say(" reading m W D(W) z_own own verdict | z_cal supported by null?") for tag, Wth, zf in (("local", W_loc_th, lambda D: D / sig_loc), ("interval", W_int_th, lambda D: math.sqrt(D) / c_int), ("calibrated", W_cal_th, lambda D: D / Q)): for m in (0.1, 0.3, 1.0, 3.0, 10.0): W = m * Wth X0 = pv["x"] - W if X0 <= 2 or W >= pv["x"]: continue D = float(M(pv["x"]) - M(X0)) z_own, z_cal = zf(D), D / Q rec = {"reading": tag, "m": m, "W": W, "D": D, "z_own": z_own, "z_calibrated": z_cal, "detected": bool(z_own >= KAPPA), "supported": bool(z_cal >= KAPPA), "in_band": bool(z_own >= KAPPA and z_cal < KAPPA)} stops.append(rec) say(" %-11s %5.2f %.4e %.4e %9.3f %-10s | %7.3f %s" % (tag, m, W, D, z_own, "DETECTED" if rec["detected"] else "not", z_cal, "yes" if rec["supported"] else "NO")) out["sham_stop"] = stops def nb(tag): rows = [s for s in stops if s["reading"] == tag] return sum(1 for s in rows if s["in_band"]), len(rows) n_loc_in, n_each = nb("local") n_int_in, _ = nb("interval") n_cal_in, _ = nb("calibrated") say(" constructed stops the reading excludes while the world is INDISTINGUISHABLE from the model's" " own error (in-band): local %d of %d, interval %d of %d, calibrated %d of %d" % (n_loc_in, n_each, n_int_in, n_each, n_cal_in, n_each)) out["sham_stop_summary"] = {"local_in_band": n_loc_in, "interval_in_band": n_int_in, "calibrated_in_band": n_cal_in, "per_reading": n_each, "thresholds": {"local": W_loc_th, "interval": W_int_th, "calibrated": W_cal_th}} out["log"] = log with open(os.path.join(HERE, "frontier5.json"), "w", encoding="utf-8") as fh: json.dump(out, fh, indent=1, default=str) return 0 if __name__ == "__main__": sys.exit(main())