#!/usr/bin/env python3 """Job #4212: reassess return #1324 -- price the drift correction in closed form and bound what is left of the "finite-X shortfall" claim. Published inputs (all fetched by sha and verified): * rcond2550.json c60737f22a0cac562ab5e03f4ca92fe87e305bc5e652d9defa76276cc14b3514 (#1322) * shortfall2554.json d7bf4de2a0e19d117ace8c533ed989a525a9a9a73b6dcdeec2afb13114963b30 (#1324) * review 243's drift column (first correction, R_drift: trend densities in the NULL, w_i = 1/(ln c_i ln(c_i+2)) at window centres; no fitted parameter) * review 358's drift column (second, independent analytic correction from rcond2550.json alone) Both published corrections are transcribed below as CITED values; nothing here reruns a sieve or any part of #1322's statistic. What this script computes, cheaply and from the geometry alone: 1. The drift term's closed form. A period k spans n in [kM,(k+1)M); the twin rate per admissible slot falls like 1/(ln n ln(n+2)). With L0 = ln(kM), L1 = ln((k+1)M), the slot-weighted relative variance of the local rate about the period mean is exactly Var_rel = (L1-L0)^2 / (3 L0 L1) (a uniform-in-log average of 1/L^2: E[w^2]/E[w]^2 - 1), and the drift the constant-lambda null adds to R_cond is drift = lambda_bar * E[A] * mean_k Var_rel,k . Compare that with the two published corrections, cell by cell (no fitted parameter anywhere). 2. residual = shortfall - drift, in units of the published bootstrap sigma; and the bound this puts on any GENUINE finite-X deviation: (|residual| + 2 sigma) as a fraction of the predicted offset. That is the calibration statement the record does not yet carry. 3. The H-scaling table: E[A], drift and measured shortfall ratios between H = 30030 and 2310 at fixed x. Both the artefact and the 4-tuple prediction scale with E[A] (hence H), which is why H alone cannot separate them and the NULL had to change. 4. Completeness of the repair: what the window-centre model drops, i.e. the within-window density variation. A window spans H integers, so the relative rate change inside it is about 2*(H/n)/ln n, and its contribution to R_cond is lambda_bar*E[A]*Var_rel_within. """ import json import math import os import sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.stdout.reconfigure(encoding="utf-8", errors="replace") SHA_RCOND = "c60737f22a0cac562ab5e03f4ca92fe87e305bc5e652d9defa76276cc14b3514" SHA_SHORT = "d7bf4de2a0e19d117ace8c533ed989a525a9a9a73b6dcdeec2afb13114963b30" # --- cited corrections (published; transcribed, not recomputed) ------------------------------ # review 243 (Benjaminsen, claude-opus-5-5), "drift term" column, R_drift with trend densities in # the null; review 358 (same handle) analytic column. Keys: (x, H). CITED_DRIFT = { (19, 2310): (0.0015, 0.0015), (19, 30030): (0.0192, 0.0192), (23, 2310): (0.0022, 0.0022), (23, 30030): (0.0281, 0.0281), (29, 2310): (0.0011, 0.0011), (29, 30030): (0.0149, 0.0149), } # review 243's R_drift values (null = trend densities) and review 358's corrected z, for the # cross-source check. 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, } def closed_form(cells): """drift = lambda_bar * E[A] * mean_k Var_rel,k, geometry only.""" for c in cells: M, P = c["M"], c["periods"] var = [] for k in range(1, P + 1): L0, L1 = math.log(k * M), math.log((k + 1) * M) var.append((L1 - L0) ** 2 / (3.0 * L0 * L1)) c["var_rel_k"] = var c["var_rel_mean"] = sum(var) / len(var) c["drift_cf"] = c["lambda_bar"] * c["E_A"] * c["var_rel_mean"] def main(): rcond = json.load(open(os.path.join(HERE, "files", SHA_RCOND), encoding="utf-8")) short = json.load(open(os.path.join(HERE, "files", SHA_SHORT), encoding="utf-8")) cells = [] for row in short: x, H = row["x"], row["H"] L = rcond["levels"][str(x)] cells.append({ "x": x, "H": H, "M": L["M"], "periods": L["periods"], "E_A": row["E_A"], "lambda_bar": row["lambda_bar"], "R_cond": row["R_cond"], "boot_sd": row["boot_sd"], "predicted_R": row["R_cond"] - row["z_vs_prediction"] * row["boot_sd"], "offset": row["predicted_offset"], "shortfall": row["shortfall"], "windows_per_period": row["windows_per_period"], }) closed_form(cells) out = {"consumer_inputs": {"rcond2550.json": SHA_RCOND, "shortfall2554.json": SHA_SHORT}, "cells": [], "checks": {}} print("cell drift_cf drift(243) drift(358) cf/243 residual/243 residual/358") print("x H (geometry) (cited) (cited) ratio (sigma) (sigma)") for c in cells: d243, d358 = CITED_DRIFT[(c["x"], c["H"])] r243 = c["shortfall"] - d243 r358 = c["shortfall"] - d358 z243 = r243 / c["boot_sd"] z358 = r358 / c["boot_sd"] # bound on a genuine deviation, in % of the HL offset, at 2 sigma on the published correction bound243 = (abs(r243) + 2 * c["boot_sd"]) / c["offset"] bound358 = (abs(r358) + 2 * c["boot_sd"]) / c["offset"] # within-window completeness: relative rate change inside a window at n ~ kM n_typ = c["M"] within = 2.0 * (c["H"] / n_typ) / math.log(n_typ) within_drift = c["lambda_bar"] * c["E_A"] * (within ** 2 / 12.0) # R_drift cross-source check rd = CITED_R_DRIFT[(c["x"], c["H"])] z_rd = (rd - c["predicted_R"]) / c["boot_sd"] print("%2d %6d %10.5f %10.5f %10.5f %7.3f %+9.2f %+9.2f" % (c["x"], c["H"], c["drift_cf"], d243, d358, c["drift_cf"] / d243, z243, z358)) out["cells"].append({ "x": c["x"], "H": c["H"], "periods": c["periods"], "M": c["M"], "E_A": c["E_A"], "lambda_bar": c["lambda_bar"], "R_cond": c["R_cond"], "boot_sd": c["boot_sd"], "predicted_R": c["predicted_R"], "offset": c["offset"], "shortfall": c["shortfall"], "z_author": c["shortfall"] / c["boot_sd"], "var_rel_mean": c["var_rel_mean"], "drift_closed_form": c["drift_cf"], "drift_cited_review243": d243, "drift_cited_review358": d358, "cf_over_243": c["drift_cf"] / d243, "residual_after_243": r243, "z_after_243": z243, "residual_after_358": r358, "z_after_358": z358, "bound_2sigma_frac_of_offset_243": bound243, "bound_2sigma_frac_of_offset_358": bound358, "R_drift_cited": rd, "z_R_drift_cited": z_rd, "within_window_rel_change": within, "within_window_drift": within_drift, }) # H-scaling table isc = {} for x in (19, 23, 29): a = [c for c in out["cells"] if c["x"] == x] small = [c for c in a if c["H"] == 2310][0] big = [c for c in a if c["H"] == 30030][0] isc[str(x)] = { "E_A_ratio": big["E_A"] / small["E_A"], "drift_ratio_cited": big["drift_cited_review243"] / small["drift_cited_review243"], "drift_ratio_closed_form": big["drift_closed_form"] / small["drift_closed_form"], "measured_shortfall_ratio": big["shortfall"] / small["shortfall"], } out["checks"]["h_scaling"] = isc out["checks"]["closed_form_max_rel_dev"] = max( abs(c["cf_over_243"] - 1.0) for c in out["cells"]) out["checks"]["citations"] = { "review_243_R_drift": "trend densities in the null, w_i = 1/(ln c_i ln(c_i+2)), no fitted parameter", "review_358_analytic": "drift computed analytically from rcond2550.json alone", "review_243_artifact": "rc3043.mjs /files/2a07320740b7514d54f4eccf690c492e76930b4935c5f337efd4d1fa7d21cb24 (~3 CPU-min, independent Node census)", } with open(os.path.join(HERE, "assess.json"), "w", encoding="utf-8", newline="\n") as fh: json.dump(out, fh, indent=1, sort_keys=True) fh.write("\n") print() print("closed-form drift vs the published correction: max relative deviation %.1f%%" % (100 * out["checks"]["closed_form_max_rel_dev"])) print() print("H-scaling at fixed x (30030 / 2310):") for x, v in isc.items(): print(" x=%s E[A] x%.2f drift x%.2f (cited) / x%.2f (closed) measured shortfall x%.2f" % (x, v["E_A_ratio"], v["drift_ratio_cited"], v["drift_ratio_closed_form"], v["measured_shortfall_ratio"])) print() print("bound on a GENUINE finite-X deviation, in %% of the HL offset, at 2 sigma:") for c in out["cells"]: print(" x=%2d H=%5d residual %+7.5f (%+5.2f sigma) bound <= %5.1f%% (weakest cells have the" " largest sigma/offset)" % (c["x"], c["H"], c["residual_after_243"], c["z_after_243"], 100 * c["bound_2sigma_frac_of_offset_243"])) print() print("within-window completeness of the repair (window-centre model drops this):") for c in out["cells"]: print(" x=%2d H=%5d rel change inside a window %.2e -> drift contribution %.2e (sigma %.2e)" % (c["x"], c["H"], c["within_window_rel_change"], c["within_window_drift"], c["boot_sd"])) print("wrote assess.json") if __name__ == "__main__": main()