erring_zones.py
Checks: SIZE: Growing widths property; observation; SIZE: The rate of the least error theorem; property; observation.
Cited on: SIZE.
To run it: save erring_zones.py, with erring_label.py, erring_widths.py beside it; needs numpy.
"""
erring_zones.py -- the continuum error of a cut set in floats, and the
zone construction at reachable m.
QUESTION. Fill [0, c) with m intervals below one overflow label
[c, 2c - 1), the error as in erring_label.py, kappa_m the limsup over c
of the least error over c^2. A cut set P scaled by k has error / (kc)^2
tending to the continuum error E(P / c): each cell's area less its
largest label area, overflow [1, 2), an upper bound on kappa_m at its
m. What does the zone construction (independent uniform phases per
zone, the route by which kappa_m -> 0) print at the m a run reaches?
DESIGN. cont_error computes E in closed form: the area of a cell
[A, A + a) x [B, B + b) below t is G(t) - G(t - a) - G(t - b) +
G(t - a - b), G(t) = max(t, 0)^2 / 2, so a label's share is a
difference of two such areas, the largest read among a cell's first
nine labels by arrays and past them by a loop. On integer cuts
erring_widths.py's areas is the same sum in integers. zones builds the
construction: [0, t0) one interval, [t0, 1) cut
into zones of width D, zone k a grid of width eps z_k^p (z_k its start)
at a phase uniform on [0, w_k), zone ends cuts. bound prints the mean
bound B = main + 2 t0 + 4 (nz + 1) wmax + 2 (nz + 2) wmax, main the
integral over x, y >= t0, x + y < 1 of (w(x) + w(y)) / (4 w(x + y)) on a
midpoint grid; the edge terms are areas, far above what the edges cost.
PREDICTIONS AND KILLS (written before the run).
C1 cont_error is within 1e-9 of areas / (2 c^2), a fifth of the
least unit of areas (5e-9 at c = 10000), at rounding's cut set
(c = 4000, u = 0.8, m = 162) and erring_widths.py's four profiles
(m = 162, 400, 400, 1000). Added after the first run, which a
scratch copy passed at all five with the loop past the arrays'
nine labels removed: a comb, (0, 40, 50, 51, ..., 60, 100), where
six cells have their largest label, [60, 100), tenth to twelfth,
as [0, 40) x [50, 51) its eleventh. cont_error and areas share
the closed form: C1 reads the floats, the rows taken 128 at a
time and the loop, not the formula, which areas' hand rows read.
Rounding's m = 162 and its areas, 3296100 (0.1030 of 2 c^2), are
pinned as first printed. By hand since, in units of w^2 = 400, n
= 160 grid steps below u c and l = 40 beside them: the grid's own
cells (n^2 + 3n - 1)/8, erring_cells.py's E(H_n); the cells cut by
x + y = c, n - 1 - l at 1/2 and two at 1/8; a grid interval with
the wide one, l^2/4 + 1/4 in each order; 4120.125 in all, and
twice 400 times it is the pin.
Z (written before the zone run, as a check of the bound) the mean of
E over 8 seeds at most B; at the scales a run reaches B's edge
areas dwarf main, so Z prints beside its bound and checks nothing.
FINDINGS (from the printed run; both controls passed first: C1 at 6 of
6, rounding's 0.1030 at m = 162, the s = 3 form exact).
Z: mean E 0.2285 (p = 1, eps 0.005, D = t0 = 0.1, m near 575)
against B 0.5536; 0.1684 (p = 1, eps 0.0025, D = t0 = 0.05, m near
1438) against 0.4870; 0.2074 and 0.2094 (p = 2, D = t0 = 0.1, eps
0.01 and 0.005, m near 1550 and 3090) against 0.7448 and 0.4937.
Eight seeds spread under 0.0014 in every case. At t0 = 0.1 the
means sit near 2 t0 = 0.2, the bound's bottom-interval term (no
arm splits E by interval), and halving eps at p = 2 moved E up by
0.0020; the construction errs above rounding's 1/10. kappa_m -> 0
rests on its proof, not on this run.
"""
import os
os.environ.setdefault("OPENBLAS_NUM_THREADS", "1")
import numpy as np # noqa: E402
from erring_label import rounding # noqa: E402
from erring_widths import ROWS, areas, profile_at # noqa: E402
CHECKS = []
def check(name, ok, detail=""):
CHECKS.append(bool(ok))
tail = f" ({detail})" if detail else ""
print(f" {'PASS' if ok else 'FAIL'} {name}{tail}")
def G(t):
t = np.maximum(t, 0.0)
return t * t / 2
def below(a, b, t):
"""Area of {(u, v) in [0, a) x [0, b) : u + v < t}."""
return G(t) - G(t - a) - G(t - b) + G(t - a - b)
def cont_error(cuts):
"""Continuum error of cuts 0 = c_0 < ... < c_m = 1, overflow [1, 2)."""
C = np.asarray(cuts, dtype=float)
lab = np.append(C, 2.0)
A, W = C[:-1], np.diff(C)
return sum(float(_rows(lab, A, W, i, i + 128).sum())
for i in range(0, len(W), 128))
def cell_errs(cuts):
"""E[a, b]: the continuum error of cell (a, b), cont_error's terms."""
C = np.asarray(cuts, dtype=float)
m = len(C) - 1
return _rows(np.append(C, 2.0), C[:-1], np.diff(C), 0, m).reshape(m, m)
def _rows(lab, A, W, i, j):
"""The errors of the cells whose first summand is interval i..j - 1."""
base = (A[i:j, None] + A[None, :]).ravel()
a = np.broadcast_to(W[i:j, None], (len(W[i:j]), len(W))).ravel()
b = np.broadcast_to(W[None, :], (len(W[i:j]), len(W))).ravel()
lo = np.searchsorted(lab, base, side="right") - 1
hi = np.searchsorted(lab, base + a + b, side="left") - 1
best = np.zeros_like(base)
K = 8
for k in range(K + 1):
L = np.minimum(lo + k, len(lab) - 2)
land = (below(a, b, lab[L + 1] - base) - below(a, b, lab[L] - base))
best = np.maximum(best, np.where(lo + k <= hi, land, 0.0))
for i in np.flatnonzero(hi - lo > K):
L = np.arange(lo[i], hi[i] + 1)
land = (below(a[i], b[i], lab[L + 1] - base[i])
- below(a[i], b[i], lab[L] - base[i]))
best[i] = land.max()
return a * b - best
def zones(p, eps, D, t0, rng):
"""[0, t0) one interval; zones of width D on [t0, 1), each a grid of
width eps z^p (z the zone start) at an independent uniform phase."""
cuts = [0.0, t0]
z, ws = t0, []
while z < 1 - 1e-12:
z2 = min(z + D, 1.0)
w = eps * z ** p
ws.append(w)
t = z + rng.uniform(0, w)
while t < z2 - 1e-12:
if t > cuts[-1] + 1e-12:
cuts.append(t)
t += w
cuts.append(z2)
z = z2
return cuts, ws
def bound(p, eps, D, t0, n=1500):
"""B: main by a midpoint grid of n x n over [t0, 1)^2, plus areas."""
starts = np.arange(t0, 1 - 1e-12, D)
wz = eps * starts ** p
def w(x):
return wz[np.minimum(((x - t0) / D).astype(int), len(wz) - 1)]
h = (1 - t0) / n
x = t0 + (np.arange(n) + 0.5) * h
X, Y = np.meshgrid(x, x)
S = X + Y
ok = S < 1
main = float(((w(X) + w(Y)) / (4 * w(np.where(ok, S, X))))[ok].sum()
* h * h)
nz, wmax = len(wz), float(wz.max())
# the sum's strips: n_z + 2 ends, one more than the n_z + 1 needed
return main, main + 2 * t0 + 4 * (nz + 1) * wmax + 2 * (nz + 2) * wmax
def section_controls():
print("controls")
sets = [(4000, rounding(4000, 0.8 * 4000 / 160, 0.8))]
sets += [(c, profile_at(c, p, m, u)) for c, p, m, u in ROWS]
sets += [(100, (0, 40) + tuple(range(50, 61)) + (100,))]
diff = [abs(cont_error([x / c for x in cu]) - areas(cu, c) / (2 * c * c))
for c, cu in sets]
ok = sum(d < 1e-9 for d in diff)
c, cu = sets[0]
check("C1: the continuum error in floats is the integer areas' at "
"rounding's cut set, four profiles' and a comb; rounding's m "
"and areas as first printed",
ok == len(sets) == 6
and (len(cu) - 1, areas(cu, c)) == (162, 3296100),
f"{ok} of {len(sets)} cut sets, the largest difference "
f"{max(diff):.1e}; rounding's m = {len(cu) - 1}, "
f"{areas(cu, c) / (2 * c * c):.4f}")
two = cont_error([0, 0.3, 1])
want = 3 * 0.3 ** 2 / 2 + 0.4 ** 2 / 2
check("control: one cut at 0.3 is the s = 3 continuum form "
"3a^2/2 + (1 - 2a)^2/2", abs(two - want) < 1e-12,
f"{two:.6f} against {want:.6f}")
def section_zones(cases, seeds=8):
"""Z: the construction's mean error over seeds beside its bound B."""
print("zone construction: mean E over seeds beside the bound B")
for p, eps, D, t0 in cases:
Es, ms = [], []
for s in range(seeds):
cuts, _ = zones(p, eps, D, t0, np.random.default_rng(s))
ms.append(len(cuts) - 1)
Es.append(cont_error(cuts))
main, B = bound(p, eps, D, t0)
print(f" p = {p} eps = {eps} D = {D} t0 = {t0} m {min(ms)}.."
f"{max(ms)}: E mean {np.mean(Es):.4f}, seeds {min(Es):.4f}.."
f"{max(Es):.4f}; main {main:.4f}, B {B:.4f}")
def main():
section_controls()
if not all(CHECKS):
print("\nCONTROL FAILED: nothing below is read.")
raise SystemExit(1)
section_zones([(1, 0.005, 0.1, 0.1), (1, 0.0025, 0.05, 0.05),
(2, 0.01, 0.1, 0.1), (2, 0.005, 0.1, 0.1)])
print(f"{sum(CHECKS)}/{len(CHECKS)} checks passed")
raise SystemExit(0 if all(CHECKS) else 1)
if __name__ == "__main__":
main()