"""spread.family hands every account's wallet_mc the same `seed`.  With `coins` supplied, wallet_mc's rng is used only for the honour draws,
so every account in a pooled wallet draws the SAME honour uniform each day (a common refusal shock).  Test: 2 accounts, 'commission and spread' at 40 pips
(front.json: ruin 2.96%, log g +0.060 at N=2), rho as the front's staggered family, same copula coins; (a) as coded: same seed; (b) honour draws independent (seed+1 for account 2)."""
import json, numpy as np
from pa_imp import *
from modules import spread as SP, hedge as HG
from scipy.special import ndtr
F=json.load(open('/root/Projects/trading-framework/src/notebooks/reports/topics/prime_academy/paper/front.json'))
c=[c for c in F['cells'] if c['scenario']=='commission and spread' and c['stop']==40.0 and c['N']==2][0]
T3=PA.Tier(3000,38,51); H=int(round(3*365/c['pace'])); n=5000
rho_n=SP.rho_normal(0.26)
print("pace",c['pace'],"H",H,"wait",c['wait'],"rf",c['rf'],"rho_normal",round(rho_n,3))
def pooled(seed_a,seed_b,cseed):
    rng=np.random.default_rng(cseed); z0=rng.standard_normal((H,n))
    co=lambda: ndtr(np.sqrt(rho_n)*z0+np.sqrt(1-rho_n)*rng.standard_normal((H,n)))
    fl=[];gt=[]
    for sd in (seed_a,seed_b):
        f,g=SP.stream(T3,0.03,c['cost']*0.03,0.7,1.0,H,n,sd,co(),p_paid=0.8,funded_risk=c['rf'],funded_cost=c['cost']*c['rf'],clip_up=True,clip_dn=True,wait=c['wait'])
        fl.append(f);gt.append(g)
    w=HG.shared_wallet(fl[0]+fl[1],1000.0,gt[0]+gt[1],paths=True); lg=w['paths']['log_g']
    return w['p_ruin'],w['e_log_growth'],lg.std(ddof=1)/np.sqrt(n)
for cseed in (5,6):
    a=pooled(13,13,cseed); b=pooled(13,14,cseed)
    print(f"copula seed {cseed}: same honour seed  ruin {a[0]:.2%} log g {a[1]:+.4f} (SE {a[2]:.4f}) | independent honour  ruin {b[0]:.2%} log g {b[1]:+.4f} (SE {b[2]:.4f})",flush=True)
