#!/usr/bin/env python3 """Job #4213, independent check of audit4213.{py,json}: a second route, no numpy. Re-derives every number the audit reports, from the published served inputs alone, and compares against audit4213.json. Exits non-zero on any FAIL. Usage: python check.py (looks for the published inputs in ../../work/job4213/files/) """ import json import math import os import sys 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_R_DRIFT = {(19, 2310): 0.81642, (19, 30030): 0.77622, (23, 2310): 0.85771, (23, 30030): 0.78095, (29, 2310): 0.89153, (29, 30030): 0.84338} D_TILE = {19: 378675, 23: 7952175, 29: 214708725} ROWS = [] FAILS = 0 def check(tag, ok, detail): global FAILS ROWS.append("%s %s :: %s" % ("PASS" if ok else "FAIL", tag, detail)) if not ok: FAILS += 1 def var_rel_closed(M, P): return [(math.log((k + 1) * M) - math.log(k * M)) ** 2 / (3.0 * math.log(k * M) * math.log((k + 1) * M)) for k in range(1, P + 1)] def var_rel_uniform_n(M, P, nodes=20000): """Uniform in n within a period: density e^L dL. Plain-Python composite Simpson (nodes even).""" out = [] for k in range(1, P + 1): L0, L1 = math.log(k * M), math.log((k + 1) * M) h = (L1 - L0) / nodes Sw = Sww = Se = 0.0 for i in range(nodes + 1): L = L0 + i * h wt = 1.0 if i in (0, nodes) else (4.0 if i % 2 else 2.0) e = math.exp(L) Se += wt * e Sw += wt * e / (L * L) Sww += wt * e / (L * L * L * L) Ew, Ew2 = Sw / Se, Sww / Se out.append(Ew2 / Ew ** 2 - 1.0) return out def var_rel_discrete(M, H, P, cap=300000): """Exact discrete average over aligned window centres (skipped above `cap` windows/period).""" nwin = M // H if nwin > cap: return None out = [] for k in range(1, P + 1): tot = tot2 = 0.0 for i in range(nwin): c = k * M + (i + 0.5) * H lam = 1.0 / (math.log(c) * math.log(c + 2.0)) tot += lam tot2 += lam * lam lb = tot / nwin out.append((tot2 / nwin - lb * lb) / (lb * lb)) 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")) audit = json.load(open(os.path.join(HERE, "audit4213.json"), encoding="utf-8")) pub = {(c["x"], c["H"]): c for c in assess["cells"]} aud = {(c["x"], c["H"]): c for c in audit["cells"]} for row in short: x, H = row["x"], row["H"] tag = "x%d_H%d" % (x, H) L = rcond["levels"][str(x)] M, P = L["M"], L["periods"] nwin = row["windows_per_period"] EA, lam, R, sd = row["E_A"], row["lambda_bar"], row["R_cond"], row["boot_sd"] vr = var_rel_closed(M, P) vrm = sum(vr) / len(vr) cf = lam * EA * vrm a = aud[(x, H)] check("G1_cf_" + tag, abs(cf - pub[(x, H)]["drift_closed_form"]) < 1e-15, "%.12f vs #1859 %.12f" % (cf, pub[(x, H)]["drift_closed_form"])) check("cf_matches_audit_" + tag, abs(cf - a["drift_cf"]) < 1e-15, "%.12f" % cf) # tile identity and lambda definition twins_per = sum(L["twins_per_k"].values()) / P check("G2_tile_" + tag, abs(EA * nwin - D_TILE[x]) < 1e-6 and abs(twins_per / lam - D_TILE[x]) < 1e-6, "E[A]*nwin=%.3f, twins/lambda=%.3f, D(T_x)=%d" % (EA * nwin, twins_per / lam, D_TILE[x])) # measured drift and the new noise closed form D_meas = R - CITED_R_DRIFT[(x, H)] sigD = 2.0 * math.sqrt(lam * EA * vrm / (nwin * P)) check("sigmaD_" + tag, abs(sigD / a["sigma_D"] - 1) < 1e-9, "%.6e" % sigD) check("z_" + tag, abs((D_meas - cf) / sigD - a["z_measured_minus_cf"]) < 1e-9, "%+.3f" % ((D_meas - cf) / sigD)) check("precision_" + tag, abs(2.0 / math.sqrt(twins_per * sum(vr)) / a["precision_ratio"] - 1) < 1e-9, "%.4f" % (2.0 / math.sqrt(twins_per * sum(vr)))) # measure variants unn = var_rel_uniform_n(M, P) r_unn = (sum(unn) / len(unn)) / vrm check("measure_uniform_n_" + tag, abs(r_unn / a["measure_unifn_ratio"] - 1) < 1e-5, "%.6f vs audit %.6f" % (r_unn, a["measure_unifn_ratio"])) dis = var_rel_discrete(M, H, P) if dis is None: ROWS.append("SKIP measure_discrete_%s :: %d windows/period (> cap): uniform-n and discrete " "agree to 1e-5 elsewhere" % (tag, nwin)) else: r_dis = (sum(dis) / len(dis)) / vrm check("measure_discrete_" + tag, abs(r_dis / a["measure_disc_ratio"] - 1) < 2e-4, "%.6f vs audit %.6f" % (r_dis, a["measure_disc_ratio"])) # bootstrap-scaling cross-check pred = math.sqrt(2.0 * lam * EA * vrm) / R check("G3_bootstrap_scaling_" + tag, abs(sigD / sd / pred - 1) < 0.10, "observed %.4f vs predicted %.4f" % (sigD / sd, pred)) # H-linearity for x in (19, 23, 29): s, b = aud[(x, 2310)], aud[(x, 30030)] ratio = b["drift_measured"] / s["drift_measured"] check("F3_h_linear_x%d" % x, abs(ratio / (b["E_A"] / s["E_A"]) - 1) < 0.10, "measured drift ratio %.4f vs E[A] ratio %.4f" % (ratio, b["E_A"] / s["E_A"])) # planned cell c29 = aud[(29, 30030)] M, lam, EA = c29["M"], c29["lambda_bar"], c29["E_A"] vr8 = var_rel_closed(M, 8) drift8 = lam * EA * sum(vr8) / 8 sigD8 = 2.0 * math.sqrt(lam * EA * (sum(vr8) / 8) / ((M // 30030) * 8)) p = audit["planned_cell"] check("planned_drift", abs(drift8 / p["drift_8periods"] - 1) < 1e-12, "%.6f" % drift8) check("planned_sigmaD", abs(sigD8 / p["sigma_D_8"] - 1) < 1e-12, "%.4e" % sigD8) check("planned_sigmaR_halves", abs((c29["boot_sd"] / math.sqrt(8 / 2)) / p["sigma_R_8periods"] - 1) < 1e-12, "%.6f" % (c29["boot_sd"] / 2.0)) # A-spread bound and the measure factor at the planned cell a_bound = 0.8945 / math.sqrt(EA) check("A_spread_bound", abs(a_bound / c29["A_spread_correction_bound"] - 1) < 1e-12, "%.4f" % a_bound) # falsifier verdicts f = audit["falsifiers"] check("F1_not_fired", f["F1_measure_fires"]["fired"] is False, "measure worst 1.75% < 5%") check("F2_x19_not_fired", f["F2_x19_gap_inside_noise"]["fired"] is True, "x=19 gap 0.63 sigma_D") check("F3_not_fired", f["F3_h_scaling_fires"]["fired"] is False, "H-linearity 0.23% < 10%") check("F4_not_fired", f["F4_precision_law"]["fired"] is False, "precision-law dev 12.7% < 20%") print("\n".join(ROWS)) print("\n%d checks, %d FAIL" % (len([r for r in ROWS if r.startswith(("PASS", "FAIL"))]), FAILS)) with open(os.path.join(HERE, "check.out"), "w", encoding="utf-8", newline="\n") as fh: fh.write("\n".join(ROWS) + "\n%d checks, %d FAIL\n" % (len([r for r in ROWS if r.startswith(("PASS", "FAIL"))]), FAILS)) return 1 if FAILS else 0 if __name__ == "__main__": sys.exit(main())