"""Sensitivity of the funded chance of any payout (and first-payout trades, cash per purchase) to the payout clock in trades
and to the funded risk, at the deployed random-side cost share. Chains only."""
import numpy as np
from pa_imp import *
g=PA.cost_share(34.791728647093755)+0.004783927673690548
e=PA.eval_chain(0.03,g*0.03,rr=1.0,clip_up=True,clip_dn=True)
print("risk grid x wait grid: p_any")
ws=[5,6,7,8,9,10,14]
rs=[0.0120,0.0130,0.0140,0.0145,0.0150,0.0155,0.01575,0.0160,0.0165,0.0170]
print("r\\wait "+" ".join(f"{w:>7d}" for w in ws))
tab={}
for r in rs:
    row=[]
    for w in ws:
        f=PA.funded_chain(r,g*r,PA.payout_threshold(r,1.0,3000,0.7),rr=1.0,clip_dn=True,wait=w)
        tab[(r,w)]=f; row.append(f['p_any'])
    print(f"{r:.5f} "+" ".join(f"{x:7.4f}" for x in row))
print("cash per $38 fee (0.7*3000*p_pass*E[W]/38):")
for r in rs:
    print(f"{r:.5f} "+" ".join(f"{0.7*3000*e['p_pass']*tab[(r,w)]['e_W']/38:7.4f}" for w in ws))
print("median trades to first payout:")
for r in rs:
    print(f"{r:.5f} "+" ".join(f"{tab[(r,w)]['med_first_pay']:7d}" for w in ws))
print("teeth r_j = m1/(j-n*gamma), n=wait:")
for w in ws: print(w,[round(x,5) for x in PA.tooth_edges(g,n=w)])
# line search per wait: argmax, plateau, pick
print("line search (step 0.00025) per wait: argmax risk / p_any ; plateau ; pick")
for w in (5,6,7,8,9):
    rows=PA.funded_line_search(g,lo=0.006,hi=0.020,step=0.00025,wait=w)
    best=max(rows,key=lambda r:r['p_any']); plat=PA.plateau(rows); pick=min(plat,key=lambda r:(r['med_first_pay'],-r['e_W']))
    print(f"wait {w}: argmax {best['risk']:.5f} ({best['p_any']:.4f}); plateau {plat[0]['risk']:.5f}-{plat[-1]['risk']:.5f}; pick {pick['risk']:.5f} p_any {pick['p_any']:.4f} med_first {pick['med_first_pay']} e_W {pick['e_W']:.4f}")
