"""Independent numerical cross-check: solve the REML score, not its objective.

Does not validate clinical source extraction or causal identification.
"""
import csv,json,math
from scipy.optimize import brentq
from scipy.stats import t

raw=list(csv.DictReader(open('data/trials.csv')))
chosen=[r for r in raw if r['eligible']=='1' and r['primary_individual']=='1' and r['algorithm'].startswith('learned')]
ys=[];vs=[]
for r in chosen:
    a=int(r['deaths_alert']);c=int(r['deaths_control'])
    b=int(r['n_alert'])-a;d=int(r['n_control'])-c
    ys.append(math.log(a/b)-math.log(c/d));vs.append(sum(1/x for x in [a,b,c,d]))
def score(z):
    w=[1/(v+z) for v in vs];mu=sum(x*y for x,y in zip(w,ys))/sum(w)
    return sum(w)-sum(x*x for x in w)/sum(w)-sum(x*x*(y-mu)**2 for x,y in zip(w,ys))
tau=0 if score(0)>=0 else brentq(score,0,10,xtol=1e-13)
w=[1/(v+tau) for v in vs];mu=sum(x*y for x,y in zip(w,ys))/sum(w)
q=sum(x*(y-mu)**2 for x,y in zip(w,ys))/(len(ys)-1)
se=math.sqrt(max(1,q)/sum(w));crit=t.ppf(.975,len(ys)-1)
out=dict(tau2_score_root=tau,estimate=math.exp(mu),lower=math.exp(mu-crit*se),upper=math.exp(mu+crit*se),p_two_sided=float(2*t.sf(abs(mu/se),len(ys)-1)))
declared=json.load(open('results/R2.json'))
for actual,expected in [(out['estimate'],declared['estimate']),(out['lower'],declared['ci95'][0]),(out['upper'],declared['ci95'][1]),(out['p_two_sided'],declared['p_two_sided']),(tau,declared['tau2'])]:
    assert abs(actual-expected)<.0001,(actual,expected)
out['passed']=True
open('results/numerical_validation.json','w').write(json.dumps(out,indent=2)+'\n')
print('Independent REML score-root cross-check passed.')
