"""Certify a rank-one lower bound for the coupled entropy minus the independent kernel.""" from fractions import Fraction from functools import lru_cache from calibrated_kernel import ball, improvement from check_rank import HALF, TAU, L, Q, diagonal_input, entropy, entropy_range from diagonal_tail import diagonal_residual from flint import arb LOW, HIGH = Fraction(114, 200), Fraction(139, 200) def band_gain(a, b, x): """Enclose sqrt(Q log(2) - D(x,x)) via the midpoint and derivative.""" mid = (a + b) / 2 left, right = 1 - mid, 1 + mid value = Q * L - left * (right * right.log() + left * left.log()) left, right = 1 - x, 1 + x derivative = right * right.log() + left * left.log() - left * (right / left).log() return (value + (x - mid) * derivative).nonnegative_part().sqrt() @lru_cache(None) def features(lo, hi, kind): a, b = ball(lo), ball(hi) x = a.union(b) h = entropy_range(a, b) if kind in ("band", "circle"): f = (HALF - x * x).sqrt() df = -x / f elif kind == "linear": f, df = x, arb(1) else: f, df = TAU.sqrt() * x * (1 - x), TAU.sqrt() * (1 - 2 * x) if kind == "band": gain = band_gain(a, b, x) else: h0 = entropy_range(diagonal_input(a), diagonal_input(b)) phi = (h0 / L).nonnegative_part().sqrt() bump = improvement(a, b, phi, kind) loss = (2 * x * h - entropy_range(a * a, b * b)).nonnegative_part() residual = Q * h0 - loss if a > 0 and b < 1 and phi > 0: residual = residual.intersection(phi * diagonal_residual(a, b, x, phi)) gain = (residual + 2 * Q * L * phi * bump).nonnegative_part().sqrt() return x, h, f, df, gain def verify_box(left, right, depth=0): a, b, k = left c, d, j = right x, hx, f, df, p = features(*left) y, hy, g, dg, v = features(*right) z = x * y + f * g independent = x * hy + y * hx - entropy_range(ball(a) * ball(c), ball(b) * ball(d)) gap = Q * entropy_range(z.lower(), z.upper()) - independent if gap >= p * v: return 1 if d < 1 and z > 0 and z < 1: xm, ym = (a + b) / 2, (c + d) / 2 u, hu, fu, _, _ = features(xm, xm, k) w, hw, fw, _, _ = features(ym, ym, j) mid = Q * entropy(u * w + fu * fw) - u * hw - w * hu + entropy(u * w) hp = Q * (1 / z - 1).log() dx = hp * (y + df * g) - hy - y * (y * (1 - x) / (1 - x * y)).log() dy = hp * (x + dg * f) - hx - x * (x * (1 - y) / (1 - x * y)).log() if mid + (x - u) * dx + (y - w) * dy >= p * v: return 1 if depth == 50: raise ArithmeticError((left, right)) if b - a > d - c: m = (a + b) / 2 return verify_box((a, m, k), right, depth + 1) + verify_box( (m, b, k), right, depth + 1 ) m = (c + d) / 2 return verify_box(left, (c, m, j), depth + 1) + verify_box( left, (m, d, j), depth + 1 ) def verify_arc(a, b, c, d, depth=0): # Divide by (x-y)^2 before taking intervals, so the diagonal is regular. x = ball(a).union(ball(b)) y = ball(c).union(ball(d)) f, g = (HALF - x * x).sqrt(), (HALF - y * y).sqrt() slope = (x + y) / (f + g) u = x * y + f * g loss = Q * (x - y) ** 2 * (1 + slope**2) ** 2 / (8 * u * (1 - u)) # v_n(x)-v_n(y) = (x-y)(1-S_n)/sqrt(n(n-1)). first_tail_term = 20 divided = arb(1) yp = arb(1) energy = arb(0) for n in range(2, first_tail_term): yp *= y divided = x * divided + yp energy += (1 - divided) ** 2 / (n * (n - 1)) tail_bound = first_tail_term * ball(HIGH) ** (first_tail_term - 1) energy += (1 - tail_bound) ** 2 / (first_tail_term - 1) if 2 * loss <= energy and band_gain(ball(a), ball(b), x) < arb(13) / 100: return 1 if depth == 30: raise ArithmeticError((a, b, c, d)) if b - a > d - c: m = (a + b) / 2 return verify_arc(a, m, c, d, depth + 1) + verify_arc(m, b, c, d, depth + 1) m = (c + d) / 2 return verify_arc(a, b, c, m, depth + 1) + verify_arc(a, b, m, d, depth + 1) def verify(): band = (LOW, HIGH, "band") ranges = [ (Fraction(1, 4096), Fraction(2, 5), "off"), (Fraction(2, 5), Fraction(1, 2), "linear"), (Fraction(1, 2), LOW, "circle"), (HIGH, Fraction(1), "off"), ] return verify_arc(LOW, HIGH, LOW, HIGH), sum( verify_box(band, part) for part in ranges )