"""Evaluate the finite-ladder recursion with Python 3. The paper proves that P_N is the optimal rank-win probability on N sites and that S_2(c_N) >= P_N for N >= 2. The last column illustrates the limit 4/9 for the rank model. These numerical values do not compute the unrestricted optimum S_2(c_N). Usage: python3 anc/ladder_values.py [N ...] """ import argparse import csv from decimal import Decimal, localcontext import sys def ladder_rows(sizes): """Yield numerical scores and scaled deficits in increasing size.""" with localcontext() as context: context.prec = 50 deficit = Decimal(2) size = 1 for target in sizes: while size < target: root = (1 + deficit).sqrt() deficit = 2 * deficit / (root * (1 + root)) size += 1 yield size, (2 - deficit) / 3, size * deficit / 3 def main(): parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("sizes", metavar="N", type=int, nargs="*") args = parser.parse_args() sizes = sorted(set(args.sizes or [1, 2, 10, 100, 1000, 10000])) if sizes[0] < 1: parser.error("each site count N must be positive") writer = csv.writer(sys.stdout, lineterminator="\n") writer.writerow(("sites", "rank_win_probability", "scaled_deficit")) for size, probability, deficit in ladder_rows(sizes): writer.writerow((size, format(probability, ".18g"), format(deficit, ".18g"))) if __name__ == "__main__": main()