#!/usr/bin/env python3 """Job #4213 (first look, route 166): pricing the closed-form drift. Pre-registration: artifacts/job4213/PREREG-4213.md (written before this run). Falsifiers F1-F4, gates G1-G3 as stated there. Published inputs (served with route 166, read read-only from work/job4213/files/): shortfall2554.json #1324: the six cells (x, H, E[A], lambda_bar, R_cond, boot_sd, offset, shortfall) rcond2550.json #1322: M per level, periods, twins per period assess.json #1859: the closed-form column (for gate G1 only) assess.py #1859: the source of the two transcribed cited corrections (243 measured, 358 analytic) rcond2550.py #1322: the statistic's definition (cited, not rerun) Nothing published is recomputed; no sieve runs. All new quantities are arithmetic on the above. """ import json import math import os import sys import numpy as np HERE = os.path.dirname(os.path.abspath(__file__)) FILES = os.path.abspath(os.path.join(HERE, "..", "..", "work", "job4213", "files")) sys.stdout.reconfigure(encoding="utf-8", errors="replace") # --------------------------------------------------------------------------------------------- # cited corrections, transcribed from assess.py (#1859); review 243's R_drift column likewise CITED_DRIFT = { # (x, H): review 243 = review 358 as transcribed by #1859 (19, 2310): 0.0015, (19, 30030): 0.0192, (23, 2310): 0.0022, (23, 30030): 0.0281, (29, 2310): 0.0011, (29, 30030): 0.0149, } CITED_R_DRIFT = { # review 243's R_drift values (trend densities in the NULL) (19, 2310): 0.81642, (19, 30030): 0.77622, (23, 2310): 0.85771, (23, 30030): 0.78095, (29, 2310): 0.89153, (29, 30030): 0.84338, } ctor = 1.0 # "+2" in ln(n+2): kept, it is a 1e-6 effect at these n PRINT = [] def say(s=""): PRINT.append(s) print(s, flush=True) def var_rel_closed(M, P): """Route 166's closed form: uniform-in-log average of w = 1/(ln n ln(n+2)) over a period.""" out = [] for k in range(1, P + 1): L0, L1 = math.log(k * M), math.log((k + 1) * M) out.append((L1 - L0) ** 2 / (3.0 * L0 * L1)) return out def lam_of(n): return 1.0 / (math.log(n) * math.log(n + 2)) def var_rel_discrete(M, H, P): """Exact discrete average over the actual aligned window centres, relative to their own mean.""" nwin = M // H out = [] for k in range(1, P + 1): cs = k * M + (np.arange(nwin, dtype=np.float64) + 0.5) * H lam = 1.0 / (np.log(cs) * np.log(cs + 2.0)) lb = float(lam.mean()) out.append(float(((lam - lb) ** 2).mean()) / lb ** 2) return out def var_rel_uniform_n(M, P, nodes=200001): """Uniform in n (density e^L dL) instead of uniform in log, continuous within the period.""" out = [] for k in range(1, P + 1): L0, L1 = math.log(k * M), math.log((k + 1) * M) L = np.linspace(L0, L1, nodes) w = 1.0 / (L * L) e = np.exp(L) # the measure: uniform in n Ew = float((e * w).sum() / e.sum()) Ew2 = float((e * w * w).sum() / e.sum()) out.append(Ew2 / Ew ** 2 - 1.0) return out def main(): short = json.load(open(os.path.join(FILES, "shortfall2554.json"), encoding="utf-8")) rcond = json.load(open(os.path.join(FILES, "rcond2550.json"), encoding="utf-8")) assess = json.load(open(os.path.join(FILES, "assess.json"), encoding="utf-8")) cf_pub = {(c["x"], c["H"]): c for c in assess["cells"]} out = {"instrument": "audit4213.py", "job": 4213, "route": 166, "preregistered": "artifacts/job4213/PREREG-4213.md", "inputs": {"shortfall2554.json": "served (sha recorded by the fetcher)", "rcond2550.json": "served", "assess.json": "served (#1859)"}, "gates": {}, "cells": [], "falsifiers": {}, "planned_cell": {}} ledger = [] def gate(tag, ok, detail): ledger.append("%s %s :: %s" % ("PASS" if ok else "FAIL", tag, detail)) out["gates"][tag] = {"pass": bool(ok), "detail": detail} # ---------------------------------------------------------------- per-cell table worst_g1 = 0.0 for row in short: x, H = row["x"], row["H"] L = rcond["levels"][str(x)] M, P = L["M"], L["periods"] nwin, nw = row["windows_per_period"], row["windows_per_period"] * L["periods"] EA, lam = row["E_A"], row["lambda_bar"] R, sd, off = row["R_cond"], row["boot_sd"], row["predicted_offset"] vr = var_rel_closed(M, P) vr_mean = sum(vr) / len(vr) cf = lam * EA * vr_mean g1 = cf_pub[(x, H)] worst_g1 = max(worst_g1, abs(cf - g1["drift_closed_form"]) / max(cf, 1e-30)) # measured drift: the two published columns' difference (243's R_drift is published) D_meas = R - CITED_R_DRIFT[(x, H)] # NEW: realisation-noise closed form for a measured drift sigD = 2.0 * math.sqrt(lam * EA * vr_mean / nw) zD = (D_meas - cf) / sigD # cited-column comparisons cited = CITED_DRIFT[(x, H)] # bootstrap-width cross-check (G3): predicted sd ratio pred_ratio = math.sqrt(2.0 * lam * EA * vr_mean) / R obs_ratio = sigD / sd # measure variants dis = var_rel_discrete(M, H, P) unn = var_rel_uniform_n(M, P) dis_mean, unn_mean = sum(dis) / len(dis), sum(unn) / len(unn) cf_meas = cf * (unn_mean / vr_mean) # apply the measure factor, not the variance itself # structural bound on the A_i-spread correction (the route's other ignored input): # drift_exact = lambda_bar * (sum A_i^2 delta_i^2)/sum A_i = lambda_bar E[A] * E_w[(A_i/E[A]) delta_i^2], # so the correction factor is 1 + Cov_w(A/E[A], delta^2)/E[delta^2]; |Cov| <= sd(A/E[A]) * sd(delta^2), # and sd_w(delta^2)/E_w[delta^2] = 0.8945 for the log-ramp shape (u uniform on [0, ln 2]). a_bound = 0.8945 / math.sqrt(EA) # internal consistency (G2) D_from_EA = EA * nwin twins = sum(L["twins_per_k"].values()) D_from_lam = (twins / P) / lam # lambda_bar = twins per period / D(tile) ok_g2 = abs(D_from_EA - D_from_lam) / D_from_lam < 5e-4 # twins per period and the precision law twins_per = twins / P sum_vr = sum(vr) prec = 2.0 / math.sqrt(twins_per * sum_vr) out["cells"].append(dict( x=x, H=H, M=M, periods=P, windows_per_period=nwin, n_windows=nw, E_A=EA, lambda_bar=lam, R_cond=R, boot_sd=sd, offset=off, var_rel_mean=vr_mean, drift_cf=cf, drift_cf_published=g1["drift_closed_form"], var_rel_discrete_mean=dis_mean, var_rel_uniform_n_mean=unn_mean, measure_disc_ratio=dis_mean / vr_mean, measure_unifn_ratio=unn_mean / vr_mean, drift_cited_243_358=cited, drift_measured=R - CITED_R_DRIFT[(x, H)], cf_over_cited=cf / cited, cf_over_measured=cf / D_meas, sigma_D=sigD, sigma_D_over_drift=sigD / cf, z_measured_minus_cf=zD, drift_cf_measure_corrected=cf_meas, drift_exact_available=float(lam * EA), z_measured_minus_cf_measure_corrected=(D_meas - cf_meas) / sigD, A_spread_correction_bound=a_bound, bootstrap_ratio_predicted=pred_ratio, bootstrap_ratio_observed=obs_ratio, D_from_EA=D_from_EA, D_from_lambda=D_from_lam, twins_total=twins, twins_per_period=twins_per, D_tile_published=dict([(19, 378675), (23, 7952175), (29, 214708725)])[x], precision_ratio=prec, g2_ok=bool(ok_g2))) gate("G2_x%d_H%d" % (x, H), ok_g2, "E[A]*nwin %.1f vs twins/lambda %.1f" % (D_from_EA, D_from_lam)) gate("G1_closed_form_reproduces_1859", worst_g1 < 1e-12, "max rel dev %.2e over 6 cells" % worst_g1) # ---------------------------------------------------------------- G3 dev = max(abs(c["bootstrap_ratio_observed"] / c["bootstrap_ratio_predicted"] - 1.0) for c in out["cells"]) gate("G3_sigmaD_vs_bootstrap_scaling", dev < 0.10, "max rel dev %.3f between sigma_D/boot_sd and sqrt(2 lam E[A] Var_rel)/R" % dev) # ---------------------------------------------------------------- F1..F4 f1 = [(c["x"], c["H"], c["measure_disc_ratio"], c["measure_unifn_ratio"]) for c in out["cells"] if abs(c["measure_disc_ratio"] - 1) >= 0.05 or abs(c["measure_unifn_ratio"] - 1) >= 0.05] f2 = [] for x in (19, 23, 29): zs = [(c["z_measured_minus_cf"], abs(c["drift_measured"] - c["drift_cf"]), c["sigma_D"]) for c in out["cells"] if c["x"] == x] f2.append(dict(x=x, gaps=[z for z, _, _ in zs], sigma_D=[s for _, _, s in zs], inside_2sigma=all(abs(z) <= 2 for z, _, _ in zs))) hratios = {} for x in (19, 23, 29): a = [c for c in out["cells"] if c["x"] == x] s, b = [c for c in a if c["H"] == 2310][0], [c for c in a if c["H"] == 30030][0] hratios[str(x)] = dict(E_A_ratio=b["E_A"] / s["E_A"], measured_drift_ratio=b["drift_measured"] / s["drift_measured"], closed_form_ratio=b["drift_cf"] / s["drift_cf"]) f3 = [x for x, v in hratios.items() if abs(v["measured_drift_ratio"] / v["E_A_ratio"] - 1.0) >= 0.10] # F4: precision law at the planned cell vs the published 2-period x=29 cell c29 = [c for c in out["cells"] if c["x"] == 29 and c["H"] == 30030][0] M29, lam29, EA29, P_plan = c29["M"], c29["lambda_bar"], c29["E_A"], 8 vr8 = var_rel_closed(M29, P_plan) sum_vr8 = sum(vr8) drift8 = lam29 * EA29 * sum_vr8 / P_plan nwin29 = M29 // 30030 nw8 = nwin29 * P_plan sigD8 = 2.0 * math.sqrt(lam29 * EA29 * (sum_vr8 / P_plan) / nw8) prec8 = 2.0 / math.sqrt(c29["twins_per_period"] * sum_vr8) sigma_R8 = c29["boot_sd"] / math.sqrt(P_plan / c29["periods"]) # bootstrap scales as 1/sqrt(n_windows) f4 = abs(prec8 / c29["precision_ratio"] - 1.0) # what the run buys, in units of the offset off = c29["offset"] resid = c29["drift_measured"] - c29["drift_cf"] planned = dict(x=29, H=30030, periods=P_plan, n_windows=nw8, drift_8periods=drift8, sigma_D_8=sigD8, sigma_D8_over_drift=prec8, sigma_R_2periods=c29["boot_sd"], sigma_R_8periods=sigma_R8, sigma_R8_over_offset=sigma_R8 / off, two_sigma_over_offset=2 * sigma_R8 / off, sigma_D8_over_sigma_R8=sigD8 / sigma_R8, detection_of_4p3pct_target=0.043 * off / sigma_R8, bound_2sigma_now=(abs(resid) + 2 * c29["boot_sd"]) / off, bound_2sigma_8periods=(abs(resid) + 2 * sigma_R8) / off, closed_form_error_over_offset=(c29["drift_measured"] - c29["drift_cf"]) / off, precision_now=c29["precision_ratio"]) out["falsifiers"] = dict( F1_measure_fires=dict(fired=bool(f1), cells=f1), F2_x19_gap_inside_noise=dict(fired=all(d["inside_2sigma"] for d in f2), detail=f2), F3_h_scaling_fires=dict(fired=bool(f3), ratios=hratios), F4_precision_law=dict(fired=bool(f4 >= 0.20), rel_dev=float(f4)), ) out["planned_cell"] = planned out["precision_law"] = {str(c["x"]): round(c["precision_ratio"], 4) for c in out["cells"]} say("cell cf D_meas cf/D_meas sigma_D (D-cf)/sigma_D cited cf/cited") for c in out["cells"]: say("x=%2d H=%5d %.6f %.6f %8.4f %.6f %+9.2f %.5f %.3f" % (c["x"], c["H"], c["drift_cf"], c["drift_measured"], c["cf_over_measured"], c["sigma_D"], c["z_measured_minus_cf"], c["drift_cited_243_358"], c["cf_over_cited"])) say("") say("G1 closed form vs #1859: max rel dev %.2e" % worst_g1) say("G3 sigma_D/boot_sd observed vs predicted: max rel dev %.3f" % dev) say("") say("measure variants (var_rel): disc/in-log ratio, uniform-n/in-log ratio") for c in out["cells"]: say(" x=%2d H=%5d %8.5f %8.5f (drift would move by %.2f%%, %.2f%%)" % (c["x"], c["H"], c["measure_disc_ratio"], c["measure_unifn_ratio"], 100 * (c["measure_disc_ratio"] - 1), 100 * (c["measure_unifn_ratio"] - 1))) say("") say("measure-corrected agreement: closed form x uniform-n/discrete factor, then the measured drift") for c in out["cells"]: say(" x=%2d H=%5d cf x%.5f -> %.6f z vs measured %+5.2f (was %+5.2f) A-spread bound %+.2f%%" % (c["x"], c["H"], c["measure_unifn_ratio"], c["drift_cf_measure_corrected"], c["z_measured_minus_cf_measure_corrected"], c["z_measured_minus_cf"], 100 * c["A_spread_correction_bound"])) say("") say("H-scaling at fixed x (measured drift ratio vs the E[A] ratio, closed form predicts them equal):") for x, v in hratios.items(): say(" x=%s E[A] x%.3f measured drift x%.3f closed form x%.3f" % (x, v["E_A_ratio"], v["measured_drift_ratio"], v["closed_form_ratio"])) say("") say("precision of a measured drift (sigma_D/drift), fixed by x, not by the exposure:") for c in out["cells"]: say(" x=%2d H=%5d sigma_D/drift %.3f (twins/period %.0f, sum_k Var_rel %.3e)" % (c["x"], c["H"], c["precision_ratio"], c["twins_per_period"], c["drift_cf"] / (c["lambda_bar"] * c["E_A"]))) say("") say("planned cell x=29, H=30030, 8 periods (same exposure, sieve to (8+1)*M = %.3e):" % (9 * M29)) say(" drift closed form %.6f (2-period value %.6f)" % (drift8, c29["drift_cf"])) say(" sigma_D (new) %.3e = %.2f%% of the correction; %.3f x the run's sigma_R" % (sigD8, 100 * prec8, sigD8 / sigma_R8)) say(" sigma_R 2 -> 8 periods %.6f -> %.6f ; sigma_R/offset = %.3f%% -> %.3f%%" % (c29["boot_sd"], sigma_R8, 100 * c29["boot_sd"] / off, 100 * sigma_R8 / off)) say(" a 4.3%%-of-offset target -> %.1f sigma_R detection" % planned["detection_of_4p3pct_target"]) say(" bound on a genuine deviation, 2 sigma: now %.2f%% of the offset, at 8 periods %.2f%%" % (100 * planned["bound_2sigma_now"], 100 * planned["bound_2sigma_8periods"])) say("") say("\n".join(ledger)) say("") say("F1 measure fires: %s" % out["falsifiers"]["F1_measure_fires"]["fired"]) say("F2 x=19 gap inside 2 sigma_D: %s (z = %s)" % (out["falsifiers"]["F2_x19_gap_inside_noise"]["fired"], ", ".join("%+.2f" % d["gaps"][0] for d in f2 if d["x"] == 19))) say("F3 H-scaling deviation >= 10%%: %s" % out["falsifiers"]["F3_h_scaling_fires"]["fired"]) say("F4 precision law deviates >= 20%%: %s (%.3f)" % (out["falsifiers"]["F4_precision_law"]["fired"], f4)) with open(os.path.join(HERE, "audit4213.json"), "w", encoding="utf-8", newline="\n") as fh: json.dump(out, fh, indent=1, sort_keys=True, default=float) fh.write("\n") with open(os.path.join(HERE, "audit4213.out"), "w", encoding="utf-8", newline="\n") as fh: fh.write("\n".join(PRINT) + "\n") return 0 if __name__ == "__main__": sys.exit(main())