"""(1) Effective risk after flooring to the 0.01-lot grid, per trade, on the walker's stops: mean realised risk for the 3% assessment and the 1.575% funded stage.
(2) Chain effect of that on cash/fee. (3) A combined 'corrected' gate: Jensen cost share, lot-floored risks, payout clock m drawn from the empirical distribution.
Wallet MC, n=5000, 3 seeds, 80% honoured, $1,000, 541 trades."""
import numpy as np, pandas as pd
from walker import trades
from wallet_variants import *
d=pd.concat([trades(seed=s) for s in range(1,6)])
st=d.stop.to_numpy()
def reff(r,s,B=3000.0): 
    lots=np.floor(r*B/(10*s)/0.01+1e-9)*0.01
    return lots*10*s/B
for r in (0.03,0.01575):
    x=reff(r,st); print(f"nominal {r:.5f}: mean realised risk {x.mean():.5f} ({x.mean()/r-1:+.2%}), min {x.min():.4f}, share of trades skipped (lots<0.01): {np.mean(x==0):.3%}")
T3=PA.Tier(3000,38,51)
g0=PA.cost_share(34.791728647093755)+0.004783927673690548
ra_eff=reff(0.03,st).mean(); rf_eff=reff(0.01575,st).mean()
e=PA.eval_chain(0.03,g0*0.03,rr=1.0,clip_up=True,clip_dn=True); e2=PA.eval_chain(ra_eff,g0*ra_eff,rr=1.0,clip_up=True,clip_dn=True)
f=PA.funded_chain(0.01575,g0*0.01575,PA.payout_threshold(0.01575,1.0,3000,0.7),rr=1.0,clip_dn=True,wait=7)
f2=PA.funded_chain(rf_eff,g0*rf_eff,PA.payout_threshold(rf_eff,1.0,3000,0.7),rr=1.0,clip_dn=True,wait=7)
c1=0.7*3000*e['p_pass']*f['e_W']/38; c2=0.7*3000*e2['p_pass']*f2['e_W']/38
print(f"chain: nominal pass {e['p_pass']:.4f} p_any {f['p_any']:.4f} cash/fee {c1:.4f};  lot-floored (mean realised risk) pass {e2['p_pass']:.4f} p_any {f2['p_any']:.4f} cash/fee {c2:.4f}  ({c2/c1-1:+.2%})")
# wallet MC: baseline, lot-floored, Jensen, clock, all
vals=np.array([7]); p=np.array([1.0])
def run(tag,ra,rf,g,wait_dist=None,wait=7,seeds=(13,14,15)):
    out=[]
    for s in seeds:
        w,ru,pu=wallet_var(T3,ra,g*ra,0.7,0.8,1000,horizon_days=541,n=5000,seed=s,funded_risk=rf,funded_cost=g*rf,wait=wait,wait_dist=wait_dist)
        out.append(glog(w,1000)[0])
    print(f"{tag:70s} e_log_growth(80%) {np.round(out,4)} mean {np.mean(out):+.4f}",flush=True)
gJ=0.029642+0.004756
run("baseline (paper inputs)",0.03,0.01575,g0)
run("lot-floored risks",ra_eff,rf_eff,g0)
run("Jensen cost share 3.44%",0.03,0.01575,gJ)
run("lot-floored + Jensen",ra_eff,rf_eff,gJ)
