| | @@ -0,0 +1,146 @@ |
| 1 | +"""Exact finite checks for Poisson fibres of a uniform fixed-size sample.""" |
| 2 | + |
| 3 | +from collections import Counter |
| 4 | +from fractions import Fraction |
| 5 | +from itertools import combinations |
| 6 | +from math import comb, exp, factorial, isqrt, sqrt |
| 7 | +from pathlib import Path |
| 8 | +from runpy import run_path |
| 9 | + |
| 10 | + |
| 11 | +EXPERIMENTS = Path(__file__).resolve().parents[1] |
| 12 | +torus_integral = run_path(str(EXPERIMENTS / "lower-small-doubling/verify.py"))["torus_integral"] |
| 13 | +occupied_exact = run_path(str(EXPERIMENTS / "beyond2/triples.py"))["occupied_exact"] |
| 14 | + |
| 15 | + |
| 16 | +def phase_tails(points, m): |
| 17 | + """Probabilities of at least r hits in a uniform cell, from r=0 to |points|+1.""" |
| 18 | + def count_at_least(cells, threshold): |
| 19 | + return sum(count >= threshold for count in Counter(cells).values()) |
| 20 | + |
| 21 | + stats = [lambda cells, r=r: count_at_least(cells, r) for r in range(1, len(points) + 1)] |
| 22 | + integrals = torus_integral(points, m, stats) |
| 23 | + # All 2m half-cells are equivalent after the common phase shift. |
| 24 | + return [Fraction(1), *(value / (2 * m) for value in integrals), Fraction(0)] |
| 25 | + |
| 26 | + |
| 27 | +def hypergeometric_tail(population, marked, draws, threshold): |
| 28 | + low = max(threshold, draws - (population - marked)) |
| 29 | + high = min(draws, marked) |
| 30 | + ways = sum(comb(marked, j) * comb(population - marked, draws - j) |
| 31 | + for j in range(low, high + 1)) |
| 32 | + return Fraction(ways, comb(population, draws)) |
| 33 | + |
| 34 | + |
| 35 | +def binomial_tail(draws, h, threshold): |
| 36 | + return sum(comb(draws, j) * h**j * (1 - h)**(draws - j) |
| 37 | + for j in range(threshold, draws + 1)) |
| 38 | + |
| 39 | + |
| 40 | +def finite_population_error(population, draws, h): |
| 41 | + if population == 1: |
| 42 | + return Fraction(0) |
| 43 | + return Fraction(draws * (draws - 1), population - 1) * h * (1 - h) |
| 44 | + |
| 45 | + |
| 46 | +def check_host(host, draws, m): |
| 47 | + population = len(host) |
| 48 | + h = Fraction(1, 2 * m) |
| 49 | + full_tails = phase_tails(host, m) |
| 50 | + mass = [upper - lower for upper, lower in zip(full_tails, full_tails[1:])] |
| 51 | + assert min(mass) >= 0 and sum(mass) == 1 |
| 52 | + assert sum(k * prob for k, prob in enumerate(mass)) == population * h |
| 53 | + variance = sum((k - population * h) ** 2 * prob for k, prob in enumerate(mass)) |
| 54 | + assert variance == population * h * (1 - h) |
| 55 | + |
| 56 | + sample_tails = {} |
| 57 | + energy_total = 0 |
| 58 | + for sample in combinations(host, draws): |
| 59 | + tails = phase_tails(sample, m) |
| 60 | + assert tails[1] == occupied_exact(sample, m) / m |
| 61 | + sample_tails[frozenset(sample)] = tails |
| 62 | + sums = Counter(a + b for a in sample for b in sample) |
| 63 | + energy_total += sum(count**2 for count in sums.values()) |
| 64 | + sample_count = comb(population, draws) |
| 65 | + energy_bound = 2 * draws**2 - draws + Fraction(draws**4 + 2 * draws**3, population) |
| 66 | + assert Fraction(energy_total, sample_count) <= energy_bound |
| 67 | + |
| 68 | + bias = finite_population_error(population, draws, h) |
| 69 | + for r in range(1, draws + 1): |
| 70 | + curve = [hypergeometric_tail(population, k, draws, r) for k in range(population + 1)] |
| 71 | + for k in range(population - 1): |
| 72 | + curvature = curve[k + 2] - 2 * curve[k + 1] + curve[k] |
| 73 | + assert abs(curvature) * population * (population - 1) <= draws * (draws - 1) |
| 74 | + mixture = sum(prob * tail for prob, tail in zip(mass, curve)) |
| 75 | + assert mixture == sum(tails[r] for tails in sample_tails.values()) / sample_count |
| 76 | + assert abs(mixture - binomial_tail(draws, h, r)) <= bias |
| 77 | + |
| 78 | + for a, b in combinations(sample_tails, 2): |
| 79 | + if len(a - b) == 1: |
| 80 | + assert all(abs(x - y) <= h for x, y in zip(sample_tails[a], sample_tails[b])) |
| 81 | + means = [] |
| 82 | + for r in (1, 3): |
| 83 | + if r <= draws: |
| 84 | + mean = sum(tails[r] for tails in sample_tails.values()) / sample_count |
| 85 | + means.append(f"F_{r}={mean}") |
| 86 | + print(f"M={population}, N={draws}, m={m}, #samples={sample_count}, {', '.join(means)}") |
| 87 | + |
| 88 | + |
| 89 | +def check_prime_field(): |
| 90 | + """A nonconstant affine map permutes a full prime-field host.""" |
| 91 | + for prime, draws in ((1009, 20), (10007, 80), (100003, 300)): |
| 92 | + assert all(prime % d for d in range(2, isqrt(prime) + 1)) |
| 93 | + m = 2 * draws // 5 |
| 94 | + marked = prime // (2 * m) |
| 95 | + h = Fraction(marked, prime) |
| 96 | + zero_slope = Fraction(1, prime) |
| 97 | + bias = finite_population_error(prime, draws, h) |
| 98 | + tails = [] |
| 99 | + for r in (1, 3): |
| 100 | + permuted = hypergeometric_tail(prime, marked, draws, r) |
| 101 | + mean = (1 - zero_slope) * permuted + zero_slope * h |
| 102 | + assert abs(mean - binomial_tail(draws, h, r)) <= bias |
| 103 | + tails.append(f"F_{r}={float(mean):.8f}") |
| 104 | + print(f"Prime-field M={prime}, N={draws}, m={m}, {', '.join(tails)}") |
| 105 | + |
| 106 | + |
| 107 | +def exp_partial(x, degree): |
| 108 | + return sum(x**j / factorial(j) for j in range(degree + 1)) |
| 109 | + |
| 110 | + |
| 111 | +def check_constants(): |
| 112 | + # Rational lower bounds for exp are sufficient for each strict comparison. |
| 113 | + assert exp_partial(Fraction(2, 3), 3) == Fraction(157, 81) |
| 114 | + assert 1 - Fraction(17, 9) / Fraction(157, 81) == Fraction(4, 157) |
| 115 | + assert Fraction(5, 8) * Fraction(4, 157) > Fraction(1, 64) |
| 116 | + assert exp_partial(Fraction(5, 4), 5) > Fraction(125, 36) |
| 117 | + assert Fraction(2, 5) * Fraction(89, 125)**2 > Fraction(9, 20)**2 > Fraction(4, 27) |
| 118 | + |
| 119 | + draws, population, q = 10**6, 10**9, 631 |
| 120 | + assert all(q % d for d in range(2, isqrt(q) + 1)) |
| 121 | + m = q**2 + q + 1 |
| 122 | + assert m == 398793 |
| 123 | + h = Fraction(1, 2 * m) |
| 124 | + rate = draws * h |
| 125 | + bias = draws * h**2 + finite_population_error(population, draws, h) |
| 126 | + t = Fraction(1, 400) |
| 127 | + assert 2 * t**2 / (draws * h**2) > 7 |
| 128 | + assert exp_partial(Fraction(7), 6) > 400 |
| 129 | + lower_size = (q + 1) * (1 - 1 / exp_partial(rate, 6) - bias - t) |
| 130 | + assert lower_size > 449 |
| 131 | + raw_fourth = Fraction(draws, 2) - Fraction(comb(draws - 1, 4), 16 * m * population) |
| 132 | + assert raw_fourth < 0 |
| 133 | + |
| 134 | + print(f"c=2/5 coefficient {sqrt(2 / 5) * (1 - exp(-5 / 4)):.10f}, above 9/20") |
| 135 | + print(f"N={draws}, M={population}, finite lower bound {float(lower_size):.6f}, " |
| 136 | + f"at least 450 elements with probability above 0.99") |
| 137 | + print(f"On the interval host the raw degree-four upper estimate is {float(raw_fourth):.3f}.") |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + check_host((0,), 1, 1) |
| 142 | + check_host((0, 1, 3, 4), 4, 1) |
| 143 | + check_host(tuple(range(-2, 5)), 4, 2) |
| 144 | + check_host((-3, -1, 0, 4, 7, 9), 3, 3) |
| 145 | + check_prime_field() |
| 146 | + check_constants() |