"""The 40-pip pick against the deployed 34.8 under the random side's swap (section 26), on the exact
chains: G, the cash rate of Proposition "the cash rate", is the only axis on which 40 pips beats
34.8 (T favours 34.8, D ties). Recompute G under the inputs section 26 fixed:
  - the payout clock: round(14 / d) gives 6 trades at 40 pips; the first trade to end on or after
    day 14 is the ceiling, 7,
  - the pace: section 26 interpolates assessment cells (target clipped); funded cells read 2.325
    days at 40 pips (s03b),
  - the cost share: c E[1/l] for the variable stop that has a 40-pip mean (s04), not c / 40.
"""
import json

from common import PA, STOP0, SWAP_COIN, cell

C_SPREAD_PIPS = 1.0     # commission 0.5 pip plus a 0.5-pip spread
JENSEN = {34.79: 1.0326, "40 beta": 1.0648, "40 floor": 1.0089}      # E[1/l] E[l], s04


def g_coin(l, jensen=1.0):
    """Section 26's cost26 for the random side with swap, the commission and spread scaled by Jensen."""
    return C_SPREAD_PIPS / l * jensen + SWAP_COIN * l / STOP0


rows = []
for label, l, wait, d, jen in (
        ("34.8 as published", STOP0, 7, 2.02508, 1.0),
        ("34.8, funded pace", STOP0, 7, 2.048, 1.0),
        ("34.8, Jensen", STOP0, 7, 2.02508, JENSEN[34.79]),
        ("40 as published", 40.0, 6, 2.2824, 1.0),
        ("40, clock 7", 40.0, 7, 2.2824, 1.0),
        ("40, funded pace", 40.0, 6, 2.325, 1.0),
        ("40, clock 7 and funded pace", 40.0, 7, 2.325, 1.0),
        ("40 beta knob, Jensen", 40.0, 6, 2.2824, JENSEN["40 beta"]),
        ("40 beta knob, Jensen, clock 7, funded pace", 40.0, 7, 2.325, JENSEN["40 beta"]),
        ("40 floor knob, Jensen", 40.0, 6, 2.2824, JENSEN["40 floor"]),
        ("45 as published", 45.0, 6, 2.5353, 1.0),
):
    g = g_coin(l, jen)
    c = cell(g, wait, pace=d)
    rows.append(dict(label=label, stop=l, wait=wait, pace=d, g=g, **{k: c[k] for k in ("rf", "p_pass", "p_any", "cash_fee", "G")}))
    print(f"{label:44}: cost {g:.3%} clock {wait} pace {d:.3f} pick {c['rf']:.3%} pass {c['p_pass']:.2%} any {c['p_any']:.2%} "
          f"cash/fee {c['cash_fee']:.3f} G ${c['G']:+.1f}/yr", flush=True)
json.dump(rows, open("/tmp/arena-prime-audit/candidate-1/out_s05.json", "w"), indent=1)
