Files
sim-numerics/tests/test_flat_gate.py
T
vvsandClaude Opus 5 9884b21204 PREREG_flat_twice outcome: stage 1 passed (twice-in-a-row fires on 27.8% of model steps on the flat families, 8.7% on APS, 4.3% on fun1; flat/APS ratio 3.20 against the single flag's 2.47), stage 2 FAILED: under the projection APS 2806 -> 2803 (W2 holds, the per-problem edge over Chandrupatla kept at 83/28/42) but CHA 3220 -> 3341 and the flat families 1823 -> 1975 (W1, W4 fail); with G2 the same. Under safe/step the twice gate is inert (3390 -> 3388) where the single one gave 3037.
Mechanism: the flat cycle is undershoot, undershoot, crossing, near-empty step. The single flag fires after the first undershoot and replaces the second - a useless step; the twice flag fires after the second and replaces the CROSSING, the one step of the cycle that collapses the bracket. The second undershoot's r is ~0.26, next to the 1/4 threshold, so the pair often does not even form. Again a detectability gain that did not carry to run cost. Next candidate (not pre-registered): the unkept promise AND a creep - the same step moving the same way as the previous one.

Removed a vacuous pin I had written (s2 <= max(s1, s2)). Regression on the branch: 935 pass twice; the only red test is the paper-number check.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 13:45:20 +01:00

75 lines
3.0 KiB
Python

"""Pins for PREREG_flat_regime_2026-09-21.md: the flat gate (prestep='flat')."""
from __future__ import annotations
import math
import pytest
from simroot.pure import pure_root
XTOL, MAXFEV = 1e-12, 200
PROBS = (
(lambda t: t ** 3 - 2.0 * t - 5.0, 1.0, 4.0),
(lambda t: math.exp(t) - 2.0, 0.0, 3.0),
(lambda t: 6.0 * (t - 2.0) ** 5, 0.0, 10.0),
(lambda t: t ** 19, -10.0, 100.0),
)
@pytest.mark.parametrize("f,a,b", PROBS)
@pytest.mark.parametrize("guard", ("project", "credit", "safe"))
def test_the_default_is_untouched(f, a, b, guard):
ref = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard=guard)
off = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard=guard,
prestep="none")
assert ref == off
@pytest.mark.parametrize("f,a,b", PROBS)
@pytest.mark.parametrize("guard", ("project", "credit"))
@pytest.mark.parametrize("ext", ("none", "pattern"))
def test_the_guarantee_holds_with_the_gate(f, a, b, guard, ext):
x, n, ok = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3",
guard=guard, prestep="flat", extrapolate=ext)
assert ok and min(a, b) <= x <= max(a, b)
half = math.ceil(math.log2(abs(b - a) / XTOL))
assert n <= (2 * half + 3 if guard == "project" else half + 4), n
@pytest.mark.parametrize("f,a,b", PROBS)
@pytest.mark.parametrize("guard", ("project", "credit"))
@pytest.mark.parametrize("ext", ("none", "pattern"))
def test_the_guarantee_holds_with_the_twice_gate(f, a, b, guard, ext):
x, n, ok = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3",
guard=guard, prestep="flat2", extrapolate=ext)
assert ok and min(a, b) <= x <= max(a, b)
half = math.ceil(math.log2(abs(b - a) / XTOL))
assert n <= (2 * half + 3 if guard == "project" else half + 4), n
def test_the_twice_gate_is_silent_where_the_model_is_exact_and_fires_on_a_power():
st = {}
pure_root(lambda t: 3.0 * t - 1.0, -2.0, 5.0, XTOL, MAXFEV, similarity="ratio",
scale="derived3", guard="project", prestep="flat2", stats=st)
assert st["pre_refused"] == 0
st = {}
pure_root(lambda t: t ** 9, -1.0, 2.0, XTOL, MAXFEV, similarity="ratio",
scale="derived3", guard="project", prestep="flat2", stats=st)
assert st["pre_refused"] > 0
def test_the_gate_never_fires_where_the_model_is_exact():
"""On a straight line the model's step lands on the root: r = 0, never >= 1/4."""
st = {}
pure_root(lambda t: 3.0 * t - 1.0, -2.0, 5.0, XTOL, MAXFEV, similarity="ratio",
scale="derived3", guard="project", prestep="flat", stats=st)
assert st["pre_refused"] == 0
def test_the_gate_fires_on_a_pure_power():
"""On x^9 the tangent step leaves (8/9)^9 = 0.35 of |f| on the same side."""
st = {}
pure_root(lambda t: t ** 9, -1.0, 2.0, XTOL, MAXFEV, similarity="ratio",
scale="derived3", guard="project", prestep="flat", stats=st)
assert st["pre_refused"] > 0