sr3, sr4, sp = (m & 1), (m >> 1) & 1, (m >> 2) & 1 sh4_list[sp | (sr3 << 1) | (sr4 << 2)] += w else: biv_sub = biv_bern(c4_pouch / c4_univ, c4_r3p / c4_univ, phi_c) d_sub = [biv_sub[(0, 0)], biv_sub[(1, 0)], biv_sub[(0, 1)], biv_sub[(1, 1)]] sh4_list = frechet_couple(d_sub, c4_r4 / c4_univ, phi_r4, lambda m: m != 0) d4 = [0.0] * 32 d4[0] = 1.0 - c4_univ for sm, ws in enumerate(sh4_list): wt = c4_univ * ws if wt == 0.0: continue sp, s3, s4 = (sm >> 0) & 1, (sm >> 1) & 1, (sm >> 2) & 1 route_masks = [] for rname, sflag, cr in [("R1", sp, c4_pouch), ("R2", sp, c4_pouch), ("R3P", s3, c4_r3p), ("R4", s4, c4_r4)]: idxs = [i for i in range(5) if routes[i] == rname] if not idxs: continue if sflag == 0: route_masks.append([(0, 1.0)]) else: fj = frechet_joint([arms[i][3] / cr for i in idxs], 1.0) route_masks.append([(sum((1 << idxs[k]) for k in range(len(idxs)) if (bm >> k) & 1), pr) for bm, pr in enumerate(fj) if pr > 0.0]) comb = [(0, wt)] for ev in route_masks: comb = [(m0 | m1, w0 * w1) for m0, w0 in comb for m1, w1 in ev] for mf, wf in comb: d4[mf] += wf c4_eff = c4_univ * (1.0 - sh4_list[0]) dists = [d1, d2, d3, d4] eff_ceils = [c1_eff, c2_eff, c3_eff, c4_eff] max_marg_err = 0.0 for h in range(4): require_cond(abs(sum(dists[h]) - 1.0) < 1e-12, f"H{h+1} dist sum != 1") require_cond(eff_ceils[h] >= (1.0 - dists[h][0]) - 1e-12, f"H{h+1} incoherent ceiling") for i in range(5): marg = sum(dists[h][m] for m in range(32) if (m >> i) & 1) err = abs(marg - arms[i][h]) max_marg_err = max(max_marg_err, err) require_cond(err < 1e-9, f"Arm {i} H{h+1} marginal mismatch: {marg} vs {arms[i][h]}") v = d1 for h in (1, 2): nxt = [0.0] * 32 dh = dists[h] for m1, w1 in enumerate(v): if w1 == 0.0: continue for m2, w2 in enumerate(dh): if w2 != 0.0: nxt[m1 & m2] += w1 * w2 v = nxt v_h123 = v locked = 0.0 mod100 = 0.0 for mc in range(1, 32): wc = v_h123[mc] if wc == 0.0: continue allowed_r = set() for i in range(5): if (mc >> i) & 1: allowed_r.update(compat_map[cell_classes[i]]) for mh in range(1, 32): wh = d4[mh] if wh == 0.0: continue if (mc & mh) != 0: locked += wc * wh if any(routes[j] in allowed_r for j in range(5) if (mh >> j) & 1): mod100 += wc * wh s_min, s_max = strassen_bipartite_bounds(v_h123, d4, routes, cell_classes, compat_map) return { "name": name, "locked_exact": locked * 100.0, "modular_exact": mod100 * 100.0, "ceiling_exact": math.prod(eff_ceils) * 100.0, "locked_pct": round(locked * 100.0, 4), "modular_pct": round(mod100 * 100.0, 4), "ceiling_pct": round(math.prod(eff_ceils) * 100.0, 4), "hurdle_caps_exact_pct": [x * 100.0 for x in eff_ceils], "hurdle_caps_pct": [round(x * 100.0, 4) for x in eff_ceils], "hurdle_any_success_exact_pct": [(1.0 - d[0]) * 100.0 for d in dists], "hurdle_any_success_pct": [round((1.0 - d[0]) * 100.0, 4) for d in dists], "strassen_h123_h4_exact": [s_min, s_max], "strassen_h123_h4_pct": [round(s_min, 4), round(s_max, 4)], "max_marginal_err": _fmt_sci_float(max_marg_err), } def _check_contract_cell_parity( computed: Dict[str, Any], target: Dict[str, Any], label: str, honest_exact: float | None = None, *, exact_tol: float = 1e-12, ) -> Tuple[float, float]: """Verify BOTH unrounded float64 (*_exact, <1e-12) AND 4-decimal display (*_pct, ==0.0) parity.""" max_exact_err = 0.0 max_disp_err = 0.0 for exact_key in ("locked_exact", "modular_exact", "ceiling_exact"): require_cond(exact_key in target and exact_key in computed, f"Missing {exact_key} in {label}") err = abs(float(computed[exact_key]) - float(target[exact_key])) max_exact_err = max(max_exact_err, err) require_cond( err < exact_tol, f"Unrounded contract parity fail on {label}.{exact_key}: got {computed[exact_key]!r}, expected {target[exact_key]!r} (err={err:.3e})", ) for disp_key in ("locked_pct", "modular_pct", "ceiling_pct"): err = abs(float(computed[disp_key]) - float(target[disp_key])) max_disp_err = max(max_disp_err, err) require_cond(err == 0.0, f"Display contract parity fail on {label}.{disp_key}: got {computed[disp_key]}, expected {target[disp_key]}") for exact_list_key in ("hurdle_caps_exact_pct", "hurdle_any_success_exact_pct", "strassen_h123_h4_exact"): if exact_list_key in target: require_cond(exact_list_key in computed, f"Missing {exact_list_key} in computed {label}") require_cond(len(computed[exact_list_key]) == len(target[exact_list_key]), f"{label}.{exact_list_key} len mismatch") for a, b in zip(computed[exact_list_key], target[exact_list_key]): err = abs(float(a) - float(b)) max_exact_err = max(max_exact_err, err) require_cond( err < exact_tol, f"Unrounded contract parity fail on {label}.{exact_list_key}: {computed[exact_list_key]} vs {target[exact_list_key]} (err={err:.3e})", ) for disp_list_key in ("hurdle_caps_pct", "hurdle_any_success_pct", "strassen_h123_h4_pct"): if disp_list_key in target: require_cond(len(computed[disp_list_key]) == len(target[disp_list_key]), f"{label}.{disp_list_key} len mismatch") for a, b in zip(computed[disp_list_key], target[disp_list_key]): err = abs(float(a) - float(b)) max_disp_err = max(max_disp_err, err) require_cond(err == 0.0, f"Display contract parity fail on {label}.{disp_list_key}: {computed[disp_list_key]} vs {target[disp_list_key]}") if honest_exact is not None and "delta_vs_honest_l1_exact_pp" in target: calc_delta_exact = float(computed["modular_exact"]) - float(honest_exact) err_ex = abs(calc_delta_exact - float(target["delta_vs_honest_l1_exact_pp"])) max_exact_err = max(max_exact_err, err_ex) require_cond( err_ex < exact_tol, f"Unrounded contract parity fail on {label}.delta_vs_honest_l1_exact_pp: {calc_delta_exact!r} vs {target['delta_vs_honest_l1_exact_pp']!r}", ) if "delta_vs_honest_l1_pct_pp" in target: calc_delta_pct = round(calc_delta_exact, 4) err_dp = abs(calc_delta_pct - float(target["delta_vs_honest_l1_pct_pp"])) max_disp_err = max(max_disp_err, err_dp) require_cond( err_dp == 0.0, f"Display contract parity fail on {label}.delta_vs_honest_l1_pct_pp: {calc_delta_pct} vs {target['delta_vs_honest_l1_pct_pp']}", ) return max_exact_err, max_disp_err def compute_portfolio_verification(contracts: Dict[str, Any]) -> Dict[str, Any]: mc = contracts["contract"] rs = mc["reachable_scenarios"] dc = mc["diagnostic_prerequisite_waived_counterfactuals"] l0 = eval_core_3arm_l0() l1_arms = [(0.88, 0.78, 0.57, 0.69), (0.88, 0.79, 0.64, 0.70), (0.88, 0.82, 0.73, 0.69), (0.88, 0.84, 0.80, 0.72), (0.62, 0.92, 0.70, 0.70)] w5_arms = [(0.90, 0.78, 0.68, 0.745), (0.90, 0.79, 0.73, 0.775), (0.90, 0.82, 0.83, 0.745), (0.90, 0.84, 0.84, 0.735), (0.66, 0.92, 0.755, 0.715)] base_r = ("R1", "R2", "R1", "R3P", "R3P") r4_r = ("R1", "R2", "R1", "R3P", "R4") base_c = ("INC", "INC", "INC", "STAGED_BASE", "STAGED_BASE") hard_c = ("INC", "INC", "INC", "STAGED_HARDENED", "STAGED_HARDENED") base_m = {"INC": frozenset({"R1", "R2"}), "STAGED_BASE": frozenset({"R3P"})} hard_r4_m = {"INC": frozenset({"R1", "R2"}), "STAGED_HARDENED": frozenset({"R3P", "R4"})} isol_r4_m = {"INC": frozenset({"R1", "R2"}), "STAGED_BASE": frozenset({"R3P", "R4"})} b_l1 = eval_5arm_2pow20_exact("L1_shipped_IIP0035_phi0", l1_arms, routes=base_r, cell_classes=base_c, compat_map=base_m, phi_inc=0.0) b_w5 = eval_5arm_2pow20_exact("Wave5_L3B_audited_caveated", w5_arms, c1_allo=0.90, c3_base=0.925, routes=base_r, cell_classes=base_c, compat_map=base_m, phi_inc=0.0) b_honest = eval_5arm_2pow20_exact("L1_honest_monoculture_phi_inc_0.50", l1_arms, routes=base_r, cell_classes=base_c, compat_map=base_m, phi_inc=0.50) honest_exact = b_honest["modular_exact"] # Reachable W6-C1 standalone baseline (required to separate conditional C2-given-C1 +4.48 pp from combined C1+C2 +6.48 pp!) c1_standalone_arms = [ (0.88, 0.78, 0.57, 0.69), # A strictly frozen (0.88, 0.80, 0.64, 0.70), # B (0.88, 0.83, 0.73, 0.69), # X (0.88, 0.85, 0.80, 0.72), # Y-v3 (0.78, 0.92, 0.68, 0.70), # D1b-Lean-K2 on R3P ] r_c1_standalone = eval_5arm_2pow20_exact( "W6_C1_standalone_over_honest_L1", c1_standalone_arms, routes=base_r, cell_classes=hard_c, compat_map={"INC": frozenset({"R1", "R2"}), "STAGED_HARDENED": frozenset({"R3P"})}, phi_inc=0.25, ) c1_standalone_exact = r_c1_standalone["modular_exact"] # Primary REACHABLE prerequisite-enforced W6-C2 + W6-C1 (R4-Omentum, zero ex-vivo rest credit, Design A strictly frozen!) c2_plus_c1_arms = [ (0.88, 0.78, 0.57, 0.69), # A strictly frozen (0.88, 0.80, 0.64, 0.70), # B (0.88, 0.83, 0.73, 0.69), # X (0.88, 0.85, 0.80, 0.72), # Y-v3 (0.78, 0.92, 0.68, 0.80), # D1b-Lean-K2 on R4-Omentum ] r_c2_plus_c1 = eval_5arm_2pow20_exact( "W6_C2_plus_C1_R4Omentum_zeroRest", c2_plus_c1_arms, c4_univ=0.950, c4_r4=0.870, phi_r4=0.30, routes=r4_r, cell_classes=hard_c, compat_map=hard_r4_m, phi_inc=0.25, ) # Diagnostic prerequisite-waived counterfactuals (explicitly disable C1 prerequisite; 0.0% released-state weight!) c2_isol_zero_rest_arms = [ (0.88, 0.78, 0.57, 0.69), # A strictly frozen (0.88, 0.79, 0.64, 0.70), # B (0.88, 0.82, 0.73, 0.69), # X (0.88, 0.84, 0.80, 0.72), # Y (0.62, 0.92, 0.70, 0.80), # D on R4-Omentum ] r_diag_c2_isol_zero = eval_5arm_2pow20_exact( "Diag_PrereqWaived_C2_isolated_zeroRest", c2_isol_zero_rest_arms, c4_univ=0.950, c4_r4=0.870, phi_r4=0.30, routes=r4_r, cell_classes=base_c, compat_map=isol_r4_m, phi_inc=0.50, ) # Diagnostic sensitivity: c4_univ held at incumbent 0.92 (site-only isolation) r_diag_c2_c4univ92 = eval_5arm_2pow20_exact( "Diag_PrereqWaived_C2_c4univ_held_0.92_site_only", c2_isol_zero_rest_arms, c4_univ=0.920, c4_r4=0.870, phi_r4=0.30, routes=r4_r, cell_classes=base_c, compat_map=isol_r4_m, phi_inc=0.50, ) # Corrected prospective sensitivity: +1.5 pp ex-vivo rest on ALL eligible non-A arms (B, X, Y, AND D on R4; Design A frozen at 0.69!) c2_prospective_rest_all_eligible_arms = [ (0.88, 0.78, 0.57, 0.69), # A STRICTLY INVARIANT (ZERO ex-vivo rest!) (0.88, 0.79, 0.64, 0.715), # B +1.5 pp prospective (0.88, 0.82, 0.73, 0.705), # X +1.5 pp prospective (0.88, 0.84, 0.80, 0.735), # Y +1.5 pp prospective (0.62, 0.92, 0.70, 0.815), # D +1.5 pp prospective on R4-Omentum (0.80 + 0.015) ] r_diag_c2_prospective_rest = eval_5arm_2pow20_exact( "Diag_PrereqWaived_C2_prospective_rest_1p5pp_all_eligible_BXYD", c2_prospective_rest_all_eligible_arms, c4_univ=0.950, c4_r4=0.870, phi_r4=0.30, routes=r4_r, cell_classes=base_c, compat_map=isol_r4_m, phi_inc=0.50, ) # Diagnostic demoted R4-SARS-Free Backup sensitivity (Dual-FailSafe Y-v3 ONLY on R4-SARS; D1b barred from R4-SARS!) sars_y_only_arms = [ (0.88, 0.78, 0.57, 0.69), (0.88, 0.79, 0.64, 0.70), (0.88, 0.82, 0.73, 0.69), (0.88, 0.84, 0.80, 0.78), # Y-v3 on R4-SARS-Free backup (Dual-FailSafe required)