| | @@ -0,0 +1,190 @@ |
| 1 | +"""Rational certificates for the Sidon container and deletion bounds in NOTE.md.""" |
| 2 | + |
| 3 | +from fractions import Fraction as Q |
| 4 | +from itertools import pairwise |
| 5 | +from math import isqrt |
| 6 | + |
| 7 | + |
| 8 | +# (window length in units of U, coefficient, boundary-ramp factor) |
| 9 | +WINDOWS = tuple((Q(length), Q(weight), Q(tilt)) for length, weight, tilt in ( |
| 10 | + ("68.7", "4.57", ".965"), ("24.2", "13.76", ".94"), |
| 11 | + ("9.65", "25", ".915"), ("5.43", "27.56", ".905"), |
| 12 | + ("2.23", "48.4", ".89"), |
| 13 | +)) |
| 14 | +NU = Q(1, 2**32) |
| 15 | +TARGET = Q("65.604") |
| 16 | +CUTOFF = Q(1, 70000) |
| 17 | +BETA = Q(1, 2**20) |
| 18 | +RATIO = Q(1045, 16) |
| 19 | + |
| 20 | + |
| 21 | +def tent_sum(x, tents): |
| 22 | + return sum(c * max(0, 1 - x / length) for length, c in tents) |
| 23 | + |
| 24 | + |
| 25 | +def primitive(x, tents): |
| 26 | + return sum(c * max(0, length - x)**2 / (2 * length) for length, c in tents) |
| 27 | + |
| 28 | + |
| 29 | +def support_score(value): |
| 30 | + # j minimizes j-1+value/j between the breakpoints (j-1)j and j(j+1). |
| 31 | + j = max(1, (1 + isqrt(1 + (4 * value.numerator) // value.denominator)) // 2) |
| 32 | + return j - 1 + value / j |
| 33 | + |
| 34 | + |
| 35 | +def kernel_moments(tents, ramps): |
| 36 | + knots = sorted({Q(0), *(length for length, _ in tents)}) |
| 37 | + score_mean = score_square = primitive_square = Q(0) |
| 38 | + for left, right in pairwise(knots): |
| 39 | + width = right - left |
| 40 | + p0 = primitive(left, ramps) |
| 41 | + p1 = primitive((left + right) / 2, ramps) |
| 42 | + p2 = primitive(right, ramps) |
| 43 | + # P is quadratic on this segment. Boole's five-node rule integrates P² exactly. |
| 44 | + p_quarter = primitive((3 * left + right) / 4, ramps) |
| 45 | + p_three_quarters = primitive((left + 3 * right) / 4, ramps) |
| 46 | + primitive_square += width * ( |
| 47 | + 7 * p0**2 + 32 * p_quarter**2 + 12 * p1**2 |
| 48 | + + 32 * p_three_quarters**2 + 7 * p2**2 |
| 49 | + ) / 90 |
| 50 | + |
| 51 | + high, low = tent_sum(left, tents), tent_sum(right, tents) |
| 52 | + scores = {left: support_score(high), right: support_score(low)} |
| 53 | + for j in range(1, isqrt(high.numerator // high.denominator) + 1): |
| 54 | + level = j * (j + 1) |
| 55 | + if low < level < high: |
| 56 | + x = left + width * (high - level) / (high - low) |
| 57 | + scores[x] = Q(2 * j) |
| 58 | + for (x, gx), (y, gy) in pairwise(sorted(scores.items())): |
| 59 | + score_mean += (y - x) * (gx + gy) / 2 |
| 60 | + score_square += (y - x) * (gx**2 + gx * gy + gy**2) / 3 |
| 61 | + return score_mean, score_square, primitive_square |
| 62 | + |
| 63 | + |
| 64 | +def container_test(target, cutoff=CUTOFF): |
| 65 | + tents = [(length, weight) for length, weight, _ in WINDOWS] |
| 66 | + ramps = [(length, weight * tilt) for length, weight, tilt in WINDOWS] |
| 67 | + mass = sum(c for _, c in tents) |
| 68 | + first = sum(c * length for length, c in tents) |
| 69 | + boundary = sum(c * length**2 * (Q(1, 6) + (1 - t)**2 / 3) |
| 70 | + for length, c, t in WINDOWS) |
| 71 | + g0 = support_score(mass) |
| 72 | + mean, square, ramp_square = kernel_moments(tents, ramps) |
| 73 | + cost = NU * mass / 2 + (1 + 2 * NU) * first + boundary |
| 74 | + surplus = ((1 - 2 * NU) * (mean - NU * g0) |
| 75 | + - (1 + NU) * cutoff * g0 - cost / target - target) |
| 76 | + variance = (2 + 2 * NU) * (1 + NU) * ( |
| 77 | + square + 2 * (ramp_square + NU * primitive(Q(0), ramps)**2) / target**2 |
| 78 | + ) |
| 79 | + return surplus, variance |
| 80 | + |
| 81 | + |
| 82 | +def transcript_factor(ratio): |
| 83 | + p, q = ratio.numerator, ratio.denominator |
| 84 | + return 4**p * ratio**q * (ratio / (ratio - 1))**(p - q) |
| 85 | + |
| 86 | + |
| 87 | +def deletion_constants(): |
| 88 | + coefficient = TARGET / (1 - NU) - 1 + NU |
| 89 | + theta = RATIO * (1 - BETA - NU) / (RATIO + coefficient) |
| 90 | + gap = 1 - BETA - NU - theta |
| 91 | + assert gap > Q(1, 4) and gap * RATIO == coefficient * theta |
| 92 | + p, q = RATIO.numerator, RATIO.denominator |
| 93 | + # The p-th power of the entropy base is factor * delta**q. |
| 94 | + factor = transcript_factor(RATIO) |
| 95 | + scale = 2**160 |
| 96 | + allowed = (1 - p * BETA**2) * scale**q / factor |
| 97 | + root = allowed.numerator // allowed.denominator |
| 98 | + assert q == 16 |
| 99 | + for _ in range(4): |
| 100 | + root = isqrt(root) |
| 101 | + delta = Q(root, scale) |
| 102 | + assert factor * delta**q <= 1 - p * BETA**2 < factor * (delta + Q(1, scale))**q |
| 103 | + assert 0 < delta < BETA**2 and delta < 1 / (RATIO - 1) |
| 104 | + assert Q(1, 2) < theta < 1 - BETA |
| 105 | + tau = theta * delta |
| 106 | + assert 0 < tau <= NU / (4 * max(length for length, _, _ in WINDOWS)) |
| 107 | + return coefficient, theta, delta, tau |
| 108 | + |
| 109 | + |
| 110 | +def saving(delta, tau): |
| 111 | + # Smallest numerator of a dyadic coefficient c with c²(1-delta-delta²) >= (1-tau)². |
| 112 | + scale = 2**164 |
| 113 | + host_density = 1 - delta - delta**2 |
| 114 | + assert Q(1, 2) < host_density < 1 |
| 115 | + needed = (scale * (1 - tau))**2 / host_density |
| 116 | + root = isqrt(needed.numerator // needed.denominator) |
| 117 | + numerator = root + int(root**2 < needed) |
| 118 | + assert (numerator - 1)**2 < needed <= numerator**2 |
| 119 | + return scale - numerator |
| 120 | + |
| 121 | + |
| 122 | +def size_threshold(delta, tau, count_factor_cubed): |
| 123 | + rate = BETA**2 / 4 |
| 124 | + entropy_cost = (2 / (rate * delta))**6 * (count_factor_cubed / tau)**2 |
| 125 | + fixed = { |
| 126 | + "short-window variance": (16 / (NU**2 * tau**2))**2, |
| 127 | + "integer windows": 200 / (NU * tau), |
| 128 | + "Erdős–Turán remainder": (2 / (NU * delta))**4, |
| 129 | + "host size": 32 / delta**3, |
| 130 | + "union probability": (4 / (rate * delta))**2, |
| 131 | + } |
| 132 | + |
| 133 | + def sufficient(exponent): |
| 134 | + size = 2**exponent |
| 135 | + return size >= max(fixed.values()) and size >= entropy_cost * (Q(7, 10) * exponent)**8 |
| 136 | + |
| 137 | + low, high = 16, 32 |
| 138 | + while not sufficient(high): |
| 139 | + low, high = high, 2 * high |
| 140 | + while high - low > 1: |
| 141 | + middle = (high + low) // 2 |
| 142 | + if sufficient(middle): |
| 143 | + high = middle |
| 144 | + else: |
| 145 | + low = middle |
| 146 | + assert sufficient(high) and not sufficient(high - 1) |
| 147 | + return high |
| 148 | + |
| 149 | + |
| 150 | +def bounds(value, places=6): |
| 151 | + scale = 10**places |
| 152 | + lower = (value.numerator * scale) // value.denominator |
| 153 | + return f"[{lower}/{scale}, {lower + 1}/{scale}]" |
| 154 | + |
| 155 | + |
| 156 | +def check_previous_coefficient(): |
| 157 | + ratio = Q(14403, 4) |
| 158 | + theta = ratio * (1 - BETA - NU) / (ratio + 3600) |
| 159 | + delta = Q(303, 2**7223) |
| 160 | + tau = theta * delta |
| 161 | + assert 1 - BETA - NU - theta == 3600 * theta / ratio > Q(1, 4) |
| 162 | + assert Q(1, 2) < theta < 1 - BETA and 0 < delta < BETA**2 |
| 163 | + assert tau <= NU / (4 * max(length for length, _, _ in WINDOWS)) |
| 164 | + assert delta < 1 / (ratio - 1) |
| 165 | + assert transcript_factor(ratio) * delta**4 <= 1 - ratio.numerator * BETA**2 |
| 166 | + assert (1 - Q(1, 2**7229))**2 * (1 - delta - delta**2) >= (1 - tau)**2 |
| 167 | + assert 4**3 * Q(3240000, 499) < 75**3 |
| 168 | + return size_threshold(delta, tau, Q(75**3)) |
| 169 | + |
| 170 | + |
| 171 | +def main(): |
| 172 | + for target, cutoff in ((TARGET, CUTOFF), (Q("67.01"), Q(1, 20))): |
| 173 | + gap, variance = container_test(target, cutoff) |
| 174 | + assert gap > 0 and gap**2 > variance |
| 175 | + print(f"Exceptional-set target {target}, cutoff {cutoff}") |
| 176 | + print(" A =", bounds(gap), " A² - variance =", bounds(gap**2 - variance)) |
| 177 | + coefficient, theta, delta, tau = deletion_constants() |
| 178 | + margin_numerator = saving(delta, tau) |
| 179 | + count_factor_cubed = ((1 + NU) * (Q(2, 3) + Q(1, 128)))**3 * 9 / CUTOFF |
| 180 | + exponent = size_threshold(delta, tau, count_factor_cubed) |
| 181 | + print("Container coefficient K in", bounds(coefficient)) |
| 182 | + print("theta in", bounds(theta, 9)) |
| 183 | + print(f"Deletion probability = {delta * 2**160} / 2^160") |
| 184 | + print(f"THEOREM: delta_* = {margin_numerator} / 2^164, N_0 = 2^{exponent}") |
| 185 | + assert Q(margin_numerator, 2**164) > Q(21, 2**151) |
| 186 | + print(f"With K = 3600: saving 2^-7229 for N >= 2^{check_previous_coefficient()}") |
| 187 | + |
| 188 | + |
| 189 | +if __name__ == "__main__": |
| 190 | + main() |