#!/usr/bin/env python3
"""INDEPENDENT RECOUNT (under the no-lazy-downgrade rule).
Re-derives: (1) row-level pulls/prices, (2) CE's own products, (3) E3/E5/E6 reproduction,
(4) corrected hit set = 4 flagship + 1 weak, (5) selection-null order statistics with
theta12 RESTORED, (6) corrected conditional own-framing floor."""
import math, json
import numpy as np
from statistics import NormalDist

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

# ---------- 1. row-level re-derivation ----------
rows = {}
# sin2 thetaW
s2w = 3/13; d_s2w = abs(s2w-0.23122)/0.23122
rows['s2w'] = dict(pred=s2w, meas=0.23122, delta=d_s2w, p_2delta=min(1,2*d_s2w))
# CKM pulls (PDG-generous errors as E3 used)
lam, A_, rb, eb = 31*ALPHA, 108*ALPHA, 21.5*ALPHA, 49*ALPHA
ckm = {'lambda': (lam,0.22501,0.00068), 'A': (A_,0.826,0.015),
       'rhobar': (rb,0.1581,0.0092), 'etabar': (eb,0.3548,0.0072)}
rows['ckm_pulls'] = {k: round(abs(v[0]-v[1])/v[2],3) for k,v in ckm.items()}
# nearest-integer check: is 108 the best-fitting integer for A?
rows['ckm_nearest_int'] = {k: round(v[1]/ALPHA,2) for k,v in ckm.items()}
ckm_chi2 = sum((abs(v[0]-v[1])/v[2])**2 for v in ckm.values())
rows['ckm_block_chi2_4dof'] = (round(ckm_chi2,2), 'p=%.3f'%(1-__import__('scipy.stats',fromlist=['chi2']).chi2.cdf(ckm_chi2,4)) if False else 'see below')
# theta12 TM1
t12 = (1-9*ALPHA)/(3*(1-3*ALPHA)); d12 = abs(t12-0.307)/0.307
rows['theta12'] = dict(pred=round(t12,5), meas=0.307, delta=d12, p_2delta=min(1,2*d12),
                       pull_vs_nufit=round(abs(t12-0.307)/0.012,2), menu_price=1/14)
# H0 / Lambda block
dH0 = abs(72.09-73.04)/73.04
rows['H0'] = dict(delta=dH0, p_2delta=2*dH0, p_AU_grammar=3/135,
                  p_two_stage_moderate=(3/135)*(2*0.95/25.7))
# rho_Lambda exponent
rows['rhoL_exponent'] = 57*math.log10(ALPHA)+math.log10(3/(8*math.pi))
# Omega_chi
dchi = abs(0.118-0.120)/0.120
rows['Omega_chi'] = dict(delta=dchi, p_2delta=2*dchi, p_hostile_4trials=min(1,4*2*dchi),
                         pull=round((0.120-0.118)/0.0012,2))
# a0: coefficient vs measurement-limited price
H0SI = 72.09*1000/3.0856775814913673e22
a0 = 2*math.sqrt(ALPHA)*2.99792458e8*H0SI
emp_coeff = 1.20e-10/(2.99792458e8*H0SI)
rows['a0'] = dict(pred_coeff=round(2*math.sqrt(ALPHA),4), emp_coeff=round(emp_coeff,4),
                  agreement=abs(a0-1.2e-10)/1.2e-10,
                  mcgaugh='1.20 +/- 0.02 stat +/- 0.24 syst e-10 (syst 20%)',
                  p_agreement_INVALID=2*abs(a0-1.2e-10)/1.2e-10,
                  p_measurement_limited_stat=2*0.02/1.20, p_measurement_limited_syst=2*0.24/1.20)
out['rows'] = rows

# ---------- 2. reproduce corpus App CE products ----------
ce6 = [0.004,0.005,0.004,0.009,0.074,0.031]
ce4 = [0.004,0.005,0.074,0.031]
out['CE_repro'] = dict(max6=dict(p=math.prod(ce6), Z=Z(math.prod(ce6))),
                       floor4=dict(p=math.prod(ce4), Z=Z(math.prod(ce4))))

# ---------- 3. reproduce E3 floor & E6 tiers ----------
e3_floor = [2*d_s2w, 2*d12, 0.031, max(2*dH0,3/135), 2*dchi]
out['E3_floor_repro'] = dict(p=math.prod(e3_floor), Z=Z(math.prod(e3_floor)))
e6_cons = [1/12, 3/135, 0.136]; e6_mod = [1/56, (3/135)*0.074, 0.136]
out['E6_repro'] = dict(cons=dict(p=math.prod(e6_cons), Z=Z(math.prod(e6_cons))),
                       mod=dict(p=math.prod(e6_mod), Z=Z(math.prod(e6_mod))))

# ---------- 4. CORRECTED hit sets ----------
# (a) hostile (menu-charged, unconditional), theta12 RESTORED at its own fair menu 1/14
host4 = {'s2w':1/12, 'theta12':1/14, 'H0block':3/135, 'Omega_chi':0.136}
host4_mod = {'s2w':1/56, 'theta12':1/14, 'H0block':(3/135)*0.074, 'Omega_chi':0.136}
# theta12 extra-hostile: charge x3 for s13=N*alpha menu N in {1,2,3}
host4_h = dict(host4); host4_h['theta12'] = 3/14
p_h4, p_h4m, p_h4h = (math.prod(d.values()) for d in (host4, host4_mod, host4_h))
out['hostile_corrected'] = dict(
    cons=dict(p=p_h4, Z=Z(p_h4)), mod=dict(p=p_h4m, Z=Z(p_h4m)),
    cons_theta12_x3=dict(p=p_h4h, Z=Z(p_h4h)))
# (b) conditional own-framing (grant theorem-grade forcing; CE 2delta methodology; FIXED rows)
cond = {'s2w':2*d_s2w, 'theta12':2*d12, 'H0block':3/135, 'Omega_chi':2*dchi}
p_c = math.prod(cond.values())
cond_a0 = dict(cond); cond_a0['a0_coeff'] = 0.3   # measurement-limited mid price
p_ca = math.prod(cond_a0.values())
cond_2stage = dict(cond); cond_2stage['H0block'] = (3/135)*(2*0.95/25.7)
p_c2 = math.prod(cond_2stage.values())
out['conditional_corrected'] = dict(
    base4=dict(p=p_c, Z=Z(p_c), hits=cond),
    plus_a0_weak=dict(p=p_ca, Z=Z(p_ca)),
    two_stage_H0=dict(p=p_c2, Z=Z(p_c2)))

# ---------- 5. selection null with theta12 restored (order stats, product of k smallest of M) ----------
rng = np.random.default_rng(77)
def sel_p(obs, M, k, n=2_000_000):
    u = rng.random((n, M)); u.partition(k-1, axis=1)
    return float(np.mean(np.prod(u[:,:k],axis=1) <= obs))
sel = {}
for tag, obs in [('host_cons', p_h4), ('host_mod', p_h4m)]:
    for M in (12, 20):
        p = sel_p(obs, M, 4)
        sel[f'{tag}_M{M}_k4'] = dict(p=p, p_kscan_x3=min(1,3*p), Z=Z(min(1,3*p)))
out['selection_null_restored'] = sel

# ---------- 6. order-stat sanity: median product of 3 smallest of 20 uniforms ----------
u = rng.random((200000,20)); u.partition(2,axis=1)
out['sanity_median_prod3of20'] = float(np.median(np.prod(u[:,:3],axis=1)))

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