Download reproducibility/checks/verify_interface_selection.py from lylybig8/mdm_coverage: direct link, hf CLI and curl.
- Browser
- Download file 4.83 kB
-
https://huggingface.co/datasets/lylybig8/mdm_coverage/resolve/main/reproducibility/checks/verify_interface_selection.py
- Command line
-
hf download hf://datasets/lylybig8/mdm_coverage/reproducibility/checks/verify_interface_selection.py
-
curl -L -o verify_interface_selection.py https://huggingface.co/datasets/lylybig8/mdm_coverage/resolve/main/reproducibility/checks/verify_interface_selection.py
4.83 kB
| #!/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() | |