# -*- coding: utf-8 -*- """The band's signed-to-absolute ratio AGAINST ITS RANDOM-SIGN BENCHMARK. R = |sum v| / sum |v| is not interpretable on its own: a sum of N unrelated terms already has R ~ sqrt(sum v^2)/sum |v| ("the class count", in the pre-registration's language). So measure rho := R / (sqrt(sum v^2)/sum |v|) -- rho ~ 1 means the band behaves like a random signed sum, rho << 1 means genuine cancellation beyond the count, rho >> 1 means positive correlation (the phases conspire). Reported with and without the corpus's 2,3,5 layer, since that layer is not neutral. """ import importlib.util, math, sys sys.stdout.reconfigure(encoding='utf-8') spec = importlib.util.spec_from_file_location('km', 'work/kl-measure.py') km = importlib.util.module_from_spec(spec) spec.loader.exec_module(km) FIVE = [(0, 15.0), (1, 7.5), (4, 7.5)] def run(x, with5, N_div=1, band=True): L, y = km.LV[x] ps = [p for p in km.primes_upto(y) if p >= 7] N = int(L * math.log(y) ** 2) // N_div Bc = L ** 0.4 K = L / 6.0 acc = [0.0, 0.0, 0.0, 0] # signed, abs, sum v^2, count def crt(c, n, c5): m = 5 * n for a in range(c % n, m, n): if a % 5 == c5: return a return None def push(v): acc[0] += v acc[1] += abs(v) acc[2] += v * v acc[3] += 1 def add(s, l0s, l1s, t, l0t, l1t): n = s * t for (dd, ee, w0, w1) in ((s, t, l0s, l1t), (t, s, l0t, l1s)): tau = (2 * pow(dd, -1, ee)) % ee c = (dd * tau) % n if with5: for c5, w5 in FIVE: cc = crt(c, n, c5) m = 5 * n r = K % m R5 = (max(0, r - cc) + max(0, r + cc - m) - r * r / m) / K push(w0 * w1 * w5 * R5) else: push(w0 * w1 * km.delta(L, n, c)) def rec_s(i, s, sprimes, l0s, l1s): for j in range(i, len(ps)): p = ps[j] ss = s * p if ss > Bc: break ns = sprimes + (p,) nl0, nl1 = l0s * 2.0 / (p - 4), l1s * 1.0 / (p - 4) lim = N // ss if lim >= ss: allowed = [q for q in ps if q <= lim and q not in ns] def rec_t(k, t, l0t, l1t): if t >= ss: add(ss, nl0, nl1, t, l0t, l1t) for mm in range(k, len(allowed)): q = allowed[mm] if t * q > lim: break rec_t(mm + 1, t * q, l0t * 2.0 / (q - 4), l1t * 1.0 / (q - 4)) for k in range(len(allowed)): q = allowed[k] if q > lim: break rec_t(k + 1, q, 2.0 / (q - 4), 1.0 / (q - 4)) rec_s(j + 1, ss, ns, nl0, nl1) rec_s(0, 1, (), 1.0, 1.0) sg, ab, v2, cnt = acc R = abs(sg) / ab if ab else float('nan') bench = math.sqrt(v2) / ab if ab else float('nan') return dict(x=x, with5=with5, N=N, signed=sg, abs=ab, count=cnt, R=R, bench=bench, rho=(R / bench if bench else float('nan')), lny=math.log(y), R_over_inv_lny=(R * math.log(y))) for x in (13, 17): for w in (False, True): r = run(x, w) print("x=%-2d 5layer=%-5s N=%-10d contribs=%-9d R=%.6f bench=%.6f rho=%.3f R*ln y=%.4f" % (x, w, r['N'], r['count'], r['R'], r['bench'], r['rho'], r['R_over_inv_lny']), flush=True)