File size: 4,827 Bytes
c2b9f11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3
"""Finite exact-arithmetic regression for the target-aware selection pilot.

For n = 2,...,6 and q in {3/16, 1/4, 5/16}, enumerate each position's
accepted-zero, accepted-one, or rejected outcome.  Then enumerate every
uniformly selected subset of m = floor(n/2) accepted positions.  Check the
unconditional joint mass of the selected labels and retained values against
the teacher product law times the exact success probability.

This is a bounded regression of the selection mechanism, not a proof of the
asymptotic three-round theorem or its final-batch discrepancy bound.
Only Python's standard library is required; all checks use rational arithmetic.
"""

from collections import defaultdict
from fractions import Fraction
from itertools import combinations, product
from math import comb, prod


TAU = Fraction(1, 4)
REJECTED = 2


def verify_case(n: int, q: Fraction) -> tuple[int, int, Fraction]:
    """Return enumerated mark vectors, checked output cells, and shortage mass."""
    m = n // 2
    alpha = min(q / TAU, (1 - q) / (1 - TAU))
    raw_probabilities = (1 - q, q)
    teacher_probabilities = (1 - TAU, TAU)
    acceptance_probabilities = tuple(
        alpha * teacher_probabilities[e] / raw_probabilities[e]
        for e in (0, 1)
    )
    assert all(0 <= probability <= 1 for probability in acceptance_probabilities)

    # Compute the three mark probabilities from the original Bernoulli proposal
    # and its value-dependent acceptance coin, rather than starting with a
    # teacher-distributed retained value.
    mark_probabilities = (
        raw_probabilities[0] * acceptance_probabilities[0],
        raw_probabilities[1] * acceptance_probabilities[1],
        sum(
            raw_probabilities[e] * (1 - acceptance_probabilities[e])
            for e in (0, 1)
        ),
    )
    assert sum(mark_probabilities) == 1
    assert sum(mark_probabilities[:2]) == alpha

    joint = defaultdict(Fraction)
    success_mass = Fraction(0)
    shortage_mass = Fraction(0)
    enumerated = 0
    for marks in product((0, 1, REJECTED), repeat=n):
        enumerated += 1
        mark_mass = prod(mark_probabilities[mark] for mark in marks)
        if mark_mass == 0:
            continue
        accepted = tuple(i for i, mark in enumerate(marks) if mark != REJECTED)
        if len(accepted) < m:
            shortage_mass += mark_mass
            committed_in_round_two = 0
        else:
            success_mass += mark_mass
            committed_in_round_two = m
            choice_mass = mark_mass / comb(len(accepted), m)
            for selected in combinations(accepted, m):
                retained_values = tuple(marks[i] for i in selected)
                joint[(selected, retained_values)] += choice_mass

        # On a shortage, commit nothing.  Round three fills every remaining
        # B-coordinate, so shortage is not a failure to finish sampling.
        remaining_for_round_three = n - committed_in_round_two
        assert committed_in_round_two + remaining_for_round_three == n
        assert 2 * n + remaining_for_round_three <= 3 * n

    exact_binomial_shortage = sum(
        Fraction(comb(n, k)) * alpha**k * (1 - alpha) ** (n - k)
        for k in range(m)
    )
    assert shortage_mass == exact_binomial_shortage
    assert success_mass + shortage_mass == 1
    assert sum(joint.values()) == success_mass

    # Compare every possible selected-label/value pair, including any cells
    # that an erroneous selection implementation might have omitted.
    checked_cells = 0
    for selected in combinations(range(n), m):
        selected_mass = Fraction(0)
        for retained_values in product((0, 1), repeat=m):
            teacher_mass = prod(teacher_probabilities[e] for e in retained_values)
            expected = success_mass * teacher_mass / comb(n, m)
            actual = joint[(selected, retained_values)]
            assert actual == expected, (n, q, selected, retained_values, actual, expected)
            selected_mass += actual
            checked_cells += 1
        assert selected_mass == success_mass / comb(n, m)

    return enumerated, checked_cells, shortage_mass


def main() -> None:
    cases = 0
    enumerated = 0
    checked_cells = 0
    for n in range(2, 7):
        for q in (Fraction(3, 16), Fraction(1, 4), Fraction(5, 16)):
            states, cells, shortage = verify_case(n, q)
            cases += 1
            enumerated += states
            checked_cells += cells
            print(f"PASS n={n}, q={q}, exact pilot-shortage probability={shortage}")
    print(
        f"PASS: {cases} cases; {enumerated} mark vectors; "
        f"{checked_cells} selected-label/value cells."
    )
    print("Finite rational-arithmetic regression only; not a proof of the theorem.")


if __name__ == "__main__":
    main()