OpenIslet

scripts/verify_wave7_cellgemma_2p24_rebase.py

Wave-7 CellGemma-Islet (IIP-0062 to IIP-0065 / GATE-40 to GATE-43) Pure-CPU

script Release v0.8.0 gen-eb955f8266035aba 1.37 MB · 90 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_wave7_cellgemma_2p24_rebase.py · SHA-256: 5f31a6369f25031d…

    c1_allo: float = 0.88,
    c1_univ: Optional[float] = 0.925,
    c1_z: Optional[float] = 0.84,
    c2_a: float = 0.87,
    c2_bx_unc: float = 0.87,
    c2_y_unc: float = 0.87,
    c2_d: float = 0.93,
    c2_univ_unc: float = 0.95,
    c3_base: float = 0.942,
    c4_univ_unc: float = 0.950,
    c4_r1: float = 0.850,
    c4_r2_unc: float = 0.850,
    c4_r3p_unc: float = 0.850,
    c4_r4: Optional[float] = 0.870,
    # Coating overlay deltas (strictly isolated to B_COAT/R2_COAT and Y_COAT/R3P_COAT!)
    coated_overlay_active: bool = False,
    coated_b_active: bool = False,
    coated_y_active: bool = False,
    delta_b_h2: float = 0.0,
    delta_y_h2: float = 0.0,
    delta_c2_univ: float = 0.0,
    delta_b_h4: float = 0.0,
    delta_y_h4: float = 0.0,
    delta_c4_univ: float = 0.0,
    # Exposure-specific H3 baseline for coated paths (GATE-42 requires DRUG_FREE_EXPOSURE!)
    arms_h3_coated_base: Optional[Sequence[float]] = None,
    c3_coated_base: Optional[float] = None,
    allow_c_on_coated_paths: bool = False,
    # Counterfactual / sensitivity flags (non-primary only!)
    include_r4_vehicle_gated_cf: bool = False,
    c4_r4_cf_lifted: Optional[float] = None,
    c4_univ_cf_lifted: Optional[float] = None,
    include_z_r3p_sensitivity: bool = False,
    z_h2_sens_lift: float = 0.0,
    z_h4_sens_lift: float = 0.0,
    phi_z_h1: float = 0.55,
    phi_z_h2: float = 0.80,
    phi_z_h3: float = 0.80,
    phi_r4: float = 0.30,
    phi_yx: float = 0.75,
    phi_c: float = 0.25,
    phi_inc: float = 0.25,
    c1_pass: bool = True,
    design_a_r1_only_when_coated: bool = True,
    compute_strassen: bool = True,
    compute_h23_lp: bool = False,
) -> dict[str, Any]:
    """
    True Zero-Spillover Wave-7 Portfolio Evaluator.
    In primary coated mode (coated_overlay_active=True):
      - Uncoated H2 and H4 joint laws remain 100% BIT-IDENTICAL to Wave-6!
      - B_COAT (bit n) pairs EXCLUSIVELY with R2_COAT (bit n); Y_COAT (bit n+1) pairs EXCLUSIVELY with R3P_COAT (bit n+1).
      - Design A (INC_A_DRUGFREE) is restricted to uncoated R1 whenever R2 is drug-coated.
      - Zero spillover onto X, D, Z, B/R1, Y/R4, and R1&R4 joint is mathematically guaranteed and asserted!
    """
    n = len(arms_uncoated)
    require(n in (5, 6), f"Only 5-arm or 6-arm portfolios supported, got {n}")
    r4_set = frozenset({"R4"}) if c4_r4 is not None else frozenset()

    # H1 (unmodified Wave-6)
    if n == 5 or c1_univ is None:
        d1 = [0.0] * (1 << n)
        u_d = arms_uncoated[4][0] / c1_allo
        if n == 5:
            d1[0], d1[15], d1[31] = 1.0 - c1_allo, c1_allo * (1.0 - u_d), c1_allo * u_d
        else:
            u_z = arms_uncoated[5][0] / c1_allo
            sub = biv_bern(u_d, u_z, phi_z_h1)
            d1[0] = 1.0 - c1_allo
            for (bd, bz), w in sub.items():
                d1[15 | (bd << 4) | (bz << 5)] += c1_allo * w
        c1_eff = c1_allo
    else:
        sh1 = biv_bern(c1_allo / c1_univ, float(c1_z) / c1_univ, phi_z_h1)
        d1 = [0.0] * 64
        d1[0] = 1.0 - c1_univ
        u_d, u_z = arms_uncoated[4][0] / c1_allo, arms_uncoated[5][0] / float(c1_z)
        for (s_sc, s_z), ws in sh1.items():
            wt = c1_univ * ws
            if s_sc == 0 and s_z == 0:
                d1[0] += wt
            elif s_sc == 1 and s_z == 0:
                d1[15] += wt * (1.0 - u_d)
                d1[31] += wt * u_d
            elif s_sc == 0 and s_z == 1:
                d1[0] += wt * (1.0 - u_z)
                d1[32] += wt * u_z
            else:
                for (bd, bz), wb in biv_bern(u_d, u_z, 0.0).items():
                    d1[15 | (bd << 4) | (bz << 5)] += wt * wb
        c1_eff = c1_univ * (1.0 - sh1[(0, 0)])

    # H2: Exact Wave-6 uncoated law + optional monotonic (n+2)-bit exposure-specific coated overlay!
    z_h2_eff = arms_uncoated[5][1] + (z_h2_sens_lift if (n == 6 and (include_z_r3p_sensitivity or include_r4_vehicle_gated_cf)) else 0.0) if n == 6 else 0.0
    d2_5, c2_eff_5 = build_h2_5arm(
        arms_uncoated[0][1], arms_uncoated[1][1], arms_uncoated[2][1], arms_uncoated[3][1], arms_uncoated[4][1],
        c2_a=c2_a, c2_bx=c2_bx_unc, c2_y=c2_y_unc, c2_univ=c2_univ_unc, c2_univ_a=c2_univ_unc, c2_auto=c2_d,
        phi_yx=phi_yx, phi_c=phi_c, phi_inc=phi_inc,
    )
    if n == 5:
        d2_unc, c2_eff_unc = d2_5, c2_eff_5
    else:
        d2_unc = [0.0] * 64
        d2_unc[0] = 1.0 - c2_univ_unc
        sub5 = [d2_5[m] / c2_univ_unc for m in range(32)]
        sub5[0] = max(0.0, (d2_5[0] - (1.0 - c2_univ_unc)) / c2_univ_unc)
        coupled2 = frechet_couple(sub5, z_h2_eff / c2_univ_unc, phi_z_h2, lambda m: (m & 15) != 0)
        for k in range(64):
            d2_unc[k] += c2_univ_unc * coupled2[k]
        c2_eff_unc = max(c2_eff_5, 1.0 - d2_unc[0])

    if coated_overlay_active:
        # Always construct the full (B_COAT, Y_COAT) joint overlay so single-pair evaluations
        # (coated_y_only / coated_b_only) are EXACT marginal projections of the joint coated law!
        d2 = couple_coated_overlay_2arm(d2_unc, 1, 3, delta_b_h2, delta_y_h2, delta_c2_univ, phi_c, n)
        c2_eff = 1.0 - d2[0]
    else:
        d2 = d2_unc
        c2_eff = c2_eff_unc

    # H3: Exposure-specific! Uncoated paths get IIP-0064 C (drug-free GATE-42 scope);
    # coated paths (B_COAT, Y_COAT) stay strictly at Wave-6 baseline H3 unless non-primary sensitivity flag set!
    def _build_d3_n(h3_vec: Sequence[float], c3_cap: float) -> tuple[list[float], float]:
        d3_5_l, c3_5_l = build_h3_5arm(
            h3_vec[0], h3_vec[1], h3_vec[2], h3_vec[3], h3_vec[4],
            c3=c3_cap, phi_yx=phi_yx, phi_c=phi_c, phi_inc=phi_inc,
        )
        if n == 5:
            return d3_5_l, c3_5_l
        d3_6 = [0.0] * 64
        d3_6[0] = 1.0 - c3_cap
        sub5_l = [d3_5_l[m] / c3_cap for m in range(32)]
        sub5_l[0] = max(0.0, (d3_5_l[0] - (1.0 - c3_cap)) / c3_cap)
        coupled3_l = frechet_couple(sub5_l, h3_vec[5] / c3_cap, phi_z_h3, lambda m: (m & 8) != 0)
        for k in range(64):
            d3_6[k] += c3_cap * coupled3_l[k]
        return d3_6, c3_cap

    d3_unc, c3_eff = _build_d3_n([a[2] for a in arms_uncoated], c3_base)
    if coated_overlay_active and (not allow_c_on_coated_paths) and arms_h3_coated_base is not None and c3_coated_base is not None:
        d3_coat_base, _ = _build_d3_n(arms_h3_coated_base, float(c3_coated_base))
        d3 = couple_h3_uncoated_c_vs_coated_base(d3_coat_base, d3_unc, 1, 3, n)
    else:
        d3 = d3_unc

    # H4: Exact Wave-6 uncoated law (100% invariant!) + optional monotonic (n+2)-bit coated overlay!
    c4_r4_use = c4_r4_cf_lifted if (include_r4_vehicle_gated_cf and c4_r4_cf_lifted is not None) else c4_r4
    c4_univ_use = c4_univ_cf_lifted if (include_r4_vehicle_gated_cf and c4_univ_cf_lifted is not None) else c4_univ_unc
    arms_h4_eval = list(arms_uncoated)
    if n == 6 and (include_z_r3p_sensitivity or include_r4_vehicle_gated_cf):
        arms_h4_eval[5] = (
            arms_h4_eval[5][0],
            z_h2_eff,
            arms_h4_eval[5][2],
            arms_h4_eval[5][3] + (z_h4_sens_lift if (include_z_r3p_sensitivity and c4_r4 is None) else 0.0),
        )

    d4_unc = [0.0] * (1 << n)
    d4_unc[0] = 1.0 - c4_univ_use
    if c4_r4_use is None:
        sh4 = biv_bern(c4_r2_unc / c4_univ_use, c4_r3p_unc / c4_univ_use, phi_c)
        sh_items = [((sp, s3, 0), ws) for (sp, s3), ws in sh4.items()]
        c4_eff_unc = c4_univ_use * (1.0 - sh4[(0, 0)])
    else:
        biv_sub = biv_bern(c4_r2_unc / c4_univ_use, c4_r3p_unc / c4_univ_use, phi_c)
        d_sub = [biv_sub[(0, 0)], biv_sub[(1, 0)], biv_sub[(0, 1)], biv_sub[(1, 1)]]
        sh4_l = frechet_couple(d_sub, c4_r4_use / c4_univ_use, phi_r4, lambda m: m != 0)
        sh_items = [(((sm >> 0) & 1, (sm >> 1) & 1, (sm >> 2) & 1), ws) for sm, ws in enumerate(sh4_l)]
        c4_eff_unc = c4_univ_use * (1.0 - sh4_l[0])

    fj_r1 = frechet_joint([arms_h4_eval[0][3] / c4_r1, arms_h4_eval[2][3] / c4_r1], 1.0)
    r1_ev = [((1 if (bm & 1) else 0) | (4 if (bm & 2) else 0), pr) for bm, pr in enumerate(fj_r1) if pr > 0.0]
    p_b_in_r2u = arms_h4_eval[1][3] / c4_r2_unc
    ev12_on: list[tuple[int, float]] = []
    for m_r1, w_r1 in r1_ev:
        ev12_on.append((m_r1 | 2, w_r1 * p_b_in_r2u))
        ev12_on.append((m_r1, w_r1 * (1.0 - p_b_in_r2u)))

    if c4_r4_use is None:
        r3_idxs = [3, 4] + ([5] if n == 6 else [])
        fj3 = frechet_joint([arms_h4_eval[i][3] / c4_r3p_unc for i in r3_idxs], 1.0)
        ev3_on = [(sum((1 << r3_idxs[k]) for k in range(len(r3_idxs)) if (bm >> k) & 1), pr) for bm, pr in enumerate(fj3) if pr > 0.0]
        ev4_on = [(0, 1.0)]
    else:
        p_y_in_r3u = arms_h4_eval[3][3] / c4_r3p_unc
        ev3_on = [(0, 1.0 - p_y_in_r3u), (8, p_y_in_r3u)]
        r4_idxs = [4] + ([5] if n == 6 else [])
        fj4 = frechet_joint([arms_h4_eval[i][3] / c4_r4_use for i in r4_idxs], 1.0)
        ev4_on = [(sum((1 << r4_idxs[k]) for k in range(len(r4_idxs)) if (bm >> k) & 1), pr) for bm, pr in enumerate(fj4) if pr > 0.0]

    for (sp, s3, s4), ws in sh_items:
        wt = c4_univ_use * ws
        if wt == 0.0:
            continue
        e12 = ev12_on if sp else [(0, 1.0)]
        e3 = ev3_on if s3 else [(0, 1.0)]
        e4 = ev4_on if (c4_r4_use is not None and s4) else [(0, 1.0)]
        for m12, w12 in e12:
            for m3, w3 in e3:
                for m4, w4 in e4:
                    d4_unc[m12 | m3 | m4] += wt * w12 * w3 * w4

    if coated_overlay_active:
        d4 = couple_coated_overlay_2arm(d4_unc, 1, 3, delta_b_h4, delta_y_h4, delta_c4_univ, phi_c, n)
        c4_eff = 1.0 - d4[0]
        # Build exact zero-spillover compatibility map:
        # Uncoated arms 0..n-1 ONLY match uncoated routes; COAT_B_ONLY matches ONLY R2_COAT; COAT_Y_ONLY matches ONLY R3P_COAT!
        r_base = (("R1", "ADV_UNC", "R1", "R3P_UNC", "R4", "R4") if c4_r4_use is not None else ("R1", "ADV_UNC", "R1", "R3P_UNC", "R3P_UNC", "R3P_UNC"))[:n]
        routes_vec = r_base + ("ADV_COAT", "R3P_COAT")
        a_routes = frozenset({"R1"}) if (coated_b_active and design_a_r1_only_when_coated) else frozenset({"R1", "ADV_UNC"})
        yd_unc_cls = "STAGED_HARDENED_UNC" if c1_pass else "STAGED_BASE_UNC"
        cell_classes = ("INC_A_DRUGFREE", "INC_UNC", "INC_UNC", yd_unc_cls, yd_unc_cls, "Z_UNC")[:n] + ("COAT_B_ONLY", "COAT_Y_ONLY")
        compat_map: dict[str, FrozenSet[str]] = {
            "INC_A_DRUGFREE": a_routes,
            "INC_UNC": frozenset({"R1", "ADV_UNC"}),
            "STAGED_HARDENED_UNC": frozenset({"R3P_UNC"}) | r4_set,
            "STAGED_BASE_UNC": frozenset({"R3P_UNC"}),
            "Z_UNC": (frozenset({"R3P_UNC", "R3P_COAT"}) if include_z_r3p_sensitivity else frozenset({"R3P_UNC"})) | r4_set,
            "COAT_B_ONLY": frozenset({"ADV_COAT"}) if coated_b_active else frozenset(),
            "COAT_Y_ONLY": frozenset({"R3P_COAT"}) if coated_y_active else frozenset(),
        }
        coated_routes_set = frozenset({"ADV_COAT", "R3P_COAT"})
        elig_mask = ((1 << n) if coated_b_active else 0) | ((1 << (n + 1)) if coated_y_active else 0) | ((1 << 5) if (n == 6 and include_z_r3p_sensitivity) else 0)
    else:
        d4 = d4_unc
        c4_eff = c4_eff_unc
        routes_vec = (("R1", "R2", "R1", "R3P", "R4", "R4") if c4_r4_use is not None else ("R1", "R2", "R1", "R3P", "R3P", "R3P"))[:n]
        yd_cls = "STAGED_HARDENED" if c1_pass else "STAGED_BASE"
        cell_classes = ("INC", "INC", "INC", yd_cls, yd_cls, "Z_HARDENED")[:n]
        compat_map = {
            "INC": frozenset({"R1", "R2"}),
            "STAGED_HARDENED": frozenset({"R3P"}) | r4_set,
            "STAGED_BASE": frozenset({"R3P"}),
            "Z_HARDENED": frozenset({"R3P"}) | r4_set,
        }
        coated_routes_set = frozenset()
        elig_mask = 0

    require("R4" not in compat_map.get("INC", frozenset()), "GY-027(1): unhardened INC cannot use R4")
    require("R4" not in compat_map.get("INC_A_DRUGFREE", frozenset()), "GY-027(1): Design A cannot use R4")
    if coated_routes_set:
        require(
            not (compat_map.get(cell_classes[0], frozenset()) & coated_routes_set),
            "Physical firewall violation: Design A overlaps coated routes",
        )

    res = eval_exact_n_arm(
        arms_h4_eval, [d1, d2, d3, d4], [c1_eff, c2_eff, c3_eff, c4_eff], routes_vec, cell_classes, compat_map,
        coated_overlay_active=coated_overlay_active,
        coated_b_active=coated_b_active,