#!/usr/bin/env python3
"""E3: Hit list + dependency matrix + effective-independent-hit collapse.
HONESTY-GUARD MAXIMAL: conservative (larger space, fewer effective hits, worst-case null).
Methodology = App.~CE of the main paper (appendix_CE_combined_evidence.tex): fractional-agreement
blind-hit price p_i = min(1, 2*delta_i) on an O(1) prior; blocks priced at ONE number.
"""
import math

ALPHA = 1/137.035999084  # CODATA

# ---------------------------------------------------------------- raw hit rows
# (name, DFD value, measured, delta_frac, pull_sigma_or_None, citation)
rows = []

# 1. alpha^-1 -- SELECTOR, not a hit (ledger 2026-07-01: k_max=60 = selection-plus-match,
#    sub-ppm alpha IS the selector; counting it as a hit double-books the selection).
rows.append(("alpha^-1", 137.036, 137.035999084, 7e-9, None,
             "app Z summary; app O 'pinned empirically by the sub-ppm'; ledger kmax entry",
             "EXCLUDED: selector of k_max=60 (rule iii)"))

# 2. sin^2 thetaW = 3/13
d = abs(3/13 - 0.23122)/0.23122
rows.append(("sin2thetaW=3/13", 3/13, 0.23122, d, None,
             "app Z thm:weinberg; App CE row 1", "HIT (engine: partition (3,2,1)+TrY^2)"))

# 3. H0 = 72.09 vs SH0ES 73.04 +/- 1.04
dH0 = abs(72.09-73.04)/73.04; pH0s = abs(72.09-73.04)/1.04
rows.append(("H0 [km/s/Mpc]", 72.09, 73.04, dH0, pH0s,
             "app O_alpha57 line 302", "HIT (engine E-B: 57-count + clock dictionary)"))

# 4. rho_Lambda exponent: (3/8pi) alpha^57 rho_P -> 10^-122.x ; scale 2.547 vs 2.24 meV
expo = 57*math.log10(ALPHA) + math.log10(3/(8*math.pi))
d_meV = abs(2.547-2.24)/2.24
rows.append(("rho_Lambda exponent", expo, -122.7, d_meV, None,
             "app AU thm:au-rhoL; look-elsewhere 3/135 (app AU line ~250)",
             "COLLAPSED into H0 block: rho_L = (3/8pi) H0^2 M_P^2 is the Friedmann closure "
             "identity given the 57-invariant -> SAME hit as H0 (rule i)"))

# 5. Omega_chi h^2 = 0.118 vs 0.1200 +/- 0.0012
dchi = abs(0.118-0.1200)/0.1200
rows.append(("Omega_chi h^2", 0.118, 0.1200, dchi, abs(0.118-0.120)/0.0012,
             "app TP thm:chi-postinflation (Q_CS=0.073, -1.5sigma)",
             "HIT (engine: SU(2)_60 CS modular-Casimir; shares k_max=60 coordinate)"))

# 6. m_beta = 9.16 meV -- untested BET, excluded per pre-registration.
rows.append(("m_beta comb", 9.16e-3, None, None, None, "app AU adjacent-rung; nu sector",
             "EXCLUDED: untested prediction = bet (rule v)"))

# 7. CKM block: (lambda,A,rhobar,etabar)=(31,108,43/2,49)*alpha + derived angles.
lam, A_, rb, eb = 31*ALPHA, 108*ALPHA, 21.5*ALPHA, 49*ALPHA
# reference global-fit values (CKMfitter/PDG-era, as used in the corpus)
pulls = {"lambda": (lam, 0.22501, 0.00068), "A": (A_, 0.826, 0.015),
         "rhobar": (rb, 0.1581, 0.0092), "etabar": (eb, 0.3548, 0.0072)}
ckm_pulls = {k: abs(v[0]-v[1])/v[2] for k, v in pulls.items()}
sin2b_dfd, sin2b_obs = 0.719, 0.708
d_s2b = abs(sin2b_dfd-sin2b_obs)/sin2b_obs
rows.append(("CKM block (lam,A,rb,eb;gamma;Vub/Vcb;sin2beta)", "(31,108,43/2,49)a", "global fit",
             d_s2b, ckm_pulls,
             "app AO (Postulate E.1 + h0(O(k)) cohomology); gamma 0.12sigma app AO:883",
             "1 EFFECTIVE HIT: 4 coords share E.1 postulate + one cohomology grammar; "
             "gamma, Vub/Vcb, sin2beta are functions of the 4 (rules i+ii); "
             "priced at sin2beta delta=1.6% exactly as App CE did"))

# 8. alpha_s(M_Z)=0.1187 vs 0.1179 +/- 0.0009 ; exponent 19/2 SELECTED not forced.
das = abs(0.1187-0.1179)/0.1179
rows.append(("alpha_s(M_Z)", 0.1187, 0.1179, das, abs(0.1187-0.1179)/0.0009,
             "app Z line 342 (0.8sigma); app AQ; audit ledger: 19/2 SELECTED",
             "SOFT: enters maximal count only, with x10 selection-trials penalty (rule vi)"))

# 9. PMNS TM1 sin^2 theta12
d12 = abs(0.3184-0.307)/0.307
rows.append(("PMNS sin2theta12 (TM1)", 0.3184, 0.307, d12, None,
             "App CE row 5; TM1 (parameter-free)", "HIT (neutrino sector, distinct)"))

# 10. fermion-mass LO 1.42% -- eta_q circularity upheld (audit ledger) -> EXCLUDE.
rows.append(("fermion-mass LO", "9 alpha-exponents", "mean 1.42% err", 0.0142, None,
             "app FM line 152", "EXCLUDED: eta_q=2.67 circularity upheld; App Z books it "
             "'Conditional' (rule: contested engine cannot supply a hit)"))

# 11. N_eff = 3.044 -- DECISION: INHERITED, not a hit.
rows.append(("N_eff", 3.044, 3.044, 0.0, None,
             "SM decoupling value; corpus books it as FALSIFIER",
             "EXCLUDED: SM-inherited decimal; DFD adds only 'no extra species' (rule iv)"))

# 12. a0 = 2 sqrt(alpha) c H0
H0_SI = 72.09*1000/3.0856775814913673e22
a0 = 2*math.sqrt(ALPHA)*2.99792458e8*H0_SI
da0 = abs(a0-1.20e-10)/1.20e-10
rows.append(("a0 (MOND)", a0, 1.20e-10, da0, None,
             "App CE row 2 (2 sqrt(alpha) c H0)",
             "HIT (residual coefficient 2*sqrt(alpha)=%.4f vs empirical a0/cH0=%.4f); "
             "WORST-CASE merges into H0 block since it reuses H0 (floor scenario)"
             % (2*math.sqrt(ALPHA), 1.20e-10/(2.99792458e8*H0_SI))))

# ---------------------------------------------------------------- collapse + combine
def p_blind(delta):   # App CE blind-hit price
    return min(1.0, 2*delta)

p_thetaW = p_blind(abs(3/13-0.23122)/0.23122)
p_a0     = p_blind(da0)
p_th12   = p_blind(d12)
p_ckm    = p_blind(d_s2b)                     # whole block priced at ONE number (CE discipline)
p_LE     = 3/135                              # corpus look-elsewhere for the 57-rung Lambda scale
p_H0L    = max(p_blind(dH0), p_LE)            # conservative: take the LARGER price for the block
p_chi    = p_blind(dchi)
p_als    = min(1.0, 10*p_blind(das))          # x10 exponent-selection trials penalty (n in ~[15/2,29/2])

def Z(P):  # one-sided Gaussian equivalent
    from math import sqrt, log, erfc
    lo, hi = 0.0, 40.0
    for _ in range(200):
        mid = (lo+hi)/2
        if 0.5*erfc(mid/math.sqrt(2)) > P: lo = mid
        else: hi = mid
    return (lo+hi)/2

floor   = [("sin2thetaW",p_thetaW),("theta12",p_th12),("CKM-block",p_ckm),
           ("H0/Lambda/a0-block",p_H0L),("Omega_chi",p_chi)]
central = [("sin2thetaW",p_thetaW),("theta12",p_th12),("CKM-block",p_ckm),
           ("H0/Lambda-block",p_H0L),("a0-coefficient",p_a0),("Omega_chi",p_chi)]
maximal = central + [("alpha_s (trials-penalized)",p_als)]

import functools, operator
for name, hits in [("FLOOR(5)",floor),("CENTRAL(6)",central),("MAXIMAL(7)",maximal)]:
    P = functools.reduce(operator.mul,[p for _,p in hits],1.0)
    print(f"{name}: P={P:.3e}  Z={Z(P):.2f}sigma   " + " ".join(f"{n}={p:.4f}" for n,p in hits))

print("\n--- per-row table ---")
for r in rows:
    print(r)
print(f"\nrho_Lambda exponent computed: 10^{expo:.2f} (vs observed ~10^-122.7)")
print(f"a0 computed: {a0:.4e} m/s^2 (delta={da0*100:.2f}%)")
print("CKM pulls:", {k: round(v,2) for k,v in ckm_pulls.items()})
print(f"H0 pull: {pH0s:.2f}sigma; Omega_chi pull: {abs(0.118-0.120)/0.0012:.2f}sigma; "
      f"alpha_s pull: {abs(0.1187-0.1179)/0.0009:.2f}sigma")
