The three neighbouring applications out of the published branch: the NLMS step size (anc), the ODE step controllers (odestep) and the relaxation factor (relax), with the six stage scripts that drive them and the five tests that cover them. They are not part of the root-finding paper and no document refers to them. stage18_bulletproof.py keeps claim A and loses claim B; tests/test_bulletproof.py loses the one test that reached into anc. Server: verify 548 OK / 0 BAD, tests x2 1205 passed (was 1240 with the three).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
vvs
2026-09-25 10:02:18 +01:00
co-authored by Claude Opus 5
parent 886b3f4244
commit 8ad09b47a6
22 changed files with 2 additions and 2335 deletions
-91
View File
@@ -1,91 +0,0 @@
"""Active noise control: the step size of an NLMS canceller.
The third domain, and the third instance of the same pattern. What is pinned
here is the pattern and the mistake, not a victory.
"""
from __future__ import annotations
import math
import os
import sys
import numpy as np
import pytest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from anc.bench import SCENARIOS, make_primary_path, make_secondary_path, run_nlms
from anc.rules import Fixed, InSampleMeasured, KwongJohnston, Measured
N, SEEDS = 8000, 4
SNRS = (30.0, 15.0)
def sweep(rule, snrs=SNRS):
"""Both noise levels, as the experiment uses.
Measuring at 30 dB alone is the wrong test and was: with almost no
measurement noise there is nothing for a safety factor to hedge against,
the exact step mu = 1 is near optimal, and any hedging costs a little.
The whole point of the rule appears at 15 dB. A claim about the grid has
to be tested on the grid."""
vals = []
for snr in snrs:
for s in range(SEEDS):
for _sname, gen in SCENARIOS:
rng = np.random.default_rng(1000 + s)
noise = gen(N, rng)
nr = run_nlms(noise, make_primary_path(rng),
make_secondary_path(rng), rule, snr_db=snr,
seed=s)
if math.isfinite(nr):
vals.append(nr)
return sum(vals) / len(vals), min(vals), len(vals)
def test_the_in_sample_observable_is_uninformative():
"""The mistake, pinned. NLMS with mu = 1 drives the a posteriori error of
the current sample to exactly zero, so a rule that measures it reads 'the
model was perfect' and proposes mu = 1 for ever. It is therefore the
fixed mu = 1 rule under another name, and the numbers say so."""
a, _lo, _n = sweep(InSampleMeasured())
b, _lo2, _n2 = sweep(Fixed(1.0))
assert abs(a - b) < 0.1, (a, b)
def test_measuring_out_of_sample_is_better_than_measuring_in_sample():
"""The fix: what mu hedges against is the part of the error the filter
cannot cancel, and that is observable as the floor the error keeps
hitting - not as the residual of the sample just fitted."""
good, _lo, _n = sweep(Measured())
bad, _lo2, _n2 = sweep(InSampleMeasured())
assert good > bad, (good, bad)
def test_the_measured_rule_beats_the_four_constant_scheme():
"""Kwong-Johnston is the classical variable-step-size rule and carries
four constants. This is the comparison the transfer is about."""
ours, _lo, _n = sweep(Measured())
kj, _lo2, _n2 = sweep(KwongJohnston())
assert ours > kj + 3.0, (ours, kj)
def test_and_loses_to_one_well_chosen_constant():
"""The other half, and the same in all three domains: measuring the
constant beats the elaborate multi-constant schemes and does not beat a
single constant chosen correctly for the benchmark."""
ours, _lo, _n = sweep(Measured())
best_fixed, _lo2, _n2 = sweep(Fixed(0.5))
assert best_fixed > ours, (best_fixed, ours)
assert best_fixed - ours < 3.0, (best_fixed, ours)
def test_no_rule_diverges():
"""A step size that is too large makes the filter blow up; none of these
may."""
for rule in (Fixed(0.1), Fixed(0.5), Fixed(1.0), KwongJohnston(),
Measured()):
_m, _lo, n = sweep(rule)
assert n == SEEDS * len(SCENARIOS) * len(SNRS), (rule.name, n)
-36
View File
@@ -123,39 +123,3 @@ def test_the_scope_of_the_claim_is_repairs_not_the_field():
t, _s2, _w2 = score(lambda f, a, b, xt, mx, _m=better: _m(f, a, b, xt,
mx))
assert t < c_total, (better.__name__, t, c_total)
# --------------------------------------------------------------------------
# the claim that does NOT survive, pinned so it is not overstated
# --------------------------------------------------------------------------
def test_the_anc_claim_is_not_uniform_and_must_be_stated_as_such():
"""Against a Kwong-Johnston oracle tuned per scenario the measured step
size wins on the mean (19.14 dB against 17.51) but on only four of ten
cells, and the mean is carried by two of them. It is a real result and
it is not bullet-proof; this test exists so the difference is not blurred."""
import numpy as np
from anc.bench import SCENARIOS, make_primary_path, make_secondary_path, \
run_nlms
from anc.rules import Measured
from stage18_bulletproof import KJTuned, KJ_GRID
wins = cells = 0
for _sname, gen in SCENARIOS:
for snr in (30.0, 15.0):
def sc(rule):
vals = []
for s in range(3):
rng = np.random.default_rng(1000 + s)
noise = gen(8000, rng)
nr = run_nlms(noise, make_primary_path(rng),
make_secondary_path(rng), rule,
snr_db=snr, seed=s)
if math.isfinite(nr):
vals.append(nr)
return sum(vals) / len(vals)
ours = sc(Measured())
best_kj = max(sc(KJTuned(a, g)) for a, g in KJ_GRID)
cells += 1
wins += ours > best_kj
assert 0 < wins < cells, (wins, cells)
-102
View File
@@ -1,102 +0,0 @@
"""The second domain: adaptive step-size control for ODEs.
What is being tested is the transfer, not the integrator: everything except
the controller is shared, so a difference can only come from the controller.
"""
from __future__ import annotations
import math
import os
import sys
import pytest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from odestep.controllers import (ALL, Classical, Elementary, GustafssonPI,
MeasuredSafety)
from odestep.driver import integrate, validate_against_scipy
from stage15_ode import PROBLEMS, TOLS, reference, scaled_err
def test_the_driver_reproduces_the_library():
"""The instrument before the experiment.
The first version of this work carried a hand-typed Dormand-Prince
tableau and validated it by measuring convergence orders. That was both
an unnecessary risk and, as it turned out, a material one: with the
hand-written tableau the measured-safety controller appeared to beat the
five-constant PI controller by 8%, and with SciPy's it is 2.6% behind.
The integrator is now SciPy's throughout and the check is direct."""
for name, mine, ref, endpoint in validate_against_scipy():
assert abs(mine - ref) / ref < 0.05, (name, mine, ref)
assert endpoint < 1e-6, (name, endpoint)
def test_every_controller_solves_an_easy_problem():
f = lambda t, y: [-y[0]]
for c in ALL:
r = integrate(f, 0.0, 10.0, [1.0], c, atol=1e-8, rtol=1e-8)
assert r["reached_end"], c.name
assert abs(r["y"][0] - math.exp(-10.0)) < 1e-6, c.name
def test_the_constant_free_baseline_is_the_one_to_beat():
"""The elementary controller is what the error model alone dictates and
what every practical code refuses to use. If it were not much worse,
there would be nothing for a safety factor - chosen or measured - to buy."""
f = lambda t, y: [y[1], 5.0 * (1 - y[0] * y[0]) * y[1] - y[0]]
elem = integrate(f, 0.0, 25.0, [2.0, 0.0], Elementary(), atol=1e-8,
rtol=1e-8)
meas = integrate(f, 0.0, 25.0, [2.0, 0.0], MeasuredSafety(), atol=1e-8,
rtol=1e-8)
# margins measured with SciPy's integrator. An earlier version demanded
# a factor of two on evaluations and five on rejections; those came from
# a hand-written tableau and are not what the library produces.
assert elem["nfev"] > 1.2 * meas["nfev"], (elem["nfev"], meas["nfev"])
assert elem["rejected"] > 1.5 * meas["rejected"], (elem["rejected"],
meas["rejected"])
def test_where_the_measured_safety_factor_actually_stands():
"""The honest position, over the whole grid, with SciPy's integrator.
It is NOT ahead of the PI controller - an earlier version of this test
asserted that, on numbers produced by a hand-written tableau. What it is:
level with a five-constant controller to within a few percent while
having none, and clearly ahead of the constant-free elementary rule that
every practical code refuses to use. It is behind the three-constant
classical controller and the test says so."""
tot = {c.name: 0 for c in ALL}
fails = {c.name: 0 for c in ALL}
refs = {name: (exact if exact is not None else reference(f, t0, t1, y0))
for name, f, t0, t1, y0, exact in PROBLEMS}
for tol in TOLS:
for name, f, t0, t1, y0, _exact in PROBLEMS:
for c in ALL:
r = integrate(f, t0, t1, y0, c, atol=tol, rtol=tol)
err = scaled_err(r["y"], refs[name], tol, tol)
bad = (not r["reached_end"]) or (math.isfinite(err)
and err > 1000.0)
tot[c.name] += r["nfev"]
fails[c.name] += bad
# nobody wins by failing more
assert len(set(fails.values())) == 1, fails
m = tot[MeasuredSafety().name]
pi = tot[GustafssonPI().name]
# level with the five-constant PI controller, within five percent
assert abs(m - pi) / pi < 0.05, (m, pi)
# clearly ahead of the constant-free rule the error model alone dictates
assert m < 0.9 * tot[Elementary().name], (m, tot[Elementary().name])
# and honestly behind the three-constant classical one
assert m > tot[Classical().name], (m, tot[Classical().name])
def test_the_controller_holds_no_unused_state():
"""It was called a credit controller while banking a margin that took no
part in any decision - the ledger was vestigial and the name unearned.
Nothing may accumulate here that the step factor does not read."""
c = MeasuredSafety()
c.reset()
fields = set(vars(c))
assert fields == {"predicted", "delta"}, fields
-77
View File
@@ -1,77 +0,0 @@
"""A prediction registered before the run, and falsified by it.
The hypothesis was that a method which wins in some places and loses in others
must have a regime where it wins outright, and that the regime is where the
quantity the constant approximates CHANGES during the run - because one
constant cannot be right at two different noise levels while a measured value
tracks.
It was stated before measuring, with the falsification condition attached: the
measured rule had to lose every constant-noise cell and win every
varying-noise cell. It lost all twelve.
The first version of the test could not have confirmed it either way - noise
reduction was scored on the second half of the run, by which time every ramp
and step had already arrived, so the metric never saw a transition. That flaw
was found and fixed before the conclusion was drawn, and the conclusion did
not change.
This file exists so the line is not reopened without new evidence.
"""
from __future__ import annotations
import math
import os
import sys
import pytest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from anc.rules import Fixed, Measured
from stage20_prediction import (FLAT_5, FLAT_30, STEP_25, SWING_25, sig_cabin,
sig_tonal, score)
SEEDS = 3
def test_the_metric_window_sees_the_transition():
"""The flaw that would have produced a false falsification. A varying
profile must not score the same as a constant one at its final level."""
varying = score(Fixed(0.1), sig_tonal, SWING_25, seeds=SEEDS)
final_level = score(Fixed(0.1), sig_tonal, FLAT_5, seeds=SEEDS)
assert varying > final_level + 2.0, (varying, final_level)
@pytest.mark.parametrize("signal", [sig_tonal, sig_cabin],
ids=["tonal", "cabin"])
@pytest.mark.parametrize("profile,kind", [(FLAT_30, "control"),
(FLAT_5, "control"),
(SWING_25, "prediction"),
(STEP_25, "prediction")],
ids=["flat30", "flat5", "ramp", "step"])
def test_an_oracle_constant_beats_the_measured_rule_everywhere(signal, profile,
kind):
"""The registered prediction required the measured rule to WIN the two
'prediction' cells. It does not, on either signal. A single constant,
chosen with knowledge of the scenario, is ahead even when the noise level
swings by 25 dB inside the run."""
ms = score(Measured(), signal, profile, seeds=SEEDS)
best = max(score(Fixed(mu), signal, profile, seeds=SEEDS)
for mu in (0.05, 0.1, 0.2, 0.5, 1.0))
assert best > ms, (kind, best, ms)
def test_the_conclusion_this_licenses():
"""Winning somewhere and losing elsewhere does not imply a regime of
outright victory exists; it is equally consistent with a middling
generalist. On this evidence there is no such regime, and the claim the
work may make is the scoped one in test_modern_repairs.py."""
losses = 0
for signal in (sig_tonal, sig_cabin):
for profile in (FLAT_30, FLAT_5, SWING_25, STEP_25):
ms = score(Measured(), signal, profile, seeds=SEEDS)
best = max(score(Fixed(mu), signal, profile, seeds=SEEDS)
for mu in (0.05, 0.2, 1.0))
losses += ms < best
assert losses == 8, losses
-95
View File
@@ -1,95 +0,0 @@
"""The relaxation parameter: the fourth domain, and the fourth time the same
pattern appears.
omega in SOR is the oldest hand-tuned step length in numerical computing. The
similarity form - omega = 1 + cos between successive corrections - lands
exactly in the admissible interval [1, 2] with nothing chosen, and it beats
Gauss-Seidel five-fold. It loses to a swept omega two-fold.
A second map, derived from the theory rather than from the geometry, was tried
and is worse than the first. Both are pinned: the working one so it is not
lost, the refuted one so it is not retried.
"""
from __future__ import annotations
import math
import os
import sys
import numpy as np
import pytest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from relax.sor import (ContractionOmega, Fixed, SimilarityOmega, poisson_rhs,
residual, solve, sweep_gs)
from stage23_relaxation import anisotropic, convection, theory_check
N = 14
F = poisson_rhs(N)
W_OPT = 2.0 / (1.0 + math.sin(math.pi / (N + 1)))
def test_the_sweep_reproduces_the_sor_theory():
"""The instrument before the experiment: for the model problem the SOR
spectral radius is exactly omega* - 1."""
_w, theory, measured = theory_check(16)
assert abs(theory - measured) < 0.02, (theory, measured)
def test_the_similarity_omega_lands_in_the_admissible_interval():
"""Range of the similarity and convergence condition of the method
coincide, which is why nothing had to be clipped by hand."""
r = SimilarityOmega()
r.reset()
d1 = np.array([1.0, 2.0, 3.0])
assert r.next_omega(None, None) == 1.0
assert r.next_omega(d1, d1) == pytest.approx(2.0) # identical
assert r.next_omega(d1, -d1) == pytest.approx(1.0) # opposed
assert r.next_omega(np.array([1.0, 0.0, 0.0]),
np.array([0.0, 1.0, 0.0])) == pytest.approx(1.0)
@pytest.mark.parametrize("label,coeff", [
("poisson", None), ("aniso", anisotropic(N, 0.1))])
def test_similarity_beats_gauss_seidel_by_a_wide_margin(label, coeff):
gs, _ = solve(F, Fixed(1.0), coeff=coeff)
sim, _ = solve(F, SimilarityOmega(), coeff=coeff)
assert sim * 3 < gs, (label, sim, gs)
@pytest.mark.parametrize("label,coeff", [
("poisson", None), ("aniso", anisotropic(N, 0.1))])
def test_and_loses_to_a_swept_omega(label, coeff):
"""The other half, stated so the claim is never inflated."""
sim, _ = solve(F, SimilarityOmega(), coeff=coeff)
best = min(solve(F, Fixed(w), coeff=coeff)[0]
for w in (1.2, 1.4, 1.6, 1.8, W_OPT))
assert best < sim, (label, best, sim)
def test_the_similarity_rule_over_relaxes():
"""The diagnosis, not just the outcome: the cosine between successive SOR
corrections sits near 1, so 1 + cos saturates near 2 and the rule picks a
larger omega than the optimum."""
_sim, mean_w = solve(F, SimilarityOmega())
assert mean_w > W_OPT, (mean_w, W_OPT)
def test_the_contraction_map_is_refuted_and_worse():
"""The second attempt, pinned so it is not tried again. Undamped
feedback pins omega at the limit from the first sweep."""
sim, _ = solve(F, SimilarityOmega())
con, mean_w = solve(F, ContractionOmega())
assert con > sim, (con, sim)
assert mean_w > 1.9, mean_w
def test_convection_is_where_it_actually_hurts():
"""On a strongly non-symmetric problem Gauss-Seidel is already fast and
the rule makes it slower - the one case where the similarity form is not
merely second but harmful."""
coeff = convection(N, 10.0)
gs, _ = solve(F, Fixed(1.0), coeff=coeff)
sim, _ = solve(F, SimilarityOmega(), coeff=coeff)
assert sim > gs, (sim, gs)
-105
View File
@@ -1,105 +0,0 @@
"""The similarity family for the NLMS step size, and what it settled.
Two corrections came from outside and both were right:
1. mu should be a SIMILARITY, not a variance estimate. The filter can only
remove what correlates with its input, so the quantity is the cosine
between the error and the filtered reference. It beats the variance
formulation on eleven of twelve cells.
2. Testing one similarity function is as incomplete here as it would have
been in the root-finding work. The whole family must be run, and when it
is, the best member is not the cosine.
And the law of the first domain - that the slope of (1 - S) at the origin
decides, not the shape of S - orders this family too, monotonically and
without exception. That is a prediction made in one domain and confirmed in
another.
What it did not settle: even the best member loses to a constant tuned per
scenario, on all twelve cells.
"""
from __future__ import annotations
import math
import os
import sys
import pytest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from anc.rules import CosineSimilarity, Fixed, Measured
from simroot.pure import PURE_SIMS
from stage20_prediction import FLAT_15, FLAT_30, STEP_25, sig_cabin, sig_tonal, \
score
SEEDS = 3
CELLS = [(sig_tonal, FLAT_30), (sig_tonal, STEP_25),
(sig_cabin, FLAT_15), (sig_cabin, STEP_25)]
def make(name):
r = CosineSimilarity()
r.S = None if name is None else PURE_SIMS[name]
return r
def mean(rule):
vals = [score(rule, gen, prof, seeds=SEEDS) for gen, prof in CELLS]
return sum(vals) / len(vals)
def test_the_similarity_formulation_beats_the_variance_one():
"""Correction 1. mu as 'how much of the error resembles what the filter
can remove' against mu as 'one minus the noise-power fraction'.
Stated carefully, because the first version of this test was not: what
beats the variance rule on eleven of twelve cells is the best member of
the FAMILY, not the raw cosine. The raw cosine can lose to it - on this
subset it does, 14.04 against 14.32 - which is precisely why correction 2
(run the whole family) was needed before the claim could be made at all.
"""
best_member = max(mean(make(n)) for n in (None, "ratio", "exp_abs",
"rational", "ratio_soft",
"sqrt", "cbrt"))
assert best_member > mean(Measured()), best_member
def test_the_raw_cosine_is_not_the_best_member():
"""Correction 2. A single member is not the family: the identity
S(d) = 1 - d is beaten by members with a steeper approach to 1."""
raw = mean(make(None))
best = max(mean(make(n)) for n in ("sqrt", "cbrt", "ratio_soft"))
assert best > raw, (best, raw)
def test_two_code_paths_for_the_same_function_agree():
"""S = None takes mu = |cos| directly; S = ratio computes
max(0, 1 - d) with d = 1 - |cos|. They are the same function on [0,1]
and reach it by different code, so they must score identically."""
a = mean(make(None))
b = mean(make("ratio"))
assert abs(a - b) < 1e-9, (a, b)
def test_the_law_of_the_first_domain_orders_this_family_too():
"""The prediction carried across domains: slope of (1 - S) at 0 decides.
Every slope-1 member must beat every slope-0 member."""
slope1 = [mean(make(n)) for n in ("ratio", "exp_abs", "rational")]
slope0 = [mean(make(n)) for n in ("rat_sq", "exp_sq", "cosine")]
assert min(slope1) > max(slope0), (slope1, slope0)
def test_and_it_still_loses_to_a_tuned_constant():
"""The half that did not work, pinned so the claim is never overstated.
The competitor knows the scenario; no deployable controller does - and it
is still ahead."""
losses = 0
for gen, prof in CELLS:
best_member = max(score(make(n), gen, prof, seeds=SEEDS)
for n in (None, "cbrt", "sqrt", "ratio"))
oracle = max(score(Fixed(mu), gen, prof, seeds=SEEDS)
for mu in (0.05, 0.1, 0.2, 0.5, 1.0))
losses += best_member < oracle
assert losses == len(CELLS), losses