Compare commits
11
Commits
34b2a30d6c
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8ad09b47a6 | ||
|
|
886b3f4244 | ||
|
|
ce5b42f284 | ||
|
|
2bc57fcc83 | ||
|
|
fc91d3b8aa | ||
|
|
1c63b829d9 | ||
|
|
9c1638af07 | ||
|
|
5c0550a0de | ||
|
|
1eb2c1664f | ||
|
|
aa304cd262 | ||
|
|
6a1c264910 |
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -0,0 +1,8 @@
|
||||
sim-numerics — Root Finding by Similarity and Analogy
|
||||
Copyright 2026 Valeri Sitnikov
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (see LICENSE).
|
||||
|
||||
This product includes the proposed bracketing method A′, its reference implementation (simroot),
|
||||
the compiled library simbracket, the benchmark harness, the problem corpora and every results file
|
||||
behind the paper.
|
||||
@@ -0,0 +1,21 @@
|
||||
# POST HOC — Лагуанель в Chandrupatla, только ветвь m ≥ 2
|
||||
|
||||
Записано 2026-09-22 **после** прогона `PREREG_estimated_transform_2026-09-22.md` (К3) и до этого
|
||||
прогона. Доработка 3, п. 5: вариант К3-Л с двумя ветвями фиксирует дробный порядок на 115
|
||||
из 154 простых корней APS (точки Chandrupatla лежат по разные стороны корня, θ < 0) и вырождается в
|
||||
бисекцию; «очевидная поправка — ограничение ветвью m ≥ 2 — должна быть запущена, с пометкой post hoc».
|
||||
|
||||
Это **не** запись до прогона: вариант выбран, зная исход К3-Л. В статье он помечается как post hoc и
|
||||
не входит ни в какое правило вывода из PREREG_estimated_transform.
|
||||
|
||||
## Определение
|
||||
|
||||
К3-Л2: всё как в К3-Л (`simroot/transformed.py`, оценщик Лагуанеля по трём последним вычислениям,
|
||||
гистерезис без сброса), кроме одного: m̂ заменяется прочитанным m **только при m ≥ 2** (включая
|
||||
m = ∞ при θ ≥ 1); ветвь 0 < m ≤ ⅔ не используется.
|
||||
|
||||
## Что сообщается
|
||||
|
||||
Те же числа, что для К3 в табл. 12: корпус 4, шесть корпусов, 148 кратных/дробных корней, APS; и
|
||||
сколько простых корней APS заканчивают прогон с m̂ ≠ 1. Один прогон; определение после него не
|
||||
меняется.
|
||||
@@ -1,82 +1,71 @@
|
||||
# sim-numerics — similarity as a step rule for root finding
|
||||
# sim-numerics — similarity and analogy as step rules for root finding
|
||||
|
||||
Artifact for the paper `article/sim_numerics.tex`. Every claim in the paper is
|
||||
produced by one of the scripts below; nothing in the text is transcribed by
|
||||
hand, and `verify_article.py` checks all 72 stated numbers against the run
|
||||
data as part of the test suite.
|
||||
A bracketing root finder derived from a similarity function, with a decision layer that recognises
|
||||
multiple and fractional roots by analogy, and a worst case of twice bisection plus three evaluations
|
||||
that holds whatever proposes the step.
|
||||
|
||||
Everything here is released under the Apache License 2.0; copyright Valeri Sitnikov (`NOTICE`).
|
||||
Archive of record, with the paper, the data and the full history:
|
||||
[10.5281/zenodo.22894336](https://doi.org/10.5281/zenodo.22894336) (version 1.0.0:
|
||||
[10.5281/zenodo.22894337](https://doi.org/10.5281/zenodo.22894337)).
|
||||
|
||||
## Using the method
|
||||
|
||||
`lib/` builds `simbracket`, the method as a compiled library (Cython, Python 3.10 or later, wheels for
|
||||
Linux and Windows in the archive). It agrees with the reference implementation bit for bit, point and
|
||||
evaluation count, on all 868 problems of the six corpora.
|
||||
|
||||
```python
|
||||
from simbracket import find_root, evaluation_bound
|
||||
|
||||
x, nfev, converged = find_root(f, a, b, xtol=1e-12, maxfev=200)
|
||||
# f(a) and f(b) of opposite signs; stops when the bracket is no wider than xtol or f(x) == 0;
|
||||
# never more than evaluation_bound(a, b, xtol) = 2*ceil(log2(|b-a|/xtol)) + 3 evaluations
|
||||
```
|
||||
|
||||
From source instead: `simroot.pure.pure_root` with the configuration named `APRIME` in
|
||||
`sweep_full.py`.
|
||||
|
||||
## What it costs
|
||||
|
||||
On the 240 problems of corpus 4, fixed before the method's code was frozen, the method needs 3656
|
||||
evaluations. Chandrupatla's method needs 5717 there, ModAB 5535, Brent 8267 and TOMS 748 8084. It is
|
||||
the cheapest of those methods on 77% of the problems, and on 88% of the multiple and fractional
|
||||
roots.
|
||||
|
||||
## Reproducing
|
||||
|
||||
Use one platform for all numbers. The test functions call `libm`, whose last digit differs between
|
||||
platforms, and the same SciPy version scores 2635 for TOMS 748 on Windows against 2636 on Linux for
|
||||
the same set. The published numbers are from Linux (Ubuntu 24.04, Python 3.12.3).
|
||||
|
||||
```
|
||||
python -m pytest tests/ -q # 490 tests
|
||||
python sweep_full.py # the full grid, writes sweep_full.json
|
||||
python verify_article.py # every paper number vs the data
|
||||
python -m pytest tests -q # 1205 tests
|
||||
python -u verify_article.py # 548 numbers of the paper against the run data
|
||||
```
|
||||
|
||||
Heavy runs go to a Linux machine (Python 3.12, run detached with a log,
|
||||
`setsid nohup ... > sweep.log 2>&1`). **Use one platform for all numbers**: the
|
||||
test functions call `libm`, whose last digit differs between Windows and Linux,
|
||||
and the same SciPy version scores 2819 for TOMS 748 on Windows against 2820 on
|
||||
Linux for the same set. The paper's numbers are the Linux ones.
|
||||
|
||||
## Where each claim comes from
|
||||
|
||||
| Paper | Claim | Script |
|
||||
|---|---|---|
|
||||
| Prop. 1 | the two-point similarity step **is** regula falsi | `tests/test_simroot.py::test_the_pure_two_point_similarity_step_is_regula_falsi_exactly` |
|
||||
| Prop. 2, Table 1 | contraction law, predicted 0.1716 vs measured 0.1716 | `stage0c_why_pure_fails.py` |
|
||||
| Cor. 1 | a chosen similarity fails on 5–8 of 8 problems | `stage0b_pure_similarity.py` |
|
||||
| Cor. 2 | faster similarity is not better (`sqrt`, `cbrt`) | `stage0d_derived_similarity.py` |
|
||||
| Cor. 3 | derived scale ⇒ the step is \|f\|/\|f'\| ⇒ superlinear | `stage0d_derived_similarity.py`, `stage0e_guard_cost.py` |
|
||||
| §3 safeguards | bracket vs step vs both, and what each costs | `stage2_guard_modes.py`, `stage3_bound_check.py` |
|
||||
| §3 order | 1.584 (2-point) vs 1.944 (3-point) | `stage5_three_point.py` |
|
||||
| §3.1 | project vs replace; the guarantee costs nothing | `stage6_projection.py` |
|
||||
| §3.1 | the projection invariant, instrumented per step | `stage6b_invariant.py` |
|
||||
| §4 fairness | ITP is not handicapped; κ₁ and n₀ sweeps | `stage2b_itp_fairness.py` |
|
||||
| Tables 2, 3 | the 154-configuration ranking on both sets | `sweep_full.py` → `report_sweep.py` |
|
||||
| Table 4 | the law across the whole grid | `sweep_full.py` → `report_sweep.py` |
|
||||
| §6 | approach vs endgame, order, boldness, step quality | `stage4_why.py` |
|
||||
| §7.1 | the prototype-aggregation form loses 0/6/147 | `bench_full.py` (`SIM.pair2d.*`) |
|
||||
| §7.2 | the 2× bound and high order are incompatible | `stage5_three_point.py`, `tests/test_three_point.py` |
|
||||
| §7.4 | the symmetric ratio similarity degenerates | `simroot/pure.py` (`PURE_SIMS["ratio"]` docstring), `stage0d` |
|
||||
Any table of the paper can be recomputed from scratch with its sweep script; each script's docstring
|
||||
names the table it produces. Heavy runs belong on a Linux machine, detached, with a log.
|
||||
|
||||
## Layout
|
||||
|
||||
```
|
||||
simroot/pure.py the method: one similarity, one step rule, three guards
|
||||
simroot/baselines.py bisection, 4 false-position variants, Ridders, Brent,
|
||||
TOMS 748 (SciPy), ITP (own, validated against its theorem)
|
||||
simroot/sim.py the prototype-aggregation form — a NEGATIVE result, kept
|
||||
because the paper reports it
|
||||
stage0*..stage5* the derivation, in the order it was actually established
|
||||
bench_full.py 17 methods on both sets
|
||||
sweep_full.py 9 similarities x 4 scales x 4 guards + 10 baselines
|
||||
report_sweep.py reads sweep_full.json, produces the paper's tables
|
||||
verify_article.py asserts the paper against the data (72 checks)
|
||||
tests/ 490 tests; every bug found in audit is pinned here
|
||||
simroot/pure.py the method: the similarity step, the decision layer, the projection
|
||||
simroot/baselines.py bisection, the false-position family, Ridders, Brent, TOMS 748, ITP
|
||||
lib/ the source of the compiled library simbracket
|
||||
corpus_*.py the problem corpora, as code
|
||||
sweep_*.py the runs; each writes its results file
|
||||
verify_article.py every number stated in the paper, against the data
|
||||
article/ the paper
|
||||
PREREG_*.md POSTHOC_*.md what was recorded before each run, and what after
|
||||
FINDINGS_*.md what the diagnoses found
|
||||
tests/ 1205 tests; every bug found in an audit is pinned here
|
||||
```
|
||||
|
||||
## Negative results, so they are not reopened
|
||||
## What was refuted
|
||||
|
||||
1. Similarity normalised by the two bracket ends **is** regula falsi, identically.
|
||||
2. A faster-changing similarity is not better; what matters is the slope of
|
||||
`1 - S` at zero matching the problem, and only the data supplies it.
|
||||
3. Prototype-pair aggregation with softmax over the whole memory loses to the
|
||||
simplest form: 0 wins / 6 ties / 147 losses against Brent on APS.
|
||||
4. "Our safeguard fires less often" — refuted: Brent discards 53% of its
|
||||
interpolations on the set it wins, 34% on the set it loses.
|
||||
5. "Our steps are bolder" — refuted: on Chandrupatla Brent is bolder (56.3%
|
||||
against 41.8%) and still loses. The mechanism is step *quality*: median
|
||||
bracket removed 0.275 against 0.058.
|
||||
6. Every safeguard that REPLACES the computed step with a midpoint destroys the
|
||||
order: with the three-point scale, order falls from 1.94 to 1.00 and the APS
|
||||
total from 2806 to 7260. **Resolved** by projecting the step onto the
|
||||
admissible interval instead (`guard="project"`), which costs nothing on APS
|
||||
(2806 either way) and gains on Chandrupatla (40/45 at 3220 against 35/45 at
|
||||
4104 unguarded).
|
||||
7. Zero slack in the projection is degenerate: a worst case of exactly bisection
|
||||
demands a halving at every step, leaving the projection no freedom.
|
||||
8. Writing the projection as a radius around the midpoint instead of a clamp
|
||||
loses the invariant to cancellation — a relative 8.9e-5 overshoot on eleven
|
||||
Chandrupatla problems.
|
||||
```
|
||||
The archive keeps eleven negative results in the form in which they were written, among them: a
|
||||
similarity normalised by the two bracket ends is regula falsi identically; a faster-changing
|
||||
similarity is not better; every safeguard that *replaces* the computed step with a midpoint destroys
|
||||
the order, which is why the step is projected instead; and the phase detector whose derivation is
|
||||
sound and whose estimator is not. They are kept so that they are not reopened.
|
||||
|
||||
-190
@@ -1,190 +0,0 @@
|
||||
"""Active noise control: the step size of an adaptive filter.
|
||||
|
||||
The third domain, and the one where the object we have is most exactly the
|
||||
object the field argues about.
|
||||
|
||||
In NLMS the coefficient update is
|
||||
|
||||
w <- w + mu * e * x / (||x||^2 + eps)
|
||||
|
||||
and mu = 1 is not an arbitrary scale: it is the step that drives the a
|
||||
posteriori error at the current sample exactly to zero. Every practical
|
||||
system uses mu < 1, because zeroing the current sample fits the measurement
|
||||
noise as well as the signal. So mu is precisely a SAFETY FACTOR on an exact
|
||||
step - the same object as the safety factor in ODE step control, and the same
|
||||
object as the projection budget in the root-finding safeguard.
|
||||
|
||||
The whole variable-step-size LMS literature exists because mu is hard to
|
||||
choose, and it answers with more constants: Kwong and Johnston's rule has
|
||||
four. What this file asks is whether the quantity mu is guessing can be
|
||||
measured instead.
|
||||
|
||||
predicted a posteriori error (1 - mu) * e if the model were exact
|
||||
observed a posteriori error d - w_new . x after the update
|
||||
|
||||
Their ratio says how much of e was signal the filter could actually remove
|
||||
and how much was noise it could not. That is the safety factor, measured.
|
||||
|
||||
The acoustic paths and the noise regimes are taken verbatim from our earlier
|
||||
ANC benchmark (branch feature/sem-anc-benchmark of the SEM repository), so
|
||||
the environment is the established one and not built to suit the result.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
import numpy as np
|
||||
|
||||
FS = 8000
|
||||
PRIMARY_PATH_LEN = 64
|
||||
SECONDARY_PATH_LEN = 32
|
||||
FILTER_LEN = 64
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- paths ---
|
||||
def make_primary_path(rng):
|
||||
h = np.zeros(PRIMARY_PATH_LEN)
|
||||
delay = rng.integers(5, 10)
|
||||
h[delay] = 0.8 + 0.2 * rng.standard_normal()
|
||||
for k in range(3):
|
||||
idx = delay + rng.integers(3, 15)
|
||||
if idx < PRIMARY_PATH_LEN:
|
||||
h[idx] = (0.3 - 0.1 * k) * rng.standard_normal()
|
||||
for i in range(delay + 5, PRIMARY_PATH_LEN):
|
||||
h[i] += 0.05 * math.exp(-0.1 * (i - delay)) * rng.standard_normal()
|
||||
return h
|
||||
|
||||
|
||||
def make_secondary_path(rng):
|
||||
h = np.zeros(SECONDARY_PATH_LEN)
|
||||
delay = rng.integers(2, 5)
|
||||
h[delay] = 0.9 + 0.1 * rng.standard_normal()
|
||||
for k in range(2):
|
||||
idx = delay + rng.integers(2, 8)
|
||||
if idx < SECONDARY_PATH_LEN:
|
||||
h[idx] = (0.2 - 0.05 * k) * rng.standard_normal()
|
||||
for i in range(delay + 3, SECONDARY_PATH_LEN):
|
||||
h[i] += 0.03 * math.exp(-0.15 * (i - delay)) * rng.standard_normal()
|
||||
return h
|
||||
|
||||
|
||||
# ------------------------------------------------------------- regimes ---
|
||||
def gen_tonal(n, rng, f0=200.0):
|
||||
t = np.arange(n) / FS
|
||||
return np.sin(2 * math.pi * f0 * t + rng.uniform(0, 2 * math.pi))
|
||||
|
||||
|
||||
def gen_broadband(n, rng):
|
||||
pink = np.zeros(n)
|
||||
rows = np.zeros(5)
|
||||
for i in range(n):
|
||||
for j in range(5):
|
||||
if i % (2 ** j) == 0:
|
||||
rows[j] = rng.standard_normal()
|
||||
pink[i] = np.sum(rows)
|
||||
return pink / (np.std(pink) + 1e-12)
|
||||
|
||||
|
||||
def gen_multitonal(n, rng, f0=100.0):
|
||||
t = np.arange(n) / FS
|
||||
ph = rng.uniform(0, 2 * math.pi, 3)
|
||||
sig = (np.sin(2 * math.pi * f0 * t + ph[0])
|
||||
+ 0.5 * np.sin(2 * math.pi * 2 * f0 * t + ph[1])
|
||||
+ 0.25 * np.sin(2 * math.pi * 3 * f0 * t + ph[2]))
|
||||
return sig / (np.max(np.abs(sig)) + 1e-12)
|
||||
|
||||
|
||||
def gen_silence(n, rng):
|
||||
return rng.standard_normal(n) * 0.01
|
||||
|
||||
|
||||
def gen_impulse_train(n, rng):
|
||||
sig = np.zeros(n)
|
||||
for _ in range(rng.integers(5, 12)):
|
||||
pos = rng.integers(0, n)
|
||||
width = rng.integers(10, 40)
|
||||
amp = rng.uniform(1.0, 3.0) * rng.choice([-1, 1])
|
||||
end = min(pos + width, n)
|
||||
sig[pos:end] = amp * np.exp(-5.0 * np.arange(end - pos) / width)
|
||||
return sig
|
||||
|
||||
|
||||
def scen_stable(n, rng):
|
||||
seg = n // 4
|
||||
return np.concatenate([gen_tonal(seg, rng), gen_broadband(seg, rng),
|
||||
gen_multitonal(seg, rng),
|
||||
gen_silence(n - 3 * seg, rng)])
|
||||
|
||||
|
||||
def scen_gradual(n, rng):
|
||||
a = np.linspace(0, 1, n)
|
||||
return (1 - a) * gen_tonal(n, rng) + a * gen_broadband(n, rng)
|
||||
|
||||
|
||||
def scen_abrupt(n, rng):
|
||||
out = np.zeros(n)
|
||||
gens = [gen_tonal, gen_broadband, gen_multitonal]
|
||||
pos = 0
|
||||
while pos < n:
|
||||
seg = min(int(rng.integers(n // 20, n // 8)), n - pos)
|
||||
out[pos:pos + seg] = gens[rng.integers(0, 3)](seg, rng)
|
||||
pos += seg
|
||||
return out
|
||||
|
||||
|
||||
def scen_cabin(n, rng):
|
||||
engine = gen_multitonal(n, rng, f0=80.0)
|
||||
road = 0.6 * gen_broadband(n, rng)
|
||||
imp = 0.4 * gen_impulse_train(n, rng)
|
||||
s = engine + road + imp
|
||||
return s / (np.max(np.abs(s)) + 1e-12)
|
||||
|
||||
|
||||
def scen_impulsive(n, rng):
|
||||
return gen_impulse_train(n, rng) + 0.2 * gen_broadband(n, rng)
|
||||
|
||||
|
||||
SCENARIOS = [("stable regimes", scen_stable), ("gradual transition", scen_gradual),
|
||||
("abrupt switching", scen_abrupt), ("car cabin", scen_cabin),
|
||||
("impulsive", scen_impulsive)]
|
||||
|
||||
|
||||
# --------------------------------------------------------- the filter ---
|
||||
def run_nlms(noise, prim, sec, rule, snr_db=30.0, seed=0):
|
||||
"""One NLMS/FxLMS run. Everything is fixed except `rule`, which returns
|
||||
the step size for the current sample."""
|
||||
rng = np.random.default_rng(seed)
|
||||
n = len(noise)
|
||||
d = np.convolve(noise, prim)[:n] # what reaches the ear
|
||||
xf = np.convolve(noise, sec)[:n] # filtered reference
|
||||
p = float(np.mean(d * d))
|
||||
d = d + rng.standard_normal(n) * math.sqrt(p / (10 ** (snr_db / 10.0)))
|
||||
|
||||
w = np.zeros(FILTER_LEN)
|
||||
buf = np.zeros(FILTER_LEN)
|
||||
err = np.zeros(n)
|
||||
rule.reset()
|
||||
for i in range(n):
|
||||
buf[1:] = buf[:-1]
|
||||
buf[0] = xf[i]
|
||||
y = float(np.dot(w, buf))
|
||||
e = d[i] - y
|
||||
err[i] = e
|
||||
nrm = float(np.dot(buf, buf)) + 1e-6
|
||||
mu = rule.step(e, buf, nrm)
|
||||
if mu != 0.0:
|
||||
w += mu * e * buf / nrm
|
||||
# the a posteriori error, which is what the rule needs to learn
|
||||
# how much of e it was able to remove
|
||||
rule.observe(e, d[i] - float(np.dot(w, buf)), mu)
|
||||
else:
|
||||
rule.observe(e, e, 0.0)
|
||||
# noise reduction: power of the residual against the power of what would
|
||||
# have reached the ear untreated, measured on the second half so the
|
||||
# transient is not counted
|
||||
half = n // 2
|
||||
p_in = float(np.mean(d[half:] ** 2))
|
||||
p_out = float(np.mean(err[half:] ** 2))
|
||||
if p_out <= 0.0 or not math.isfinite(p_out):
|
||||
return float("nan")
|
||||
return 10.0 * math.log10(p_in / p_out)
|
||||
-204
@@ -1,204 +0,0 @@
|
||||
"""Step-size rules for NLMS. Everything else in the filter is shared.
|
||||
|
||||
mu = 1 drives the a posteriori error of the current sample to zero
|
||||
mu < 1 hedges, because that sample carries measurement noise too
|
||||
|
||||
So mu is a safety factor on an exact step, exactly as in ODE step control.
|
||||
The variable-step-size literature answers the difficulty of choosing it with
|
||||
more constants; the rule at the bottom measures it instead.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
|
||||
class Rule:
|
||||
name = "?"
|
||||
constants = 0
|
||||
|
||||
def reset(self):
|
||||
pass
|
||||
|
||||
def step(self, e, x, nrm):
|
||||
raise NotImplementedError
|
||||
|
||||
def observe(self, e_pre, e_post, mu):
|
||||
pass
|
||||
|
||||
|
||||
class Fixed(Rule):
|
||||
def __init__(self, mu):
|
||||
self.mu = mu
|
||||
self.name = f"fixed mu={mu:g}"
|
||||
self.constants = 1
|
||||
|
||||
def step(self, e, x, nrm):
|
||||
return self.mu
|
||||
|
||||
|
||||
class KwongJohnston(Rule):
|
||||
"""The classical variable step size: mu(n+1) = a*mu(n) + g*e(n)^2, clamped.
|
||||
|
||||
Four constants, and the paper that introduced it spends its length on how
|
||||
to pick them."""
|
||||
|
||||
name = "Kwong-Johnston VSS"
|
||||
constants = 4
|
||||
A, G, MU_MIN, MU_MAX = 0.97, 0.02, 0.001, 1.0
|
||||
|
||||
def reset(self):
|
||||
self.mu = 0.5
|
||||
|
||||
def step(self, e, x, nrm):
|
||||
return self.mu
|
||||
|
||||
def observe(self, e_pre, e_post, mu):
|
||||
self.mu = self.A * self.mu + self.G * e_pre * e_pre
|
||||
self.mu = min(self.MU_MAX, max(self.MU_MIN, self.mu))
|
||||
|
||||
|
||||
class InSampleMeasured(Rule):
|
||||
"""REFUTED - kept so the mistake is reproducible.
|
||||
|
||||
The first attempt measured the A POSTERIORI error of the sample the update
|
||||
had just been fitted to. That observable is uninformative by
|
||||
construction: NLMS with mu = 1 drives it to exactly zero, so the rule
|
||||
reads "the model was perfect", proposes mu = 1 again, and is locked there.
|
||||
Measured, it scores 18.50 dB against fixed mu = 1's 18.49 - the same rule
|
||||
wearing a different name.
|
||||
|
||||
It is the same error as the rho detector in the root-finding work:
|
||||
measuring a quantity in the one place where it is identically zero."""
|
||||
|
||||
name = "in-sample (refuted)"
|
||||
constants = 0
|
||||
|
||||
def reset(self):
|
||||
self.rho = 1.0
|
||||
|
||||
def step(self, e, x, nrm):
|
||||
return min(1.0, 1.0 / self.rho) if self.rho > 0.0 else 1.0
|
||||
|
||||
def observe(self, e_pre, e_post, mu):
|
||||
if mu <= 0.0 or e_pre == 0.0:
|
||||
return
|
||||
predicted = abs((1.0 - mu) * e_pre)
|
||||
observed = abs(e_post)
|
||||
if predicted <= 0.0:
|
||||
self.rho = 1.0 + observed / (abs(e_pre) + 1e-300)
|
||||
else:
|
||||
r = observed / predicted
|
||||
if math.isfinite(r) and r > 0.0:
|
||||
self.rho = math.sqrt(self.rho * max(r, 1e-6))
|
||||
|
||||
|
||||
class Measured(Rule):
|
||||
"""The safety factor measured out of sample.
|
||||
|
||||
What mu is hedging against is the part of the error the filter cannot
|
||||
cancel - the measurement noise. NLMS theory gives the step that minimises
|
||||
the mean square deviation as the SIGNAL FRACTION of the error,
|
||||
|
||||
mu = 1 - p_noise / p_error,
|
||||
|
||||
and both quantities are observable. p_error is the running power of e.
|
||||
p_noise is what e cannot be driven below, so it is estimated as the
|
||||
smallest error power the run has sustained - the floor the filter keeps
|
||||
hitting.
|
||||
|
||||
Nothing here is chosen. The two smoothing weights are 1/2, an average of
|
||||
the current observation with the accumulated one, not a tuned forgetting
|
||||
factor; and the first samples, having no floor to compare against, take
|
||||
the exact step - the licence the root-finding safeguard grants.
|
||||
"""
|
||||
|
||||
name = "measured (no constants)"
|
||||
constants = 0
|
||||
|
||||
def reset(self):
|
||||
self.p_err = None # running error power
|
||||
self.floor = None # the smallest sustained error power seen
|
||||
|
||||
def step(self, e, x, nrm):
|
||||
if self.p_err is None or self.floor is None or self.p_err <= 0.0:
|
||||
return 1.0
|
||||
frac = 1.0 - self.floor / self.p_err
|
||||
return min(1.0, max(0.0, frac))
|
||||
|
||||
def observe(self, e_pre, e_post, mu):
|
||||
p = e_pre * e_pre
|
||||
self.p_err = p if self.p_err is None else 0.5 * (self.p_err + p)
|
||||
if self.floor is None:
|
||||
self.floor = self.p_err
|
||||
elif self.p_err < self.floor:
|
||||
self.floor = self.p_err
|
||||
else:
|
||||
# the floor may rise when the environment changes; let it drift up
|
||||
# by the same averaging, so a regime change is not remembered for
|
||||
# ever as a floor that no longer exists
|
||||
self.floor = 0.5 * (self.floor + min(self.p_err, self.floor * 2.0))
|
||||
|
||||
|
||||
class CosineSimilarity(Rule):
|
||||
"""mu as a SIMILARITY, which is what this project was supposed to be about.
|
||||
|
||||
The two rules above estimate mu from variances - a noise floor, a model
|
||||
discrepancy. Neither is a similarity; both are statistics of the error.
|
||||
The similarity formulation asks a different question:
|
||||
|
||||
how much does the error RESEMBLE something the filter can remove?
|
||||
|
||||
The filter can only remove what is correlated with its input. So the
|
||||
quantity is the cosine similarity between the error and the filtered
|
||||
reference,
|
||||
|
||||
mu = |<e, x>| / (||e|| ||x||) in [0, 1],
|
||||
|
||||
which is the cosine of the original paper, applied to the two signals the
|
||||
filter actually has. It equals 1 when the error is entirely explainable
|
||||
by the reference - the exact step is then right - and 0 when the error is
|
||||
orthogonal to it, which is measurement noise the update can only fit.
|
||||
|
||||
The three running means use the weight 1/2, an average of the current
|
||||
observation with the accumulated one, and the first sample takes the exact
|
||||
step for want of anything to compare. No constant is chosen.
|
||||
"""
|
||||
|
||||
name = "cosine similarity (no constants)"
|
||||
constants = 0
|
||||
S = None # None means the identity: mu = |cos| directly
|
||||
|
||||
def reset(self):
|
||||
self.ex = None # running <e, x>
|
||||
self.ee = None # running <e, e>
|
||||
self.xx = None # running <x, x>
|
||||
|
||||
def step(self, e, x, nrm):
|
||||
# decide from what has been accumulated SO FAR, then fold in the
|
||||
# current sample - so the step never uses the observation it is about
|
||||
# to be judged on
|
||||
if self.ex is None:
|
||||
mu = 1.0
|
||||
else:
|
||||
den = math.sqrt(self.ee * self.xx)
|
||||
if den <= 0.0 or not math.isfinite(den):
|
||||
mu = 1.0
|
||||
else:
|
||||
cos = min(1.0, abs(self.ex) / den)
|
||||
if self.S is None:
|
||||
mu = cos
|
||||
else:
|
||||
# the general family: mu = S(d) with d = 1 - |cos| the
|
||||
# dissimilarity between the error and what the filter can
|
||||
# remove. d = 0 means fully removable, so S(0) = 1 and the
|
||||
# exact step is taken; the identity above is S(d) = 1 - d.
|
||||
mu = max(0.0, min(1.0, self.S(1.0 - cos)))
|
||||
xv = float(x[0])
|
||||
self.ex = e * xv if self.ex is None else 0.5 * (self.ex + e * xv)
|
||||
self.ee = e * e if self.ee is None else 0.5 * (self.ee + e * e)
|
||||
self.xx = xv * xv if self.xx is None else 0.5 * (self.xx + xv * xv)
|
||||
return mu
|
||||
|
||||
|
||||
ALL = [Fixed(0.1), Fixed(0.5), Fixed(1.0), KwongJohnston(),
|
||||
InSampleMeasured(), Measured(), CosineSimilarity()]
|
||||
@@ -0,0 +1,133 @@
|
||||
"""Revision 2: points 2-7 into article/sim_numerics.md.
|
||||
|
||||
2 'pre-registered' -> 'recorded in advance'; Appendix D says the order is attested only by the
|
||||
author's git history
|
||||
3 why Dekker, Illinois and false position show the same totals under both criteria
|
||||
4 exact-zero stops in Table 11, per method
|
||||
5 control K3 (PREREG_estimated_transform): Table 12 and its paragraph; 6.6, conclusion, future work
|
||||
6 Algorithms 1-2: lo/hi, the return on a stall, L_x and L_f, an empty memory, the step pattern
|
||||
as a formula, the O(n^2) search
|
||||
7 Table 3's two missing bracket-scaled rows; dashes in Table 5; the abstract's last sentence
|
||||
Exact anchors, each found exactly once, or nothing is written.
|
||||
|
||||
python -u apply_article_revision2.py
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
PATH = "article/sim_numerics.md"
|
||||
R = []
|
||||
|
||||
|
||||
def E(old, new):
|
||||
R.append((old, new))
|
||||
|
||||
|
||||
# ---- point 2
|
||||
E("The code, the corpora and the pre-registrations are released", "The code, the corpora and the documents that recorded each test before its run are released")
|
||||
E("Every rule below was pre-registered with a stop rule (Appendix D);", "Every rule below was recorded, with its criteria and stop rule, before its code and run (Appendix D);")
|
||||
E("because the pre-registered rule removes only a component", "because the rule recorded in advance removes only a component")
|
||||
E("Pre-registration protects each decision but not the chain:", "Recording each decision in advance protects the decision but not the chain:")
|
||||
E("by every pre-registered criterion (per problem", "by every criterion recorded in advance (per problem")
|
||||
E("Both were pre-registered, with what the paper would say under every outcome, before the run", "Both were recorded, with what the paper would say under every outcome, before the run")
|
||||
E("as the pre-registered criterion required.", "as the criterion recorded in advance required.")
|
||||
E("the pre-registrations with the commits of Appendix D;", "the advance records with the commits of Appendix D;")
|
||||
E("and it passed every pre-registered criterion there.", "and it passed every criterion recorded for it there.")
|
||||
E("was rejected by its own pre-registration,", "was rejected by its own advance record,")
|
||||
E("## Appendix D. Pre-registrations and the frozen code", "## Appendix D. Advance records and the frozen code")
|
||||
E("The code of A′ was committed (`8027d7f`)",
|
||||
"We call these documents advance records, not pre-registrations: they live in the author's repository, and their order relative to the runs is attested only by the author's git history, whose timestamps the author sets; no external registry was used. A deposit with an external timestamp (Zenodo, OSF) before a run would remove that limitation for future tests. The code of A′ was committed (`8027d7f`)")
|
||||
E("**Table D1.** Pre-registrations, in the order they were written.", "**Table D1.** Advance records, in the order they were written.")
|
||||
E("| pre-registration | commit | results file | commit | order |", "| advance record | commit | results file | commit | order |")
|
||||
|
||||
# ---- point 3
|
||||
E("and 79/5/70 against Brent.",
|
||||
"and 79/5/70 against Brent. Dekker, Illinois and false position fail `aps.13.00` under either criterion and so keep their totals: they spend the whole budget without converging, Dekker and Illinois stalled on one side of the zero where $|f|<10^{-62}$ but $f$ does not change sign within $10^{-6}$.")
|
||||
|
||||
# ---- point 4
|
||||
E("With the multiplicity known the transformation is 2.4 to 6.8 times cheaper than A′.",
|
||||
"With the multiplicity known the transformation is 2.4 to 6.8 times cheaper than A′. Every method, A′ included, stops on an exact zero of the function it evaluates, and on the pure powers of the Chandrupatla set $g$ is exactly linear, so the secant lands on the root: 15 of Brent's 20 runs and 4 of Chandrupatla's end that way there (A′: 2). On new corpus 1 no transformed run ends on a zero, and the transformation is still 3.4 times cheaper.")
|
||||
|
||||
# ---- point 5: K3
|
||||
K3 = """
|
||||
|
||||
**The multiplicity estimated during the run.** Between A′ and the oracle lies the natural classical method: Chandrupatla's interpolation on $g=\\operatorname{sign}(f)|f|^{1/\\hat m}$ with $\\hat m$ estimated during the run. Our implementation of Chandrupatla's method (identical to the library on all 199 problems, and to itself when $\\hat m\\equiv1$) keeps the true $f$ at its three points and reads $G(v)=\\operatorname{sign}(v)|v|^{1/\\hat m}$ only in the acceptance band and the interpolation; $\\hat m$ starts at 1 and takes an order read from the last three evaluations whenever that order lies on a branch of §6.1 ($m\\ge2$ or $0<m\\le\\tfrac23$), without reset — the hysteresis of the controls above. The order is read either by Lagouanelle's estimate or by A′'s own analogy (recorded in advance, `sweep_estimated.py`):
|
||||
|
||||
**Table 12.** Chandrupatla on the transformed function: the multiplicity estimated during the run, against A′, the oracle of Table 11 and Chandrupatla on $f$. Totals, failures charged; the 148 multiple and fractional roots of Table 11, and the simple-root canonical set.
|
||||
|
||||
| method | corpus 4 | six corpora | multiple/fractional (148) | APS |
|
||||
|---|---|---|---|---|
|
||||
| **A′** | **3656** | **13032** | 4042 | **2259** |
|
||||
| Chandrupatla on $g$, $\\hat m$ by analogy | 3806 | 15733 | 2809 | 3453 |
|
||||
| Chandrupatla on $g$, $\\hat m$ by Lagouanelle | 7526 | 30991 | 6041 | 7215 |
|
||||
| Chandrupatla on $g$, $m^*$ known (Table 11) | — | — | **1355** | — |
|
||||
| Chandrupatla on $f$ | 5717 | 16481 | 7011 | 2595 |
|
||||
|
||||
A′ is ahead of both estimated forms on corpus 4, in total and problem by problem (against the analogy form 126/38/76, $p=5\\cdot10^{-4}$), and over the six corpora. The two estimators behave differently in this frame. Lagouanelle's reads the parabola through Chandrupatla's points, which alternate sides of the root; there it reports a fractional order on 115 of the 154 simple roots of the canonical set, holds it, and turns the method into little more than bisection. (A variant restricted to the branch $m\\ge2$ was not among the controls recorded.) The analogy's order is the better one here too, and on the multiple and fractional roots it does better inside Chandrupatla's interpolation than inside A′'s split: it closes 59%, 58% and 31% of the gap from A′ to the oracle on the Chandrupatla set, corpus 1 and corpus 4 (322 against 654, 721 against 1229, 1766 against 2159). It pays on simple roots, where it has no exit to take a false reading back (3453 against 2595 on the canonical set). So the decision layer of A′ earns its place against the classical method with an estimated order as well; and the split (7), not the order estimate, is where A′ leaves most to the transformation."""
|
||||
E("large, and the clearest direction for improving the method.", "large, and the clearest direction for improving the method." + K3)
|
||||
E("§7.1 puts this claim against the detectors a reader would try first, and against the transformation with the multiplicity known in advance.",
|
||||
"§7.1 puts this claim against the detectors a reader would try first, and against the transformation with the multiplicity known in advance or estimated during the run.")
|
||||
E("Its decision layer beats Lagouanelle's estimate, Chandrupatla's band and no detector put in its place;",
|
||||
"Its decision layer beats Lagouanelle's estimate, Chandrupatla's band and no detector put in its place, and Chandrupatla's method on the transformed function with the order estimated during the run;")
|
||||
E("the order read from the analogous pair, or its use in the split, loses most of what the transformation could deliver.",
|
||||
"closing that gap is open.")
|
||||
|
||||
# ---- point 6: the algorithms
|
||||
E(" xn <- clamp(xn, hi - allow, lo + allow) # projection, eq (3)",
|
||||
" lo, hi <- min(A,B), max(A,B)\n xn <- clamp(xn, hi - allow, lo + allow) # projection, eq (3)")
|
||||
E(" if xn outside (A,B) or already evaluated: xn <- midpoint(A,B)",
|
||||
" if xn outside (A,B) or already evaluated: xn <- midpoint(A,B)\n (if the midpoint too was evaluated: return it)")
|
||||
E(""" # order (6) of the analogous pair: the past configuration
|
||||
# nearest to c (Euclidean in (xi, phi)), if nearer than the
|
||||
# line phi = xi; inf if the pair shows L_f < 1 <= L_x; else none
|
||||
m <- order of the analogous pair""",
|
||||
""" # order (6) of the analogous pair. q <- the configuration in
|
||||
# configs nearest to c (Euclidean in (xi, phi)); no m if c or
|
||||
# configs is empty or q is not nearer than the line phi = xi.
|
||||
# L_x <- |x-xp| / q.dx, L_f <- |fx-fp| / q.df
|
||||
# m <- inf if L_f < 1 <= L_x (translation), else ln L_f / ln L_x
|
||||
m <- order of the analogous pair""")
|
||||
E(""" # step pattern: the lag 2 .. half the history of highest
|
||||
# mean similarity of successive length ratios has mean > 1/2,
|
||||
# and step and x - xp point the same way
|
||||
if the pattern repeats:""",
|
||||
""" # step pattern: q_j <- |d_j|/|d_(j-1)| over successive nonzero
|
||||
# moves d (k of them); rho(u,v) <- min(u,v)/max(u,v); repeats iff
|
||||
# max over p = 2..floor(k/2) of mean_j rho(q_j, q_(j-p)) > 1/2
|
||||
if repeats and step, x - xp point the same way:""")
|
||||
E(" xn <- clamp(xn, hi - allow, lo + allow) # projection (3)",
|
||||
" lo, hi <- min(A,B), max(A,B)\n xn <- clamp(xn, hi - allow, lo + allow) # projection (3)")
|
||||
E(" xn <- mid; if mid in {x, xp}: stop", " xn <- mid; if mid in {x, xp}: return mid")
|
||||
E("implement exactly this and agree bit for bit.",
|
||||
"implement exactly this and agree bit for bit. The nearest-configuration search runs over the run's memory: $O(n)$ per step, $O(n^2)$ per run of $n$ evaluations ($n\\le200$ here), arithmetic that matters only when $f$ is cheap (Appendix B).")
|
||||
|
||||
# ---- point 7
|
||||
E("| 3 | similarity, $e^{-\\lvert z\\rvert}$, bracket scale, both guards | no | 45/45 | 1778 | 0 |",
|
||||
"| 3 | similarity, $e^{-\\lvert z\\rvert}$, bracket scale, both guards | no | 45/45 | 1778 | 0 |\n| 4 | similarity, $e^{-\\lvert z\\rvert}$, bracket scale, bracket guard | no | 45/45 | 1848 | 0 |")
|
||||
E("| 5 | ITP, recommended constants | yes | 45/45 | 1946 | 0 |",
|
||||
"| 5 | ITP, recommended constants | yes | 45/45 | 1946 | 0 |\n| 6 | similarity, $e^{-\\lvert z\\rvert}$, bracket scale, earned projection | no | 45/45 | 1952 | 0 |")
|
||||
E("| A′, earned budget (no free constant) | 3417 | 1852 | 5269 | | |", "| A′, earned budget (no free constant) | 3417 | 1852 | 5269 | — | — |")
|
||||
E("| base rule (§3) | 2634 | 2357 | 4991 | | |", "| base rule (§3) | 2634 | 2357 | 4991 | — | — |")
|
||||
E("We replace bisection's midpoint rule by a similarity rule — the step grows with how dissimilar $f(x)$ is to the target 0, under a similarity $S$ with $S(0)=1$ — and derive",
|
||||
"We replace bisection's midpoint rule by a similarity rule — the step grows with the dissimilarity of $f(x)$ to 0 under a similarity $S$ — and derive")
|
||||
E("With the multiplicity known, the classical transformation $\\operatorname{sign}(f)|f|^{1/m}$ is 2.4 to 6.8 times cheaper on multiple roots.",
|
||||
"With the multiplicity known, the classical transformation $\\operatorname{sign}(f)|f|^{1/m}$ is 2.4 to 6.8 times cheaper on multiple roots; what A′ adds is an estimate of the order made without knowing it, within a guarantee of twice bisection.")
|
||||
|
||||
|
||||
def main():
|
||||
src = open(PATH, encoding="utf8").read()
|
||||
bad = [o[:80] for o, _ in R if src.count(o) != 1]
|
||||
if bad:
|
||||
for b in bad:
|
||||
print("ANCHOR NOT FOUND EXACTLY ONCE:", repr(b))
|
||||
raise SystemExit(f"{len(bad)} anchors failed; nothing written")
|
||||
for o, n in R:
|
||||
src = src.replace(o, n)
|
||||
open(PATH, "w", encoding="utf8").write(src)
|
||||
ab = src.split("## Abstract\n\n")[1].split("\n\n---\n")[0]
|
||||
code = src.split("**Algorithm 2 (A′).**")[1].split("```")[1]
|
||||
print(f"applied {len(R)}; abstract {len(ab.split())} words; longest Algorithm 2 line "
|
||||
f"{max(len(l) for l in code.split(chr(10)))}; 'pre-regist' left: {src.lower().count('pre-regist')}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,49 @@
|
||||
"""Revision 3 into article/sim_numerics.md:
|
||||
4 the exact-zero stop stated in section 4 (it holds for every method on every corpus)
|
||||
5 Table 12: the post hoc Lagouanelle variant on the branch m >= 2 (marked), the missing exit of
|
||||
the controls in the caption, and the conclusion from Table 12 restated in 7.1, 6.6 and 8
|
||||
Exact anchors, each found exactly once, or nothing is written.
|
||||
|
||||
python -u apply_article_revision3.py
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
PATH = "article/sim_numerics.md"
|
||||
R = []
|
||||
|
||||
|
||||
def E(old, new):
|
||||
R.append((old, new))
|
||||
|
||||
|
||||
E("$x$-tolerance $10^{-12}$; budget 200 evaluations.",
|
||||
"$x$-tolerance $10^{-12}$; budget 200 evaluations. Every method, ours included, stops when its bracket is no wider than $\\varepsilon_x$ or when it evaluates an exact zero of the function it works on; none stops on a small $|f|$.")
|
||||
E("**Table 12.** Chandrupatla on the transformed function: the multiplicity estimated during the run, against A′, the oracle of Table 11 and Chandrupatla on $f$. Totals, failures charged; the 148 multiple and fractional roots of Table 11, and the simple-root canonical set.",
|
||||
"**Table 12.** Chandrupatla on the transformed function: the multiplicity estimated during the run, against A′, the oracle of Table 11 and Chandrupatla on $f$. Totals, failures charged; the 148 multiple and fractional roots of Table 11, and the simple-root canonical set. The estimated forms have no exit: an order once taken is held to the end of the run. A′ leaves its regime by the exit of §6.2.")
|
||||
E("| Chandrupatla on $g$, $\\hat m$ by Lagouanelle | 7526 | 30991 | 6041 | 7215 |",
|
||||
"| Chandrupatla on $g$, $\\hat m$ by Lagouanelle | 7526 | 30991 | 6041 | 7215 |\n| the same, branch $m\\ge2$ only (post hoc) | 5568 | 18372 | 6540 | 2960 |")
|
||||
E("(A variant restricted to the branch $m\\ge2$ was not among the controls recorded.)",
|
||||
"Restricted after the fact to the branch $m\\ge2$ (post hoc, `POSTHOC_lag_branch_2026-09-22.md`), it no longer breaks, but in this frame it hardly sees a multiple root either: 6540 on the 148 multiple and fractional roots against 7011 for Chandrupatla on $f$, and 5568 on corpus 4, with A′ ahead 178/34/28.")
|
||||
E("So the decision layer of A′ earns its place against the classical method with an estimated order as well; and the split (7), not the order estimate, is where A′ leaves most to the transformation.",
|
||||
"Two conclusions follow. First, the order read by analogy transfers to the classical frame and works better there than in A′'s own split: on the multiple and fractional roots Chandrupatla's interpolation with that order beats A′ itself (2809 against 4042). Second, A′'s decision layer as a whole — the detector together with its exit — earns its place: without an exit the transferred detector loses more on simple roots than it gains on multiple ones, and A′ stays ahead on corpus 4 and over the six corpora.")
|
||||
E("§7.1 puts this claim against the detectors a reader would try first, and against the transformation with the multiplicity known in advance or estimated during the run.",
|
||||
"§7.1 tests this against the detectors a reader would try first and against the classical transformation with the multiplicity known or estimated, and the outcome is sharper than the claim: the order read by analogy transfers to the classical frame and works better there than in A′'s own split, while A′'s exit is what keeps simple roots cheap.")
|
||||
E("Its decision layer beats Lagouanelle's estimate, Chandrupatla's band and no detector put in its place, and Chandrupatla's method on the transformed function with the order estimated during the run;",
|
||||
"Its detector, the order read by analogy, beats Lagouanelle's estimate, Chandrupatla's band and no detector put in its place, and it transfers: inside Chandrupatla's interpolation on the transformed function it does better on multiple roots than A′'s own split, while only A′'s exit keeps simple roots cheap;")
|
||||
|
||||
|
||||
def main():
|
||||
src = open(PATH, encoding="utf8").read()
|
||||
bad = [o[:80] for o, _ in R if src.count(o) != 1]
|
||||
if bad:
|
||||
for b in bad:
|
||||
print("ANCHOR NOT FOUND EXACTLY ONCE:", repr(b))
|
||||
raise SystemExit(f"{len(bad)} anchors failed; nothing written")
|
||||
for o, n in R:
|
||||
src = src.replace(o, n)
|
||||
open(PATH, "w", encoding="utf8").write(src)
|
||||
print(f"applied {len(R)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,28 @@
|
||||
"""Revision 2 follow-up: the step-pattern comment of Algorithm 2 within 72 characters a line, and the
|
||||
abstract back under 250 words (same claims).
|
||||
|
||||
python -u apply_revision2_fit.py
|
||||
"""
|
||||
PATH = "article/sim_numerics.md"
|
||||
R = [
|
||||
(""" # step pattern: q_j <- |d_j|/|d_(j-1)| over successive nonzero
|
||||
# moves d (k of them); rho(u,v) <- min(u,v)/max(u,v); repeats iff
|
||||
# max over p = 2..floor(k/2) of mean_j rho(q_j, q_(j-p)) > 1/2""",
|
||||
""" # step pattern: q_j <- |d_j|/|d_(j-1)| over the
|
||||
# successive nonzero moves d (k ratios);
|
||||
# rho(u,v) <- min(u,v)/max(u,v); repeats iff
|
||||
# max_{p=2..floor(k/2)} mean_j rho(q_j, q_(j-p)) > 1/2"""),
|
||||
("The derivation fixes a base rule before any benchmark is consulted:", "It fixes, before any benchmark, a base rule:"),
|
||||
("This decision layer, about ten discrete rules selected on two development sets, gives the method A′.",
|
||||
"This decision layer — about ten discrete rules selected on two development sets — gives A′."),
|
||||
("what A′ adds is an estimate of the order made without knowing it, within a guarantee of twice bisection.",
|
||||
"A′ adds an estimate of the order, within a guarantee of twice bisection."),
|
||||
]
|
||||
src = open(PATH, encoding="utf8").read()
|
||||
for o, n in R:
|
||||
assert src.count(o) == 1, o[:60]
|
||||
src = src.replace(o, n)
|
||||
open(PATH, "w", encoding="utf8").write(src)
|
||||
ab = src.split("## Abstract\n\n")[1].split("\n\n---\n")[0]
|
||||
code = src.split("**Algorithm 2 (A′).**")[1].split("```")[1]
|
||||
print(f"abstract {len(ab.split())} words; longest Algorithm 2 line {max(len(l) for l in code.split(chr(10)))}")
|
||||
@@ -0,0 +1,106 @@
|
||||
"""verify_article.py checks the numbers the second revision added: Table 12 and its paragraph
|
||||
(sweep_estimated.json), the exact-zero stops of Table 11 (diag_revision2.point4, fresh), the three
|
||||
unchanged rows of Table 2 (fresh runs on aps.13.00), the two new rows of Table 3, and the fractional
|
||||
readings of Lagouanelle's estimate on the canonical set (fresh). Adds verify_revision2() and its call.
|
||||
|
||||
python -u apply_verify_revision2.py
|
||||
"""
|
||||
PATH = "verify_article.py"
|
||||
FUNC = '''
|
||||
|
||||
def verify_revision2():
|
||||
"""Revision 2 (points 3, 4, 5, 7)."""
|
||||
import math
|
||||
print("\\nRevision 2:")
|
||||
|
||||
def cost(v):
|
||||
return v[0] if v[1] else 200
|
||||
|
||||
def tot(r, keep=None):
|
||||
return sum(cost(v) for p, v in r.items() if keep is None or keep(p))
|
||||
|
||||
def sign_p(w, l):
|
||||
k, n = min(w, l), w + l
|
||||
return min(1.0, 2.0 * sum(math.comb(n, i) for i in range(k + 1)) / 2.0 ** n)
|
||||
sets = ("APS", "CHA", "NEW", "RJL", "APP", "D")
|
||||
K = _load("sweep_estimated.json")
|
||||
seen, four = _load("sweep_fourth_seen.json"), _load("sweep_fourth_fourth.json")
|
||||
ap, ch = dict(seen["Aprime"]), dict(seen["chandrupatla"])
|
||||
ap.update(four["Aprime"])
|
||||
ch.update(four["chandrupatla"])
|
||||
K1 = _load("sweep_controls.json")["K1"]
|
||||
nonsimple = lambda m: m >= 2.0 or m <= 2.0 / 3.0 # noqa: E731
|
||||
|
||||
def group(r):
|
||||
return sum(tot(r[s], lambda p, _m=K1[s]["mstar"]: nonsimple(_m[p])) for s in ("CHA", "NEW", "D"))
|
||||
rows = {"A'": ap, "K3A": K["k3_analogy"], "K3L": K["k3_lag"], "Ch(f)": ch}
|
||||
want = {"A'": (3656, 13032, 4042, 2259), "K3A": (3806, 15733, 2809, 3453),
|
||||
"K3L": (7526, 30991, 6041, 7215), "Ch(f)": (5717, 16481, 7011, 2595)}
|
||||
for k, r in rows.items():
|
||||
check(f"Table 12 {k}", (tot(r["D"]), sum(tot(r[s]) for s in sets), group(r), tot(r["APS"])), want[k])
|
||||
oracle = sum(tot(K1[s]["chandrupatla_g"], lambda p, _m=K1[s]["mstar"]: nonsimple(_m[p]))
|
||||
for s in ("CHA", "NEW", "D"))
|
||||
check("Table 12 oracle row (Table 11 sum)", oracle, 1355)
|
||||
ps = list(ap["D"])
|
||||
w = sum(cost(ap["D"][p]) < cost(K["k3_analogy"]["D"][p]) for p in ps)
|
||||
l = sum(cost(ap["D"][p]) > cost(K["k3_analogy"]["D"][p]) for p in ps)
|
||||
check("A' vs Chandrupatla-on-g with the analogy order, corpus 4", (w, len(ps) - w - l, l), (126, 38, 76))
|
||||
check("its p (1 digit)", f"{sign_p(w, l):.1g}", "0.0005")
|
||||
shares = []
|
||||
for s in ("CHA", "NEW", "D"):
|
||||
keep = lambda p, _m=K1[s]["mstar"]: nonsimple(_m[p]) # noqa: E731
|
||||
a_, k_, o_ = tot(ap[s], keep), tot(K["k3_analogy"][s], keep), tot(K1[s]["chandrupatla_g"], keep)
|
||||
shares.append((k_, a_, round(100 * (a_ - k_) / (a_ - o_))))
|
||||
check("share of the A'-to-oracle gap closed by the analogy order", shares,
|
||||
[(322, 654, 59), (721, 1229, 58), (1766, 2159, 31)])
|
||||
check("K3 bound violations", sum(v[2] for k in K for s in sets for v in K[k][s].values()), 0)
|
||||
# Lagouanelle's fractional readings on the simple roots of the canonical set (fresh)
|
||||
from simroot.transformed import chandrupatla_transformed
|
||||
from sweep_full import load_problems
|
||||
frac = 0
|
||||
for s, pid, f, a, b, _r in load_problems():
|
||||
if s != "APS":
|
||||
continue
|
||||
st = {}
|
||||
chandrupatla_transformed(f, a, b, 1e-12, 200, estimator="lag", stats=st)
|
||||
m = st.get("m_hat", 1.0)
|
||||
frac += (0.0 < m <= 2.0 / 3.0)
|
||||
check("Lagouanelle holds a fractional order on simple APS roots", frac, 115)
|
||||
# point 4: exact-zero stops (fresh)
|
||||
import contextlib
|
||||
import io
|
||||
import diag_revision2
|
||||
with contextlib.redirect_stdout(io.StringIO()):
|
||||
z = diag_revision2.point4()
|
||||
check("exact-zero stops on CHA: Brent(g), Chandrupatla(g), A'",
|
||||
(z["CHA"]["brent_g"][1], z["CHA"]["chandrupatla_g"][1], z["CHA"]["Aprime"][1]), (15, 4, 2))
|
||||
check("exact-zero stops of the transformed runs on corpus 1",
|
||||
(z["NEW"]["chandrupatla_g"][1], z["NEW"]["brent_g"][1]), (0, 0))
|
||||
check("corpus 1: transformation 3.4 times cheaper", round(1229 / 358, 1), 3.4)
|
||||
# point 3: the three unchanged rows of Table 2 (fresh)
|
||||
from corpus_roots_jl import solved
|
||||
from sweep_full import make_method
|
||||
f, a, b = next((f, a, b) for s, pid, f, a, b, _r in load_problems() if pid == "aps.13.00")
|
||||
got = []
|
||||
for name in ("dekker", "illinois", "regula_falsi"):
|
||||
x, n, conv = make_method(("base", name))(f, a, b, 1e-12, 200)
|
||||
got.append((name, int(n), bool(conv), bool(solved(f, float(x)))))
|
||||
check("aps.13.00: Dekker, Illinois, false position spend the budget unsolved",
|
||||
got, [("dekker", 200, False, False), ("illinois", 200, False, False),
|
||||
("regula_falsi", 200, False, False)])
|
||||
small = [abs(float(f(float(make_method(("base", nm))(f, a, b, 1e-12, 200)[0])))) < 1e-62
|
||||
for nm in ("dekker", "illinois")]
|
||||
check("Dekker and Illinois stall where |f| < 1e-62", small, [True, True])
|
||||
# point 7: Table 3's two added rows
|
||||
full = {r["label"]: r for r in _load("sweep_full.json")}
|
||||
for lb, want_ in (("pure.exp_abs.bracket.bracket", (1848, 45, 0)), ("pure.exp_abs.bracket.credit", (1952, 45, 0))):
|
||||
c = full[lb]["CHA"]
|
||||
check(f"Table 3 row {lb}", (c["total"], c["solved"], c["over"]), want_)
|
||||
'''
|
||||
src = open(PATH, encoding="utf8").read()
|
||||
call_old = " verify_controls()\n\n print()"
|
||||
marker = '\n\nif __name__ == "__main__":\n main()\n'
|
||||
assert src.count(call_old) == 1 and src.count(marker) == 1
|
||||
src = src.replace(call_old, " verify_controls()\n verify_revision2()\n\n print()").replace(marker, FUNC + marker)
|
||||
open(PATH, "w", encoding="utf8").write(src)
|
||||
print("added verify_revision2")
|
||||
@@ -0,0 +1,48 @@
|
||||
"""verify_article.py checks the round-3 additions: the post hoc row of Table 12
|
||||
(sweep_estimated_posthoc.json), A' against it on corpus 4, its fractional-free holds, and the
|
||||
statement that the transferred detector loses more on simple roots than it gains on multiple ones.
|
||||
|
||||
python -u apply_verify_revision3.py
|
||||
"""
|
||||
PATH = "verify_article.py"
|
||||
FUNC = '''
|
||||
|
||||
def verify_revision3():
|
||||
"""Revision 3 (point 5): the post hoc branch m >= 2 and the reading of Table 12."""
|
||||
print("\\nRevision 3:")
|
||||
|
||||
def cost(v):
|
||||
return v[0] if v[1] else 200
|
||||
|
||||
def tot(r, keep=None):
|
||||
return sum(cost(v) for p, v in r.items() if keep is None or keep(p))
|
||||
sets = ("APS", "CHA", "NEW", "RJL", "APP", "D")
|
||||
P = _load("sweep_estimated_posthoc.json")
|
||||
K = _load("sweep_estimated.json")["k3_analogy"]
|
||||
seen, four = _load("sweep_fourth_seen.json"), _load("sweep_fourth_fourth.json")
|
||||
ap = dict(seen["Aprime"])
|
||||
ap.update(four["Aprime"])
|
||||
K1 = _load("sweep_controls.json")["K1"]
|
||||
nonsimple = lambda m: m >= 2.0 or m <= 2.0 / 3.0 # noqa: E731
|
||||
|
||||
def group(r):
|
||||
return sum(tot(r[s], lambda p, _m=K1[s]["mstar"]: nonsimple(_m[p])) for s in ("CHA", "NEW", "D"))
|
||||
check("Table 12 post hoc row", (tot(P["D"]), sum(tot(P[s]) for s in sets), group(P), tot(P["APS"])),
|
||||
(5568, 18372, 6540, 2960))
|
||||
ps = list(ap["D"])
|
||||
w = sum(cost(ap["D"][p]) < cost(P["D"][p]) for p in ps)
|
||||
l = sum(cost(ap["D"][p]) > cost(P["D"][p]) for p in ps)
|
||||
check("A' vs the post hoc variant on corpus 4", (w, len(ps) - w - l, l), (178, 34, 28))
|
||||
check("post hoc violations", sum(v[2] for s in sets for v in P[s].values()), 0)
|
||||
gain = group(ap) - group(K)
|
||||
loss = (sum(tot(K[s]) for s in sets) - group(K)) - (sum(tot(ap[s]) for s in sets) - group(ap))
|
||||
check("transferred detector: gain on multiple roots < loss elsewhere (vs A')", (gain, loss, gain < loss),
|
||||
(1233, 3934, True))
|
||||
'''
|
||||
src = open(PATH, encoding="utf8").read()
|
||||
call_old = " verify_revision2()\n\n print()"
|
||||
marker = '\n\nif __name__ == "__main__":\n main()\n'
|
||||
assert src.count(call_old) == 1 and src.count(marker) == 1
|
||||
src = src.replace(call_old, " verify_revision2()\n verify_revision3()\n\n print()").replace(marker, FUNC + marker)
|
||||
open(PATH, "w", encoding="utf8").write(src)
|
||||
print("added verify_revision3")
|
||||
+55
-32
@@ -4,7 +4,7 @@ V. Sitnikov
|
||||
|
||||
## Abstract
|
||||
|
||||
We replace bisection's midpoint rule by a similarity rule — the step grows with how dissimilar $f(x)$ is to the target 0, under a similarity $S$ with $S(0)=1$ — and derive its contraction factor in closed form. The derivation fixes a base rule before any benchmark is consulted: with the tent similarity and a three-point divided-difference scale it is a Newton step of Muller's order 1.839, clipped to the bracket and projected onto a budget of twice bisection whose multiplier is its one numerical constant. On the 154 problems of Alefeld, Potra and Shi it is level with TOMS 748 and 1.5% behind Chandrupatla's method; on wide brackets it is clearly behind. The gap is a decision, when to give the model up, and we take it by analogy: at a flat root the dimensionless configuration of the last three points recurs under zoom, at a simple root it tends to a line, and the analogous pair gives the order that says whether and where to split. This decision layer, about ten discrete rules selected on two development sets, gives the method A′. On 240 problems fixed before its code was frozen it needs 3656 evaluations against 5717 for Chandrupatla, 5535 for ModAB and 4488 with Lagouanelle's multiplicity estimate in place of the analogy; the lead lies in multiple and fractional roots, present in two of the six families. With the multiplicity known, the classical transformation $\operatorname{sign}(f)|f|^{1/m}$ is 2.4 to 6.8 times cheaper on multiple roots.
|
||||
We replace bisection's midpoint rule by a similarity rule — the step grows with the dissimilarity of $f(x)$ to 0 under a similarity $S$ — and derive its contraction factor in closed form. It fixes, before any benchmark, a base rule: with the tent similarity and a three-point divided-difference scale it is a Newton step of Muller's order 1.839, clipped to the bracket and projected onto a budget of twice bisection whose multiplier is its one numerical constant. On the 154 problems of Alefeld, Potra and Shi it is level with TOMS 748 and 1.5% behind Chandrupatla's method; on wide brackets it is clearly behind. The gap is a decision, when to give the model up, and we take it by analogy: at a flat root the dimensionless configuration of the last three points recurs under zoom, at a simple root it tends to a line, and the analogous pair gives the order that says whether and where to split. This decision layer — about ten discrete rules selected on two development sets — gives A′. On 240 problems fixed before its code was frozen it needs 3656 evaluations against 5717 for Chandrupatla, 5535 for ModAB and 4488 with Lagouanelle's multiplicity estimate in place of the analogy; the lead lies in multiple and fractional roots, present in two of six families. With the multiplicity known, the transformation $\operatorname{sign}(f)|f|^{1/m}$ is 2.4 to 6.8 times cheaper on multiple roots; A′ adds an estimate of the order, within a guarantee of twice bisection.
|
||||
|
||||
---
|
||||
|
||||
@@ -21,7 +21,7 @@ The paper makes four claims, each measured.
|
||||
3. The base rule's gap on wide brackets is a decision — when to give the model up — and a similarity of the run to its own past takes it (§6). The resulting method, A′, is tested once on a corpus fixed before its code was frozen, against simpler detectors put in its place, and against the classical transformation with the multiplicity known in advance (§7).
|
||||
4. Every table uses one criterion, a sign change within $10^{-6}$ of the answer, because the benchmarks' published roots score six problems unsolved for every method (§4, §A.5).
|
||||
|
||||
The search that produced these results — readings of the similarity that did not pay, the decomposition of the wide-bracket gap, wall-clock time, and the rejected rules — is reported in Appendices A–C. The code, the corpora and the pre-registrations are released (Appendix D and the availability statement).
|
||||
The search that produced these results — readings of the similarity that did not pay, the decomposition of the wide-bracket gap, wall-clock time, and the rejected rules — is reported in Appendices A–C. The code, the corpora and the documents that recorded each test before its run are released (Appendix D and the availability statement).
|
||||
|
||||
---
|
||||
|
||||
@@ -116,8 +116,10 @@ while |B - A| > xtol:
|
||||
if sigma is 0 or not finite: xn <- midpoint(A,B)
|
||||
else:
|
||||
xn <- x + min(1, |f(x)|/sigma) * (far - x) # the step, eq (4)
|
||||
lo, hi <- min(A,B), max(A,B)
|
||||
xn <- clamp(xn, hi - allow, lo + allow) # projection, eq (3)
|
||||
if xn outside (A,B) or already evaluated: xn <- midpoint(A,B)
|
||||
(if the midpoint too was evaluated: return it)
|
||||
evaluate fn = f(xn); allow <- allow / 2
|
||||
xp2, xp, x <- xp, x, xn
|
||||
update the bracket: whichever of A,B keeps the sign change
|
||||
@@ -181,7 +183,7 @@ Three remarks apply to both forms.
|
||||
|
||||
**Problem sets.** The 154 problems of Alefeld, Potra and Shi [1] and the 45 of Chandrupatla [3], taken programmatically from SciPy's `scipy.optimize._tstutils` so that no bracket or root is transcribed by hand.
|
||||
|
||||
**Cost model.** Evaluations of $f$. A run counts as solved when it reports convergence and $f$ changes sign within $10^{-6}$ of the returned point, or vanishes there — the bracketing notion of a root, the same criterion on every corpus of this paper; $x$-tolerance $10^{-12}$; budget 200 evaluations. Scoring against the published root instead, as the benchmarks' harness does, charges every method the full budget on six problems where $f$ vanishes identically around the root in floating point (§A.5). Head-to-head counts (wins/ties/losses) are taken over every problem of a set with the same charging as the totals. *Failures are charged the full budget*, so no method can buy a low total by giving up early, and totals are reported over all problems rather than over the subset a method happened to solve. Rankings are by failures first, then by total.
|
||||
**Cost model.** Evaluations of $f$. A run counts as solved when it reports convergence and $f$ changes sign within $10^{-6}$ of the returned point, or vanishes there — the bracketing notion of a root, the same criterion on every corpus of this paper; $x$-tolerance $10^{-12}$; budget 200 evaluations. Every method, ours included, stops when its bracket is no wider than $\varepsilon_x$ or when it evaluates an exact zero of the function it works on; none stops on a small $|f|$. Scoring against the published root instead, as the benchmarks' harness does, charges every method the full budget on six problems where $f$ vanishes identically around the root in floating point (§A.5). Head-to-head counts (wins/ties/losses) are taken over every problem of a set with the same charging as the totals. *Failures are charged the full budget*, so no method can buy a low total by giving up early, and totals are reported over all problems rather than over the subset a method happened to solve. Rankings are by failures first, then by total.
|
||||
|
||||
**Competitors.** Where SciPy ships the method, the competitor is the library — Chandrupatla is `scipy.optimize.elementwise.find_root`, Brent `brentq`, TOMS 748 `toms748` — run with the stopping rule of every method (a bracket no wider than $\varepsilon_x$, the relative term at machine precision, no stopping on $|f|$) and counted by the same wrapper around $f$. ITP, which has no library implementation, is ours, run with $\epsilon=\varepsilon_x/2$ because it terminates on $b-a\le2\epsilon$, checked against its own theorem ($n_{1/2}+n_0$ iterations at most, on all 154 problems for $n_0\in\{0,1,2,5\}$), and reported both at the authors' constants and as a *per-problem oracle* over a $(\kappa_1,n_0)$ grid — not an achievable method, included so that it cannot be said to have been handicapped. Bisection and the false-position family are ours.
|
||||
|
||||
@@ -211,7 +213,7 @@ The grid is nine similarity functions × four scales × seven safeguards = 252 c
|
||||
| 237 | Illinois [7] | yes | 153/154 | 7045 | 0 |
|
||||
| 244 | false position | no | 127/154 | 10560 | — |
|
||||
|
||||
The base rule and the same rule without any safeguard tie at 2634: the projection never binds on this set. By total, Chandrupatla's method is first with 2595, 1.5% fewer; the base rule and TOMS 748 are level (2634 against 2636). Problem by problem the base rule wins against Chandrupatla 87/24/43 (wins/ties/losses; exact two-sided sign test $p=1.4\cdot10^{-4}$), against TOMS 748 86/16/52, Brent 102/11/41, ITP at its recommended constants 132/3/19, the per-problem ITP oracle 121/4/29, and bisection 152/1/1. It trails Chandrupatla in total because its losses are the larger ones; by function family, the unit in which these problems are independent (§7), the comparison is 6/1/8 of 15 families, a tie. The constant-free variant goes 79/3/72 against Chandrupatla ($p=0.63$), 78/9/67 against TOMS 748 and 79/5/70 against Brent.
|
||||
The base rule and the same rule without any safeguard tie at 2634: the projection never binds on this set. By total, Chandrupatla's method is first with 2595, 1.5% fewer; the base rule and TOMS 748 are level (2634 against 2636). Problem by problem the base rule wins against Chandrupatla 87/24/43 (wins/ties/losses; exact two-sided sign test $p=1.4\cdot10^{-4}$), against TOMS 748 86/16/52, Brent 102/11/41, ITP at its recommended constants 132/3/19, the per-problem ITP oracle 121/4/29, and bisection 152/1/1. It trails Chandrupatla in total because its losses are the larger ones; by function family, the unit in which these problems are independent (§7), the comparison is 6/1/8 of 15 families, a tie. The constant-free variant goes 79/3/72 against Chandrupatla ($p=0.63$), 78/9/67 against TOMS 748 and 79/5/70 against Brent. Dekker, Illinois and false position fail `aps.13.00` under either criterion and so keep their totals: they spend the whole budget without converging, Dekker and Illinois stalled on one side of the zero where $|f|<10^{-62}$ but $f$ does not change sign within $10^{-6}$.
|
||||
|
||||
**Table 3.** Chandrupatla, 45 problems, brackets up to $10^{12}$ wide, out of 264 configurations.
|
||||
|
||||
@@ -220,7 +222,9 @@ The base rule and the same rule without any safeguard tie at 2634: the projectio
|
||||
| 1 | Chandrupatla | no | 45/45 | 1505 | 0 |
|
||||
| 2 | ITP, per-problem oracle | yes | 45/45 | 1675 | 0 |
|
||||
| 3 | similarity, $e^{-\lvert z\rvert}$, bracket scale, both guards | no | 45/45 | 1778 | 0 |
|
||||
| 4 | similarity, $e^{-\lvert z\rvert}$, bracket scale, bracket guard | no | 45/45 | 1848 | 0 |
|
||||
| 5 | ITP, recommended constants | yes | 45/45 | 1946 | 0 |
|
||||
| 6 | similarity, $e^{-\lvert z\rvert}$, bracket scale, earned projection | no | 45/45 | 1952 | 0 |
|
||||
| 9 | constant-free variant: earned projection | no | 45/45 | 2073 | 0 |
|
||||
| 15 | bisection | no | 45/45 | 2136 | 0 |
|
||||
| **49** | **base rule: tent, 3-point, projection ($c=2$)** | **no** | 45/45 | **2357** | 0 |
|
||||
@@ -253,7 +257,7 @@ Ranked individually and best over scales, the similarities come out $1-|z|$ (542
|
||||
|
||||
## 6. The flat regime, recognised by analogy
|
||||
|
||||
The base rule's gap on wide brackets is a decision, *when to give the model up* (§A.6): on the pure powers `fun3`–`fun6` the right move is to halve, Chandrupatla's method halves on up to all of its steps, and no reading of a single state measured takes that decision correctly. This section takes it by a similarity of the run to *its own past* — an analogy in Pólya's sense [8], two situations agreeing in more than one relation. Every rule below was pre-registered with a stop rule (Appendix D); the rejected ones are in Appendix C.
|
||||
The base rule's gap on wide brackets is a decision, *when to give the model up* (§A.6): on the pure powers `fun3`–`fun6` the right move is to halve, Chandrupatla's method halves on up to all of its steps, and no reading of a single state measured takes that decision correctly. This section takes it by a similarity of the run to *its own past* — an analogy in Pólya's sense [8], two situations agreeing in more than one relation. Every rule below was recorded, with its criteria and stop rule, before its code and run (Appendix D); the rejected ones are in Appendix C.
|
||||
|
||||
### 6.1 The flag: a promise that was not kept
|
||||
|
||||
@@ -305,7 +309,7 @@ which is exact for a pure power and becomes the midpoint as $m\to\infty$. It is
|
||||
|
||||
The method this paper proposes, **A′**, is the base rule of §3 — tent similarity, three-point scale, projection with the declared multiplier $c=2$ — with the regime of §6.2–6.3 (the analogy remembered, its translation limit, the split (7), the exit) and the five readings of §6.4. Its constants are bisection's $\tfrac12$ and what follows from it: $\tfrac14=(1-\tfrac12)^2$ and the two branches $m\ge2$, $m\le\tfrac23$ of $|1-1/m|>\tfrac12$. Three choices are declared rather than derived: the multiplier $c$ of the projection (§3.1), the Euclidean geometry of the $(\xi,\varphi)$ plane, and the pattern test of the step lengthening (lags from 2 to half the history, threshold $\tfrac12$). The projection carries the guarantee, $2\lceil\log_2(w_0/\varepsilon_x)\rceil+3$ evaluations; it is verified with no violation on every corpus of this paper and under every safeguard of the grid.
|
||||
|
||||
Counting numerical constants does not measure the method's freedom, however. Beyond these numbers A′ is a decision layer of about ten discrete rules — the flag, the analogy and its memory, the entry, the exit, the overshoot branch, the split, the step pattern, the half, the contradiction, the tolerance step — chosen on the two development sets among the many candidates of Appendix C. That selection is why the method was tested once on a corpus fixed before its code was frozen, and against simpler rules put in its place (§7). Algorithm 2 is the complete method; `simroot/pure.py` and the compiled `simbracket` implement exactly this and agree bit for bit.
|
||||
Counting numerical constants does not measure the method's freedom, however. Beyond these numbers A′ is a decision layer of about ten discrete rules — the flag, the analogy and its memory, the entry, the exit, the overshoot branch, the split, the step pattern, the half, the contradiction, the tolerance step — chosen on the two development sets among the many candidates of Appendix C. That selection is why the method was tested once on a corpus fixed before its code was frozen, and against simpler rules put in its place (§7). Algorithm 2 is the complete method; `simroot/pure.py` and the compiled `simbracket` implement exactly this and agree bit for bit. The nearest-configuration search runs over the run's memory: $O(n)$ per step, $O(n^2)$ per run of $n$ evaluations ($n\le200$ here), arithmetic that matters only when $f$ is cheap (Appendix B).
|
||||
|
||||
**Algorithm 2 (A′).** Notation of Algorithm 1; $S(z)=\max(0,1-|z|)$.
|
||||
|
||||
@@ -327,9 +331,11 @@ while |B - A| > xtol:
|
||||
# unkept promise, and unkept promise with a crossing
|
||||
flag <- not in_regime and last_model and fx/fp >= 1/4
|
||||
flag_over <- not in_regime and last_model and fx/fp <= -1/4
|
||||
# order (6) of the analogous pair: the past configuration
|
||||
# nearest to c (Euclidean in (xi, phi)), if nearer than the
|
||||
# line phi = xi; inf if the pair shows L_f < 1 <= L_x; else none
|
||||
# order (6) of the analogous pair. q <- the configuration in
|
||||
# configs nearest to c (Euclidean in (xi, phi)); no m if c or
|
||||
# configs is empty or q is not nearer than the line phi = xi.
|
||||
# L_x <- |x-xp| / q.dx, L_f <- |fx-fp| / q.df
|
||||
# m <- inf if L_f < 1 <= L_x (translation), else ln L_f / ln L_x
|
||||
m <- order of the analogous pair
|
||||
if m >= 2: analogy_seen <- true; m_a <- m
|
||||
if 0 < m <= 2/3: shallow_seen <- true; m_s <- m
|
||||
@@ -343,18 +349,20 @@ while |B - A| > xtol:
|
||||
else:
|
||||
step <- (1 - S(z)) * (far - x) # eq. (4)
|
||||
append x - xp to moves
|
||||
# step pattern: the lag 2 .. half the history of highest
|
||||
# mean similarity of successive length ratios has mean > 1/2,
|
||||
# and step and x - xp point the same way
|
||||
if the pattern repeats:
|
||||
# step pattern: q_j <- |d_j|/|d_(j-1)| over the
|
||||
# successive nonzero moves d (k ratios);
|
||||
# rho(u,v) <- min(u,v)/max(u,v); repeats iff
|
||||
# max_{p=2..floor(k/2)} mean_j rho(q_j, q_(j-p)) > 1/2
|
||||
if repeats and step, x - xp point the same way:
|
||||
s <- min(|step|,|x-xp|) / max(|step|,|x-xp|)
|
||||
step <- step / (1 - s), at most |far - x| long
|
||||
if |step| < xtol/2 and last_model and |fx/fp| < 1/4:
|
||||
step <- sign(far - x) * xtol/2 # tolerance step
|
||||
xn <- x + step; model <- true
|
||||
lo, hi <- min(A,B), max(A,B)
|
||||
xn <- clamp(xn, hi - allow, lo + allow) # projection (3)
|
||||
if xn outside (A,B) or xn in {x, xp}:
|
||||
xn <- mid; if mid in {x, xp}: stop
|
||||
xn <- mid; if mid in {x, xp}: return mid
|
||||
last_model <- model and xn is the model's own point
|
||||
fn <- f(xn)
|
||||
if in_regime: # exit
|
||||
@@ -371,8 +379,8 @@ return midpoint(A,B)
|
||||
| | APS-154 | CHA-45 | both | A′ per problem, APS | A′ per problem, CHA |
|
||||
|---|---|---|---|---|---|
|
||||
| **A′ (proposed)** | **2259** | **1074** | **3333** | — | — |
|
||||
| A′, earned budget (no free constant) | 3417 | 1852 | 5269 | | |
|
||||
| base rule (§3) | 2634 | 2357 | 4991 | | |
|
||||
| A′, earned budget (no free constant) | 3417 | 1852 | 5269 | — | — |
|
||||
| base rule (§3) | 2634 | 2357 | 4991 | — | — |
|
||||
| Chandrupatla | 2595 | 1505 | 4100 | 103/20/31, $p=3\cdot10^{-10}$ | 31/4/10, $p=0.001$ |
|
||||
| TOMS 748 | 2636 | 3088 | 5724 | 127/6/21 | 42/2/1 |
|
||||
| Brent | 2707 | 3238 | 5945 | 118/17/19 | 39/3/3 |
|
||||
@@ -394,7 +402,7 @@ A′ is 12.9% below Chandrupatla on the canonical set and 28.6% below on the wid
|
||||
| repeated step pattern | −22 | −6 | +123 | −30 | −10 | +34 | +89 |
|
||||
| sign contradiction | +11 | −12 | +29 | 0 | +1 | −1 | +28 |
|
||||
|
||||
Every component is positive in sum; the regime and the split carry the method. The step pattern (earns on two corpora of six) and the sign contradiction (+28 over 855 problems) are marginal — together under one per cent, 117 of 13032 evaluations — and are kept because the pre-registered rule removes only a component that earns nowhere.
|
||||
Every component is positive in sum; the regime and the split carry the method. The step pattern (earns on two corpora of six) and the sign contradiction (+28 over 855 problems) are marginal — together under one per cent, 117 of 13032 evaluations — and are kept because the rule recorded in advance removes only a component that earns nowhere.
|
||||
|
||||
**Table 7.** The similarity in the step of A′: the same method with each of the nine similarity functions of §5.2 in place of the tent, everything else unchanged. Totals on the two sets of §4 and over the six corpora of §7; the guarantee holds with every function.
|
||||
|
||||
@@ -427,13 +435,13 @@ Several mechanisms of A′ coincide with classical ones, reached from similarity
|
||||
| the tolerance step | Dekker [9], Brent [2] — borrowed and named as such |
|
||||
| the exit judgment of §6.2 (the value at a split against the line) | ModAB's linearity check at the midpoint [10, 20], $|y_m-y_3|<k\,(|y_3|+|y_m|)$ with $k$ from a symmetry factor; ours compares two predictions, the line and 0, without a factor |
|
||||
|
||||
What we found no counterpart for, in the literature we could reach, is the part that carries the gain in Table 6: the *decision* layer. A bracketing method that recognises the regime in which its model has lost by the similarity of the run to its own past — the recurrence of a dimensionless configuration against the linear prototype — reads the order from the analogous pair itself, distinguishes similarity by scaling from similarity by translation, and leaves the regime by a similarity judgment at a point the model did not choose; and a step whose length is set by a similarity function whose form, measured, is decisive. The methods for multiple roots we found are single-point iterations without a bracket, with the multiplicity assumed, estimated from derivatives, or removed by a transformation ([17, 23]; Roots.jl's `Order1B` and `Order2B` [12]); inside a bracket, [22] needs the multiplicity given and recognises a multiple root only by the number of evaluations spent, against a third of bisection's; Chandrupatla takes the decision to halve by an empirical acceptance band [3]. §7.1 puts this claim against the detectors a reader would try first, and against the transformation with the multiplicity known in advance.
|
||||
What we found no counterpart for, in the literature we could reach, is the part that carries the gain in Table 6: the *decision* layer. A bracketing method that recognises the regime in which its model has lost by the similarity of the run to its own past — the recurrence of a dimensionless configuration against the linear prototype — reads the order from the analogous pair itself, distinguishes similarity by scaling from similarity by translation, and leaves the regime by a similarity judgment at a point the model did not choose; and a step whose length is set by a similarity function whose form, measured, is decisive. The methods for multiple roots we found are single-point iterations without a bracket, with the multiplicity assumed, estimated from derivatives, or removed by a transformation ([17, 23]; Roots.jl's `Order1B` and `Order2B` [12]); inside a bracket, [22] needs the multiplicity given and recognises a multiple root only by the number of evaluations spent, against a third of bisection's; Chandrupatla takes the decision to halve by an empirical acceptance band [3]. §7.1 tests this against the detectors a reader would try first and against the classical transformation with the multiplicity known or estimated, and the outcome is sharper than the claim: the order read by analogy transfers to the classical frame and works better there than in A′'s own split, while A′'s exit is what keeps simple roots cheap.
|
||||
|
||||
---
|
||||
|
||||
## 7. Out-of-sample evidence
|
||||
|
||||
Every decision above was taken on the two sets of §4. Pre-registration protects each decision but not the chain: the final configuration — a decision layer of about ten discrete rules, chosen among the many candidates listed in Appendix C — was selected on the same 199 problems. Four further corpora were therefore used, each fixed and committed before any method was run on it, each with its criteria and stop rule written down first.
|
||||
Every decision above was taken on the two sets of §4. Recording each decision in advance protects the decision but not the chain: the final configuration — a decision layer of about ten discrete rules, chosen among the many candidates listed in Appendix C — was selected on the same 199 problems. Four further corpora were therefore used, each fixed and committed before any method was run on it, each with its criteria and stop rule written down first.
|
||||
|
||||
1. **New corpus 1** — 206 problems: SciPy's `original` set (6) and five generated classes of 40 (seed 20260921): smooth and transcendental simple roots, multiple roots $|x-r|^p$ with $p\sim U(2,20)$, exponential flanks, near-cancelling terms.
|
||||
2. **Roots.jl / ModAB** — the bracketing benchmark of Roots.jl [12] used by ModAB's authors [10]: 91 problems, 13 of which coincide with APS or Chandrupatla functions and are excluded, leaving 78.
|
||||
@@ -444,7 +452,7 @@ A root must carry a sign change to be bracketed, so the multiple roots in these
|
||||
|
||||
ModAB, the most recent bracketing method we know of, is run from its authors' package (`pymodab` 1.0.9) with the stopping rule and the counting of every other method. The criterion, tolerance, budget and charging are those of §4.
|
||||
|
||||
The variant carried to corpora 1–3 was A′ without the tolerance step and the overshoot branch. It transferred to corpus 1 by every pre-registered criterion (per problem 105/34/67 against Chandrupatla, $p=0.005$), did *not* transfer to Roots.jl (38/10/30, $p=0.4$) and lost on the applied corpus; the two readings that closed those gaps were developed on the seen corpora and tested once, on corpus 4.
|
||||
The variant carried to corpora 1–3 was A′ without the tolerance step and the overshoot branch. It transferred to corpus 1 by every criterion recorded in advance (per problem 105/34/67 against Chandrupatla, $p=0.005$), did *not* transfer to Roots.jl (38/10/30, $p=0.4$) and lost on the applied corpus; the two readings that closed those gaps were developed on the seen corpora and tested once, on corpus 4.
|
||||
|
||||
**Table 8.** A′ against the library rivals on all six corpora (uniform criterion): total evaluations; in brackets the runs that break the $2\times$-bisection bound.
|
||||
|
||||
@@ -488,7 +496,7 @@ Only the last line of corpora is evidence for A′ in the strict sense; the firs
|
||||
|
||||
### 7.1 Controls: simpler detectors, and the multiplicity known in advance
|
||||
|
||||
Two controls ask whether the gain of §6 comes from the recognition by analogy or only from the split (7) with any reasonable detector. Both were pre-registered, with what the paper would say under every outcome, before the run (`sweep_controls.py`).
|
||||
Two controls ask whether the gain of §6 comes from the recognition by analogy or only from the split (7) with any reasonable detector. Both were recorded, with what the paper would say under every outcome, before the run (`sweep_controls.py`).
|
||||
|
||||
**Other detectors in A′.** A′ is run with only its detector swapped; the memory, the split, the exit and the five readings of §6.4 are unchanged. (a) *Lagouanelle's estimate* $m_L=1/(1-\theta)$, $\theta=ff''/f'^2$ from the parabola through the last three points ($m_L=\infty$ for $\theta\ge1$), with the same memory — once seen, held until the exit, a hysteresis — entering at $m_L\ge2$ and at $0<m_L\le\tfrac23$ and splitting at $m_L$. (b) *Chandrupatla's acceptance band*, entering when the band rejects the current triple and splitting at the current $m_L$. (c) *No detector*: the regime is entered at every unkept promise.
|
||||
|
||||
@@ -501,7 +509,7 @@ Two controls ask whether the gain of §6 comes from the recognition by analogy o
|
||||
| Chandrupatla's band | 2682 | 2526 | 6236 | 20668 | 110/96/34 | $1.5\cdot10^{-10}$ |
|
||||
| none | 2693 | 2526 | 6165 | 20749 | 113/87/40 | $3\cdot10^{-9}$ |
|
||||
|
||||
On corpus 4, the one test, A′ beats all three in total and problem by problem, as the pre-registered criterion required. The competent alternative is Lagouanelle's estimate: it is slightly better on the canonical set (2235 against 2259), and on corpus 4 A′'s lead over it lies in the multiple-root and fractional-power families (1163 against 1739, 924 against 1207); it is behind on $\log(1+q^2)$ of a cubic (503 against 477) and level on the rest. The band and the regime without a detector are worse than no regime at all. The decision layer by analogy therefore earns its place against the detectors a reader would try first.
|
||||
On corpus 4, the one test, A′ beats all three in total and problem by problem, as the criterion recorded in advance required. The competent alternative is Lagouanelle's estimate: it is slightly better on the canonical set (2235 against 2259), and on corpus 4 A′'s lead over it lies in the multiple-root and fractional-power families (1163 against 1739, 924 against 1207); it is behind on $\log(1+q^2)$ of a cubic (503 against 477) and level on the rest. The band and the regime without a detector are worse than no regime at all. The decision layer by analogy therefore earns its place against the detectors a reader would try first.
|
||||
|
||||
**The multiplicity known in advance.** The second control asks how good the classical transformation is when its multiplicity is given. Library Chandrupatla and Brent are run on $g=\operatorname{sign}(f)\,|f|^{1/m^*}$, with $m^*$ the order read at the known root — the slope of $\ln|f|$ against $\ln|x-r|$ on both sides at $h=10^{-5}$, rounded to 0.01, and 1 where either side shows no zero (a jump). It is not an achievable method but an upper bound. On the multiple and fractional roots, $m^*\ge2$ or $m^*\le\tfrac23$ — the branches of §6.1 where a tangent step loses to a halving:
|
||||
|
||||
@@ -513,7 +521,22 @@ On corpus 4, the one test, A′ beats all three in total and problem by problem,
|
||||
| new corpus 1 | 40 | 1229 | 358 | 337 | 1792 |
|
||||
| corpus 4 | 88 | 2159 | 901 | 844 | 4151 |
|
||||
|
||||
With the multiplicity known the transformation is 2.4 to 6.8 times cheaper than A′. A′ is far ahead of Chandrupatla on $f$ itself, which does not know the multiplicity either. On the remaining problems the transformed and untransformed runs differ little and A′ is close to them: ahead on the canonical sets (2259 against 2640, 420 against 437 for Chandrupatla on $g$), behind on corpora 1 and 4 (2061 against 1924, 1497 against 1427). So on multiple roots the contribution of A′ is the estimate of the order without knowing it, and the gap to Table 11 is the price of that estimate — large, and the clearest direction for improving the method.
|
||||
With the multiplicity known the transformation is 2.4 to 6.8 times cheaper than A′. Every method, A′ included, stops on an exact zero of the function it evaluates, and on the pure powers of the Chandrupatla set $g$ is exactly linear, so the secant lands on the root: 15 of Brent's 20 runs and 4 of Chandrupatla's end that way there (A′: 2). On new corpus 1 no transformed run ends on a zero, and the transformation is still 3.4 times cheaper. A′ is far ahead of Chandrupatla on $f$ itself, which does not know the multiplicity either. On the remaining problems the transformed and untransformed runs differ little and A′ is close to them: ahead on the canonical sets (2259 against 2640, 420 against 437 for Chandrupatla on $g$), behind on corpora 1 and 4 (2061 against 1924, 1497 against 1427). So on multiple roots the contribution of A′ is the estimate of the order without knowing it, and the gap to Table 11 is the price of that estimate — large, and the clearest direction for improving the method.
|
||||
|
||||
**The multiplicity estimated during the run.** Between A′ and the oracle lies the natural classical method: Chandrupatla's interpolation on $g=\operatorname{sign}(f)|f|^{1/\hat m}$ with $\hat m$ estimated during the run. Our implementation of Chandrupatla's method (identical to the library on all 199 problems, and to itself when $\hat m\equiv1$) keeps the true $f$ at its three points and reads $G(v)=\operatorname{sign}(v)|v|^{1/\hat m}$ only in the acceptance band and the interpolation; $\hat m$ starts at 1 and takes an order read from the last three evaluations whenever that order lies on a branch of §6.1 ($m\ge2$ or $0<m\le\tfrac23$), without reset — the hysteresis of the controls above. The order is read either by Lagouanelle's estimate or by A′'s own analogy (recorded in advance, `sweep_estimated.py`):
|
||||
|
||||
**Table 12.** Chandrupatla on the transformed function: the multiplicity estimated during the run, against A′, the oracle of Table 11 and Chandrupatla on $f$. Totals, failures charged; the 148 multiple and fractional roots of Table 11, and the simple-root canonical set. The estimated forms have no exit: an order once taken is held to the end of the run. A′ leaves its regime by the exit of §6.2.
|
||||
|
||||
| method | corpus 4 | six corpora | multiple/fractional (148) | APS |
|
||||
|---|---|---|---|---|
|
||||
| **A′** | **3656** | **13032** | 4042 | **2259** |
|
||||
| Chandrupatla on $g$, $\hat m$ by analogy | 3806 | 15733 | 2809 | 3453 |
|
||||
| Chandrupatla on $g$, $\hat m$ by Lagouanelle | 7526 | 30991 | 6041 | 7215 |
|
||||
| the same, branch $m\ge2$ only (post hoc) | 5568 | 18372 | 6540 | 2960 |
|
||||
| Chandrupatla on $g$, $m^*$ known (Table 11) | — | — | **1355** | — |
|
||||
| Chandrupatla on $f$ | 5717 | 16481 | 7011 | 2595 |
|
||||
|
||||
A′ is ahead of both estimated forms on corpus 4, in total and problem by problem (against the analogy form 126/38/76, $p=5\cdot10^{-4}$), and over the six corpora. The two estimators behave differently in this frame. Lagouanelle's reads the parabola through Chandrupatla's points, which alternate sides of the root; there it reports a fractional order on 115 of the 154 simple roots of the canonical set, holds it, and turns the method into little more than bisection. Restricted after the fact to the branch $m\ge2$ (post hoc, `POSTHOC_lag_branch_2026-09-22.md`), it no longer breaks, but in this frame it hardly sees a multiple root either: 6540 on the 148 multiple and fractional roots against 7011 for Chandrupatla on $f$, and 5568 on corpus 4, with A′ ahead 178/34/28. The analogy's order is the better one here too, and on the multiple and fractional roots it does better inside Chandrupatla's interpolation than inside A′'s split: it closes 59%, 58% and 31% of the gap from A′ to the oracle on the Chandrupatla set, corpus 1 and corpus 4 (322 against 654, 721 against 1229, 1766 against 2159). It pays on simple roots, where it has no exit to take a false reading back (3453 against 2595 on the canonical set). Two conclusions follow. First, the order read by analogy transfers to the classical frame and works better there than in A′'s own split: on the multiple and fractional roots Chandrupatla's interpolation with that order beats A′ itself (2809 against 4042). Second, A′'s decision layer as a whole — the detector together with its exit — earns its place: without an exit the transferred detector loses more on simple roots than it gains on multiple ones, and A′ stays ahead on corpus 4 and over the six corpora.
|
||||
|
||||
---
|
||||
|
||||
@@ -523,11 +546,11 @@ A root finder can be derived from a similarity function alone, and the derivatio
|
||||
|
||||
The base rule is level with TOMS 748 and 1.5% behind Chandrupatla's method on the canonical set, and clearly behind Chandrupatla on wide brackets (1505 against 2357); the cause is not the order of the model but the decision to give it up.
|
||||
|
||||
That decision is a similarity too, of the run to its own past: a pure power repeats its configuration under zoom, an exponential under translation, a simple root tends to the line, and the analogous pair reads the order that decides between the model and a split. The method built on it, A′, is ahead of Chandrupatla on both canonical sets, and on a corpus fixed before its code was frozen it needs 36% fewer evaluations and wins 177 problems to 28. Its decision layer beats Lagouanelle's estimate, Chandrupatla's band and no detector put in its place; with the multiplicity known in advance the classical transformation is 2.4 to 6.8 times cheaper on multiple roots, which measures what estimating the order costs. The claim the measurements support is a lead over Brent, TOMS 748, ITP and ModAB everywhere, within a bound of twice bisection that Brent, TOMS 748 and ModAB do not promise and exceed, and over Chandrupatla a lead that is large where roots are multiple, fractional or nearly flat and small where they are simple — not superiority by the same margin everywhere. `fun9`, near-cancelling terms and smooth applied equations are where it is behind or level.
|
||||
That decision is a similarity too, of the run to its own past: a pure power repeats its configuration under zoom, an exponential under translation, a simple root tends to the line, and the analogous pair reads the order that decides between the model and a split. The method built on it, A′, is ahead of Chandrupatla on both canonical sets, and on a corpus fixed before its code was frozen it needs 36% fewer evaluations and wins 177 problems to 28. Its detector, the order read by analogy, beats Lagouanelle's estimate, Chandrupatla's band and no detector put in its place, and it transfers: inside Chandrupatla's interpolation on the transformed function it does better on multiple roots than A′'s own split, while only A′'s exit keeps simple roots cheap; with the multiplicity known in advance the classical transformation is 2.4 to 6.8 times cheaper on multiple roots, which measures what estimating the order costs. The claim the measurements support is a lead over Brent, TOMS 748, ITP and ModAB everywhere, within a bound of twice bisection that Brent, TOMS 748 and ModAB do not promise and exceed, and over Chandrupatla a lead that is large where roots are multiple, fractional or nearly flat and small where they are simple — not superiority by the same margin everywhere. `fun9`, near-cancelling terms and smooth applied equations are where it is behind or level.
|
||||
|
||||
### Future work
|
||||
|
||||
The first is the price of estimating the order. With the multiplicity given, the transformation of §6.3 is 2.4 to 6.8 times cheaper than A′ on multiple and fractional roots (Table 11): the order read from the analogous pair, or its use in the split, loses most of what the transformation could deliver.
|
||||
The first is the price of estimating the order. With the multiplicity given, the transformation of §6.3 is 2.4 to 6.8 times cheaper than A′ on multiple and fractional roots (Table 11): closing that gap is open.
|
||||
|
||||
The second is the steep flank. Where the loss remains — `fun9`, the near-cancelling class, 4 − tan x at its pole — the base step approaches along a flank far steeper than its model, and the half contradiction of §6.4 removes only part of the cost. The third is the one geometric choice of §6.2, the Euclidean metric of the $(\xi,\varphi)$ plane: a metric derived from the configuration's own sensitivity would remove it.
|
||||
|
||||
@@ -535,7 +558,7 @@ The second is the steep flank. Where the loss remains — `fun9`, the near-cance
|
||||
|
||||
## Code and data availability
|
||||
|
||||
Everything behind this paper is released with it: the reference implementation of the base rule and of A′ (`simroot/pure.py`); `simbracket`, a compiled library of A′ (Cython; wheels for Linux and Windows, Python 3.10 or later) whose results equal the reference bit for bit on all 868 problems of the six corpora; the corpora as code (`corpus_*.py`); every sweep script with its results file; the pre-registrations with the commits of Appendix D; and `verify_article.py`, which recomputes or re-reads every number stated in this paper and is part of the test suite. All numbers come from one Linux machine (§4). Repository: ⟨repository URL⟩.
|
||||
Everything behind this paper is released with it: the reference implementation of the base rule and of A′ (`simroot/pure.py`); `simbracket`, a compiled library of A′ (Cython; wheels for Linux and Windows, Python 3.10 or later) whose results equal the reference bit for bit on all 868 problems of the six corpora; the corpora as code (`corpus_*.py`); every sweep script with its results file; the advance records with the commits of Appendix D; and `verify_article.py`, which recomputes or re-reads every number stated in this paper and is part of the test suite. All of it is released under the Apache License 2.0. All numbers come from one Linux machine (§4). Archive (code, data and history): https://doi.org/10.5281/zenodo.22894337 (DOI 10.5281/zenodo.22894337).
|
||||
|
||||
---
|
||||
|
||||
@@ -687,19 +710,19 @@ Three caveats, all of them limitations of the measurement rather than of the met
|
||||
|
||||
14. **The model's own point cannot tell a flat root from a simple one.** Judging the evaluated point against the line through the bracket ends and against 0 — the exit judgment of §6.2 — also at *entry* gives 2820 and 1532 against 2935 and 1546 for the exit alone: the model aims at the root, so $f$ is small there by construction and looks flat at a simple root as readily as at a power. The same judgment works at a split, which the model did not choose.
|
||||
|
||||
15. **The decision reading "defect" does not transfer.** Combined with the regime it was the best configuration on the two development sets (§A.4's defect reading with the flat families of §6), and it passed every pre-registered criterion there. On new corpus 1 it did not: per problem 95/33/78 against Chandrupatla ($p=0.22$), losing the class of exponential flanks 9 to 26. It is therefore not part of A′.
|
||||
15. **The decision reading "defect" does not transfer.** Combined with the regime it was the best configuration on the two development sets (§A.4's defect reading with the flat families of §6), and it passed every criterion recorded for it there. On new corpus 1 it did not: per problem 95/33/78 against Chandrupatla ($p=0.22$), losing the class of exponential flanks 9 to 26. It is therefore not part of A′.
|
||||
|
||||
16. **A component rejected alone can pay in combination.** Remembering the analogy for the run was rejected by its own pre-registration, under the harness scoring of the time (canonical set 2814 against 2806 without the regime; under the sign-change criterion the two are level, 2633 against 2634), and with the split of §6.3 it is the better form (with the step pattern, wide brackets 1250 against 1388 for the analogy read in one state). Composition was measured, not assumed, throughout — which is also why §A.2's readings (2) and (3), each an improvement, are not combined.
|
||||
16. **A component rejected alone can pay in combination.** Remembering the analogy for the run was rejected by its own advance record, under the harness scoring of the time (canonical set 2814 against 2806 without the regime; under the sign-change criterion the two are level, 2633 against 2634), and with the split of §6.3 it is the better form (with the step pattern, wide brackets 1250 against 1388 for the analogy read in one state). Composition was measured, not assumed, throughout — which is also why §A.2's readings (2) and (3), each an improvement, are not combined.
|
||||
|
||||
---
|
||||
|
||||
## Appendix D. Pre-registrations and the frozen code
|
||||
## Appendix D. Advance records and the frozen code
|
||||
|
||||
Every rule of §6 and every test of §7 was written down, with its criteria and stop rule, before its code and its run. Table D1 gives, for each document, the commit that first added it and the commit that first added the results its run wrote, both read from git by `check_prereg_order.py`: 25 of the 30 were committed before their results; four were committed together with them, so git cannot order them; one was decided on its smoke run and has no results file. The code of A′ was committed (`8027d7f`) after corpus 4 was defined (`657df10`) and before its results (`6d2531e`); `simroot/pure.py` did not change in between.
|
||||
Every rule of §6 and every test of §7 was written down, with its criteria and stop rule, before its code and its run. Table D1 gives, for each document, the commit that first added it and the commit that first added the results its run wrote, both read from git by `check_prereg_order.py`: 25 of the 30 were committed before their results; four were committed together with them, so git cannot order them; one was decided on its smoke run and has no results file. We call these documents advance records, not pre-registrations: they live in the author's repository, and their order relative to the runs is attested only by the author's git history, whose timestamps the author sets; no external registry was used. A deposit with an external timestamp (Zenodo, OSF) before a run would remove that limitation for future tests. Before publication the history was rewritten to remove infrastructure details (a server address) and working notes not meant for publication (a few files deleted; file names, comments and a few words in three advance records made neutral, cited commit hashes mapped to the new commits; no rule, criterion or number of a record changed); the commits cited here are those of the published history, in which `check_prereg_order.py` recomputes Table D1. The code of A′ was committed (`8027d7f`) after corpus 4 was defined (`657df10`) and before its results (`6d2531e`); `simroot/pure.py` did not change in between.
|
||||
|
||||
**Table D1.** Pre-registrations, in the order they were written.
|
||||
**Table D1.** Advance records, in the order they were written.
|
||||
|
||||
| pre-registration | commit | results file | commit | order |
|
||||
| advance record | commit | results file | commit | order |
|
||||
|------------------------------|---------|--------------------------------------|---------|-------------|
|
||||
| `logarithmic_2026-09-20` | `f9b062e` | `sweep_log.json` | `84ff1c7` | before |
|
||||
| `prestep_gate_2026-09-21` | `0617243` | `sweep_gate.json` | `a6116cd` | before |
|
||||
|
||||
Binary file not shown.
+219
-94
@@ -88,27 +88,27 @@ a Data-Selected Decision Layer}
|
||||
\maketitle
|
||||
\begin{abstract}
|
||||
We replace bisection's midpoint rule by a similarity rule --- the step
|
||||
grows with how dissimilar \(f(x)\) is to the target 0, under a
|
||||
similarity \(S\) with \(S(0)=1\) --- and derive its contraction factor
|
||||
in closed form. The derivation fixes a base rule before any benchmark is
|
||||
consulted: with the tent similarity and a three-point divided-difference
|
||||
scale it is a Newton step of Muller's order 1.839, clipped to the
|
||||
bracket and projected onto a budget of twice bisection whose multiplier
|
||||
is its one numerical constant. On the 154 problems of Alefeld, Potra and
|
||||
Shi it is level with TOMS 748 and 1.5\% behind Chandrupatla's method; on
|
||||
wide brackets it is clearly behind. The gap is a decision, when to give
|
||||
the model up, and we take it by analogy: at a flat root the
|
||||
dimensionless configuration of the last three points recurs under zoom,
|
||||
at a simple root it tends to a line, and the analogous pair gives the
|
||||
order that says whether and where to split. This decision layer, about
|
||||
ten discrete rules selected on two development sets, gives the method
|
||||
A′. On 240 problems fixed before its code was frozen it needs 3656
|
||||
evaluations against 5717 for Chandrupatla, 5535 for ModAB and 4488 with
|
||||
Lagouanelle's multiplicity estimate in place of the analogy; the lead
|
||||
lies in multiple and fractional roots, present in two of the six
|
||||
families. With the multiplicity known, the classical transformation
|
||||
grows with the dissimilarity of \(f(x)\) to 0 under a similarity \(S\)
|
||||
--- and derive its contraction factor in closed form. It fixes, before
|
||||
any benchmark, a base rule: with the tent similarity and a three-point
|
||||
divided-difference scale it is a Newton step of Muller's order 1.839,
|
||||
clipped to the bracket and projected onto a budget of twice bisection
|
||||
whose multiplier is its one numerical constant. On the 154 problems of
|
||||
Alefeld, Potra and Shi it is level with TOMS 748 and 1.5\% behind
|
||||
Chandrupatla's method; on wide brackets it is clearly behind. The gap is
|
||||
a decision, when to give the model up, and we take it by analogy: at a
|
||||
flat root the dimensionless configuration of the last three points
|
||||
recurs under zoom, at a simple root it tends to a line, and the
|
||||
analogous pair gives the order that says whether and where to split.
|
||||
This decision layer --- about ten discrete rules selected on two
|
||||
development sets --- gives A′. On 240 problems fixed before its code was
|
||||
frozen it needs 3656 evaluations against 5717 for Chandrupatla, 5535 for
|
||||
ModAB and 4488 with Lagouanelle's multiplicity estimate in place of the
|
||||
analogy; the lead lies in multiple and fractional roots, present in two
|
||||
of six families. With the multiplicity known, the transformation
|
||||
\(\operatorname{sign}(f)|f|^{1/m}\) is 2.4 to 6.8 times cheaper on
|
||||
multiple roots.
|
||||
multiple roots; A′ adds an estimate of the order, within a guarantee of
|
||||
twice bisection.
|
||||
\end{abstract}
|
||||
|
||||
\hypertarget{introduction}{%
|
||||
@@ -162,8 +162,8 @@ The paper makes four claims, each measured.
|
||||
The search that produced these results --- readings of the similarity
|
||||
that did not pay, the decomposition of the wide-bracket gap, wall-clock
|
||||
time, and the rejected rules --- is reported in Appendices A--C. The
|
||||
code, the corpora and the pre-registrations are released (Appendix D and
|
||||
the availability statement).
|
||||
code, the corpora and the documents that recorded each test before its
|
||||
run are released (Appendix D and the availability statement).
|
||||
|
||||
\begin{center}\rule{0.5\linewidth}{0.5pt}\end{center}
|
||||
|
||||
@@ -400,8 +400,10 @@ while |B - A| > xtol:
|
||||
if sigma is 0 or not finite: xn <- midpoint(A,B)
|
||||
else:
|
||||
xn <- x + min(1, |f(x)|/sigma) * (far - x) # the step, eq (4)
|
||||
lo, hi <- min(A,B), max(A,B)
|
||||
xn <- clamp(xn, hi - allow, lo + allow) # projection, eq (3)
|
||||
if xn outside (A,B) or already evaluated: xn <- midpoint(A,B)
|
||||
(if the midpoint too was evaluated: return it)
|
||||
evaluate fn = f(xn); allow <- allow / 2
|
||||
xp2, xp, x <- xp, x, xn
|
||||
update the bracket: whichever of A,B keeps the sign change
|
||||
@@ -554,15 +556,17 @@ transcribed by hand.
|
||||
it reports convergence and \(f\) changes sign within \(10^{-6}\) of the
|
||||
returned point, or vanishes there --- the bracketing notion of a root,
|
||||
the same criterion on every corpus of this paper; \(x\)-tolerance
|
||||
\(10^{-12}\); budget 200 evaluations. Scoring against the published root
|
||||
instead, as the benchmarks' harness does, charges every method the full
|
||||
budget on six problems where \(f\) vanishes identically around the root
|
||||
in floating point (§A.5). Head-to-head counts (wins/ties/losses) are
|
||||
taken over every problem of a set with the same charging as the totals.
|
||||
\emph{Failures are charged the full budget}, so no method can buy a low
|
||||
total by giving up early, and totals are reported over all problems
|
||||
rather than over the subset a method happened to solve. Rankings are by
|
||||
failures first, then by total.
|
||||
\(10^{-12}\); budget 200 evaluations. Every method, ours included, stops
|
||||
when its bracket is no wider than \(\varepsilon_x\) or when it evaluates
|
||||
an exact zero of the function it works on; none stops on a small
|
||||
\(|f|\). Scoring against the published root instead, as the benchmarks'
|
||||
harness does, charges every method the full budget on six problems where
|
||||
\(f\) vanishes identically around the root in floating point (§A.5).
|
||||
Head-to-head counts (wins/ties/losses) are taken over every problem of a
|
||||
set with the same charging as the totals. \emph{Failures are charged the
|
||||
full budget}, so no method can buy a low total by giving up early, and
|
||||
totals are reported over all problems rather than over the subset a
|
||||
method happened to solve. Rankings are by failures first, then by total.
|
||||
|
||||
\textbf{Competitors.} Where SciPy ships the method, the competitor is
|
||||
the library --- Chandrupatla is
|
||||
@@ -655,7 +659,11 @@ its losses are the larger ones; by function family, the unit in which
|
||||
these problems are independent (§7), the comparison is 6/1/8 of 15
|
||||
families, a tie. The constant-free variant goes 79/3/72 against
|
||||
Chandrupatla (\(p=0.63\)), 78/9/67 against TOMS 748 and 79/5/70 against
|
||||
Brent.
|
||||
Brent. Dekker, Illinois and false position fail \texttt{aps.13.00} under
|
||||
either criterion and so keep their totals: they spend the whole budget
|
||||
without converging, Dekker and Illinois stalled on one side of the zero
|
||||
where \(|f|<10^{-62}\) but \(f\) does not change sign within
|
||||
\(10^{-6}\).
|
||||
|
||||
\textbf{Table 3.} Chandrupatla, 45 problems, brackets up to \(10^{12}\)
|
||||
wide, out of 264 configurations.
|
||||
@@ -689,7 +697,11 @@ total evals
|
||||
2 & ITP, per-problem oracle & yes & 45/45 & 1675 & 0 \\
|
||||
3 & similarity, \(e^{-\lvert z\rvert}\), bracket scale, both guards & no
|
||||
& 45/45 & 1778 & 0 \\
|
||||
4 & similarity, \(e^{-\lvert z\rvert}\), bracket scale, bracket guard &
|
||||
no & 45/45 & 1848 & 0 \\
|
||||
5 & ITP, recommended constants & yes & 45/45 & 1946 & 0 \\
|
||||
6 & similarity, \(e^{-\lvert z\rvert}\), bracket scale, earned
|
||||
projection & no & 45/45 & 1952 & 0 \\
|
||||
9 & constant-free variant: earned projection & no & 45/45 & 2073 & 0 \\
|
||||
15 & bisection & no & 45/45 & 2136 & 0 \\
|
||||
\textbf{49} & \textbf{base rule: tent, 3-point, projection (\(c=2\))} &
|
||||
@@ -774,8 +786,9 @@ the right move is to halve, Chandrupatla's method halves on up to all of
|
||||
its steps, and no reading of a single state measured takes that decision
|
||||
correctly. This section takes it by a similarity of the run to \emph{its
|
||||
own past} --- an analogy in Pólya's sense {[}8{]}, two situations
|
||||
agreeing in more than one relation. Every rule below was pre-registered
|
||||
with a stop rule (Appendix D); the rejected ones are in Appendix C.
|
||||
agreeing in more than one relation. Every rule below was recorded, with
|
||||
its criteria and stop rule, before its code and run (Appendix D); the
|
||||
rejected ones are in Appendix C.
|
||||
|
||||
\hypertarget{the-flag-a-promise-that-was-not-kept}{%
|
||||
\subsubsection{6.1 The flag: a promise that was not
|
||||
@@ -964,7 +977,10 @@ among the many candidates of Appendix C. That selection is why the
|
||||
method was tested once on a corpus fixed before its code was frozen, and
|
||||
against simpler rules put in its place (§7). Algorithm 2 is the complete
|
||||
method; \texttt{simroot/pure.py} and the compiled \texttt{simbracket}
|
||||
implement exactly this and agree bit for bit.
|
||||
implement exactly this and agree bit for bit. The nearest-configuration
|
||||
search runs over the run's memory: \(O(n)\) per step, \(O(n^2)\) per run
|
||||
of \(n\) evaluations (\(n\le200\) here), arithmetic that matters only
|
||||
when \(f\) is cheap (Appendix B).
|
||||
|
||||
\textbf{Algorithm 2 (A′).} Notation of Algorithm 1;
|
||||
\(S(z)=\max(0,1-|z|)\).
|
||||
@@ -987,9 +1003,11 @@ while |B - A| > xtol:
|
||||
# unkept promise, and unkept promise with a crossing
|
||||
flag <- not in_regime and last_model and fx/fp >= 1/4
|
||||
flag_over <- not in_regime and last_model and fx/fp <= -1/4
|
||||
# order (6) of the analogous pair: the past configuration
|
||||
# nearest to c (Euclidean in (xi, phi)), if nearer than the
|
||||
# line phi = xi; inf if the pair shows L_f < 1 <= L_x; else none
|
||||
# order (6) of the analogous pair. q <- the configuration in
|
||||
# configs nearest to c (Euclidean in (xi, phi)); no m if c or
|
||||
# configs is empty or q is not nearer than the line phi = xi.
|
||||
# L_x <- |x-xp| / q.dx, L_f <- |fx-fp| / q.df
|
||||
# m <- inf if L_f < 1 <= L_x (translation), else ln L_f / ln L_x
|
||||
m <- order of the analogous pair
|
||||
if m >= 2: analogy_seen <- true; m_a <- m
|
||||
if 0 < m <= 2/3: shallow_seen <- true; m_s <- m
|
||||
@@ -1003,18 +1021,20 @@ while |B - A| > xtol:
|
||||
else:
|
||||
step <- (1 - S(z)) * (far - x) # eq. (4)
|
||||
append x - xp to moves
|
||||
# step pattern: the lag 2 .. half the history of highest
|
||||
# mean similarity of successive length ratios has mean > 1/2,
|
||||
# and step and x - xp point the same way
|
||||
if the pattern repeats:
|
||||
# step pattern: q_j <- |d_j|/|d_(j-1)| over the
|
||||
# successive nonzero moves d (k ratios);
|
||||
# rho(u,v) <- min(u,v)/max(u,v); repeats iff
|
||||
# max_{p=2..floor(k/2)} mean_j rho(q_j, q_(j-p)) > 1/2
|
||||
if repeats and step, x - xp point the same way:
|
||||
s <- min(|step|,|x-xp|) / max(|step|,|x-xp|)
|
||||
step <- step / (1 - s), at most |far - x| long
|
||||
if |step| < xtol/2 and last_model and |fx/fp| < 1/4:
|
||||
step <- sign(far - x) * xtol/2 # tolerance step
|
||||
xn <- x + step; model <- true
|
||||
lo, hi <- min(A,B), max(A,B)
|
||||
xn <- clamp(xn, hi - allow, lo + allow) # projection (3)
|
||||
if xn outside (A,B) or xn in {x, xp}:
|
||||
xn <- mid; if mid in {x, xp}: stop
|
||||
xn <- mid; if mid in {x, xp}: return mid
|
||||
last_model <- model and xn is the model's own point
|
||||
fn <- f(xn)
|
||||
if in_regime: # exit
|
||||
@@ -1056,8 +1076,8 @@ A′ per problem, CHA
|
||||
\endlastfoot
|
||||
\textbf{A′ (proposed)} & \textbf{2259} & \textbf{1074} & \textbf{3333} &
|
||||
--- & --- \\
|
||||
A′, earned budget (no free constant) & 3417 & 1852 & 5269 & & \\
|
||||
base rule (§3) & 2634 & 2357 & 4991 & & \\
|
||||
A′, earned budget (no free constant) & 3417 & 1852 & 5269 & --- & --- \\
|
||||
base rule (§3) & 2634 & 2357 & 4991 & --- & --- \\
|
||||
Chandrupatla & 2595 & 1505 & 4100 & 103/20/31, \(p=3\cdot10^{-10}\) &
|
||||
31/4/10, \(p=0.001\) \\
|
||||
TOMS 748 & 2636 & 3088 & 5724 & 127/6/21 & 42/2/1 \\
|
||||
@@ -1126,8 +1146,8 @@ sign contradiction & +11 & −12 & +29 & 0 & +1 & −1 & +28 \\
|
||||
Every component is positive in sum; the regime and the split carry the
|
||||
method. The step pattern (earns on two corpora of six) and the sign
|
||||
contradiction (+28 over 855 problems) are marginal --- together under
|
||||
one per cent, 117 of 13032 evaluations --- and are kept because the
|
||||
pre-registered rule removes only a component that earns nowhere.
|
||||
one per cent, 117 of 13032 evaluations --- and are kept because the rule
|
||||
recorded in advance removes only a component that earns nowhere.
|
||||
|
||||
\textbf{Table 7.} The similarity in the step of A′: the same method with
|
||||
each of the nine similarity functions of §5.2 in place of the tent,
|
||||
@@ -1220,22 +1240,25 @@ Roots.jl's \texttt{Order1B} and \texttt{Order2B} {[}12{]}); inside a
|
||||
bracket, {[}22{]} needs the multiplicity given and recognises a multiple
|
||||
root only by the number of evaluations spent, against a third of
|
||||
bisection's; Chandrupatla takes the decision to halve by an empirical
|
||||
acceptance band {[}3{]}. §7.1 puts this claim against the detectors a
|
||||
reader would try first, and against the transformation with the
|
||||
multiplicity known in advance.
|
||||
acceptance band {[}3{]}. §7.1 tests this against the detectors a reader
|
||||
would try first and against the classical transformation with the
|
||||
multiplicity known or estimated, and the outcome is sharper than the
|
||||
claim: the order read by analogy transfers to the classical frame and
|
||||
works better there than in A′'s own split, while A′'s exit is what keeps
|
||||
simple roots cheap.
|
||||
|
||||
\begin{center}\rule{0.5\linewidth}{0.5pt}\end{center}
|
||||
|
||||
\hypertarget{out-of-sample-evidence}{%
|
||||
\subsection{7. Out-of-sample evidence}\label{out-of-sample-evidence}}
|
||||
|
||||
Every decision above was taken on the two sets of §4. Pre-registration
|
||||
protects each decision but not the chain: the final configuration --- a
|
||||
decision layer of about ten discrete rules, chosen among the many
|
||||
candidates listed in Appendix C --- was selected on the same 199
|
||||
problems. Four further corpora were therefore used, each fixed and
|
||||
committed before any method was run on it, each with its criteria and
|
||||
stop rule written down first.
|
||||
Every decision above was taken on the two sets of §4. Recording each
|
||||
decision in advance protects the decision but not the chain: the final
|
||||
configuration --- a decision layer of about ten discrete rules, chosen
|
||||
among the many candidates listed in Appendix C --- was selected on the
|
||||
same 199 problems. Four further corpora were therefore used, each fixed
|
||||
and committed before any method was run on it, each with its criteria
|
||||
and stop rule written down first.
|
||||
|
||||
\begin{enumerate}
|
||||
\def\labelenumi{\arabic{enumi}.}
|
||||
@@ -1279,8 +1302,8 @@ counting of every other method. The criterion, tolerance, budget and
|
||||
charging are those of §4.
|
||||
|
||||
The variant carried to corpora 1--3 was A′ without the tolerance step
|
||||
and the overshoot branch. It transferred to corpus 1 by every
|
||||
pre-registered criterion (per problem 105/34/67 against Chandrupatla,
|
||||
and the overshoot branch. It transferred to corpus 1 by every criterion
|
||||
recorded in advance (per problem 105/34/67 against Chandrupatla,
|
||||
\(p=0.005\)), did \emph{not} transfer to Roots.jl (38/10/30, \(p=0.4\))
|
||||
and lost on the applied corpus; the two readings that closed those gaps
|
||||
were developed on the seen corpora and tested once, on corpus 4.
|
||||
@@ -1426,8 +1449,8 @@ advance}\label{controls-simpler-detectors-and-the-multiplicity-known-in-advance}
|
||||
|
||||
Two controls ask whether the gain of §6 comes from the recognition by
|
||||
analogy or only from the split (7) with any reasonable detector. Both
|
||||
were pre-registered, with what the paper would say under every outcome,
|
||||
before the run (\texttt{sweep\_controls.py}).
|
||||
were recorded, with what the paper would say under every outcome, before
|
||||
the run (\texttt{sweep\_controls.py}).
|
||||
|
||||
\textbf{Other detectors in A′.} A′ is run with only its detector
|
||||
swapped; the memory, the split, the exit and the five readings of §6.4
|
||||
@@ -1483,7 +1506,7 @@ none & 2693 & 2526 & 6165 & 20749 & 113/87/40 & \(3\cdot10^{-9}\) \\
|
||||
\end{longtable}
|
||||
|
||||
On corpus 4, the one test, A′ beats all three in total and problem by
|
||||
problem, as the pre-registered criterion required. The competent
|
||||
problem, as the criterion recorded in advance required. The competent
|
||||
alternative is Lagouanelle's estimate: it is slightly better on the
|
||||
canonical set (2235 against 2259), and on corpus 4 A′'s lead over it
|
||||
lies in the multiple-root and fractional-power families (1163 against
|
||||
@@ -1538,15 +1561,102 @@ corpus 4 & 88 & 2159 & 901 & 844 & 4151 \\
|
||||
\end{longtable}
|
||||
|
||||
With the multiplicity known the transformation is 2.4 to 6.8 times
|
||||
cheaper than A′. A′ is far ahead of Chandrupatla on \(f\) itself, which
|
||||
does not know the multiplicity either. On the remaining problems the
|
||||
transformed and untransformed runs differ little and A′ is close to
|
||||
them: ahead on the canonical sets (2259 against 2640, 420 against 437
|
||||
for Chandrupatla on \(g\)), behind on corpora 1 and 4 (2061 against
|
||||
1924, 1497 against 1427). So on multiple roots the contribution of A′ is
|
||||
the estimate of the order without knowing it, and the gap to Table 11 is
|
||||
the price of that estimate --- large, and the clearest direction for
|
||||
improving the method.
|
||||
cheaper than A′. Every method, A′ included, stops on an exact zero of
|
||||
the function it evaluates, and on the pure powers of the Chandrupatla
|
||||
set \(g\) is exactly linear, so the secant lands on the root: 15 of
|
||||
Brent's 20 runs and 4 of Chandrupatla's end that way there (A′: 2). On
|
||||
new corpus 1 no transformed run ends on a zero, and the transformation
|
||||
is still 3.4 times cheaper. A′ is far ahead of Chandrupatla on \(f\)
|
||||
itself, which does not know the multiplicity either. On the remaining
|
||||
problems the transformed and untransformed runs differ little and A′ is
|
||||
close to them: ahead on the canonical sets (2259 against 2640, 420
|
||||
against 437 for Chandrupatla on \(g\)), behind on corpora 1 and 4 (2061
|
||||
against 1924, 1497 against 1427). So on multiple roots the contribution
|
||||
of A′ is the estimate of the order without knowing it, and the gap to
|
||||
Table 11 is the price of that estimate --- large, and the clearest
|
||||
direction for improving the method.
|
||||
|
||||
\textbf{The multiplicity estimated during the run.} Between A′ and the
|
||||
oracle lies the natural classical method: Chandrupatla's interpolation
|
||||
on \(g=\operatorname{sign}(f)|f|^{1/\hat m}\) with \(\hat m\) estimated
|
||||
during the run. Our implementation of Chandrupatla's method (identical
|
||||
to the library on all 199 problems, and to itself when
|
||||
\(\hat m\equiv1\)) keeps the true \(f\) at its three points and reads
|
||||
\(G(v)=\operatorname{sign}(v)|v|^{1/\hat m}\) only in the acceptance
|
||||
band and the interpolation; \(\hat m\) starts at 1 and takes an order
|
||||
read from the last three evaluations whenever that order lies on a
|
||||
branch of §6.1 (\(m\ge2\) or \(0<m\le\tfrac23\)), without reset --- the
|
||||
hysteresis of the controls above. The order is read either by
|
||||
Lagouanelle's estimate or by A′'s own analogy (recorded in advance,
|
||||
\texttt{sweep\_estimated.py}):
|
||||
|
||||
\textbf{Table 12.} Chandrupatla on the transformed function: the
|
||||
multiplicity estimated during the run, against A′, the oracle of Table
|
||||
11 and Chandrupatla on \(f\). Totals, failures charged; the 148 multiple
|
||||
and fractional roots of Table 11, and the simple-root canonical set. The
|
||||
estimated forms have no exit: an order once taken is held to the end of
|
||||
the run. A′ leaves its regime by the exit of §6.2.
|
||||
|
||||
\begin{longtable}[]{@{}
|
||||
>{\raggedright\arraybackslash}p{(\columnwidth - 8\tabcolsep) * \real{0.2000}}
|
||||
>{\raggedright\arraybackslash}p{(\columnwidth - 8\tabcolsep) * \real{0.2000}}
|
||||
>{\raggedright\arraybackslash}p{(\columnwidth - 8\tabcolsep) * \real{0.2000}}
|
||||
>{\raggedright\arraybackslash}p{(\columnwidth - 8\tabcolsep) * \real{0.2000}}
|
||||
>{\raggedright\arraybackslash}p{(\columnwidth - 8\tabcolsep) * \real{0.2000}}@{}}
|
||||
\toprule\noalign{}
|
||||
\begin{minipage}[b]{\linewidth}\raggedright
|
||||
method
|
||||
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
|
||||
corpus 4
|
||||
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
|
||||
six corpora
|
||||
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
|
||||
multiple/fractional (148)
|
||||
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
|
||||
APS
|
||||
\end{minipage} \\
|
||||
\midrule\noalign{}
|
||||
\endhead
|
||||
\bottomrule\noalign{}
|
||||
\endlastfoot
|
||||
\textbf{A′} & \textbf{3656} & \textbf{13032} & 4042 & \textbf{2259} \\
|
||||
Chandrupatla on \(g\), \(\hat m\) by analogy & 3806 & 15733 & 2809 &
|
||||
3453 \\
|
||||
Chandrupatla on \(g\), \(\hat m\) by Lagouanelle & 7526 & 30991 & 6041 &
|
||||
7215 \\
|
||||
the same, branch \(m\ge2\) only (post hoc) & 5568 & 18372 & 6540 &
|
||||
2960 \\
|
||||
Chandrupatla on \(g\), \(m^*\) known (Table 11) & --- & --- &
|
||||
\textbf{1355} & --- \\
|
||||
Chandrupatla on \(f\) & 5717 & 16481 & 7011 & 2595 \\
|
||||
\end{longtable}
|
||||
|
||||
A′ is ahead of both estimated forms on corpus 4, in total and problem by
|
||||
problem (against the analogy form 126/38/76, \(p=5\cdot10^{-4}\)), and
|
||||
over the six corpora. The two estimators behave differently in this
|
||||
frame. Lagouanelle's reads the parabola through Chandrupatla's points,
|
||||
which alternate sides of the root; there it reports a fractional order
|
||||
on 115 of the 154 simple roots of the canonical set, holds it, and turns
|
||||
the method into little more than bisection. Restricted after the fact to
|
||||
the branch \(m\ge2\) (post hoc,
|
||||
\texttt{POSTHOC\_lag\_branch\_2026-09-22.md}), it no longer breaks, but
|
||||
in this frame it hardly sees a multiple root either: 6540 on the 148
|
||||
multiple and fractional roots against 7011 for Chandrupatla on \(f\),
|
||||
and 5568 on corpus 4, with A′ ahead 178/34/28. The analogy's order is
|
||||
the better one here too, and on the multiple and fractional roots it
|
||||
does better inside Chandrupatla's interpolation than inside A′'s split:
|
||||
it closes 59\%, 58\% and 31\% of the gap from A′ to the oracle on the
|
||||
Chandrupatla set, corpus 1 and corpus 4 (322 against 654, 721 against
|
||||
1229, 1766 against 2159). It pays on simple roots, where it has no exit
|
||||
to take a false reading back (3453 against 2595 on the canonical set).
|
||||
Two conclusions follow. First, the order read by analogy transfers to
|
||||
the classical frame and works better there than in A′'s own split: on
|
||||
the multiple and fractional roots Chandrupatla's interpolation with that
|
||||
order beats A′ itself (2809 against 4042). Second, A′'s decision layer
|
||||
as a whole --- the detector together with its exit --- earns its place:
|
||||
without an exit the transferred detector loses more on simple roots than
|
||||
it gains on multiple ones, and A′ stays ahead on corpus 4 and over the
|
||||
six corpora.
|
||||
|
||||
\begin{center}\rule{0.5\linewidth}{0.5pt}\end{center}
|
||||
|
||||
@@ -1574,9 +1684,12 @@ translation, a simple root tends to the line, and the analogous pair
|
||||
reads the order that decides between the model and a split. The method
|
||||
built on it, A′, is ahead of Chandrupatla on both canonical sets, and on
|
||||
a corpus fixed before its code was frozen it needs 36\% fewer
|
||||
evaluations and wins 177 problems to 28. Its decision layer beats
|
||||
Lagouanelle's estimate, Chandrupatla's band and no detector put in its
|
||||
place; with the multiplicity known in advance the classical
|
||||
evaluations and wins 177 problems to 28. Its detector, the order read by
|
||||
analogy, beats Lagouanelle's estimate, Chandrupatla's band and no
|
||||
detector put in its place, and it transfers: inside Chandrupatla's
|
||||
interpolation on the transformed function it does better on multiple
|
||||
roots than A′'s own split, while only A′'s exit keeps simple roots
|
||||
cheap; with the multiplicity known in advance the classical
|
||||
transformation is 2.4 to 6.8 times cheaper on multiple roots, which
|
||||
measures what estimating the order costs. The claim the measurements
|
||||
support is a lead over Brent, TOMS 748, ITP and ModAB everywhere, within
|
||||
@@ -1592,9 +1705,7 @@ behind or level.
|
||||
|
||||
The first is the price of estimating the order. With the multiplicity
|
||||
given, the transformation of §6.3 is 2.4 to 6.8 times cheaper than A′ on
|
||||
multiple and fractional roots (Table 11): the order read from the
|
||||
analogous pair, or its use in the split, loses most of what the
|
||||
transformation could deliver.
|
||||
multiple and fractional roots (Table 11): closing that gap is open.
|
||||
|
||||
The second is the steep flank. Where the loss remains --- \texttt{fun9},
|
||||
the near-cancelling class, 4 − tan x at its pole --- the base step
|
||||
@@ -1616,10 +1727,12 @@ implementation of the base rule and of A′ (\texttt{simroot/pure.py});
|
||||
and Windows, Python 3.10 or later) whose results equal the reference bit
|
||||
for bit on all 868 problems of the six corpora; the corpora as code
|
||||
(\texttt{corpus\_*.py}); every sweep script with its results file; the
|
||||
pre-registrations with the commits of Appendix D; and
|
||||
advance records with the commits of Appendix D; and
|
||||
\texttt{verify\_article.py}, which recomputes or re-reads every number
|
||||
stated in this paper and is part of the test suite. All numbers come
|
||||
from one Linux machine (§4). Repository: ⟨repository URL⟩.
|
||||
stated in this paper and is part of the test suite. All of it is
|
||||
released under the Apache License 2.0. All numbers come from one Linux
|
||||
machine (§4). Archive (code, data and history):
|
||||
https://doi.org/10.5281/zenodo.22894337 (DOI 10.5281/zenodo.22894337).
|
||||
|
||||
\begin{center}\rule{0.5\linewidth}{0.5pt}\end{center}
|
||||
|
||||
@@ -2254,17 +2367,17 @@ results}\label{appendix-c.-negative-results}}
|
||||
\textbf{The decision reading ``defect'' does not transfer.} Combined
|
||||
with the regime it was the best configuration on the two development
|
||||
sets (§A.4's defect reading with the flat families of §6), and it
|
||||
passed every pre-registered criterion there. On new corpus 1 it did
|
||||
passed every criterion recorded for it there. On new corpus 1 it did
|
||||
not: per problem 95/33/78 against Chandrupatla (\(p=0.22\)), losing
|
||||
the class of exponential flanks 9 to 26. It is therefore not part of
|
||||
A′.
|
||||
\item
|
||||
\textbf{A component rejected alone can pay in combination.}
|
||||
Remembering the analogy for the run was rejected by its own
|
||||
pre-registration, under the harness scoring of the time (canonical set
|
||||
2814 against 2806 without the regime; under the sign-change criterion
|
||||
the two are level, 2633 against 2634), and with the split of §6.3 it
|
||||
is the better form (with the step pattern, wide brackets 1250 against
|
||||
Remembering the analogy for the run was rejected by its own advance
|
||||
record, under the harness scoring of the time (canonical set 2814
|
||||
against 2806 without the regime; under the sign-change criterion the
|
||||
two are level, 2633 against 2634), and with the split of §6.3 it is
|
||||
the better form (with the step pattern, wide brackets 1250 against
|
||||
1388 for the analogy read in one state). Composition was measured, not
|
||||
assumed, throughout --- which is also why §A.2's readings (2) and (3),
|
||||
each an improvement, are not combined.
|
||||
@@ -2272,9 +2385,9 @@ results}\label{appendix-c.-negative-results}}
|
||||
|
||||
\begin{center}\rule{0.5\linewidth}{0.5pt}\end{center}
|
||||
|
||||
\hypertarget{appendix-d.-pre-registrations-and-the-frozen-code}{%
|
||||
\subsection{Appendix D. Pre-registrations and the frozen
|
||||
code}\label{appendix-d.-pre-registrations-and-the-frozen-code}}
|
||||
\hypertarget{appendix-d.-advance-records-and-the-frozen-code}{%
|
||||
\subsection{Appendix D. Advance records and the frozen
|
||||
code}\label{appendix-d.-advance-records-and-the-frozen-code}}
|
||||
|
||||
Every rule of §6 and every test of §7 was written down, with its
|
||||
criteria and stop rule, before its code and its run. Table D1 gives, for
|
||||
@@ -2282,12 +2395,24 @@ each document, the commit that first added it and the commit that first
|
||||
added the results its run wrote, both read from git by
|
||||
\texttt{check\_prereg\_order.py}: 25 of the 30 were committed before
|
||||
their results; four were committed together with them, so git cannot
|
||||
order them; one was decided on its smoke run and has no results file.
|
||||
The code of A′ was committed (\texttt{8027d7f}) after corpus 4 was
|
||||
defined (\texttt{657df10}) and before its results (\texttt{6d2531e});
|
||||
order them; one was decided on its smoke run and has no results file. We
|
||||
call these documents advance records, not pre-registrations: they live
|
||||
in the author's repository, and their order relative to the runs is
|
||||
attested only by the author's git history, whose timestamps the author
|
||||
sets; no external registry was used. A deposit with an external
|
||||
timestamp (Zenodo, OSF) before a run would remove that limitation for
|
||||
future tests. Before publication the history was rewritten to remove
|
||||
infrastructure details (a server address) and working notes not meant
|
||||
for publication (a few files deleted; file names, comments and a few
|
||||
words in three advance records made neutral, cited commit hashes mapped
|
||||
to the new commits; no rule, criterion or number of a record changed);
|
||||
the commits cited here are those of the published history, in which
|
||||
\texttt{check\_prereg\_order.py} recomputes Table D1. The code of A′ was
|
||||
committed (\texttt{8027d7f}) after corpus 4 was defined
|
||||
(\texttt{657df10}) and before its results (\texttt{6d2531e});
|
||||
\texttt{simroot/pure.py} did not change in between.
|
||||
|
||||
\textbf{Table D1.} Pre-registrations, in the order they were written.
|
||||
\textbf{Table D1.} Advance records, in the order they were written.
|
||||
|
||||
\begin{longtable}[]{@{}
|
||||
>{\raggedright\arraybackslash}p{(\columnwidth - 8\tabcolsep) * \real{0.3030}}
|
||||
@@ -2297,7 +2422,7 @@ defined (\texttt{657df10}) and before its results (\texttt{6d2531e});
|
||||
>{\raggedright\arraybackslash}p{(\columnwidth - 8\tabcolsep) * \real{0.1313}}@{}}
|
||||
\toprule\noalign{}
|
||||
\begin{minipage}[b]{\linewidth}\raggedright
|
||||
pre-registration
|
||||
advance record
|
||||
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
|
||||
commit
|
||||
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
|
||||
|
||||
@@ -58,6 +58,8 @@ def ends_on_zero(method, f, g, a, b):
|
||||
|
||||
|
||||
def point4():
|
||||
"""Returns {corpus: {method: [problems, exact-zero stops, evaluations]}} and prints it."""
|
||||
out = {}
|
||||
for s in ("CHA", "NEW", "D"):
|
||||
tally = {"chandrupatla_g": [0, 0, 0], "brent_g": [0, 0, 0], "Aprime": [0, 0, 0]}
|
||||
for pid, f, a, b, r in k1_problems(s):
|
||||
@@ -78,6 +80,8 @@ def point4():
|
||||
t[2] += n
|
||||
print(f"{s}: problems {tally['Aprime'][0]}; runs ending on an exact zero / total evaluations: " +
|
||||
"; ".join(f"{k} {v[1]}/{v[2]}" for k, v in tally.items()))
|
||||
out[s] = tally
|
||||
return out
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
+202
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -36,6 +36,7 @@ setup(
|
||||
name="simbracket",
|
||||
version="1.0.0",
|
||||
description="Bracketing root finding by similarity and analogy (compiled).",
|
||||
license="Apache-2.0",
|
||||
packages=["simbracket"],
|
||||
package_data={},
|
||||
include_package_data=False,
|
||||
|
||||
@@ -1,157 +0,0 @@
|
||||
"""Step-size controllers. Everything else in the integration is SciPy's
|
||||
(the RK45 tableau, the stage combination, the embedded error estimate), so
|
||||
these are the only thing under comparison.
|
||||
|
||||
The classical controllers are made of constants somebody chose - a safety
|
||||
factor, growth clamps, PI gains. The question this module asks is what
|
||||
happens if the quantity a constant is GUESSING is measured instead.
|
||||
|
||||
root finding step-size control
|
||||
------------ -----------------
|
||||
the scale of the value axis the safety factor
|
||||
chosen -> brittle, linear chosen -> works, three constants
|
||||
measured-> superlinear measured-> works, none
|
||||
|
||||
What did NOT transfer is the credit ledger itself. A first version banked an
|
||||
accuracy margin exactly as the root-finding safeguard does; the margin turned
|
||||
out to take no part in any decision and was removed. The move that transfers
|
||||
is the more general one - replace a chosen constant by the measured value of
|
||||
what it approximates - not the particular bookkeeping that carried it there.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
|
||||
class Controller:
|
||||
name = "?"
|
||||
constants = 0
|
||||
|
||||
def reset(self):
|
||||
pass
|
||||
|
||||
def update(self, h, e, ok, order):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class Elementary(Controller):
|
||||
"""h_new = h * (1/e)^(1/(p+1)) with no safety factor and no clamps.
|
||||
|
||||
Included because it is the controller the error model alone dictates. It
|
||||
is what every practical code refuses to use, and the reason for that
|
||||
refusal is exactly what the constants in the others are buying."""
|
||||
|
||||
name = "elementary (no constants)"
|
||||
constants = 0
|
||||
|
||||
def update(self, h, e, ok, order):
|
||||
if e == 0.0:
|
||||
return h * 10.0
|
||||
return h * (1.0 / e) ** (1.0 / (order + 1))
|
||||
|
||||
|
||||
class Classical(Controller):
|
||||
"""The textbook controller: a safety factor and two growth clamps."""
|
||||
|
||||
name = "classical (scipy: safety 0.9, clamps 0.2/10)"
|
||||
constants = 3
|
||||
# exactly the constants scipy.integrate._ivp.rk uses, so the driver can
|
||||
# be validated against solve_ivp with the same control law
|
||||
SAFETY, MINF, MAXF = 0.9, 0.2, 10.0
|
||||
|
||||
def update(self, h, e, ok, order):
|
||||
if e == 0.0:
|
||||
fac = self.MAXF
|
||||
else:
|
||||
fac = self.SAFETY * (1.0 / e) ** (1.0 / (order + 1))
|
||||
fac = min(self.MAXF, max(self.MINF, fac))
|
||||
return h * fac
|
||||
|
||||
|
||||
class GustafssonPI(Controller):
|
||||
"""The PI controller used in modern codes: two gains, a safety factor and
|
||||
two clamps."""
|
||||
|
||||
name = "PI (Gustafsson, 5 constants)"
|
||||
constants = 5
|
||||
SAFETY, MINF, MAXF = 0.9, 0.2, 10.0
|
||||
K_I, K_P = 0.3, 0.4
|
||||
|
||||
def reset(self):
|
||||
self.e_prev = None
|
||||
|
||||
def update(self, h, e, ok, order):
|
||||
if e == 0.0:
|
||||
return h * self.MAXF
|
||||
p1 = order + 1
|
||||
if self.e_prev is None or not ok:
|
||||
fac = self.SAFETY * (1.0 / e) ** (1.0 / p1)
|
||||
else:
|
||||
fac = (self.SAFETY * (1.0 / e) ** (self.K_I / p1)
|
||||
* (self.e_prev / e) ** (self.K_P / p1))
|
||||
fac = min(self.MAXF, max(self.MINF, fac))
|
||||
if ok:
|
||||
self.e_prev = e
|
||||
return h * fac
|
||||
|
||||
|
||||
class MeasuredSafety(Controller):
|
||||
"""The safety factor measured instead of chosen.
|
||||
|
||||
NOTE ON THE NAME. This began as "the credit rule transferred", and it
|
||||
banked a margin exactly as the root-finding safeguard does. The margin
|
||||
turned out to play no part in the decision - the step factor was computed
|
||||
from the measured model discrepancy alone - so the banking was vestigial
|
||||
and the name was unearned. What transfers is not the credit ledger but
|
||||
the deeper move behind it: replace a chosen constant with the quantity it
|
||||
is guessing, measured from the run. The ledger is left out because it did
|
||||
nothing here, and calling this a credit controller would be a mislabel.
|
||||
|
||||
The elementary controller aims at err = tol exactly, and is refused in
|
||||
practice because the model err ~ C h^(p+1) is only approximate: aiming at
|
||||
the boundary means missing it about half the time, and every miss costs a
|
||||
whole rejected step. The classical answer is a safety factor chosen by
|
||||
hand.
|
||||
|
||||
The answer here is to measure the thing the safety factor is guessing.
|
||||
After proposing a step, the model predicts what its error will be; the
|
||||
step is taken and the error is observed. The discrepancy
|
||||
|
||||
delta = log2(observed / predicted)
|
||||
|
||||
is exactly how wrong the model was, in bits, and the next step aims at
|
||||
2^-delta instead of at 1. When the model is right, delta -> 0 and the
|
||||
controller becomes the elementary one; when the model has just been
|
||||
optimistic, the controller backs off by precisely the observed amount.
|
||||
|
||||
The first step has nothing to have learned from, so it aims at the
|
||||
tolerance itself - the same licence the root-finding guard grants.
|
||||
"""
|
||||
|
||||
name = "measured safety (no constants)"
|
||||
constants = 0
|
||||
|
||||
def reset(self):
|
||||
self.predicted = None # error the model expected for this step
|
||||
self.delta = 0.0 # bits by which the model was last wrong
|
||||
|
||||
def update(self, h, e, ok, order):
|
||||
p1 = order + 1
|
||||
if self.predicted is not None and self.predicted > 0.0 and e > 0.0:
|
||||
# only optimism matters: the model predicting too SMALL an error
|
||||
# is what causes rejections. Pessimism costs nothing and is not
|
||||
# corrected for.
|
||||
self.delta = max(0.0, math.log2(e / self.predicted))
|
||||
if e == 0.0:
|
||||
fac = 10.0
|
||||
else:
|
||||
# aim at 2^-delta of the tolerance rather than at the tolerance
|
||||
fac = (2.0 ** (-self.delta) / e) ** (1.0 / p1)
|
||||
h_new = h * fac
|
||||
# record what the model now predicts for the step just chosen, so the
|
||||
# next call can measure how wrong it was
|
||||
self.predicted = e * (h_new / h) ** p1
|
||||
return h_new
|
||||
|
||||
|
||||
ALL = [Elementary(), Classical(), GustafssonPI(), MeasuredSafety()]
|
||||
@@ -1,114 +0,0 @@
|
||||
"""Integration driver whose only original part is the step-size controller.
|
||||
|
||||
The tableau, the stage combination and the embedded error estimate are
|
||||
SciPy's - `scipy.integrate._ivp.rk.rk_step` with RK45's A, B, C and E - so
|
||||
nothing about the method itself is a transcription of ours. An earlier
|
||||
version of this file carried a hand-typed Dormand-Prince tableau; that was an
|
||||
unnecessary risk and an unnecessary thing for a reader to have to check.
|
||||
|
||||
What remains ours is the loop that decides the next step size, which is the
|
||||
object under study. Everything else is shared between controllers, so a
|
||||
difference between two runs can only come from the controller.
|
||||
|
||||
The driver is validated against SciPy's own `solve_ivp(method="RK45")`: with
|
||||
the classical controller - SciPy's own safety factor and clamps - the two
|
||||
must agree closely on the number of right-hand-side evaluations.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
import numpy as np
|
||||
from scipy.integrate._ivp.rk import RK45, rk_step
|
||||
|
||||
ORDER = RK45.error_estimator_order # 4; the step exponent is ORDER + 1
|
||||
|
||||
|
||||
class Counter:
|
||||
"""Counts evaluations of the right-hand side - the cost model."""
|
||||
|
||||
__slots__ = ("f", "n")
|
||||
|
||||
def __init__(self, f):
|
||||
self.f = f
|
||||
self.n = 0
|
||||
|
||||
def __call__(self, t, y):
|
||||
self.n += 1
|
||||
return np.asarray(self.f(t, y), dtype=float)
|
||||
|
||||
|
||||
def _norm(v, y, y_new, atol, rtol):
|
||||
scale = atol + rtol * np.maximum(np.abs(y), np.abs(y_new))
|
||||
return float(np.sqrt(np.mean((v / scale) ** 2)))
|
||||
|
||||
|
||||
def integrate(f, t0, t1, y0, controller, atol=1e-8, rtol=1e-8,
|
||||
max_steps=200000):
|
||||
"""Integrate with the given controller. Returns what the comparison
|
||||
needs. The initial step is chosen the same way for every controller, so
|
||||
none can win by guessing it better."""
|
||||
g = Counter(f)
|
||||
t = float(t0)
|
||||
y = np.asarray(y0, dtype=float)
|
||||
fv = g(t, y)
|
||||
K = np.empty((RK45.n_stages + 1, y.size), dtype=float)
|
||||
|
||||
d0 = _norm(y, y, y, atol, rtol)
|
||||
d1 = _norm(fv, y, y, atol, rtol)
|
||||
h = 1e-6 if (d0 < 1e-5 or d1 < 1e-5) else 0.01 * d0 / d1
|
||||
h = min(h, abs(t1 - t0))
|
||||
controller.reset()
|
||||
|
||||
accepted = rejected = 0
|
||||
while t < t1 and accepted + rejected < max_steps:
|
||||
h = min(h, t1 - t)
|
||||
if h <= 0.0:
|
||||
break
|
||||
y_new, f_new = rk_step(g, t, y, fv, h, RK45.A, RK45.B, RK45.C, K)
|
||||
err = np.dot(K.T, RK45.E) * h # RK45's own error estimate
|
||||
e = _norm(err, y, y_new, atol, rtol)
|
||||
ok = e <= 1.0
|
||||
h_next = controller.update(h, e, ok, ORDER)
|
||||
if ok:
|
||||
accepted += 1
|
||||
t, y, fv = t + h, y_new, f_new
|
||||
else:
|
||||
rejected += 1
|
||||
if not math.isfinite(h_next) or h_next <= 0.0:
|
||||
break
|
||||
h = h_next
|
||||
return {"t": t, "y": list(y), "nfev": g.n, "accepted": accepted,
|
||||
"rejected": rejected,
|
||||
"reached_end": abs(t - t1) < 1e-12 * max(1.0, abs(t1))}
|
||||
|
||||
|
||||
def validate_against_scipy(atol=1e-8, rtol=1e-8):
|
||||
"""The driver against SciPy's own solver, with SciPy's own controller.
|
||||
|
||||
This replaces the hand-written order check that the previous version
|
||||
needed: if the driver reproduces solve_ivp's evaluation count with the
|
||||
same control law, the tableau, the stage combination, the error estimate
|
||||
and the loop are all the library's behaviour."""
|
||||
from scipy.integrate import solve_ivp
|
||||
|
||||
from .controllers import Classical
|
||||
|
||||
out = []
|
||||
probs = [
|
||||
("decay", lambda t, y: [-y[0]], 0.0, 10.0, [1.0]),
|
||||
("forced", lambda t, y: [-y[0] + math.sin(t)], 0.0, 10.0, [0.0]),
|
||||
("lotka", lambda t, y: [1.5 * y[0] - y[0] * y[1],
|
||||
-3.0 * y[1] + y[0] * y[1]], 0.0, 15.0,
|
||||
[10.0, 5.0]),
|
||||
("vdp", lambda t, y: [y[1], 5.0 * (1 - y[0] ** 2) * y[1] - y[0]],
|
||||
0.0, 25.0, [2.0, 0.0]),
|
||||
]
|
||||
for name, f, t0, t1, y0 in probs:
|
||||
mine = integrate(f, t0, t1, y0, Classical(), atol=atol, rtol=rtol)
|
||||
ref = solve_ivp(f, (t0, t1), y0, method="RK45", atol=atol,
|
||||
rtol=rtol)
|
||||
out.append((name, mine["nfev"], int(ref.nfev),
|
||||
max(abs(a - b) for a, b in zip(mine["y"],
|
||||
ref.y[:, -1]))))
|
||||
return out
|
||||
-238
@@ -1,238 +0,0 @@
|
||||
"""Relaxation for iterative linear solvers, with the relaxation factor
|
||||
pluggable.
|
||||
|
||||
The relaxation parameter omega of SOR is the oldest hand-tuned constant in
|
||||
numerical linear algebra. Convergence requires 0 < omega < 2; the optimal
|
||||
value is known in closed form only for model problems, and everywhere else it
|
||||
is guessed, swept, or left at a folklore value - the under-relaxation factors
|
||||
of 0.7 and 0.3 in every SIMPLE-type CFD code being the best-known example.
|
||||
|
||||
The similarity formulation. SOR over-relaxes because the Gauss-Seidel
|
||||
correction under-shoots CONSISTENTLY IN THE SAME DIRECTION: the iteration
|
||||
creeps towards the solution instead of stepping to it. How consistent the
|
||||
direction is, is a similarity between successive corrections:
|
||||
|
||||
s = cos(d_k, d_{k-1}) in [-1, 1], omega = 1 + max(0, s) in [1, 2]
|
||||
|
||||
s = 1 means the correction is repeating itself exactly, so the step should be
|
||||
doubled - the maximum over-relaxation the theory allows. s <= 0 means the
|
||||
corrections are fighting each other, so no over-relaxation is warranted and
|
||||
the method falls back to Gauss-Seidel. The admissible interval of the theory
|
||||
and the range of the similarity coincide, and nothing is chosen.
|
||||
|
||||
The sweeps below are written out because SciPy ships no SOR. They are
|
||||
validated against theory: for the model Poisson problem the SOR spectral
|
||||
radius is exactly omega_opt - 1, which the measured convergence rate must
|
||||
reproduce.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
def poisson_rhs(n, rng=None):
|
||||
"""A right-hand side on an n x n interior grid."""
|
||||
if rng is None:
|
||||
rng = np.random.default_rng(0)
|
||||
return rng.standard_normal((n, n))
|
||||
|
||||
|
||||
def sweep_gs(u, f, h2, omega, coeff=None):
|
||||
"""One Gauss-Seidel / SOR sweep in place, returning the correction.
|
||||
|
||||
coeff is None for the 5-point Laplacian; otherwise (cw, ce, cs, cn, cc)
|
||||
arrays giving a variable-coefficient or anisotropic stencil.
|
||||
"""
|
||||
n = u.shape[0]
|
||||
before = u.copy()
|
||||
if coeff is None:
|
||||
for i in range(n):
|
||||
for j in range(n):
|
||||
s = 0.0
|
||||
if i > 0:
|
||||
s += u[i - 1, j]
|
||||
if i < n - 1:
|
||||
s += u[i + 1, j]
|
||||
if j > 0:
|
||||
s += u[i, j - 1]
|
||||
if j < n - 1:
|
||||
s += u[i, j + 1]
|
||||
new = (s - h2 * f[i, j]) / 4.0
|
||||
u[i, j] += omega * (new - u[i, j])
|
||||
else:
|
||||
cw, ce, cs, cn, cc = coeff
|
||||
for i in range(n):
|
||||
for j in range(n):
|
||||
s = 0.0
|
||||
if i > 0:
|
||||
s += cw[i, j] * u[i - 1, j]
|
||||
if i < n - 1:
|
||||
s += ce[i, j] * u[i + 1, j]
|
||||
if j > 0:
|
||||
s += cs[i, j] * u[i, j - 1]
|
||||
if j < n - 1:
|
||||
s += cn[i, j] * u[i, j + 1]
|
||||
new = (s - h2 * f[i, j]) / cc[i, j]
|
||||
u[i, j] += omega * (new - u[i, j])
|
||||
return u - before
|
||||
|
||||
|
||||
def residual(u, f, h2, coeff=None):
|
||||
n = u.shape[0]
|
||||
r = np.zeros_like(u)
|
||||
if coeff is None:
|
||||
for i in range(n):
|
||||
for j in range(n):
|
||||
s = -4.0 * u[i, j]
|
||||
if i > 0:
|
||||
s += u[i - 1, j]
|
||||
if i < n - 1:
|
||||
s += u[i + 1, j]
|
||||
if j > 0:
|
||||
s += u[i, j - 1]
|
||||
if j < n - 1:
|
||||
s += u[i, j + 1]
|
||||
r[i, j] = h2 * f[i, j] - s
|
||||
else:
|
||||
cw, ce, cs, cn, cc = coeff
|
||||
for i in range(n):
|
||||
for j in range(n):
|
||||
s = -cc[i, j] * u[i, j]
|
||||
if i > 0:
|
||||
s += cw[i, j] * u[i - 1, j]
|
||||
if i < n - 1:
|
||||
s += ce[i, j] * u[i + 1, j]
|
||||
if j > 0:
|
||||
s += cs[i, j] * u[i, j - 1]
|
||||
if j < n - 1:
|
||||
s += cn[i, j] * u[i, j + 1]
|
||||
r[i, j] = h2 * f[i, j] - s
|
||||
return r
|
||||
|
||||
|
||||
class Fixed:
|
||||
def __init__(self, omega):
|
||||
self.omega = omega
|
||||
self.name = f"omega = {omega:g}"
|
||||
self.constants = 1
|
||||
|
||||
def reset(self):
|
||||
pass
|
||||
|
||||
def next_omega(self, d_prev, d_now):
|
||||
return self.omega
|
||||
|
||||
|
||||
class OptimalPoisson(Fixed):
|
||||
"""The closed-form optimum for the model problem - an oracle that needs no
|
||||
tuning because the answer is a theorem."""
|
||||
|
||||
def __init__(self, n):
|
||||
w = 2.0 / (1.0 + math.sin(math.pi / (n + 1)))
|
||||
super().__init__(w)
|
||||
self.name = f"omega* = {w:.4f} (theory)"
|
||||
|
||||
|
||||
class SimilarityOmega:
|
||||
"""omega = 1 + max(0, cos(d_k, d_{k-1})).
|
||||
|
||||
The first sweep has no predecessor to compare with, so it runs at
|
||||
Gauss-Seidel - the same licence granted in the root-finding safeguard.
|
||||
"""
|
||||
|
||||
name = "similarity (no constants)"
|
||||
constants = 0
|
||||
|
||||
def reset(self):
|
||||
pass
|
||||
|
||||
def next_omega(self, d_prev, d_now):
|
||||
if d_prev is None or d_now is None:
|
||||
return 1.0
|
||||
a = float(np.vdot(d_prev, d_now))
|
||||
na = float(np.linalg.norm(d_prev)) * float(np.linalg.norm(d_now))
|
||||
if na <= 0.0 or not math.isfinite(na):
|
||||
return 1.0
|
||||
return 1.0 + max(0.0, a / na)
|
||||
|
||||
|
||||
class ContractionOmega:
|
||||
"""omega driven by the contraction the iteration actually achieves.
|
||||
|
||||
The direction-similarity rule above over-relaxes: the cosine between
|
||||
successive SOR corrections sits near 1 almost always, so 1 + cos saturates
|
||||
at 2. Measured, it picks 1.87 where the optimum is 1.777 and 1.43 where
|
||||
it is 1.2. The measure was right and the map into omega was not.
|
||||
|
||||
The map that is derived rather than chosen comes from the theory itself:
|
||||
for omega >= omega* the SOR spectral radius is exactly omega - 1. So the
|
||||
observed contraction of the residual, rho = ||r_k|| / ||r_{k-1}||, says on
|
||||
which side of the optimum the iteration is sitting:
|
||||
|
||||
rho > omega - 1 -> below the optimum, lengthen
|
||||
rho ~ omega - 1 -> at or above it, hold
|
||||
|
||||
and the correction is the discrepancy itself,
|
||||
|
||||
omega <- omega + (rho - (omega - 1)), clipped to [1, 2).
|
||||
|
||||
The clip is the convergence condition of the method, not a tuning range,
|
||||
and the first sweep runs at omega = 1 for want of a previous residual.
|
||||
|
||||
REFUTED, and worse than the rule it was meant to repair. Measured: 235
|
||||
sweeps against the direction rule's 107 on the model problem, 589 against
|
||||
102 on the anisotropic one. The update is undamped, so on the first
|
||||
sweep, with omega = 1 and rho near 1, it sets omega = 1 + (1 - 0) = 2 and
|
||||
stays pinned at the limit. Damping it would take a constant, which is the
|
||||
thing the exercise is trying to avoid, so the line stops here rather than
|
||||
trying a third map.
|
||||
"""
|
||||
|
||||
name = "contraction-driven (no constants)"
|
||||
constants = 0
|
||||
|
||||
def reset(self):
|
||||
self.omega = 1.0
|
||||
self.r_prev = None
|
||||
|
||||
def next_omega(self, d_prev, d_now):
|
||||
return self.omega
|
||||
|
||||
def note_residual(self, rn):
|
||||
if self.r_prev is not None and self.r_prev > 0.0:
|
||||
rho = rn / self.r_prev
|
||||
if math.isfinite(rho):
|
||||
w = self.omega + (rho - (self.omega - 1.0))
|
||||
# 2 is the convergence limit of SOR, 1 is Gauss-Seidel
|
||||
self.omega = min(1.999, max(1.0, w))
|
||||
self.r_prev = rn
|
||||
|
||||
|
||||
def solve(f, rule, tol=1e-8, maxit=20000, coeff=None):
|
||||
"""Iterate to ||r|| <= tol * ||r_0||. Cost is sweeps, which is what a
|
||||
practitioner pays. Everything except `rule` is shared."""
|
||||
n = f.shape[0]
|
||||
h2 = 1.0 / (n + 1) ** 2
|
||||
u = np.zeros_like(f)
|
||||
r0 = float(np.linalg.norm(residual(u, f, h2, coeff)))
|
||||
if r0 == 0.0:
|
||||
return 0, 0.0
|
||||
rule.reset()
|
||||
d1 = d2 = None # the two most recent corrections, newest first
|
||||
omegas = []
|
||||
for k in range(1, maxit + 1):
|
||||
# the rule sees the last two corrections; on the first two sweeps one
|
||||
# of them does not exist yet and the rule returns 1 (Gauss-Seidel)
|
||||
omega = rule.next_omega(d2, d1)
|
||||
omegas.append(omega)
|
||||
d1, d2 = sweep_gs(u, f, h2, omega, coeff), d1
|
||||
rn = float(np.linalg.norm(residual(u, f, h2, coeff)))
|
||||
if hasattr(rule, "note_residual"):
|
||||
rule.note_residual(rn)
|
||||
if not math.isfinite(rn):
|
||||
return maxit, float("nan")
|
||||
if rn <= tol * r0:
|
||||
return k, float(np.mean(omegas))
|
||||
return maxit, float(np.mean(omegas))
|
||||
@@ -0,0 +1,8 @@
|
||||
10:04:29 up 20 days, 19:02, 5 users, load average: 0.19, 0.90, 0.86
|
||||
7 passed in 1.37s
|
||||
K3-L2 (post hoc): corpus 4 5568 six corpora 18372 multiple/fractional 6540 APS 2960
|
||||
A' on corpus 4 against it: 178/34/28 p=6.8e-28
|
||||
per corpus: APS 2960 CHA 1751 NEW 4698 RJL 1676 APP 1719 D 5568
|
||||
violations 0
|
||||
APS runs ending with m_hat != 1: 11 of 154
|
||||
posthoc done
|
||||
@@ -17,7 +17,8 @@ import math
|
||||
from .baselines import Counter, _Budget
|
||||
from .pure import _analogy_order, _config3, _lagouanelle_raw
|
||||
|
||||
ESTIMATORS = ("none", "lag", "analogy")
|
||||
# "lag2": POSTHOC_lag_branch_2026-09-22 - Lagouanelle on the branch m >= 2 only (post hoc)
|
||||
ESTIMATORS = ("none", "lag", "analogy", "lag2")
|
||||
|
||||
|
||||
def _G(v, inv):
|
||||
@@ -49,8 +50,10 @@ def chandrupatla_transformed(f, a, b, xtol, maxfev, estimator="none", stats=None
|
||||
if estimator == "none" or len(hist) < 3:
|
||||
return
|
||||
(xp2, fp2), (xp, fp), (xc, fc) = hist[-3], hist[-2], hist[-1]
|
||||
if estimator == "lag":
|
||||
if estimator in ("lag", "lag2"):
|
||||
m = _lagouanelle_raw(xc, fc, xp, fp, xp2, fp2)
|
||||
if estimator == "lag2" and m is not None and m < 2.0:
|
||||
m = None # the fractional branch is not used (post hoc variant)
|
||||
else:
|
||||
cur = _config3(xc, fc, xp, fp, xp2, fp2)
|
||||
m = _analogy_order(cur, configs, translation=True)
|
||||
|
||||
-189
@@ -1,189 +0,0 @@
|
||||
"""Stage 15 - the credit rule in a second domain: ODE step-size control.
|
||||
|
||||
The root-finding work ended with one transferable object, the credit
|
||||
safeguard, and one honest conclusion: as a one-dimensional root finder the
|
||||
method is subsumed by Chandrupatla 1997. Adaptive step-size control is the
|
||||
natural place to test whether the transferable object transfers, because it
|
||||
has the same shape - propose a bold step, look at the outcome, adjust - and
|
||||
because every practical controller in it is made of constants somebody chose.
|
||||
|
||||
classical scipy's own: safety 0.9, clamps 0.2 and 10 3 constants
|
||||
PI the same plus two gains 5 constants
|
||||
elementary what the error model alone dictates 0 constants
|
||||
measured the safety factor MEASURED instead of chosen 0 constants
|
||||
|
||||
Everything except the controller comes from SciPy: the RK45 tableau, the
|
||||
stage combination and the embedded error estimate, via
|
||||
scipy.integrate._ivp.rk. The driver is checked against solve_ivp(RK45) with
|
||||
the same control law before any comparison is made. A difference between two
|
||||
rows can only come from the controller.
|
||||
|
||||
Cost model: evaluations of the right-hand side, as evaluations of f were in
|
||||
the root-finding work. A run that does not reach the end of the interval is
|
||||
a failure and is reported as one. Accuracy is checked against a reference so
|
||||
that a controller cannot win by being fast and wrong.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
from odestep.controllers import ALL
|
||||
from odestep.driver import integrate, validate_against_scipy
|
||||
|
||||
TOLS = (1e-4, 1e-6, 1e-8, 1e-10)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# problems: classics with known or high-accuracy reference endpoints
|
||||
# --------------------------------------------------------------------------
|
||||
def p_decay():
|
||||
f = lambda t, y: [-y[0]]
|
||||
return "exponential decay", f, 0.0, 10.0, [1.0], [math.exp(-10.0)]
|
||||
|
||||
|
||||
def p_forced():
|
||||
f = lambda t, y: [-y[0] + math.sin(t)]
|
||||
ex = 0.5 * (math.sin(10.0) - math.cos(10.0) + math.exp(-10.0))
|
||||
return "y' = -y + sin t", f, 0.0, 10.0, [0.0], [ex]
|
||||
|
||||
|
||||
def p_lotka():
|
||||
def f(t, y):
|
||||
return [1.5 * y[0] - y[0] * y[1], -3.0 * y[1] + y[0] * y[1]]
|
||||
return "Lotka-Volterra", f, 0.0, 15.0, [10.0, 5.0], None
|
||||
|
||||
|
||||
def p_vanderpol(mu=5.0):
|
||||
def f(t, y):
|
||||
return [y[1], mu * (1 - y[0] * y[0]) * y[1] - y[0]]
|
||||
return f"van der Pol mu={mu:g}", f, 0.0, 25.0, [2.0, 0.0], None
|
||||
|
||||
|
||||
def p_brusselator():
|
||||
def f(t, y):
|
||||
return [1.0 + y[0] * y[0] * y[1] - 4.0 * y[0],
|
||||
3.0 * y[0] - y[0] * y[0] * y[1]]
|
||||
return "Brusselator", f, 0.0, 20.0, [1.5, 3.0], None
|
||||
|
||||
|
||||
def p_kepler(ecc):
|
||||
"""The two-body problem at eccentricity e - DETEST's D-class, the classic
|
||||
hard non-stiff test: the solution has a sharp passage at perihelion."""
|
||||
def f(t, y):
|
||||
x1, x2, v1, v2 = y
|
||||
r3 = (x1 * x1 + x2 * x2) ** 1.5
|
||||
return [v1, v2, -x1 / r3, -x2 / r3]
|
||||
y0 = [1.0 - ecc, 0.0, 0.0, math.sqrt((1 + ecc) / (1 - ecc))]
|
||||
return f"Kepler e={ecc:g}", f, 0.0, 20.0, y0, None
|
||||
|
||||
|
||||
def p_arenstorf():
|
||||
"""The restricted three-body orbit, the standard showcase for adaptive
|
||||
step control: the step must vary by orders of magnitude along the path."""
|
||||
mu = 0.012277471
|
||||
mp = 1.0 - mu
|
||||
|
||||
def f(t, y):
|
||||
y1, y2, y3, y4 = y
|
||||
d1 = ((y1 + mu) ** 2 + y2 * y2) ** 1.5
|
||||
d2 = ((y1 - mp) ** 2 + y2 * y2) ** 1.5
|
||||
return [y3, y4,
|
||||
y1 + 2 * y4 - mp * (y1 + mu) / d1 - mu * (y1 - mp) / d2,
|
||||
y2 - 2 * y3 - mp * y2 / d1 - mu * y2 / d2]
|
||||
y0 = [0.994, 0.0, 0.0, -2.00158510637908252240537862224]
|
||||
return "Arenstorf orbit", f, 0.0, 17.0652165601579625588917206249, y0, y0
|
||||
|
||||
|
||||
PROBLEMS = [p_decay(), p_forced(), p_lotka(), p_vanderpol(), p_brusselator(),
|
||||
p_kepler(0.5), p_kepler(0.9), p_arenstorf()]
|
||||
|
||||
|
||||
def reference(f, t0, t1, y0):
|
||||
"""A reference endpoint from the same integrator at a far tighter
|
||||
tolerance - not from a different code, so that a disagreement cannot be
|
||||
blamed on a different method."""
|
||||
from odestep.controllers import Classical
|
||||
r = integrate(f, t0, t1, y0, Classical(), atol=1e-13, rtol=1e-13,
|
||||
max_steps=2000000)
|
||||
return r["y"] if r["reached_end"] else None
|
||||
|
||||
|
||||
def scaled_err(y, ref, atol, rtol):
|
||||
"""The achieved error in the SAME mixed norm the integrator controls.
|
||||
|
||||
A purely relative criterion is wrong here and was: the decay problem ends
|
||||
at exp(-10) ~ 4.5e-5, so at atol = 1e-4 the tolerance permits a hundred
|
||||
per cent relative error, and a correct run was being marked as a failure.
|
||||
What a run must be judged against is the accuracy it was asked for."""
|
||||
if ref is None:
|
||||
return float("nan")
|
||||
s = 0.0
|
||||
for a, b in zip(y, ref):
|
||||
sc = atol + rtol * abs(b)
|
||||
s += ((a - b) / sc) ** 2
|
||||
return math.sqrt(s / len(ref))
|
||||
|
||||
|
||||
def main():
|
||||
print("instrument check: the driver against scipy solve_ivp(RK45), "
|
||||
"same control law")
|
||||
worst = 0.0
|
||||
for name, mine, ref, d in validate_against_scipy():
|
||||
rel = abs(mine - ref) / ref
|
||||
worst = max(worst, rel)
|
||||
print(f" {name:10s} ours {mine:>6d} solve_ivp {ref:>6d} "
|
||||
f"{100 * (mine - ref) / ref:+6.1f}% endpoint agrees to {d:.1e}")
|
||||
if worst > 0.05:
|
||||
print(" driver does NOT reproduce the library - stopping")
|
||||
return
|
||||
print(" the tableau, the stages and the error estimate are scipy's;")
|
||||
print(" only the controller is ours\n")
|
||||
|
||||
refs = {}
|
||||
for name, f, t0, t1, y0, exact in PROBLEMS:
|
||||
refs[name] = exact if exact is not None else reference(f, t0, t1, y0)
|
||||
|
||||
grand = {c.name: [0, 0, 0] for c in ALL} # nfev, rejected, failures
|
||||
for tol in TOLS:
|
||||
print("=" * 96)
|
||||
print(f"tolerance {tol:g} (atol = rtol = tol)")
|
||||
print("=" * 96)
|
||||
print(f"{'problem':22s}" + "".join(f"{c.name.split(' ')[0]:>15s}"
|
||||
for c in ALL))
|
||||
print("-" * 96)
|
||||
for name, f, t0, t1, y0, _exact in PROBLEMS:
|
||||
cells = []
|
||||
for c in ALL:
|
||||
r = integrate(f, t0, t1, y0, c, atol=tol, rtol=tol)
|
||||
err = scaled_err(r["y"], refs[name], tol, tol)
|
||||
# local error control does not bound the global error, so the
|
||||
# bar is generous: a run fails only if it misses the accuracy
|
||||
# it asked for by more than three orders of magnitude, or does
|
||||
# not reach the end at all
|
||||
bad = (not r["reached_end"]) or (
|
||||
math.isfinite(err) and err > 1000.0)
|
||||
grand[c.name][0] += r["nfev"]
|
||||
grand[c.name][1] += r["rejected"]
|
||||
grand[c.name][2] += bad
|
||||
cells.append(f"{r['nfev']:>9d}{'!' if bad else ' '}"
|
||||
f"{r['rejected']:>4d}")
|
||||
print(f"{name:22s}" + "".join(f"{c:>15s}" for c in cells))
|
||||
print()
|
||||
|
||||
print("=" * 96)
|
||||
print("TOTALS over every problem and tolerance "
|
||||
"(nfev, rejected steps, failures)")
|
||||
print("=" * 96)
|
||||
print(f"{'controller':34s}{'constants':>11s}{'nfev':>10s}"
|
||||
f"{'rejected':>11s}{'failures':>11s}")
|
||||
print("-" * 96)
|
||||
for c in ALL:
|
||||
n, rej, bad = grand[c.name]
|
||||
print(f"{c.name:34s}{c.constants:>11d}{n:>10d}{rej:>11d}{bad:>11d}")
|
||||
print("\n'!' marks a run that did not reach the end or missed the "
|
||||
"reference by more\nthan a thousand tolerances - a controller "
|
||||
"cannot win by being fast and wrong.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,81 +0,0 @@
|
||||
"""Stage 17 - the third domain: the step size of an adaptive noise canceller.
|
||||
|
||||
Root finding: the method lands second behind Chandrupatla 1997.
|
||||
ODE step control: level with a five-constant PI controller, behind the
|
||||
three-constant classical one.
|
||||
Softening somebody else's acceptance test: fails, three times over.
|
||||
|
||||
Active noise control is the domain where the object we have - a step size
|
||||
that is a safety factor on an exact step - is exactly the object the field
|
||||
argues about, and where the standard answers are openly constant-ridden.
|
||||
|
||||
The acoustic paths, the noise regimes and the scenarios come from our earlier
|
||||
ANC benchmark (SEM repository, branch feature/sem-anc-benchmark), so the
|
||||
environment is one that existed before this question was asked.
|
||||
|
||||
Metric: noise reduction in dB on the second half of each run, so that the
|
||||
adaptation transient is not counted. Higher is better. Ten seeds per cell.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
import numpy as np
|
||||
|
||||
from anc.bench import (FS, SCENARIOS, make_primary_path, make_secondary_path,
|
||||
run_nlms)
|
||||
from anc.rules import ALL
|
||||
|
||||
N = 8000 # one second
|
||||
SEEDS = 10
|
||||
SNRS = (30.0, 15.0)
|
||||
|
||||
|
||||
def main():
|
||||
print(f"{len(SCENARIOS)} scenarios x {len(SNRS)} noise levels x {SEEDS} "
|
||||
f"seeds, {N / FS:g} s each\n")
|
||||
grand = {r.name: [] for r in ALL}
|
||||
for snr in SNRS:
|
||||
print("=" * 88)
|
||||
print(f"reference SNR {snr:g} dB noise reduction, dB (higher is "
|
||||
f"better)")
|
||||
print("=" * 88)
|
||||
print(f"{'scenario':22s}" + "".join(f"{r.name[:13]:>13s}" for r in ALL))
|
||||
print("-" * 88)
|
||||
for sname, gen in SCENARIOS:
|
||||
row = {r.name: [] for r in ALL}
|
||||
for s in range(SEEDS):
|
||||
rng = np.random.default_rng(1000 + s)
|
||||
noise = gen(N, rng)
|
||||
prim = make_primary_path(rng)
|
||||
sec = make_secondary_path(rng)
|
||||
for r in ALL:
|
||||
nr = run_nlms(noise, prim, sec, r, snr_db=snr, seed=s)
|
||||
if math.isfinite(nr):
|
||||
row[r.name].append(nr)
|
||||
grand[r.name].append(nr)
|
||||
cells = []
|
||||
for r in ALL:
|
||||
v = row[r.name]
|
||||
cells.append(f"{(sum(v) / len(v)) if v else float('nan'):>13.2f}")
|
||||
print(f"{sname:22s}" + "".join(cells), flush=True)
|
||||
print()
|
||||
|
||||
print("=" * 88)
|
||||
print("OVERALL")
|
||||
print("=" * 88)
|
||||
print(f"{'rule':26s}{'constants':>11s}{'mean NR dB':>13s}"
|
||||
f"{'worst cell':>13s}{'diverged':>11s}")
|
||||
print("-" * 88)
|
||||
for r in ALL:
|
||||
v = grand[r.name]
|
||||
n_bad = SEEDS * len(SCENARIOS) * len(SNRS) - len(v)
|
||||
print(f"{r.name:26s}{r.constants:>11d}"
|
||||
f"{(sum(v) / len(v)) if v else float('nan'):>13.2f}"
|
||||
f"{min(v) if v else float('nan'):>13.2f}{n_bad:>11d}")
|
||||
print("\n'diverged' counts runs whose residual power was not finite - the "
|
||||
"failure mode\na step size that is too large actually produces.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -12,15 +12,6 @@ falsi goes from 133 solved and 18799 evaluations to 193 and 8056.
|
||||
TEST: the same rule, the same driver, the same everything, with the
|
||||
stale-endpoint correction swapped for each classical fix in turn and for
|
||||
the credit projection. Only the repair differs.
|
||||
|
||||
CLAIM B. In active noise control the measured step size beats Kwong-Johnston
|
||||
by 7 dB.
|
||||
OBJECTION: Kwong-Johnston's gains are scale-dependent, and yours are the
|
||||
published defaults run on a signal they were not scaled for. Give it its
|
||||
best constants and the gap will close.
|
||||
TEST: an oracle over its (alpha, gamma) grid, chosen separately for every
|
||||
scenario - not an achievable controller, exactly as the ITP oracle in the
|
||||
root-finding work was not.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -32,8 +23,6 @@ import numpy as np
|
||||
from scipy.optimize._tstutils import (_APS_TESTS_DICTS,
|
||||
_CHANDRUPATLA_TESTS_DICTS)
|
||||
|
||||
from anc.bench import SCENARIOS, make_primary_path, make_secondary_path, run_nlms
|
||||
from anc.rules import Fixed, KwongJohnston, Measured, Rule
|
||||
from simroot.baselines import Counter, _Budget
|
||||
from simroot.pure import _Safeguard
|
||||
|
||||
@@ -162,69 +151,5 @@ def claim_a():
|
||||
return rows
|
||||
|
||||
|
||||
# ==========================================================================
|
||||
# CLAIM B: the measured step size against a Kwong-Johnston oracle
|
||||
# ==========================================================================
|
||||
class KJTuned(KwongJohnston):
|
||||
def __init__(self, a, g):
|
||||
self.A, self.G = a, g
|
||||
self.name = f"KJ a={a:g} g={g:g}"
|
||||
self.constants = 4
|
||||
|
||||
|
||||
N, SEEDS, SNRS = 8000, 6, (30.0, 15.0)
|
||||
KJ_GRID = [(a, g) for a in (0.9, 0.97, 0.99)
|
||||
for g in (2e-4, 2e-3, 2e-2, 2e-1)]
|
||||
|
||||
|
||||
def anc_score(rule, scen_gen, snr, seeds=SEEDS):
|
||||
vals = []
|
||||
for s in range(seeds):
|
||||
rng = np.random.default_rng(1000 + s)
|
||||
noise = scen_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)) if vals else float("nan")
|
||||
|
||||
|
||||
def claim_b():
|
||||
print("\n" + "=" * 84)
|
||||
print("CLAIM B - the measured step size against a Kwong-Johnston ORACLE")
|
||||
print("=" * 84)
|
||||
print(f"the oracle is given the best of {len(KJ_GRID)} (alpha, gamma) "
|
||||
f"pairs for every scenario\nand noise level separately - not an "
|
||||
f"achievable controller\n")
|
||||
print(f"{'scenario':22s}{'SNR':>6s}{'KJ default':>12s}{'KJ oracle':>12s}"
|
||||
f"{'measured':>11s}{'fixed 0.5':>11s}")
|
||||
print("-" * 84)
|
||||
tot = {"kj": 0.0, "orc": 0.0, "meas": 0.0, "fix": 0.0}
|
||||
cells = 0
|
||||
for sname, gen in SCENARIOS:
|
||||
for snr in SNRS:
|
||||
kj = anc_score(KwongJohnston(), gen, snr)
|
||||
orc = max(anc_score(KJTuned(a, g), gen, snr) for a, g in KJ_GRID)
|
||||
meas = anc_score(Measured(), gen, snr)
|
||||
fix = anc_score(Fixed(0.5), gen, snr)
|
||||
tot["kj"] += kj
|
||||
tot["orc"] += orc
|
||||
tot["meas"] += meas
|
||||
tot["fix"] += fix
|
||||
cells += 1
|
||||
print(f"{sname:22s}{snr:>6.0f}{kj:>12.2f}{orc:>12.2f}"
|
||||
f"{meas:>11.2f}{fix:>11.2f}", flush=True)
|
||||
print("-" * 84)
|
||||
print(f"{'mean':22s}{'':6s}{tot['kj'] / cells:>12.2f}"
|
||||
f"{tot['orc'] / cells:>12.2f}{tot['meas'] / cells:>11.2f}"
|
||||
f"{tot['fix'] / cells:>11.2f}")
|
||||
survives = tot["meas"] > tot["orc"]
|
||||
print(f"\n measured {tot['meas'] / cells:.2f} dB against the oracle "
|
||||
f"{tot['orc'] / cells:.2f} dB => "
|
||||
f"{'SURVIVES' if survives else 'FAILS'} the objection")
|
||||
return survives
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
claim_a()
|
||||
claim_b()
|
||||
|
||||
@@ -1,204 +0,0 @@
|
||||
"""Stage 20 - a prediction registered before the run, then the run.
|
||||
|
||||
The request was to find the domain where the method wins unambiguously. The
|
||||
way to get that wrong is to try domains until one of them comes out ahead;
|
||||
that is fitting, spread over time. So this file states the prediction and the
|
||||
falsification condition FIRST, from what the quantity does, and then measures.
|
||||
|
||||
WHAT MU IS. In NLMS the update w <- w + mu * e * x / ||x||^2 with mu = 1
|
||||
drives the a posteriori error of the current sample to exactly zero. Every
|
||||
practical system uses mu < 1 because part of e is measurement noise the filter
|
||||
cannot cancel, and fitting it is harmful. So mu is the SIGNAL FRACTION of the
|
||||
error, and its correct value is
|
||||
|
||||
mu* = 1 - p_noise / p_error.
|
||||
|
||||
THE PREDICTION. mu* is a property of the noise level. Therefore:
|
||||
|
||||
(A) CONTROL - when the noise level is constant, a constant mu can be exactly
|
||||
right. An oracle that picks the best fixed mu for that scenario should
|
||||
MATCH OR BEAT the measured rule. If the measured rule wins here, the
|
||||
reasoning is wrong and everything below is suspect.
|
||||
|
||||
(B) PREDICTION - when the noise level CHANGES during the run, no constant is
|
||||
right throughout, while a measured quantity tracks. The measured rule
|
||||
should BEAT the oracle fixed mu, and the margin should grow with the size
|
||||
of the change.
|
||||
|
||||
FALSIFICATION. If (B) fails - if an oracle constant matches the measured rule
|
||||
even when the noise level swings by 25 dB within a single run - then there is
|
||||
no such domain on this evidence, and that is the finding.
|
||||
|
||||
The oracle is the strongest competitor available: the best of a mu grid chosen
|
||||
separately for every scenario, with knowledge of that scenario, which no
|
||||
deployable controller has. Kwong-Johnston is given the same treatment.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
import numpy as np
|
||||
|
||||
from anc.bench import (FS, gen_broadband, gen_impulse_train, gen_multitonal,
|
||||
gen_tonal, make_primary_path, make_secondary_path)
|
||||
from anc.rules import Fixed, KwongJohnston, Measured, Rule
|
||||
|
||||
N = 16000 # two seconds
|
||||
SEEDS = 8
|
||||
MU_GRID = (0.05, 0.1, 0.2, 0.3, 0.5, 0.7, 0.85, 1.0)
|
||||
KJ_GRID = [(a, g) for a in (0.9, 0.97, 0.99)
|
||||
for g in (2e-4, 2e-3, 2e-2, 2e-1)]
|
||||
FILTER_LEN = 64
|
||||
|
||||
|
||||
class KJTuned(KwongJohnston):
|
||||
def __init__(self, a, g):
|
||||
self.A, self.G = a, g
|
||||
self.name = f"KJ({a:g},{g:g})"
|
||||
|
||||
|
||||
def run(noise, prim, sec, rule, snr_profile, seed=0):
|
||||
"""One run. snr_profile(i) gives the instantaneous SNR in dB, so the
|
||||
stationary and the varying case go through exactly the same code."""
|
||||
rng = np.random.default_rng(seed)
|
||||
n = len(noise)
|
||||
d = np.convolve(noise, prim)[:n]
|
||||
xf = np.convolve(noise, sec)[:n]
|
||||
|
||||
# the disturbance is added with a time-varying power, which is the only
|
||||
# thing that differs between the control and the prediction
|
||||
p_sig = float(np.mean(d * d)) + 1e-30
|
||||
snr = np.array([snr_profile(i / n) for i in range(n)])
|
||||
amp = np.sqrt(p_sig / (10.0 ** (snr / 10.0)))
|
||||
d = d + rng.standard_normal(n) * amp
|
||||
|
||||
w = np.zeros(FILTER_LEN)
|
||||
buf = np.zeros(FILTER_LEN)
|
||||
err = np.zeros(n)
|
||||
rule.reset()
|
||||
for i in range(n):
|
||||
buf[1:] = buf[:-1]
|
||||
buf[0] = xf[i]
|
||||
e = d[i] - float(np.dot(w, buf))
|
||||
err[i] = e
|
||||
nrm = float(np.dot(buf, buf)) + 1e-6
|
||||
mu = rule.step(e, buf, nrm)
|
||||
if mu != 0.0:
|
||||
w += mu * e * buf / nrm
|
||||
rule.observe(e, d[i] - float(np.dot(w, buf)), mu)
|
||||
else:
|
||||
rule.observe(e, e, 0.0)
|
||||
# The measurement window matters, and the first version got it wrong.
|
||||
# Scoring only the second half made the varying-noise scenarios
|
||||
# indistinguishable from a constant run at their FINAL level - the ramp
|
||||
# and the step have both arrived by then, so the metric never saw the
|
||||
# transition and the prediction could not have been confirmed even if it
|
||||
# were true. The window now excludes only the initial adaptation
|
||||
# transient, which every controller pays alike.
|
||||
warm = n // 10
|
||||
p_in = float(np.mean(d[warm:] ** 2))
|
||||
p_out = float(np.mean(err[warm:] ** 2))
|
||||
if p_out <= 0.0 or not math.isfinite(p_out):
|
||||
return float("nan")
|
||||
return 10.0 * math.log10(p_in / p_out)
|
||||
|
||||
|
||||
def score(rule, gen, profile, seeds=SEEDS):
|
||||
vals = []
|
||||
for s in range(seeds):
|
||||
rng = np.random.default_rng(2000 + s)
|
||||
noise = gen(N, rng)
|
||||
nr = run(noise, make_primary_path(rng), make_secondary_path(rng),
|
||||
rule, profile, seed=s)
|
||||
if math.isfinite(nr):
|
||||
vals.append(nr)
|
||||
return (sum(vals) / len(vals)) if vals else float("nan")
|
||||
|
||||
|
||||
def oracle_fixed(gen, profile):
|
||||
"""The ceiling of what any constant can do on this scenario: the best mu
|
||||
of the grid, chosen WITH knowledge of the scenario."""
|
||||
best, arg = -1e30, None
|
||||
for mu in MU_GRID:
|
||||
v = score(Fixed(mu), gen, profile)
|
||||
if math.isfinite(v) and v > best:
|
||||
best, arg = v, mu
|
||||
return best, arg
|
||||
|
||||
|
||||
def oracle_kj(gen, profile):
|
||||
best, arg = -1e30, None
|
||||
for a, g in KJ_GRID:
|
||||
v = score(KJTuned(a, g), gen, profile)
|
||||
if math.isfinite(v) and v > best:
|
||||
best, arg = v, (a, g)
|
||||
return best, arg
|
||||
|
||||
|
||||
# --------------------------------------------------------------- scenarios --
|
||||
def sig_tonal(n, rng):
|
||||
return gen_tonal(n, rng)
|
||||
|
||||
|
||||
def sig_cabin(n, rng):
|
||||
s = gen_multitonal(n, rng, f0=80.0) + 0.6 * gen_broadband(n, rng) \
|
||||
+ 0.4 * gen_impulse_train(n, rng)
|
||||
return s / (np.max(np.abs(s)) + 1e-12)
|
||||
|
||||
|
||||
SIGNALS = [("tonal", sig_tonal), ("car cabin", sig_cabin)]
|
||||
|
||||
# noise-level profiles, as functions of the fraction of the run elapsed
|
||||
FLAT_30 = lambda u: 30.0
|
||||
FLAT_15 = lambda u: 15.0
|
||||
FLAT_5 = lambda u: 5.0
|
||||
SWING_10 = lambda u: 25.0 - 10.0 * u
|
||||
SWING_25 = lambda u: 30.0 - 25.0 * u
|
||||
STEP_25 = lambda u: 30.0 if u < 0.5 else 5.0
|
||||
|
||||
PROFILES = [("flat 30 dB", FLAT_30, "control"),
|
||||
("flat 15 dB", FLAT_15, "control"),
|
||||
("flat 5 dB", FLAT_5, "control"),
|
||||
("ramp 25->15 dB", SWING_10, "prediction"),
|
||||
("ramp 30->5 dB", SWING_25, "prediction"),
|
||||
("step 30->5 dB", STEP_25, "prediction")]
|
||||
|
||||
|
||||
def main():
|
||||
print(__doc__.split("The oracle is")[0])
|
||||
print("=" * 92)
|
||||
print("THE RUN")
|
||||
print("=" * 92)
|
||||
print(f"{'signal':11s}{'noise profile':17s}{'kind':12s}"
|
||||
f"{'oracle mu':>11s}{'best mu':>9s}{'oracle KJ':>11s}"
|
||||
f"{'measured':>10s}{'verdict':>12s}")
|
||||
print("-" * 92)
|
||||
control_wins = pred_wins = control_n = pred_n = 0
|
||||
for signame, gen in SIGNALS:
|
||||
for pname, prof, kind in PROFILES:
|
||||
om, arg = oracle_fixed(gen, prof)
|
||||
okj, _a = oracle_kj(gen, prof)
|
||||
ms = score(Measured(), gen, prof)
|
||||
beats = ms > max(om, okj)
|
||||
if kind == "control":
|
||||
control_n += 1
|
||||
control_wins += beats
|
||||
else:
|
||||
pred_n += 1
|
||||
pred_wins += beats
|
||||
print(f"{signame:11s}{pname:17s}{kind:12s}{om:>11.2f}"
|
||||
f"{arg:>9.2f}{okj:>11.2f}{ms:>10.2f}"
|
||||
f"{'MEASURED' if beats else 'oracle':>12s}", flush=True)
|
||||
print("-" * 92)
|
||||
print(f"\n control cells (constant noise): measured wins "
|
||||
f"{control_wins} of {control_n}")
|
||||
print(f" predicted cells (varying noise): measured wins "
|
||||
f"{pred_wins} of {pred_n}")
|
||||
ok = control_wins == 0 and pred_wins == pred_n
|
||||
print(f"\n the prediction is {'CONFIRMED' if ok else 'NOT confirmed'}: "
|
||||
f"it required the measured rule to lose every\n control cell and "
|
||||
f"win every predicted one.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,131 +0,0 @@
|
||||
"""Stage 21 - the WHOLE similarity family for mu, against the oracle constant.
|
||||
|
||||
Why this is not another throw of the dice. The first two constant-free rules
|
||||
estimated mu from VARIANCES - a noise floor, a model discrepancy. Neither is
|
||||
a similarity, and this project is about similarity. The formulation the
|
||||
thesis dictates asks how much the error RESEMBLES what the filter can remove,
|
||||
and the filter can only remove what correlates with its input. With
|
||||
|
||||
d = 1 - |<e, x>| / (||e|| ||x||) the dissimilarity, 0 = removable
|
||||
|
||||
the family is mu = S(d) over the same nine similarity functions the
|
||||
root-finding work used. Testing one member would be as incomplete here as it
|
||||
would have been there.
|
||||
|
||||
A PREDICTION, FROM THE LAW OF THE FIRST DOMAIN. The contraction law found in
|
||||
root finding says the shape of S is nearly irrelevant and the slope of (1 - S)
|
||||
at the origin decides everything: functions approaching 1 linearly work,
|
||||
functions approaching it quadratically do not. If that law is a property of
|
||||
similarity rules and not of root finding, it must order this family too:
|
||||
|
||||
predicted strong 1 - d, e^-d, 1/(1+d) slope 1
|
||||
predicted middle (1+d)^-2, 1 - sqrt(d), 1 - d^(1/3)
|
||||
predicted weak 1/(1+d^2), e^-(d^2), 1/sqrt(1+d^2) slope 0
|
||||
|
||||
THE BAR FOR THE METHOD, STATED BEFORE THE RUN. The competitor is the
|
||||
strongest a constant can be: the best mu of a grid chosen with knowledge of
|
||||
the scenario and the noise profile, which no deployable controller has. For
|
||||
the claim to be bullet-proof the best similarity must beat that oracle on a
|
||||
MAJORITY of the twelve cells, stationary cells included, where a constant is
|
||||
ideal by construction.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
from anc.rules import CosineSimilarity, Fixed, Measured
|
||||
from simroot.pure import PURE_SIMS
|
||||
from stage20_prediction import MU_GRID, PROFILES, SIGNALS, score
|
||||
|
||||
# the nine functions of the paper, applied to the dissimilarity d = 1 - |cos|,
|
||||
# plus the identity S(d) = 1 - d which is the raw cosine
|
||||
FAMILY = [("1 - d (raw cosine)", None, 1.0)]
|
||||
_SLOPE = {"ratio": 1.0, "exp_abs": 1.0, "rational": 1.0, "ratio_soft": 2.0,
|
||||
"sqrt": math.inf, "cbrt": math.inf, "rat_sq": 0.0, "exp_sq": 0.0,
|
||||
"cosine": 0.0}
|
||||
for _name in ("ratio", "exp_abs", "rational", "ratio_soft", "sqrt", "cbrt",
|
||||
"rat_sq", "exp_sq", "cosine"):
|
||||
FAMILY.append((f"S = {_name}", PURE_SIMS[_name], _SLOPE[_name]))
|
||||
|
||||
|
||||
def make(S):
|
||||
r = CosineSimilarity()
|
||||
r.S = S
|
||||
return r
|
||||
|
||||
|
||||
def main():
|
||||
print(__doc__.split("THE BAR")[0])
|
||||
|
||||
# ---------------- part 1: does the law order this family too? ----------
|
||||
print("=" * 88)
|
||||
print("1. THE FAMILY, ranked - the law of the first domain predicts the "
|
||||
"order")
|
||||
print("=" * 88)
|
||||
print(f"{'similarity':22s}{'slope of (1-S) at 0':>21s}"
|
||||
f"{'mean NR over 12 cells':>24s}")
|
||||
print("-" * 88)
|
||||
scores = []
|
||||
for label, S, slope in FAMILY:
|
||||
vals = []
|
||||
for _sig, gen in SIGNALS:
|
||||
for _pn, prof, _k in PROFILES:
|
||||
v = score(make(S), gen, prof)
|
||||
if math.isfinite(v):
|
||||
vals.append(v)
|
||||
m = sum(vals) / len(vals)
|
||||
scores.append((label, slope, m))
|
||||
print(f"{label:22s}{slope:>21.3g}{m:>24.2f}", flush=True)
|
||||
print("-" * 88)
|
||||
ranked = sorted(scores, key=lambda t: t[2], reverse=True)
|
||||
print("\n ranked best first:")
|
||||
for label, slope, m in ranked:
|
||||
print(f" {label:22s} slope {slope:>8.3g} {m:6.2f} dB")
|
||||
lin = [m for _l, s, m in scores if s == 1.0]
|
||||
quad = [m for _l, s, m in scores if s == 0.0]
|
||||
print(f"\n slope 1 mean {sum(lin) / len(lin):.2f} dB, "
|
||||
f"slope 0 mean {sum(quad) / len(quad):.2f} dB => the law "
|
||||
f"{'HOLDS' if min(lin) > max(quad) else 'does NOT hold'} here")
|
||||
|
||||
# ---------------- part 2: the best member against the oracle -----------
|
||||
best_label, _s, _m = ranked[0]
|
||||
best_S = dict((l, S) for l, S, _sl in FAMILY)[best_label]
|
||||
print("\n" + "=" * 88)
|
||||
print(f"2. {best_label} against the per-scenario oracle constant")
|
||||
print("=" * 88)
|
||||
print(f"{'signal':11s}{'noise profile':17s}{'kind':12s}"
|
||||
f"{'oracle mu':>11s}{'best mu':>9s}{'variance':>10s}"
|
||||
f"{'similarity':>12s}{'verdict':>10s}")
|
||||
print("-" * 88)
|
||||
wins = {"control": 0, "prediction": 0}
|
||||
cells = {"control": 0, "prediction": 0}
|
||||
margins = []
|
||||
for signame, gen in SIGNALS:
|
||||
for pname, prof, kind in PROFILES:
|
||||
best, arg = -1e30, None
|
||||
for mu in MU_GRID:
|
||||
v = score(Fixed(mu), gen, prof)
|
||||
if math.isfinite(v) and v > best:
|
||||
best, arg = v, mu
|
||||
var = score(Measured(), gen, prof)
|
||||
sim = score(make(best_S), gen, prof)
|
||||
beats = sim > best
|
||||
cells[kind] += 1
|
||||
wins[kind] += beats
|
||||
margins.append(sim - best)
|
||||
print(f"{signame:11s}{pname:17s}{kind:12s}{best:>11.2f}"
|
||||
f"{arg:>9.2f}{var:>10.2f}{sim:>12.2f}"
|
||||
f"{'SIMILARITY' if beats else 'oracle':>10s}", flush=True)
|
||||
tot_w = wins["control"] + wins["prediction"]
|
||||
tot_c = cells["control"] + cells["prediction"]
|
||||
print("-" * 88)
|
||||
print(f"\n beats the per-scenario oracle on {tot_w} of {tot_c} cells"
|
||||
f" (stationary {wins['control']}/{cells['control']}, "
|
||||
f"varying {wins['prediction']}/{cells['prediction']})")
|
||||
print(f" mean margin {sum(margins) / len(margins):+.2f} dB")
|
||||
print(f"\n the bar was a majority of the twelve. "
|
||||
f"{'MET' if tot_w > tot_c / 2 else 'NOT MET'}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,99 +0,0 @@
|
||||
"""Stage 22 - the comparison a practitioner actually faces.
|
||||
|
||||
Every comparison so far gave the constant an oracle: the best mu of a grid,
|
||||
chosen with knowledge of the scenario and the noise profile. That is the
|
||||
right bar for "can measurement beat perfect tuning?", and the answer is no,
|
||||
0 of 12.
|
||||
|
||||
But reporting only that bar is its own distortion, the mirror image of
|
||||
reporting only a bar we pass. A deployable controller does not know the
|
||||
scenario. The question a practitioner faces is: I must pick mu once, for a
|
||||
device that will meet conditions I have not seen - how does a fixed choice
|
||||
compare with measuring?
|
||||
|
||||
So this file asks the same twelve cells a different, and equally
|
||||
pre-registered, question:
|
||||
|
||||
a single mu, fixed across ALL cells (as a shipped device would have),
|
||||
against the similarity rule, which is fixed across all cells too because
|
||||
it has nothing to fix.
|
||||
|
||||
The constant is still given an advantage: it is the best single value over
|
||||
the whole grid, chosen with hindsight over all twelve cells - a device maker
|
||||
who guessed perfectly on the first try. What it is not given is a different
|
||||
value per scenario.
|
||||
|
||||
Both bars are reported together. Neither alone is the answer.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
from anc.rules import CosineSimilarity, Fixed, KwongJohnston, Measured
|
||||
from simroot.pure import PURE_SIMS
|
||||
from stage20_prediction import MU_GRID, PROFILES, SIGNALS, score
|
||||
|
||||
CELLS = [(sig, gen, pn, prof, kind)
|
||||
for sig, gen in SIGNALS for pn, prof, kind in PROFILES]
|
||||
|
||||
|
||||
def member(name):
|
||||
r = CosineSimilarity()
|
||||
r.S = None if name is None else PURE_SIMS[name]
|
||||
return r
|
||||
|
||||
|
||||
def per_cell(rule):
|
||||
return [score(rule, gen, prof) for _s, gen, _pn, prof, _k in CELLS]
|
||||
|
||||
|
||||
def main():
|
||||
print(__doc__.split("Both bars")[0])
|
||||
print("=" * 90)
|
||||
|
||||
sim = per_cell(member("cbrt"))
|
||||
var = per_cell(Measured())
|
||||
kj = per_cell(KwongJohnston())
|
||||
fixed = {mu: per_cell(Fixed(mu)) for mu in MU_GRID}
|
||||
|
||||
# bar 1: the per-cell oracle - a different constant for every scenario
|
||||
oracle_cell = [max(fixed[mu][i] for mu in MU_GRID) for i in range(len(CELLS))]
|
||||
# bar 2: the best SINGLE constant across all cells, chosen with hindsight
|
||||
best_single_mu = max(MU_GRID, key=lambda mu: sum(fixed[mu]))
|
||||
single = fixed[best_single_mu]
|
||||
|
||||
print(f"{'signal':11s}{'profile':17s}{'per-cell oracle':>17s}"
|
||||
f"{'best single mu':>16s}{'similarity':>12s}{'KJ':>8s}")
|
||||
print("-" * 90)
|
||||
for i, (s, _g, pn, _p, _k) in enumerate(CELLS):
|
||||
print(f"{s:11s}{pn:17s}{oracle_cell[i]:>17.2f}{single[i]:>16.2f}"
|
||||
f"{sim[i]:>12.2f}{kj[i]:>8.2f}", flush=True)
|
||||
print("-" * 90)
|
||||
n = len(CELLS)
|
||||
print(f"{'mean':28s}{sum(oracle_cell) / n:>17.2f}{sum(single) / n:>16.2f}"
|
||||
f"{sum(sim) / n:>12.2f}{sum(kj) / n:>8.2f}")
|
||||
print(f"\n the best single constant over all twelve cells is "
|
||||
f"mu = {best_single_mu:g}")
|
||||
|
||||
w_oracle = sum(1 for i in range(n) if sim[i] > oracle_cell[i])
|
||||
w_single = sum(1 for i in range(n) if sim[i] > single[i])
|
||||
w_kj = sum(1 for i in range(n) if sim[i] > kj[i])
|
||||
w_var = sum(1 for i in range(n) if sim[i] > var[i])
|
||||
print(f"\n similarity beats the per-cell oracle on {w_oracle:>2d}/{n}"
|
||||
f" (mean margin {sum(sim[i] - oracle_cell[i] for i in range(n)) / n:+.2f} dB)")
|
||||
print(f" similarity beats the best single constant on {w_single:>2d}/{n}"
|
||||
f" (mean margin {sum(sim[i] - single[i] for i in range(n)) / n:+.2f} dB)")
|
||||
print(f" similarity beats Kwong-Johnston on {w_kj:>2d}/{n}"
|
||||
f" (mean margin {sum(sim[i] - kj[i] for i in range(n)) / n:+.2f} dB)")
|
||||
print(f" similarity beats the variance rule on {w_var:>2d}/{n}"
|
||||
f" (mean margin {sum(sim[i] - var[i] for i in range(n)) / n:+.2f} dB)")
|
||||
|
||||
print("\n Every arbitrary constant, cell by cell:")
|
||||
print(f" {'mu':>6s}{'mean dB':>10s}{'cells where similarity wins':>30s}")
|
||||
for mu in MU_GRID:
|
||||
w = sum(1 for i in range(n) if sim[i] > fixed[mu][i])
|
||||
print(f" {mu:>6g}{sum(fixed[mu]) / n:>10.2f}{w:>25d}/{n}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,145 +0,0 @@
|
||||
"""Stage 23 - the relaxation parameter, which is where this principle belongs.
|
||||
|
||||
The step rule was stuck on equations that bisection also solves. The wider
|
||||
statement is: wherever an iterative method has a step length, and there is a
|
||||
current result and a required one, the step should follow how SIMILAR the two
|
||||
are. The oldest hand-tuned step length in numerical computing is the
|
||||
relaxation factor omega of SOR.
|
||||
|
||||
convergence needs 0 < omega < 2
|
||||
the optimum known in closed form only for model problems
|
||||
everywhere else guessed, swept, or left at folklore values
|
||||
|
||||
The similarity form. SOR over-relaxes because the Gauss-Seidel correction
|
||||
under-shoots consistently in the same direction. How consistent that
|
||||
direction is, is the cosine between successive corrections, and
|
||||
|
||||
omega = 1 + max(0, cos(d_k, d_{k-1})) lands exactly in [1, 2]
|
||||
|
||||
so the range of the similarity and the admissible interval of the theory
|
||||
coincide. No constant is chosen.
|
||||
|
||||
The instrument is checked first: on the model Poisson problem the SOR spectral
|
||||
radius is exactly omega* - 1, so the measured contraction of the oracle must
|
||||
reproduce it. If it does not, the sweep is wrong and nothing below counts.
|
||||
|
||||
Then three problem classes, in increasing order of how unavailable the optimum
|
||||
is:
|
||||
|
||||
model Poisson omega* is a theorem - we should LOSE
|
||||
anisotropic omega* has no closed form - open
|
||||
variable coefficient / convection - no theory at all - open
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
import numpy as np
|
||||
|
||||
from relax.sor import (Fixed, OptimalPoisson, SimilarityOmega, poisson_rhs,
|
||||
residual, solve, sweep_gs)
|
||||
|
||||
TOL = 1e-8
|
||||
|
||||
|
||||
def theory_check(n=16):
|
||||
"""The SOR spectral radius for the model problem is omega* - 1."""
|
||||
w = 2.0 / (1.0 + math.sin(math.pi / (n + 1)))
|
||||
f = poisson_rhs(n)
|
||||
h2 = 1.0 / (n + 1) ** 2
|
||||
u = np.zeros_like(f)
|
||||
rs = []
|
||||
for _ in range(60):
|
||||
sweep_gs(u, f, h2, w)
|
||||
rs.append(float(np.linalg.norm(residual(u, f, h2))))
|
||||
ratios = [rs[i + 1] / rs[i] for i in range(30, len(rs) - 1) if rs[i] > 0]
|
||||
measured = sum(ratios) / len(ratios) if ratios else float("nan")
|
||||
return w, w - 1.0, measured
|
||||
|
||||
|
||||
def anisotropic(n, eps):
|
||||
"""-u_xx - eps*u_yy: the classic problem where a single omega is wrong."""
|
||||
cw = np.full((n, n), 1.0)
|
||||
ce = np.full((n, n), 1.0)
|
||||
cs = np.full((n, n), eps)
|
||||
cn = np.full((n, n), eps)
|
||||
cc = np.full((n, n), 2.0 + 2.0 * eps)
|
||||
return (cw, ce, cs, cn, cc)
|
||||
|
||||
|
||||
def variable(n, rng):
|
||||
"""A jumping diffusion coefficient - no closed-form optimum exists."""
|
||||
a = np.exp(rng.uniform(-2.0, 2.0, (n, n)))
|
||||
cw = np.roll(a, 1, 0) * 0.5 + a * 0.5
|
||||
ce = np.roll(a, -1, 0) * 0.5 + a * 0.5
|
||||
cs = np.roll(a, 1, 1) * 0.5 + a * 0.5
|
||||
cn = np.roll(a, -1, 1) * 0.5 + a * 0.5
|
||||
cc = cw + ce + cs + cn
|
||||
return (cw, ce, cs, cn, cc)
|
||||
|
||||
|
||||
def convection(n, pe):
|
||||
"""Convection-diffusion, upwind: non-symmetric, and the SOR theory for
|
||||
symmetric problems does not apply at all."""
|
||||
cw = np.full((n, n), 1.0 + pe)
|
||||
ce = np.full((n, n), 1.0)
|
||||
cs = np.full((n, n), 1.0)
|
||||
cn = np.full((n, n), 1.0)
|
||||
cc = np.full((n, n), 4.0 + pe)
|
||||
return (cw, ce, cs, cn, cc)
|
||||
|
||||
|
||||
def main():
|
||||
n = 24
|
||||
w_opt, rho_theory, rho_measured = theory_check(n)
|
||||
print("instrument check on the model Poisson problem")
|
||||
print(f" omega* = {w_opt:.6f} spectral radius: theory "
|
||||
f"{rho_theory:.6f}, measured {rho_measured:.6f}")
|
||||
if abs(rho_theory - rho_measured) > 0.02:
|
||||
print(" the sweep does NOT reproduce the theory - stopping")
|
||||
return
|
||||
print(" the sweep reproduces the theory\n")
|
||||
|
||||
rng = np.random.default_rng(7)
|
||||
f = poisson_rhs(n, rng)
|
||||
grid = [
|
||||
("model Poisson", None, "omega* is a theorem"),
|
||||
("anisotropic eps=0.1", anisotropic(n, 0.1), "no closed form"),
|
||||
("anisotropic eps=0.01", anisotropic(n, 0.01), "no closed form"),
|
||||
("variable coefficient", variable(n, np.random.default_rng(3)),
|
||||
"no theory"),
|
||||
("convection Pe=2", convection(n, 2.0), "non-symmetric"),
|
||||
("convection Pe=10", convection(n, 10.0), "non-symmetric"),
|
||||
]
|
||||
sweep_grid = (1.0, 1.2, 1.4, 1.6, 1.8, 1.9, w_opt)
|
||||
|
||||
print("=" * 96)
|
||||
print("sweeps to ||r|| <= 1e-8 ||r0|| (lower is better; 20000 = did "
|
||||
"not converge)")
|
||||
print("=" * 96)
|
||||
print(f"{'problem':24s}{'why':16s}{'Gauss-Seidel':>14s}"
|
||||
f"{'best fixed w':>14s}{'(that w)':>10s}{'similarity':>12s}"
|
||||
f"{'mean w':>8s}")
|
||||
print("-" * 96)
|
||||
wins = total = 0
|
||||
for label, coeff, why in grid:
|
||||
gs, _ = solve(f, Fixed(1.0), tol=TOL, coeff=coeff)
|
||||
best, bw = 10 ** 9, None
|
||||
for w in sweep_grid:
|
||||
k, _ = solve(f, Fixed(w), tol=TOL, coeff=coeff)
|
||||
if k < best:
|
||||
best, bw = k, w
|
||||
sim, mw = solve(f, SimilarityOmega(), tol=TOL, coeff=coeff)
|
||||
total += 1
|
||||
wins += sim < best
|
||||
print(f"{label:24s}{why:16s}{gs:>14d}{best:>14d}{bw:>10.3f}"
|
||||
f"{sim:>12d}{mw:>8.3f}", flush=True)
|
||||
print("-" * 96)
|
||||
print(f"\n similarity beats the best swept omega on {wins} of {total} "
|
||||
f"problems")
|
||||
print(" the swept omega is an oracle: it costs seven full solves to "
|
||||
"find,\n and the similarity rule costs none.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,84 @@
|
||||
"""POSTHOC_lag_branch_2026-09-22: Chandrupatla on sign(f)|f|^(1/m_hat) with Lagouanelle's estimate
|
||||
restricted to the branch m >= 2 (K3-L2), post hoc, with the harness of sweep_fourth.py. The
|
||||
registered K3 run (sweep_estimated.py / .json) is not touched.
|
||||
|
||||
python -u sweep_estimated_posthoc.py run -> sweep_estimated_posthoc.json, report
|
||||
python -u sweep_estimated_posthoc.py --report
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import functools
|
||||
import json
|
||||
import sys
|
||||
from multiprocessing import Pool
|
||||
|
||||
import sweep_fourth
|
||||
import sweep_full
|
||||
from simroot.transformed import chandrupatla_transformed
|
||||
from sweep_controls import nonsimple
|
||||
from sweep_oos import sign_p
|
||||
|
||||
sweep_full.EXTRA_BASELINES["k3_lag2"] = functools.partial(chandrupatla_transformed, estimator="lag2")
|
||||
sweep_fourth.CONFIGS["k3_lag2"] = ("base", "k3_lag2")
|
||||
SETS = ("APS", "CHA", "NEW", "RJL", "APP", "D")
|
||||
|
||||
|
||||
def job(which):
|
||||
return sweep_fourth.evaluate(("k3_lag2", which))[1]
|
||||
|
||||
|
||||
def cost(v):
|
||||
return v[0] if v[1] else 200
|
||||
|
||||
|
||||
def tot(r, keep=None):
|
||||
return sum(cost(v) for p, v in r.items() if keep is None or keep(p))
|
||||
|
||||
|
||||
def held_on_simple_roots():
|
||||
"""How many APS runs end holding m_hat != 1 (fresh)."""
|
||||
n = 0
|
||||
for s, pid, f, a, b, _r in sweep_full.load_problems():
|
||||
if s != "APS":
|
||||
continue
|
||||
st = {}
|
||||
chandrupatla_transformed(f, a, b, 1e-12, 200, estimator="lag2", stats=st)
|
||||
n += st.get("m_hat", 1.0) != 1.0
|
||||
return n
|
||||
|
||||
|
||||
def report(R):
|
||||
seen = json.load(open("sweep_fourth_seen.json", encoding="utf8"))
|
||||
four = json.load(open("sweep_fourth_fourth.json", encoding="utf8"))
|
||||
ap = dict(seen["Aprime"])
|
||||
ap.update(four["Aprime"])
|
||||
K1 = json.load(open("sweep_controls.json", encoding="utf8"))["K1"]
|
||||
grp = sum(tot(R[s], lambda p, _m=K1[s]["mstar"]: nonsimple(_m[p])) for s in ("CHA", "NEW", "D"))
|
||||
ps = list(ap["D"])
|
||||
w = sum(cost(ap["D"][p]) < cost(R["D"][p]) for p in ps)
|
||||
l = sum(cost(ap["D"][p]) > cost(R["D"][p]) for p in ps)
|
||||
print(f"K3-L2 (post hoc): corpus 4 {tot(R['D'])} six corpora {sum(tot(R[s]) for s in SETS)} "
|
||||
f"multiple/fractional {grp} APS {tot(R['APS'])}")
|
||||
print(f" A' on corpus 4 against it: {w}/{len(ps) - w - l}/{l} p={sign_p(w, l):.2g}")
|
||||
print(f" per corpus: " + " ".join(f"{s} {tot(R[s])}" for s in SETS))
|
||||
print(f" violations {sum(v[2] for s in SETS for v in R[s].values())}")
|
||||
print(f" APS runs ending with m_hat != 1: {held_on_simple_roots()} of 154")
|
||||
|
||||
|
||||
def main():
|
||||
if "--report" not in sys.argv:
|
||||
with Pool(2) as pool:
|
||||
parts = pool.map(job, ("seen", "fourth"))
|
||||
R = {}
|
||||
for p in parts:
|
||||
R.update(p)
|
||||
with open("sweep_estimated_posthoc.json", "w", encoding="utf8") as fh:
|
||||
json.dump(R, fh)
|
||||
with open("sweep_estimated_posthoc.json", encoding="utf8") as fh:
|
||||
R = json.load(fh)
|
||||
report(R)
|
||||
print("posthoc done")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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)
|
||||
@@ -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
|
||||
@@ -23,6 +23,15 @@ def test_without_an_estimate_it_is_chandrupatla_bit_for_bit():
|
||||
assert not bad, bad[:5]
|
||||
|
||||
|
||||
def test_the_post_hoc_branch_never_holds_a_fractional_order():
|
||||
"""POSTHOC_lag_branch_2026-09-22: 'lag2' takes only readings m >= 2."""
|
||||
for s, pid, f, a, b, _r in PROBS:
|
||||
st = {}
|
||||
x, n, ok = chandrupatla_transformed(f, a, b, 1e-12, 200, estimator="lag2", stats=st)
|
||||
assert min(a, b) <= x <= max(a, b), pid
|
||||
assert st.get("m_hat", 1.0) == 1.0 or st["m_hat"] >= 2.0, (pid, st)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("estimator", ("lag", "analogy"))
|
||||
def test_with_an_estimate_it_still_brackets(estimator):
|
||||
for s, pid, f, a, b, _r in PROBS:
|
||||
|
||||
@@ -222,6 +222,8 @@ def main():
|
||||
verify_flat_regime_and_oos()
|
||||
verify_revision1()
|
||||
verify_controls()
|
||||
verify_revision2()
|
||||
verify_revision3()
|
||||
|
||||
print()
|
||||
if FAILS:
|
||||
@@ -1089,5 +1091,128 @@ def verify_controls():
|
||||
want)
|
||||
|
||||
|
||||
def verify_revision2():
|
||||
"""Revision 2 (points 3, 4, 5, 7)."""
|
||||
import math
|
||||
print("\nRevision 2:")
|
||||
|
||||
def cost(v):
|
||||
return v[0] if v[1] else 200
|
||||
|
||||
def tot(r, keep=None):
|
||||
return sum(cost(v) for p, v in r.items() if keep is None or keep(p))
|
||||
|
||||
def sign_p(w, l):
|
||||
k, n = min(w, l), w + l
|
||||
return min(1.0, 2.0 * sum(math.comb(n, i) for i in range(k + 1)) / 2.0 ** n)
|
||||
sets = ("APS", "CHA", "NEW", "RJL", "APP", "D")
|
||||
K = _load("sweep_estimated.json")
|
||||
seen, four = _load("sweep_fourth_seen.json"), _load("sweep_fourth_fourth.json")
|
||||
ap, ch = dict(seen["Aprime"]), dict(seen["chandrupatla"])
|
||||
ap.update(four["Aprime"])
|
||||
ch.update(four["chandrupatla"])
|
||||
K1 = _load("sweep_controls.json")["K1"]
|
||||
nonsimple = lambda m: m >= 2.0 or m <= 2.0 / 3.0 # noqa: E731
|
||||
|
||||
def group(r):
|
||||
return sum(tot(r[s], lambda p, _m=K1[s]["mstar"]: nonsimple(_m[p])) for s in ("CHA", "NEW", "D"))
|
||||
rows = {"A'": ap, "K3A": K["k3_analogy"], "K3L": K["k3_lag"], "Ch(f)": ch}
|
||||
want = {"A'": (3656, 13032, 4042, 2259), "K3A": (3806, 15733, 2809, 3453),
|
||||
"K3L": (7526, 30991, 6041, 7215), "Ch(f)": (5717, 16481, 7011, 2595)}
|
||||
for k, r in rows.items():
|
||||
check(f"Table 12 {k}", (tot(r["D"]), sum(tot(r[s]) for s in sets), group(r), tot(r["APS"])), want[k])
|
||||
oracle = sum(tot(K1[s]["chandrupatla_g"], lambda p, _m=K1[s]["mstar"]: nonsimple(_m[p]))
|
||||
for s in ("CHA", "NEW", "D"))
|
||||
check("Table 12 oracle row (Table 11 sum)", oracle, 1355)
|
||||
ps = list(ap["D"])
|
||||
w = sum(cost(ap["D"][p]) < cost(K["k3_analogy"]["D"][p]) for p in ps)
|
||||
l = sum(cost(ap["D"][p]) > cost(K["k3_analogy"]["D"][p]) for p in ps)
|
||||
check("A' vs Chandrupatla-on-g with the analogy order, corpus 4", (w, len(ps) - w - l, l), (126, 38, 76))
|
||||
check("its p (1 digit)", f"{sign_p(w, l):.1g}", "0.0005")
|
||||
shares = []
|
||||
for s in ("CHA", "NEW", "D"):
|
||||
keep = lambda p, _m=K1[s]["mstar"]: nonsimple(_m[p]) # noqa: E731
|
||||
a_, k_, o_ = tot(ap[s], keep), tot(K["k3_analogy"][s], keep), tot(K1[s]["chandrupatla_g"], keep)
|
||||
shares.append((k_, a_, round(100 * (a_ - k_) / (a_ - o_))))
|
||||
check("share of the A'-to-oracle gap closed by the analogy order", shares,
|
||||
[(322, 654, 59), (721, 1229, 58), (1766, 2159, 31)])
|
||||
check("K3 bound violations", sum(v[2] for k in K for s in sets for v in K[k][s].values()), 0)
|
||||
# Lagouanelle's fractional readings on the simple roots of the canonical set (fresh)
|
||||
from simroot.transformed import chandrupatla_transformed
|
||||
from sweep_full import load_problems
|
||||
frac = 0
|
||||
for s, pid, f, a, b, _r in load_problems():
|
||||
if s != "APS":
|
||||
continue
|
||||
st = {}
|
||||
chandrupatla_transformed(f, a, b, 1e-12, 200, estimator="lag", stats=st)
|
||||
m = st.get("m_hat", 1.0)
|
||||
frac += (0.0 < m <= 2.0 / 3.0)
|
||||
check("Lagouanelle holds a fractional order on simple APS roots", frac, 115)
|
||||
# point 4: exact-zero stops (fresh)
|
||||
import contextlib
|
||||
import io
|
||||
import diag_revision2
|
||||
with contextlib.redirect_stdout(io.StringIO()):
|
||||
z = diag_revision2.point4()
|
||||
check("exact-zero stops on CHA: Brent(g), Chandrupatla(g), A'",
|
||||
(z["CHA"]["brent_g"][1], z["CHA"]["chandrupatla_g"][1], z["CHA"]["Aprime"][1]), (15, 4, 2))
|
||||
check("exact-zero stops of the transformed runs on corpus 1",
|
||||
(z["NEW"]["chandrupatla_g"][1], z["NEW"]["brent_g"][1]), (0, 0))
|
||||
check("corpus 1: transformation 3.4 times cheaper", round(1229 / 358, 1), 3.4)
|
||||
# point 3: the three unchanged rows of Table 2 (fresh)
|
||||
from corpus_roots_jl import solved
|
||||
from sweep_full import make_method
|
||||
f, a, b = next((f, a, b) for s, pid, f, a, b, _r in load_problems() if pid == "aps.13.00")
|
||||
got = []
|
||||
for name in ("dekker", "illinois", "regula_falsi"):
|
||||
x, n, conv = make_method(("base", name))(f, a, b, 1e-12, 200)
|
||||
got.append((name, int(n), bool(conv), bool(solved(f, float(x)))))
|
||||
check("aps.13.00: Dekker, Illinois, false position spend the budget unsolved",
|
||||
got, [("dekker", 200, False, False), ("illinois", 200, False, False),
|
||||
("regula_falsi", 200, False, False)])
|
||||
small = [abs(float(f(float(make_method(("base", nm))(f, a, b, 1e-12, 200)[0])))) < 1e-62
|
||||
for nm in ("dekker", "illinois")]
|
||||
check("Dekker and Illinois stall where |f| < 1e-62", small, [True, True])
|
||||
# point 7: Table 3's two added rows
|
||||
full = {r["label"]: r for r in _load("sweep_full.json")}
|
||||
for lb, want_ in (("pure.exp_abs.bracket.bracket", (1848, 45, 0)), ("pure.exp_abs.bracket.credit", (1952, 45, 0))):
|
||||
c = full[lb]["CHA"]
|
||||
check(f"Table 3 row {lb}", (c["total"], c["solved"], c["over"]), want_)
|
||||
|
||||
|
||||
def verify_revision3():
|
||||
"""Revision 3 (point 5): the post hoc branch m >= 2 and the reading of Table 12."""
|
||||
print("\nRevision 3:")
|
||||
|
||||
def cost(v):
|
||||
return v[0] if v[1] else 200
|
||||
|
||||
def tot(r, keep=None):
|
||||
return sum(cost(v) for p, v in r.items() if keep is None or keep(p))
|
||||
sets = ("APS", "CHA", "NEW", "RJL", "APP", "D")
|
||||
P = _load("sweep_estimated_posthoc.json")
|
||||
K = _load("sweep_estimated.json")["k3_analogy"]
|
||||
seen, four = _load("sweep_fourth_seen.json"), _load("sweep_fourth_fourth.json")
|
||||
ap = dict(seen["Aprime"])
|
||||
ap.update(four["Aprime"])
|
||||
K1 = _load("sweep_controls.json")["K1"]
|
||||
nonsimple = lambda m: m >= 2.0 or m <= 2.0 / 3.0 # noqa: E731
|
||||
|
||||
def group(r):
|
||||
return sum(tot(r[s], lambda p, _m=K1[s]["mstar"]: nonsimple(_m[p])) for s in ("CHA", "NEW", "D"))
|
||||
check("Table 12 post hoc row", (tot(P["D"]), sum(tot(P[s]) for s in sets), group(P), tot(P["APS"])),
|
||||
(5568, 18372, 6540, 2960))
|
||||
ps = list(ap["D"])
|
||||
w = sum(cost(ap["D"][p]) < cost(P["D"][p]) for p in ps)
|
||||
l = sum(cost(ap["D"][p]) > cost(P["D"][p]) for p in ps)
|
||||
check("A' vs the post hoc variant on corpus 4", (w, len(ps) - w - l, l), (178, 34, 28))
|
||||
check("post hoc violations", sum(v[2] for s in sets for v in P[s].values()), 0)
|
||||
gain = group(ap) - group(K)
|
||||
loss = (sum(tot(K[s]) for s in sets) - group(K)) - (sum(tot(ap[s]) for s in sets) - group(ap))
|
||||
check("transferred detector: gain on multiple roots < loss elsewhere (vs A')", (gain, loss, gain < loss),
|
||||
(1233, 3934, True))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
# Root Finding by Similarity and Analogy — code, data and history
|
||||
|
||||
Code, data and full development history for the paper *Root Finding by Similarity and Analogy: A
|
||||
Bracketing Method with a Data-Selected Decision Layer* (V. Sitnikov, 2026; manuscript, included in
|
||||
`source/article/`). Everything behind the paper is here, under the Apache License 2.0: the method's
|
||||
reference implementation, a compiled library of the proposed method A′, the benchmark harness, the
|
||||
problem corpora, every results file, the records written before each run, the manuscript itself, and
|
||||
the full development history.
|
||||
|
||||
## Contents
|
||||
|
||||
| path | what |
|
||||
|---|---|
|
||||
| `source/` | the repository at the commit the paper was built from: `simroot/` (the reference implementation of the base rule, A′ and every variant the paper measures), `lib/` (the source of the compiled library `simbracket`), the sweep scripts, corpora (`corpus_*.py`), diagnoses, the verifier `verify_article.py`, the tests, every results file (`sweep_*.json`, `results_*.txt`), the records written before the runs (`PREREG_*.md`, `POSTHOC_*.md`), the findings (`FINDINGS_*.md`) and the paper (`article/`) |
|
||||
| `sim-numerics.bundle` | the full git history of that branch (`git clone sim-numerics.bundle`); the commits cited in the paper's Appendix D are in it |
|
||||
| `wheels/` | `simbracket`, A′ as a compiled library (Python ≥ 3.10; Linux and Windows builds) |
|
||||
| `requirements.txt`, `reproduce.sh` | the Python packages used, and the one-command reproduction |
|
||||
| `SHA256SUMS` | checksums of every file in the archive |
|
||||
|
||||
## Requirements
|
||||
|
||||
**Linux x86_64 for reproducing the numbers.** The paper's numbers come from one Linux machine (Ubuntu
|
||||
24.04, glibc 2.39, Python 3.12.3). The test functions call the platform's `libm`, whose last digit
|
||||
differs between platforms; on Windows, TOMS 748 needs 2635 evaluations on the Alefeld–Potra–Shi set
|
||||
instead of 2636, and two of the paper's fresh recomputations move by a few evaluations. Using the
|
||||
method needs no particular platform.
|
||||
|
||||
## Reproduce
|
||||
|
||||
```bash
|
||||
./reproduce.sh
|
||||
```
|
||||
|
||||
creates a virtual environment, installs `requirements.txt` and the `simbracket` wheel, and in `source/`
|
||||
runs
|
||||
|
||||
1. `python -u verify_article.py` — recomputes or re-reads every number stated in the paper (548) and
|
||||
compares it with the value printed there; it ends with *every number stated in the paper matches the
|
||||
sweep data*;
|
||||
2. `python -m pytest tests/test_simbracket_equivalence.py` — the compiled library equals the reference
|
||||
implementation bit for bit, point and evaluation count, on all 868 problems of the six corpora.
|
||||
|
||||
Any table can be recomputed from scratch with its sweep script; each script's docstring names the table
|
||||
it produces (for example `python -u rescore_uniform.py` for the protocol tables, `python -u
|
||||
sweep_fourth.py --fourth` for corpus 4, `python -u sweep_controls.py` and `python -u sweep_estimated.py`
|
||||
for the controls). The full test suite is `python -m pytest tests`.
|
||||
|
||||
## Use A′ on your own problem
|
||||
|
||||
```python
|
||||
from simbracket import find_root, evaluation_bound
|
||||
|
||||
x, nfev, converged = find_root(f, a, b, xtol=1e-12, maxfev=200)
|
||||
# f(a) and f(b) of opposite signs; stops when the bracket is no wider than xtol or f(x) == 0;
|
||||
# never more than evaluation_bound(a, b, xtol) = 2*ceil(log2(|b-a|/xtol)) + 3 evaluations
|
||||
```
|
||||
|
||||
or, from source, `simroot.pure.pure_root` with the configuration named `APRIME` in `sweep_full.py`.
|
||||
|
||||
## The records and their order
|
||||
|
||||
`PREREG_*.md` were written, and committed, before the runs they describe; `POSTHOC_*.md` after, as
|
||||
marked. Appendix D of the paper lists each record with its commit and the commit of its results;
|
||||
`check_prereg_order.py` recomputes that table from the history in `sim-numerics.bundle`. The order is
|
||||
attested by the author's git history, not by an external registry. Before publication the history was
|
||||
rewritten to remove infrastructure details (a server address) and working notes not meant for
|
||||
publication (a few files deleted; file names, comments and a few words in three advance records made
|
||||
neutral, cited commit hashes mapped to the new commits; no rule, criterion or number of a record
|
||||
changed); the commit hashes cited in the paper are those of the rewritten history in this archive.
|
||||
|
||||
## Citing
|
||||
|
||||
The archive: https://doi.org/10.5281/zenodo.22894337 (DOI 10.5281/zenodo.22894337). Cite this archive by its DOI.
|
||||
|
||||
## Licence
|
||||
|
||||
Apache License 2.0 (`LICENSE`); copyright Valeri Sitnikov (`NOTICE`).
|
||||
@@ -0,0 +1,34 @@
|
||||
#!/bin/bash
|
||||
# Clean-room check of the Zenodo archive on Linux: unzip into an empty directory, install only the
|
||||
# simbracket wheel from the archive (dependencies from the existing environment, nothing downloaded),
|
||||
# run the verifier and the equivalence test from source/, and recompute Appendix D from the bundled
|
||||
# history. Usage: cleanroom_check.sh <zip>
|
||||
set -e
|
||||
export OMP_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 MKL_NUM_THREADS=1
|
||||
PY=~/torchenv/bin/python
|
||||
Z=$(readlink -f "$1")
|
||||
R=~/zenodo_cleanroom
|
||||
rm -rf $R && mkdir -p $R && cd $R
|
||||
$PY -c "import zipfile,sys; zipfile.ZipFile(sys.argv[1]).extractall('.')" "$Z"
|
||||
D=$(ls -d sim-numerics-*/)
|
||||
cd $R/$D
|
||||
echo "== checksums"; sha256sum -c --quiet SHA256SUMS && echo "all match"
|
||||
$PY -m pip install --quiet --no-deps --target $R/site wheels/simbracket-*-linux_x86_64.whl
|
||||
cd source
|
||||
export PYTHONPATH=$R/site:.
|
||||
$PY -c "import simbracket, simroot.pure; print('simbracket', simbracket.__file__); print('reference', simroot.pure.__file__)"
|
||||
echo "== verify_article.py"
|
||||
$PY -u verify_article.py > $R/verify.log 2>&1 || true
|
||||
echo "OK $(grep -c '^ OK' $R/verify.log) BAD $(grep -c '^ BAD' $R/verify.log)"
|
||||
grep '^ BAD' $R/verify.log || true
|
||||
grep -A6 '^Traceback' $R/verify.log || true
|
||||
tail -1 $R/verify.log
|
||||
echo "== equivalence test"
|
||||
$PY -m pytest tests/test_simbracket_equivalence.py -q -p no:cacheprovider 2>&1 | tail -1
|
||||
echo "== Appendix D from the bundled history"
|
||||
cd $R && git clone --quiet $R/$D/sim-numerics.bundle hist && cd hist
|
||||
$PY -u check_prereg_order.py | grep '^| `' > $R/appD_from_history.txt
|
||||
grep '^| `' $R/$D/source/results_prereg_order.txt > $R/appD_in_archive.txt
|
||||
if diff -q $R/appD_from_history.txt $R/appD_in_archive.txt; then echo "Appendix D reproduced from the history: identical"; else echo "Appendix D DIFFERS"; fi
|
||||
grep -c '^| `' $R/appD_from_history.txt
|
||||
echo "=== CLEANROOM DONE ==="
|
||||
@@ -0,0 +1,97 @@
|
||||
"""Build the Zenodo archive (open release, Apache-2.0): the repository at HEAD of release
|
||||
(`git archive`), its full history (`git bundle`), the simbracket wheels, README, requirements, the
|
||||
reproduction script, LICENSE, NOTICE and SHA256SUMS - zipped as sim-numerics-<version>.zip.
|
||||
|
||||
Refuses to build if the tree or the bundled history contains the server address or the
|
||||
specifics of the next method, if HEAD is not release, or if a text file of the archive has
|
||||
Windows line ends.
|
||||
|
||||
python -u zenodo/make_open_package.py 1.0.0
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import zipfile
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
VERSION = sys.argv[1] if len(sys.argv) > 1 else "1.0.0"
|
||||
NAME = f"sim-numerics-{VERSION}"
|
||||
OUT = os.path.join(ROOT, "zenodo_build")
|
||||
PKG = os.path.join(OUT, NAME)
|
||||
# The strings that must appear in no commit are read from a file OUTSIDE the repository (named by
|
||||
# $ZENODO_FORBIDDEN): listing them here would put them into the history this check protects.
|
||||
FORBIDDEN_FILE = os.environ.get("ZENODO_FORBIDDEN", "")
|
||||
WHEELS = ["lib/dist/simbracket-1.0.0-cp310-abi3-linux_x86_64.whl",
|
||||
"lib/dist/simbracket-1.0.0-cp310-abi3-win_amd64.whl"]
|
||||
CRLF = bytes([13, 10])
|
||||
|
||||
|
||||
def git(*args, **kw):
|
||||
return subprocess.run(["git", "-C", ROOT, *args], check=True, capture_output=True, **kw)
|
||||
|
||||
|
||||
def main():
|
||||
branch = git("branch", "--show-current", text=True).stdout.strip()
|
||||
assert branch == "release", branch
|
||||
assert not git("status", "--porcelain", "--untracked-files=no", text=True).stdout.strip(), \
|
||||
"commit everything first: the archive is HEAD"
|
||||
assert FORBIDDEN_FILE and os.path.isfile(FORBIDDEN_FILE), "set ZENODO_FORBIDDEN to the private list"
|
||||
assert not os.path.abspath(FORBIDDEN_FILE).startswith(ROOT), "the list must live outside the repository"
|
||||
forbidden = [ln.strip() for ln in open(FORBIDDEN_FILE, encoding="utf8") if ln.strip()]
|
||||
for s in forbidden: # every commit of the branch
|
||||
hits = subprocess.run(["git", "-C", ROOT, "log", "release", "-p", "-S", s, "--format=%h"],
|
||||
capture_output=True, text=True, encoding="utf8").stdout.split()
|
||||
assert not hits, (s, hits[:3])
|
||||
if os.path.isdir(OUT):
|
||||
shutil.rmtree(OUT)
|
||||
os.makedirs(os.path.join(PKG, "wheels"))
|
||||
tar = os.path.join(OUT, "source.tar")
|
||||
# core.autocrlf=false: the archive carries the repository's LF line ends, not Windows' CRLF
|
||||
git("-c", "core.autocrlf=false", "archive", "--format=tar", "--prefix=source/", "-o", tar, "HEAD")
|
||||
shutil.unpack_archive(tar, PKG)
|
||||
os.remove(tar)
|
||||
# HEAD too, so that `git clone sim-numerics.bundle` checks the branch out
|
||||
git("bundle", "create", os.path.join(PKG, "sim-numerics.bundle"), "HEAD", "release")
|
||||
for w in WHEELS:
|
||||
shutil.copy(os.path.join(ROOT, w), os.path.join(PKG, "wheels"))
|
||||
src = os.path.join(PKG, "source") # from the archive: LF, exactly as committed
|
||||
for f in ("README.md", "requirements.txt", "reproduce.sh"):
|
||||
shutil.copy(os.path.join(src, "zenodo", f), os.path.join(PKG, f))
|
||||
for f in ("LICENSE", "NOTICE"):
|
||||
shutil.copy(os.path.join(src, f), os.path.join(PKG, f))
|
||||
bad = []
|
||||
for d, _, files in os.walk(PKG):
|
||||
for f in files:
|
||||
if f.endswith((".py", ".sh", ".md", ".txt")) or f in ("LICENSE", "NOTICE"):
|
||||
if CRLF in open(os.path.join(d, f), "rb").read():
|
||||
bad.append(os.path.relpath(os.path.join(d, f), PKG))
|
||||
assert not bad, ("Windows line ends", bad[:5])
|
||||
sums = []
|
||||
for d, _, files in os.walk(PKG):
|
||||
for f in sorted(files):
|
||||
p = os.path.join(d, f)
|
||||
rel = os.path.relpath(p, PKG).replace("\\", "/")
|
||||
sums.append(f"{hashlib.sha256(open(p, 'rb').read()).hexdigest()} {rel}")
|
||||
with open(os.path.join(PKG, "SHA256SUMS"), "w", encoding="ascii", newline="\n") as fh:
|
||||
fh.write("\n".join(sorted(sums, key=lambda s: s[66:])) + "\n")
|
||||
zp = os.path.join(OUT, NAME + ".zip")
|
||||
with zipfile.ZipFile(zp, "w", zipfile.ZIP_DEFLATED) as z:
|
||||
for d, _, files in os.walk(PKG):
|
||||
for f in files:
|
||||
p = os.path.join(d, f)
|
||||
info = zipfile.ZipInfo.from_file(p, os.path.relpath(p, OUT).replace("\\", "/"))
|
||||
if f.endswith(".sh"):
|
||||
info.external_attr = 0o755 << 16
|
||||
with open(p, "rb") as src_fh:
|
||||
z.writestr(info, src_fh.read(), zipfile.ZIP_DEFLATED)
|
||||
head = git("rev-parse", "--short", "HEAD", text=True).stdout.strip()
|
||||
print(f"{zp}: {os.path.getsize(zp) / 1e6:.1f} MB, {len(sums)} files, source at {head}")
|
||||
print("zenodo package done")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,16 @@
|
||||
#!/bin/sh
|
||||
# Reproduce the paper's numbers (Linux x86_64, Python 3.12). See README.md.
|
||||
set -e
|
||||
cd "$(dirname "$0")"
|
||||
python3.12 -m venv .venv
|
||||
. .venv/bin/activate
|
||||
python -m pip install --quiet -r requirements.txt
|
||||
python -m pip install --quiet --no-deps wheels/simbracket-*-linux_x86_64.whl
|
||||
export OMP_NUM_THREADS=1
|
||||
cd source
|
||||
echo "== verify_article.py (every number stated in the paper) =="
|
||||
python -u verify_article.py > ../verify.log 2>&1 || true
|
||||
echo "OK $(grep -c '^ OK' ../verify.log) BAD $(grep -c '^ BAD' ../verify.log)"
|
||||
tail -1 ../verify.log
|
||||
echo "== simbracket against the reference implementation, bit for bit =="
|
||||
python -m pytest tests/test_simbracket_equivalence.py -q -p no:cacheprovider | tail -1
|
||||
@@ -0,0 +1,5 @@
|
||||
numpy==2.4.3
|
||||
scipy==1.17.1
|
||||
mpmath==1.3.0
|
||||
pymodab==1.0.9
|
||||
pytest==9.1.1
|
||||
Reference in New Issue
Block a user