Files
sim-numerics/diag_step_extrapolation.py
T
vvsandClaude Opus 5 2498c66358 Metric defect found by B's first run (mean = inf): an exact hit made the new width 0 and the floor (ulp of the root, 5e-324 when the root is 0) overflowed. Fixed to XTOL - an exact hit ends the run, so it is worth the halvings still needed to reach xtol - identical for every candidate; A re-run with it.
A with the fixed metric: still PASSES A1 and A2 in both configurations - flat 0.26 -> 2.82 (proposed) and 0.35 -> 0.77 (reading 3), canonical 6.32 -> 8.49 and 9.00 -> 10.44 - but a correction to the first report: for reading 3 on the flat families step A (0.77) beats our step and NOT bisection (0.99); the first run said 1.16. B (outcome memory) FAILS its falsifier: flat 0.31 -> 0.97 and 0.58 -> 0.82 (B1 holds) but canonical 4.74 -> 2.49 and 5.52 -> 2.12 (B2 fails), bisecting on 66% and 53% of canonical states. Mechanism: the known trap - a good step can collapse the error without moving the bracket, so an outcome measured in bracket shrink condemns it; the same trap that sank the bracket-progress gate (4449) and the bracket guard (7260).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 11:49:39 +01:00

107 lines
4.4 KiB
Python

"""Replay check for PREREG_step_extrapolation_2026-09-21.md (A). Method untouched.
At every traced state with a known previous displacement, three candidate landings from
the SAME state get one evaluation each: our step, step A = d_n/(1 - S) with S the ratio
similarity of the two successive steps (0 if they point opposite ways), and the midpoint.
Measured: halvings of the proven bracket that single evaluation buys.
python -u diag_step_extrapolation.py
"""
from __future__ import annotations
import math
from scipy.optimize._tstutils import _APS_TESTS_DICTS, _CHANDRUPATLA_TESTS_DICTS
from diag_contraction_similarity import CONFIGS, FLAT
from diag_model_order import bracket_step
from simroot.pure import pure_root
XTOL, MAXFEV, ACC = 1e-12, 200, 1e-6
def halvings_of(f, lo, flo, hi, fhi, landing, r):
"""Halvings of the bracket bought by one evaluation at `landing`.
FIXED 2026-09-21 after the first run of PREREG B printed `inf`: an exact hit makes
the new width 0, and the old floor (ulp of the root, 5e-324 when the root is 0)
overflowed W/floor. An exact hit ends the run, so it is worth exactly the halvings
still needed to reach xtol: the floor is XTOL, the same for every candidate. A was
re-run with this metric; its first numbers used the old floor."""
W = hi - lo
Wn = bracket_step(f, lo, flo, hi, fhi, landing)
return math.log2(W / max(Wn, XTOL)) if W > XTOL else 0.0
def replay(f, a, b, r, cfg):
st = {"trace": []}
x, _n, conv = pure_root(f, a, b, XTOL, MAXFEV, stats=st, **cfg)
if not (conv and abs(float(x) - r) <= ACC):
return []
rows = []
for s in st["trace"]:
if s["xp"] is None:
continue
A, FA, B, FB = s["A"], s["FA"], s["B"], s["FB"]
lo, flo, hi, fhi = (A, FA, B, FB) if A < B else (B, FB, A, FA)
if hi - lo <= 0.0:
continue
d_n, d_p = s["raw"], s["x"] - s["xp"]
if d_n == 0.0 or d_p == 0.0 or (d_n > 0.0) != (d_p > 0.0):
S = 0.0
else:
S = min(abs(d_n), abs(d_p)) / max(abs(d_n), abs(d_p))
if S >= 1.0:
continue # a geometric series needs S < 1
ours_L = s["x"] + d_n
ext_L = s["x"] + d_n / (1.0 - S)
mid_L = 0.5 * (lo + hi)
if not (lo < ours_L < hi):
continue
h_ours = halvings_of(f, lo, flo, hi, fhi, ours_L, r)
h_mid = halvings_of(f, lo, flo, hi, fhi, mid_L, r)
ext_ok = lo < ext_L < hi
h_ext = halvings_of(f, lo, flo, hi, fhi, ext_L, r) if ext_ok else None
rows.append((S, h_ours, h_ext, h_mid, ext_ok))
return rows
def main():
out = {}
for cname, cfg in CONFIGS.items():
groups = {"APS": [], "CHA fun1": [], "CHA flat fun3-6": [], "CHA other": []}
for d in _APS_TESTS_DICTS:
a, b = (float(v) for v in d["bracket"])
f = (lambda ff, aa: lambda t: ff(t, *aa))(d["f"], d.get("args", ()))
groups["APS"] += replay(f, a, b, float(d["root"]), cfg)
for d in _CHANDRUPATLA_TESTS_DICTS:
a, b = (float(v) for v in d["bracket"])
fam = str(d["ID"]).split(".")[0]
g = ("CHA flat fun3-6" if fam in FLAT else "CHA fun1" if fam == "fun1"
else "CHA other")
groups[g] += replay(d["f"], a, b, float(d["root"]), cfg)
print(f"\n== {cname}")
print(f" {'group':<16} {'states':>6} {'S>0':>6} {'A lands out':>11} "
f"{'paired n':>8} {'ours':>6} {'step A':>7} {'bisect':>7} (bracket halvings per eval)")
res = {}
for g, rows in groups.items():
n = len(rows)
pos = [r_ for r_ in rows if r_[0] > 0.0]
out_ = sum(1 for r_ in pos if not r_[4])
paired = [r_ for r_ in pos if r_[4]]
m = lambda v: sum(v) / len(v) if v else float("nan") # noqa: E731
ho = m([p[1] for p in paired])
he = m([p[2] for p in paired])
hm = m([p[3] for p in paired])
res[g] = dict(states=n, s_pos=len(pos), out=out_, paired=len(paired),
ours=ho, ext=he, mid=hm)
print(f" {g:<16} {n:6d} {100.0 * len(pos) / max(n, 1):5.1f}% "
f"{100.0 * out_ / max(len(pos), 1):10.1f}% {len(paired):8d} "
f"{ho:6.2f} {he:7.2f} {hm:7.2f}")
out[cname] = res
return out
if __name__ == "__main__":
main()