OpenIslet

scripts/verify_iip0042_master_multilayer_portfolio_crosswalk.py

IIP-0042 / DEC-121 (CAND-033 / EXP-030): Master Exact 2^20 Multi-Layer Portfolio

script Release v0.8.0 gen-eb955f8266035aba 881 KB · 63 sections

Use this file in any AI chat (paste-only or attachment-capable):

Download as text (.txt) Download section (.txt)

Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0042_master_multilayer_portfolio_crosswalk.py · SHA-256: 16964a158458b5cf…

        single_arms = {}
        for idx, nm in enumerate(ARM_NAMES):
            vec = arms[idx]
            single_arms[nm] = {
                "route": routes[idx],
                "route_live_registered": routes[idx] in compat_strict_live[cell_classes[idx]],
                "cell_class": cell_classes[idx],
                "hurdles": [round(x, 6) for x in vec],
                "standalone_pct": round(100.0 * math.prod(vec), 4),
            }

        out: dict[str, Any] = {
            "layer_id": layer_id,
            "label": label,
            "epistemic_tier": epistemic_tier,
            "drug_status": drug_status,
            "n_arms": 5,
            "state_space": 2 ** 20,
            "routes": list(routes),
            "has_unregistered_route_assignment": has_unreg,
            "route_registration_status": (
                "PARKED_COUNTERFACTUAL_UNREGISTERED_R3B_GY027_BLOCK"
                if has_unreg
                else "STRICT_LIVE_REGISTERED_ROUTES_ONLY"
            ),
            "single_arms": single_arms,
            "eff_ceilings_pct": [round(100.0 * c, 2) for c in eff_ceils],
            "composite_ceiling_pct": round(100.0 * ceil, 4),
            "ceiling_exact": ceil,
            # Strict live-eligible metrics (ALWAYS mask unregistered R3B!)
            "strict_live_eligible_locked_pct": round(100.0 * strict_locked, 4),
            "strict_live_eligible_mod90_pct": round(100.0 * strict_mod90, 4),
            "strict_live_eligible_mod100_pct": round(100.0 * strict_mod100, 4),
            "strict_live_eligible_locked_exact": strict_locked,
            "strict_live_eligible_mod100_exact": strict_mod100,
            "incumbent_4096_state_max_abs_err": max_4096_err,
        }
        if not has_unreg:
            out.update({
                "locked_pct": round(100.0 * strict_locked, 4),
                "modular_k90_pct": round(100.0 * strict_mod90, 4),
                "modular_k100_pct": round(100.0 * strict_mod100, 4),
                "locked_exact": strict_locked,
                "modular_k100_exact": strict_mod100,
                "delta_locked_vs_l0_pp": round(100.0 * (strict_locked - l0_locked), 4),
                "delta_mod100_vs_l0_pp": round(100.0 * (strict_mod100 - l0_mod100), 4),
                "delta_locked_vs_l1_pp": round(100.0 * (strict_locked - l1_locked), 4),
                "delta_mod100_vs_l1_pp": round(100.0 * (strict_mod100 - l1_mod100), 4),
                "residual_risk_reduction_vs_l0_locked_pct": round(100.0 * (strict_locked - l0_locked) / (1.0 - l0_locked), 2),
                "residual_risk_reduction_vs_l0_mod100_pct": round(100.0 * (strict_mod100 - l0_mod100) / (1.0 - l0_mod100), 2),
            })
        else:
            out.update({
                "counterfactual_if_r3b_registered_locked_pct": round(100.0 * cf_locked, 4),
                "counterfactual_if_r3b_registered_mod90_pct": round(100.0 * cf_mod90, 4),
                "counterfactual_if_r3b_registered_mod100_pct": round(100.0 * cf_mod100, 4),
                "counterfactual_hypo_transfer_k100_pct": round(100.0 * cf_mod100_hypo, 4),
                "counterfactual_if_r3b_registered_locked_exact": cf_locked,
                "counterfactual_if_r3b_registered_mod100_exact": cf_mod100,
                "counterfactual_hypo_transfer_k100_exact": cf_mod100_hypo,
                "delta_strict_live_locked_vs_l1_pp": round(100.0 * (strict_locked - l1_locked), 4),
                "delta_counterfactual_r3b_locked_vs_l1_pp": round(100.0 * (cf_locked - l1_locked), 4),
                "delta_counterfactual_r3b_mod100_vs_l1_pp": round(100.0 * (cf_mod100 - l1_mod100), 4),
            })
        if return_state_laws:
            out["_internal_v_h123"] = v_h123
            out["_internal_dh4"] = dh4
            out["_internal_dists"] = dists
            out["_internal_arms"] = arms
            out["_internal_routes"] = routes
            out["_internal_cell_classes"] = cell_classes
        return out

    def compute_checkerboard_stress_test(
        *,
        label: str,
        v_h123: list[float],
        dh4: list[float],
        baseline_locked_exact: float,
        target_locked_exact: float,
        eps: float = 0.003,
    ) -> dict[str, Any]:
        """
        Reproduce the legal 0.003 checkerboard mass-transfer witness between
        joint (m_cell, m_h4) cells {(0, 15), (0, 31), (16, 15), (16, 31)} involving
        only the added 5th arm (bit 4 = 16). Preserves every hurdle marginal and
        the full 3-incumbent-arm / 4,096-state projection while shifting locked
        success by exactly -eps (-0.3000 pp).
        """
        cells = ((0, 15), (0, 31), (16, 15), (16, 31))
        before = [v_h123[mc] * dh4[mh] for mc, mh in cells]
        after = [before[0] - eps, before[1] + eps, before[2] + eps, before[3] - eps]
        if min(after) < 0.0:
            raise RuntimeError(f"Checkerboard eps={eps} exceeds support in {label}: {before}")
        # Verify exact marginal and incumbent projection invariance
        m_cell_0_diff = (after[0] + after[1]) - (before[0] + before[1])
        m_cell_16_diff = (after[2] + after[3]) - (before[2] + before[3])
        m_h4_15_diff = (after[0] + after[2]) - (before[0] + before[2])
        m_h4_31_diff = (after[1] + after[3]) - (before[1] + before[3])
        max_inv_err = max(abs(m_cell_0_diff), abs(m_cell_16_diff), abs(m_h4_15_diff), abs(m_h4_31_diff))
        if max_inv_err > 1e-15:
            raise RuntimeError(f"Checkerboard invariance check failed: {max_inv_err}")
        # Only cell (16, 31) satisfies (mc & mh) != 0 (since 16 & 31 = 16, whereas 0&15=0, 0&31=0, 16&15=0)
        coupled_locked_exact = target_locked_exact - eps
        indep_delta_pp = 100.0 * (target_locked_exact - baseline_locked_exact)
        coupled_delta_pp = 100.0 * (coupled_locked_exact - baseline_locked_exact)
        return {
            "label": label,
            "transfer_epsilon": eps,
            "cell_route_masks_mcell_mh4": [list(c) for c in cells],
            "joint_masses_before": [round(x, 8) for x in before],
            "joint_masses_after": [round(x, 8) for x in after],
            "min_mass_after": round(min(after), 8),
            "marginal_and_incumbent_invariance_max_err": max_inv_err,
            "independent_locked_pct": round(100.0 * target_locked_exact, 4),
            "coupled_locked_pct": round(100.0 * coupled_locked_exact, 4),
            "independent_delta_vs_baseline_pp": round(indep_delta_pp, 4),
            "coupled_delta_vs_baseline_pp": round(coupled_delta_pp, 4),
            "flips_sign_negative": coupled_delta_pp < 0.0,
        }

    def build_layer_scenarios(phi_c: float, l0_locked: float, l0_mod100: float) -> dict[str, Any]:
        def _run(
            layer_id: str,
            label: str,
            epistemic_tier: str,
            drug_status: str,
            arms: list[tuple[float, float, float, float]],
            *,
            c1_allo: float,
            c2_inc: float = 0.87,
            c2_y: float = 0.87,
            c2_univ: float = 0.95,
            c2_auto: float = 0.93,
            c3_a: float,
            c3_bx: float,
            c3_y: float,
            c3_d: float,
            h4_topology: str,
            c4_univ: float = 0.92,
            c4_pouch: float = 0.85,
            c4_r3p: float = 0.85,
            c4_r3b: float = 0.85,
            phi_yx: float = 0.75,
            l1_ref: tuple[float, float] | None = None,
            check_4096: bool = False,
            return_state_laws: bool = False,
        ) -> dict[str, Any]:
            pa, pb, px, py, pd = arms
            d1, c1_e = build_h1_dist_5arm(c1_allo, pd[0])
            d2, c2_e = build_h2_dist_5arm(
                pa[1], pb[1], px[1], py[1], pd[1],
                c2_inc=c2_inc, c2_y=c2_y, c2_univ=c2_univ, c2_auto=c2_auto,
                phi_yx=phi_yx, phi_c=phi_c,
            )
            d3, c3_e = build_h3_dist_5arm(
                pa[2], pb[2], px[2], py[2], pd[2],
                c3_a=c3_a, c3_bx=c3_bx, c3_y=c3_y, c3_d=c3_d,
                phi_yx=phi_yx, phi_c=phi_c,
            )
            d4, c4_e, routes = build_h4_dist_5arm(
                pa[3], pb[3], px[3], py[3], pd[3],
                h4_topology=h4_topology, c4_univ=c4_univ,
                c4_pouch=c4_pouch, c4_r3p=c4_r3p, c4_r3b=c4_r3b,
                phi_c=phi_c,
            )
            ccf = compat_d1_r3b_counterfactual if h4_topology == "THREE_PARADIGM_D1_ON_R3B" else None
            ctr = compat_d1_r3b_hypo_transfer if h4_topology == "THREE_PARADIGM_D1_ON_R3B" else None
            l1_l = l1_ref[0] if l1_ref else 0.0
            l1_m = l1_ref[1] if l1_ref else 0.0
            out = evaluate_5arm_exact_2pow20(
                layer_id=layer_id,
                label=label,
                epistemic_tier=epistemic_tier,
                drug_status=drug_status,
                arms=arms,
                dists=[d1, d2, d3, d4],
                eff_ceils=[c1_e, c2_e, c3_e, c4_e],
                routes=routes,
                compat_counterfactual=ccf,
                compat_hypo_transfer=ctr,
                cell_classes=std_classes,
                l0_locked=l0_locked,
                l0_mod100=l0_mod100,
                l1_locked=l1_l,
                l1_mod100=l1_m,
                check_4096_incumbent_parity=check_4096,
                return_state_laws=return_state_laws,
            )
            if l1_ref is None and not out["has_unregistered_route_assignment"]:
                out["delta_locked_vs_l1_pp"] = 0.0
                out["delta_mod100_vs_l1_pp"] = 0.0
            return out

        strict_rows: list[dict[str, Any]] = []
        ineligible_full_cocktail_diag_rows: list[dict[str, Any]] = []
        parked_r3b_rows: list[dict[str, Any]] = []

        # =====================================================================
        # PART A: HEADLINE LIVE-ELIGIBLE STRICT SERIES (ZERO BLOCKED R3B!)
        # =====================================================================
        wu3_arms = [
            (0.88, 0.78, 0.57, 0.69),  # A @ R1
            (0.88, 0.79, 0.64, 0.70),  # B @ R2
            (0.88, 0.82, 0.73, 0.69),  # X @ R1
            (0.88, 0.84, 0.80, 0.72),  # Y @ R3P
            (0.62, 0.92, 0.70, 0.70),  # D @ R3P
        ]
        l1_layer_id = "L1_LIVE_5ARM_WU3" if abs(phi_c - 0.25) < 1e-9 else "L1_LIVE_5ARM_WU4"
        l1_wu_tag = "W_U3" if abs(phi_c - 0.25) < 1e-9 else "W_U4"
        l1 = _run(
            l1_layer_id,
            f"Layer 1: Shipped IIP-0035 5-Arm {l1_wu_tag} Baseline (phi={phi_c:.2f})",
            "Tier-0_Shipped_IIP0035_Baseline",
            "ZERO_DRUG_100PCT_STAGED",
            wu3_arms,
            c1_allo=0.88, c2_inc=0.87, c2_y=0.87,
            c3_a=0.89, c3_bx=0.89, c3_y=0.89, c3_d=0.89,
            h4_topology="TWO_PARADIGM_WU3", c4_univ=0.92,
            l1_ref=None, check_4096=True,
        )
        l1_ref = (l1["locked_exact"], l1["modular_k100_exact"])
        strict_rows.append(l1)

        # Layer 2A (EXP-028 S3 parity: c2=0.87, c3=0.89)
        l2a_s2a_arms = [
            (0.90, 0.78, 0.57, 0.69),
            (0.90, 0.79, 0.64, 0.70),
            (0.90, 0.82, 0.73, 0.69),
            (0.90, 0.84, 0.80, 0.72),
            (0.62, 0.92, 0.70, 0.70),
        ]
        strict_rows.append(_run(
            "L2A_EXP028_S2a_H1_90_ZERO_H3_LIFT",
            "Layer 2A (EXP-028 S2a): Conditional H1=90% + ABX CD64t/IL1RN Defensive Recovery (Zero H2/H3 Lift)",
            "Tier-A_Conditional_Clinical_H1_and_Stage0_ModeB",
            "ZERO_DRUG_100PCT",
            l2a_s2a_arms,
            c1_allo=0.90, c2_inc=0.87, c2_y=0.87,
            c3_a=0.89, c3_bx=0.89, c3_y=0.89, c3_d=0.89,
            h4_topology="TWO_PARADIGM_WU3", c4_univ=0.92,
            l1_ref=l1_ref,
        ))

        l2a_s2c_arms = [
            (0.90, 0.78, 0.62, 0.69),
            (0.90, 0.79, 0.68, 0.70),
            (0.90, 0.82, 0.78, 0.69),
            (0.90, 0.84, 0.80, 0.72),
            (0.62, 0.92, 0.70, 0.70),