#!/usr/bin/env python3 """Check the same-sign stationarity bound used in Appendix D.""" import sympy as sp def equal(left, right): difference = sp.cancel(left - right) assert difference == 0, difference def main(): rho_left, rho_right, gamma = sp.symbols("rho_left rho_right gamma", positive=True) margin, neighbor = sp.symbols("margin neighbor", positive=True) n_left, n_right, n_cross = sp.symbols("n_left n_right n_cross", real=True) log_ratio = sp.Symbol("log_ratio", real=True) mu = margin**2 + 4 action = 3*margin*neighbor - 4 - (margin**2 + neighbor**2)*sp.log(margin + neighbor) action += margin**2*sp.log(mu)/2 + neighbor**2*sp.log(neighbor**2 + 4)/2 rational_row = sp.diff(action, margin)/margin + 2*sp.log(margin + neighbor) - sp.log(mu) def action_row(share): row = rational_row.subs(neighbor, margin*(1/share - 1)) row = sp.cancel(row - margin**2/mu) + gamma equal(row, 2/share - 1 - 2*share + gamma) return row # The logarithms in the mean of the two derivatives combine to # log(mu/(alpha_left*alpha_right)) = -log(gamma/(rho_left*rho_right)). equal( mu / ((margin/rho_left)*(margin/rho_right)), (rho_left*rho_right/gamma).subs(gamma, margin**2/mu), ) # Both signs of a same-sign visit cancel out of the stationarity equation. mean = (action_row(rho_left) + action_row(rho_right))/2 - log_ratio cross = 8*(n_left/rho_left + n_right/rho_right) - mean def switch_bound(share): return (1 - share + share**2)/8 comparison = cross.subs({n_left: switch_bound(rho_left), n_right: switch_bound(rho_right)}) upper_or_lower = -1 + 2*(rho_left + rho_right) - gamma + log_ratio equal(comparison, upper_or_lower) assert cross.diff(n_left).is_positive and cross.diff(n_right).is_positive # Reverse: replace gamma by its upper bound, using log(gamma)-gamma. equal((sp.log(gamma) - gamma).diff(gamma), (1 - gamma)/gamma) a, b = sp.symbols("a b", positive=True) reverse_bound = upper_or_lower.subs({log_ratio: 0, gamma: rho_left*rho_right}) equal(sp.Rational(3, 4) - reverse_bound.subs({rho_left: sp.Rational(1, 2)-a, rho_right: sp.Rational(1, 2)-b}), 3*(a + b)/2 + a*b) # Forward: log(q)>0 and gamma