File size: 3,487 Bytes
7956103 | 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 | """Finite regression for the expanded main-text examples, not a proof certificate."""
from fractions import Fraction as F
from itertools import product
import json
import math
def mass(bits, p):
return p ** sum(bits) * (1 - p) ** (len(bits) - sum(bits))
def binomial_mass(n, k, p):
return math.comb(n, k) * p ** k * (1 - p) ** (n - k)
def main():
tau = F(1, 4)
counts = {"full_output_atoms": 0, "posterior_heads": 0,
"count_tv_identities": 0, "reverse_sensitivities": 0,
"nonvacuous_bound_checks": 0}
for n, w, delta in product(range(1, 5), [F(1, 8), F(1, 16)],
[F(1, 4), F(1, 8)]):
vectors = list(product([0, 1], repeat=n))
model = {e: (1 - w) * mass(e, tau) + w * mass(e, tau + delta)
for e in vectors}
heads = [sum(prob for e, prob in model.items() if e[i]) for i in range(n)]
assert heads == [tau + w * delta] * n
full_tv = F(0)
for a, b in product(vectors, repeat=2):
e = tuple(x ^ y for x, y in zip(a, b))
# Each all-A context has the same probability and leaves the model
# mismatch distribution unchanged; then all B heads are independent.
q = F(1, 2 ** n)
for i in range(n):
q *= heads[i] if e[i] else 1 - heads[i]
assert q == F(1, 2 ** n) * mass(e, tau + w * delta)
full_tv += abs(q - F(1, 2 ** n) * mass(e, tau)) / 2
counts["full_output_atoms"] += 1
count_tv = sum(abs(binomial_mass(n, k, tau + w * delta)
- binomial_mass(n, k, tau)) for k in range(n + 1)) / 2
assert full_tv == count_tv
counts["count_tv_identities"] += 1
for c in range(n):
for prefix in product([0, 1], repeat=c):
compatible = {e: prob for e, prob in model.items() if e[:c] == prefix}
literal = sum(prob for e, prob in compatible.items() if e[c]) / sum(compatible.values())
odds = w / (1 - w) * mass(prefix, tau + delta) / mass(prefix, tau)
assert literal == tau + delta * odds / (1 + odds)
counts["posterior_heads"] += 1
for a, s, z in product([F(1, 1000), F(1, 4), F(1, 2), F(3, 4), F(999, 1000)],
[F(1, 4), F(5, 16), F(3, 8), F(7, 16), F(1, 2)], [0, 1]):
g = a * (1 - a)
r = a + (1 - 2 * a) * s
derivative = g / (r if z else 1 - r) ** 2
assert F(16, 9) * g <= derivative <= 16 * g
counts["reverse_sensitivities"] += 1
# Stable floating-point binomial TV at dimensions where the displayed
# warm-up lower bound is positive. This concerns one fixed schedule only.
w, delta, t = 1 / 8, 1 / 4, float(tau)
def log_mass(n, k, p):
return (math.lgamma(n + 1) - math.lgamma(k + 1) - math.lgamma(n - k + 1)
+ k * math.log(p) + (n - k) * math.log1p(-p))
for n in [2048, 4096, 8192, 16384]:
tv = sum(abs(math.exp(log_mass(n, k, t + w * delta))
- math.exp(log_mass(n, k, t))) for k in range(n + 1)) / 2
lower = 1 - 2 * math.exp(-n * (w * delta) ** 2 / 2)
assert lower > 0 and tv + 1e-8 >= lower and tv <= 1 + 1e-8
counts["nonvacuous_bound_checks"] += 1
print(json.dumps({"status": "PASS", **counts,
"scope": "finite examples only; not an all-policy test"}, indent=2))
if __name__ == "__main__":
main()
|