#!/usr/bin/env python3
"""SYNTHESIS verifier: independent recompute of every decisive number cited by
the five audit reports, so the final ruling rests on arithmetic recomputed here."""
import math, json
from statistics import NormalDist

nd = NormalDist()
Z = lambda p: nd.inv_cdf(1 - p) if 0 < p < 1 else 0.0
out = {}

# ---- 1. App CE as written (six stated p's; floor four) ----
p6 = [0.004, 0.005, 0.004, 0.009, 0.074, 0.031]
P6 = math.prod(p6)
p4 = [0.004, 0.005, 0.074, 0.031]           # CE floor rows: s2w, a0, th12, sin2b
P4 = math.prod(p4)
out['CE_as_written'] = dict(P6=P6, Z6=Z(P6), P4=P4, Z4=Z(P4))

# ---- 2. G8d NULL-C flagship (E6) ----
cons3 = (1/12) * (3/135) * 0.136
mod3  = (1/56) * ((3/135)*0.074) * 0.136
out['E6_flagship'] = dict(cons3=(cons3, Z(cons3)), mod3=(mod3, Z(mod3)))

# ---- 3. Corrected tiers: restore theta12 at its own fair-menu price 1/14 ----
th12 = 1/14
cons4 = cons3 * th12
mod4  = mod3 * th12
out['corrected_hostile'] = dict(cons4=(cons4, Z(cons4)), mod4=(mod4, Z(mod4)),
                                cons4_x3_s13menu=(cons4*3, Z(cons4*3)))

# ---- 4. Conditional own-framing corrected floor (lens-1 rows) ----
base4 = 0.003899 * 0.074345 * (3/135) * (1/30)   # s2w 2d, th12 2d, H0/L grammar, Ochi 2d
out['conditional_base4'] = (base4, Z(base4))
plus_a0 = base4 * 0.3                            # a0 weak, measurement-limited
out['conditional_plus_a0_weak'] = (plus_a0, Z(plus_a0))

# ---- 5. CKM pulls (zeroing check) ----
a = 1/137.035999084
# PDG-style: lambda=0.22500+-0.00067, A=0.826+-0.012(ish), rhobar=0.159+-0.010, etabar=0.348+-0.010
vals = dict(lam=(31*a, 0.22500, 0.00067), A=(108*a, 0.8260, 0.0120),
            rho=(43/2*a, 0.1590, 0.0100), eta=(49*a, 0.3480, 0.0100))
pulls = {k: (pred - m)/s for k, (pred, m, s) in vals.items()}
chi2 = sum(v*v for v in pulls.values())
nearest = {k: m/a for k, (pred, m, s) in vals.items()}
out['ckm'] = dict(pulls={k: round(v,2) for k,v in pulls.items()}, chi2_4dof=round(chi2,1),
                  nearest_int={k: round(v,2) for k,v in nearest.items()})

# ---- 6. Alpha ladder (Part I) ----
step = 1/(2*math.sqrt(a))*2*a/ math.sqrt(a) # placeholder replaced below by direct ladder
# ladder: alpha_inv(k) per E5 convention -- reproduce via published G8d numbers instead:
alpha_inv_60 = 137.03599985412
codata18 = 137.035999084
resid_ppm = abs(alpha_inv_60 - codata18)/codata18 * 1e6
step = 2.3178643578388574
p_as_obs   = 2*codata18*resid_ppm*1e-6/step
p_subppm   = 2*codata18*1e-6/step
p_band01   = 2*codata18*0.1e-6/step
n_lad = 10*10*12*3*3
out['alpha_ladder'] = dict(
    resid_ppm=resid_ppm, n_ladders=n_lad,
    p_as_obs=p_as_obs, E_as_obs=p_as_obs*n_lad, margin=1/p_as_obs/n_lad,
    E_band01=p_band01*n_lad, margin_band01=1/p_band01/n_lad,
    E_subppm=p_subppm*n_lad)

# ---- 7. selection-null sanity: median product of 3 smallest of 20 uniforms ----
import random
random.seed(7)
meds = []
for _ in range(200000):
    u = sorted(random.random() for _ in range(20))[:3]
    meds.append(u[0]*u[1]*u[2])
meds.sort()
out['median_prod3of20'] = meds[len(meds)//2]

print(json.dumps(out, indent=1, default=str))
json.dump(out, open('verify3x/synth_check.json','w'), indent=1, default=str)
