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…

        coated_y_active=coated_y_active,
        coated_routes=coated_routes_set,
        coated_eligible_arm_mask=elig_mask,
        compute_strassen=compute_strassen,
        compute_h23_lp=compute_h23_lp,
    )
    return {"name": name, **res}


def eval_core_3arm_l0() -> dict[str, Any]:
    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)]
    c1, c2, c3, c4 = 0.88, 0.87, 0.89, 0.85
    d1 = [0.0] * 8
    d1[0], d1[7] = 1.0 - c1, c1
    d2 = [0.0] * 8
    d2[0] = 1.0 - c2
    fj2 = frechet_joint([a[1] / c2 for a in arms], 0.0)
    for k in range(8):
        d2[k] += c2 * fj2[k]
    d3 = [0.0] * 8
    d3[0] = 1.0 - c3
    fj3 = frechet_joint([a[2] / c3 for a in arms], 0.0)
    for k in range(8):
        d3[k] += c3 * fj3[k]
    d4 = [0.0] * 8
    d4[0] = 1.0 - c4
    u_ax = 0.69 / c4
    u_b = 0.70 / c4
    for m_ax, w_ax in ((0, 1.0 - u_ax), (5, u_ax)):
        for m_b, w_b in ((0, 1.0 - u_b), (2, u_b)):
            d4[m_ax | m_b] += c4 * w_ax * w_b
    d123: dict[int, float] = {}
    for m1, p1 in enumerate(d1):
        for m2, p2 in enumerate(d2):
            for m3, p3 in enumerate(d3):
                m = m1 & m2 & m3
                if m and p1 * p2 * p3 > 0.0:
                    d123[m] = d123.get(m, 0.0) + p1 * p2 * p3
    p_locked = sum(p123 * p4 for m123, p123 in d123.items() for m4, p4 in enumerate(d4) if (m123 & m4))
    p_mod = sum(p123 * p4 for m123, p123 in d123.items() for m4, p4 in enumerate(d4) if m4 != 0)
    return {
        "name": "L0_core_3arm",
        "locked_exact": p_locked * 100.0,
        "modular_exact": p_mod * 100.0,
        "locked_pct": round(p_locked * 100.0, 4),
        "modular_pct": round(p_mod * 100.0, 4),
    }


def _tier_params(tier: str) -> dict[str, float]:
    table = {
        "LOW": {
            "da_h4": 0.020, "da_c4r": 0.020, "da_c4u": 0.015 * SCALE_C4_UNIV,
            "db_b2": 0.0025, "db_y2": 0.005, "db_b4": 0.005, "db_y4": 0.010,
            "db_c2": 0.010, "db_c2u": 0.003, "db_c4r": 0.010, "db_c4u": 0.008 * SCALE_C4_UNIV,
            "dc_abx": 0.008 * SCALE_C3_BASE, "dc_ydz": 0.005 * SCALE_C3_BASE, "dc_c3": 0.005 * SCALE_C3_BASE,
        },
        "HIGH": {
            "da_h4": 0.035, "da_c4r": 0.035, "da_c4u": 0.025 * SCALE_C4_UNIV,
            "db_b2": 0.005, "db_y2": 0.010, "db_b4": 0.010, "db_y4": 0.020,
            "db_c2": 0.020, "db_c2u": 0.006, "db_c4r": 0.020, "db_c4u": 0.015 * SCALE_C4_UNIV,
            "dc_abx": 0.018 * SCALE_C3_BASE, "dc_ydz": 0.011 * SCALE_C3_BASE, "dc_c3": 0.012 * SCALE_C3_BASE,
        },
    }
    if tier in table:
        return table[tier]
    require(tier == "CENTRAL", f"Unknown tier: {tier}")
    return {k: 0.5 * (table["LOW"][k] + table["HIGH"][k]) for k in table["LOW"]}


def _decorate_delta(
    res: dict[str, Any],
    ref: dict[str, Any],
    *,
    firewall_ref: Optional[dict[str, Any]] = None,
    verify_zero_uncoated_spillover: bool = False,
) -> dict[str, Any]:
    out = dict(res)
    out["delta_modular_vs_wave6_exact_pp"] = res["modular_exact"] - ref["modular_exact"]
    out["delta_modular_vs_wave6_pct_pp"] = round(out["delta_modular_vs_wave6_exact_pp"], 4)
    out["delta_locked_vs_wave6_exact_pp"] = res["locked_exact"] - ref["locked_exact"]
    out["delta_locked_vs_wave6_pct_pp"] = round(out["delta_locked_vs_wave6_exact_pp"], 4)
    if firewall_ref is not None:
        for h in range(4):
            require(
                abs(res["design_a_4_marginals_exact"][h] - firewall_ref["design_a_4_marginals_exact"][h]) <= 1e-12,
                f"Design-A H{h+1} marginal firewall breach in {res['name']}",
            )
        require(
            abs(res["design_a_standalone_exact"] - firewall_ref["design_a_standalone_exact"]) <= 1e-12,
            f"Design-A standalone firewall breach in {res['name']}",
        )
        for c in range(4):
            require(
                abs(res["design_a_d_h2_joint_4cell_exact"][c] - firewall_ref["design_a_d_h2_joint_4cell_exact"][c]) <= 1e-12,
                f"Design-(A,D) H2 4-cell firewall breach in {res['name']}",
            )
        require(res["design_a_only_coated_route_modular_mass_pp"] == 0.0, "Design-A coated-route modular leak")
        require(res["non_eligible_product_coated_route_modular_leak_pp"] == 0.0, "Non-eligible product coated-route modular leak")
        out["design_a_firewall_verified"] = True
        if verify_zero_uncoated_spillover:
            xdz_diff = abs(res["uncoated_xdz_only_modular_exact"] - firewall_ref["uncoated_xdz_only_modular_exact"])
            br1_diff = abs(res["uncoated_b_r1_marginal_exact"] - firewall_ref["uncoated_b_r1_marginal_exact"])
            r14_diff = abs(res["uncoated_r1_and_r4_joint_exact"] - firewall_ref["uncoated_r1_and_r4_joint_exact"])
            require(xdz_diff <= 1e-11, f"Uncoated X/D/Z spillover in {res['name']}: {xdz_diff}")
            require(br1_diff <= 1e-11, f"Uncoated B/R1 spillover in {res['name']}: {br1_diff}")
            require(r14_diff <= 1e-11, f"Uncoated R1&R4 spillover in {res['name']}: {r14_diff}")
            out["uncoated_xdz_spillover_pp"] = 0.0
            out["uncoated_b_r1_spillover_pp"] = 0.0
            out["uncoated_r1_and_r4_drift_pp"] = 0.0
    return out


def _eval_wave7_in_wave6_state(
    name: str,
    *,
    c1_pass: bool = True,
    c2_pass: bool = True,
    c3_level: str = "full",
    c4_pass: bool = True,
    a_on: bool = False,
    b_on: bool = False,
    c_on: bool = False,
    coated_b_only: bool = False,
    coated_y_only: bool = False,
    tier: str = "CENTRAL",
    ceiling_lift: bool = True,
    allow_h2_ceiling_lift: bool = True,
    phi_c: float = 0.25,
    phi_inc_override: Optional[float] = None,
    omega_h4: float = 0.50,
    design_b_tyk2_discount: float = 0.0,
    freeze_a_on_c: bool = True,
    design_a_r1_only_when_coated: bool = True,
    include_r4_vehicle_gated: bool = False,
    include_z_r3p_sensitivity: bool = False,
    allow_c_on_coated_paths: bool = False,
    compute_strassen: bool = True,
    compute_h23_lp: bool = False,
) -> dict[str, Any]:
    """True Zero-Spillover Wave-6 x Wave-7 state evaluator isolating coated B/R2 & Y/R3P in BOTH H2 and H4."""
    require(c3_level in ("none", "partial", "full"), f"Bad c3_level={c3_level}")
    require(not (coated_b_only and coated_y_only), "Cannot set both coated_b_only and coated_y_only")
    p = _tier_params(tier)
    b_mult = 1.0 - design_b_tyk2_discount
    require(0.0 <= b_mult <= 1.0, f"Invalid design_b_tyk2_discount={design_b_tyk2_discount}")

    r4_active = c2_pass and c1_pass
    z_active = c4_pass and c1_pass
    n_arms = 6 if z_active else 5
    phi_inc = phi_inc_override if phi_inc_override is not None else (0.25 if c1_pass else 0.50)

    d_h1 = 0.78 if c1_pass else 0.62
    a_h2 = 0.78
    b_h2 = 0.80 if c1_pass else 0.79
    x_h2 = 0.83 if c1_pass else 0.82
    y_h2 = 0.85 if c1_pass else 0.84
    d_h2 = 0.92
    z_h2 = 0.83

    a_h3 = 0.57
    if c3_level == "none":
        base_c3 = 0.890
        b_h3, x_h3, y_h3 = 0.64, 0.73, 0.80
        d_h3 = 0.68 if c1_pass else 0.70
        z_h3 = 0.76
    elif c3_level == "partial":
        base_c3 = 0.930
        b_h3, x_h3, y_h3 = 0.70, 0.78, 0.83
        d_h3 = 0.74 if c1_pass else 0.75
        z_h3 = 0.79
    else:
        base_c3 = 0.942
        b_h3, x_h3, y_h3 = 0.73, 0.80, 0.85
        d_h3 = 0.78 if c1_pass else 0.77
        z_h3 = 0.82

    a_h4 = 0.69
    b_h4 = 0.70
    x_h4 = 0.69
    y_h4 = 0.72
    d_h4 = 0.80 if r4_active else 0.70
    z_h4 = 0.80 if r4_active else 0.72
    base_c4_univ = 0.950 if r4_active else 0.920

    state_c4u_scale = (1.0 - base_c4_univ) / (1.0 - 0.920)
    state_c3_scale = (1.0 - base_c3) / (1.0 - 0.925)

    da_h4 = p["da_h4"] if a_on else 0.0
    da_c4r = p["da_c4r"] if (a_on and ceiling_lift) else 0.0
    da_c4u = ((p["da_c4u"] / SCALE_C4_UNIV) * state_c4u_scale) if (a_on and ceiling_lift) else 0.0

    db_b2 = p["db_b2"] * b_mult if b_on else 0.0
    db_y2 = p["db_y2"] if b_on else 0.0
    db_b4 = p["db_b4"] * b_mult if b_on else 0.0
    db_y4 = p["db_y4"] if b_on else 0.0
    db_c2u = p["db_c2u"] if (b_on and ceiling_lift and allow_h2_ceiling_lift) else 0.0
    db_c4r = p["db_c4r"] if (b_on and ceiling_lift) else 0.0
    db_c4u = ((p["db_c4u"] / SCALE_C4_UNIV) * state_c4u_scale) if (b_on and ceiling_lift) else 0.0

    dc_abx_eff = ((p["dc_abx"] / SCALE_C3_BASE) * state_c3_scale) if c_on else 0.0
    dc_ydz_eff = ((p["dc_ydz"] / SCALE_C3_BASE) * state_c3_scale) if c_on else 0.0
    dc_a = 0.0 if freeze_a_on_c else dc_abx_eff
    dc_c3 = ((p["dc_c3"] / SCALE_C3_BASE) * state_c3_scale) if (c_on and ceiling_lift) else 0.0

    b4_coat = subadditive_overlap(b_h4, da_h4, db_b4, omega_h4)
    y4_coat = subadditive_overlap(y_h4, da_h4, db_y4, omega_h4)
    c4_univ_coat = subadditive_overlap(base_c4_univ, da_c4u, db_c4u, omega_h4)

    coating_active = a_on or b_on
    coated_b_active = coating_active and (not coated_y_only)
    coated_y_active = coating_active and (not coated_b_only)

    # Non-primary R4 counterfactual lifts D & Z on R4 explicitly (never used in primary headline!)
    d_h4_in = subadditive_overlap(d_h4, da_h4, db_y4, omega_h4) if (include_r4_vehicle_gated and r4_active) else d_h4
    z_h4_in = subadditive_overlap(z_h4, da_h4, db_y4, omega_h4) if (include_r4_vehicle_gated and r4_active) else z_h4
    r4_scale = (1.0 - 0.870) / (1.0 - 0.850)
    c4_r4_cf = subadditive_overlap(0.870, da_c4r * r4_scale, db_c4r * r4_scale, omega_h4) if (include_r4_vehicle_gated and r4_active) else None
    z_h4_sens = (y4_coat - y_h4) if (include_z_r3p_sensitivity and not r4_active) else 0.0
    z_h2_sens = db_y2 if ((include_z_r3p_sensitivity and not r4_active) or (include_r4_vehicle_gated and r4_active)) else 0.0

    arms_uncoated: list[tuple[float, float, float, float]] = [
        (0.88, a_h2, a_h3 + dc_a, a_h4),
        (0.88, b_h2, b_h3 + dc_abx_eff, b_h4),
        (0.88, x_h2, x_h3 + dc_abx_eff, x_h4),
        (0.88, y_h2, y_h3 + dc_ydz_eff, y_h4),
        (d_h1, d_h2, d_h3 + dc_ydz_eff, d_h4_in),
    ]
    arms_h3_base: list[float] = [a_h3, b_h3, x_h3, y_h3, d_h3]
    if z_active:
        arms_uncoated.append((0.84, z_h2, z_h3 + dc_ydz_eff, z_h4_in))
        arms_h3_base.append(z_h3)

    return eval_wave7_portfolio(
        name,
        arms_uncoated,
        c1_allo=0.88,
        c1_univ=0.925 if z_active else None,
        c1_z=0.84 if z_active else None,
        c2_a=0.87,
        c2_bx_unc=0.87,
        c2_y_unc=0.87,
        c2_d=0.93,
        c2_univ_unc=0.95,
        c3_base=base_c3 + dc_c3,
        c4_univ_unc=base_c4_univ,
        c4_r1=0.850,
        c4_r2_unc=0.850,
        c4_r3p_unc=0.850,
        c4_r4=0.870 if r4_active else None,