"""How much premium, on which legs? The full grid.

Measured fill rates at the touch + premium, queue-aware, 711 legs:

    favourite (>=45c)   49.7  33.1  24.2  17.8   at +0,1,2,3c
    mid home/away       41.0  23.3  16.4  11.0
    draw                17.7   6.8   2.1   0.4

A book completes only when all three fill, so expected value per book quoted is
the PRODUCT of the three rates times what the book pays (2c of overround plus
whatever premium we added). Charging more raises the payment linearly and cuts
completion multiplicatively, which is why intuition is a poor guide here.

Also reports stranding -- P(the favourite fills but the book does not finish) --
because that is the term that actually cost money: -42c and -37c on two books
today, against completions worth 2-4c.
"""
import itertools

FAV = [49.7, 33.1, 24.2, 17.8]
MID = [41.0, 23.3, 16.4, 11.0]
DRAW = [17.7, 6.8, 2.1, 0.4]
BASE_OVERROUND = 2.0          # cents the book pays before any premium


def main():
    rows = []
    for f, m, d in itertools.product(range(4), repeat=3):
        comp = (FAV[f] / 100) * (MID[m] / 100) * (DRAW[d] / 100)
        pays = BASE_OVERROUND + f + m + d
        ev = comp * pays
        strand = (FAV[f] / 100) * (1 - (MID[m] / 100) * (DRAW[d] / 100))
        rows.append((ev, f, m, d, comp, pays, strand))
    rows.sort(reverse=True)

    base = next(r for r in rows if r[1] == 0 and r[2] == 0 and r[3] == 0)
    print(f'{"fav":>4} {"mid":>4} {"draw":>5} {"completion":>11} {"pays":>6} '
          f'{"EV/book":>9} {"vs touch":>9} {"stranding":>10}')
    print('  --- best ten by expected value ---')
    for ev, f, m, d, comp, pays, st in rows[:10]:
        print(f'{"+"+str(f)+"c":>4} {"+"+str(m)+"c":>4} {"+"+str(d)+"c":>5} '
              f'{comp*100:>10.2f}% {pays:>5.1f}c {ev*100:>8.3f}c '
              f'{(ev-base[0])*100:>+8.3f}c {st*100:>9.1f}%')

    print('\n  --- what we run now, and the neighbours ---')
    for want in ((0, 0, 0), (1, 0, 0), (2, 0, 0), (3, 0, 0),
                 (1, 1, 0), (2, 1, 0)):
        r = next(x for x in rows if (x[1], x[2], x[3]) == want)
        ev, f, m, d, comp, pays, st = r
        tag = '  <-- live now' if want == (1, 0, 0) else ''
        print(f'{"+"+str(f)+"c":>4} {"+"+str(m)+"c":>4} {"+"+str(d)+"c":>5} '
              f'{comp*100:>10.2f}% {pays:>5.1f}c {ev*100:>8.3f}c '
              f'{(ev-base[0])*100:>+8.3f}c {st*100:>9.1f}%{tag}')

    print('\n  EV/book is per book QUOTED, not per completion. Stranding is')
    print('  P(short the favourite, book unfinished) -- the -40c tail.')


if __name__ == '__main__':
    main()
