OpenIslet

scripts/verify_iip0058_armz_ensc_isletsort_v2_and_2p24_portfolio.py

Provisional IIP-0058 / DEC-139 / GATE-37 / GY-036 (Round-14 Blueprint Alignment; staged in iip0058/):

script Release v0.8.0 gen-eb955f8266035aba 837 KB · 62 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_iip0058_armz_ensc_isletsort_v2_and_2p24_portfolio.py · SHA-256: 33439d619c00fcc3…

        cls_set = frozenset(cell_classes[i] for i in range(n) if (m123 >> i) & 1)
        for m4, p4 in enumerate(d4):
            if p4 == 0.0:
                continue
            w = p123 * p4
            if m123 & m4:
                p_locked += w
            r_set = frozenset(routes[i] for i in range(n) if (m4 >> i) & 1)
            if is_mod_compat(cls_set, r_set):
                p_modular += w

    p_ceiling = caps[0] * caps[1] * caps[2] * caps[3]
    out: dict[str, Any] = {
        "locked_exact": p_locked * 100.0,
        "modular_exact": p_modular * 100.0,
        "ceiling_exact": p_ceiling * 100.0,
        "locked_pct": round(p_locked * 100.0, 4),
        "modular_pct": round(p_modular * 100.0, 4),
        "ceiling_pct": round(p_ceiling * 100.0, 4),
        "hurdle_caps_exact_pct": [c * 100.0 for c in caps],
        "hurdle_caps_pct": [round(c * 100.0, 4) for c in caps],
        "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],
    }

    if compute_strassen:
        s_min, s_max = bipartite_strassen_bounds(p_cell_cls, p_route_set, is_mod_compat)
        out["strassen_h123_h4_exact"] = [s_min * 100.0, s_max * 100.0]
        out["strassen_h123_h4_pct"] = [round(s_min * 100.0, 4), round(s_max * 100.0, 4)]

    if compute_h23_lp:
        # Exact SHARP H2/H3 transportation LP holding H1, H4 independent and H2, H3 marginals fixed!
        d1_nz = [(m1, p1) for m1, p1 in enumerate(d1) if p1 > 1e-15]
        payoff_m23: dict[int, float] = {}
        for m23 in range(1 << n):
            val = 0.0
            for m1, p1 in d1_nz:
                m123 = m1 & m23
                if m123 == 0:
                    continue
                cls_set = frozenset(cell_classes[i] for i in range(n) if (m123 >> i) & 1)
                p_r_ok = sum(pr for r_set, pr in p_route_set.items() if is_mod_compat(cls_set, r_set))
                val += p1 * p_r_ok
            payoff_m23[m23] = val

        nz2 = [(m, p) for m, p in enumerate(d2) if p > 1e-15]
        nz3 = [(m, p) for m, p in enumerate(d3) if p > 1e-15]
        sup = [p for _, p in nz2]
        dem = [p for _, p in nz3]
        c_min = [[payoff_m23[m2 & m3] for m3, _ in nz3] for m2, _ in nz2]
        c_max = [[-payoff_m23[m2 & m3] for m3, _ in nz3] for m2, _ in nz2]
        h23_min = solve_transportation_min_cost(sup, dem, c_min)
        h23_max = -solve_transportation_min_cost(sup, dem, c_max)
        _require(
            h23_min <= p_modular + 1e-11 and p_modular <= h23_max + 1e-11,
            f"H2/H3 LP sanity fail: [{h23_min}, {h23_max}] vs ind={p_modular}",
        )
        out["sharp_h23_transport_lp_exact"] = [h23_min * 100.0, h23_max * 100.0]
        out["sharp_h23_transport_lp_pct"] = [round(h23_min * 100.0, 4), round(h23_max * 100.0, 4)]

        # Explicitly named any-arm-per-hurdle Frechet diagnostic (NOT compatible-graft lower bound!)
        p_h1_any = 1.0 - d1[0]
        p_h2_any = 1.0 - d2[0]
        p_h3_any = 1.0 - d3[0]
        p_h4_any = 1.0 - d4[0]
        any_lo = max(0.0, p_h1_any + p_h2_any + p_h3_any + p_h4_any - 3.0)
        any_hi = min(p_h1_any, p_h2_any, p_h3_any, p_h4_any)
        out["any_arm_per_hurdle_frechet_diagnostic_exact"] = [any_lo * 100.0, any_hi * 100.0]
        out["any_arm_per_hurdle_frechet_diagnostic_pct"] = [round(any_lo * 100.0, 4), round(any_hi * 100.0, 4)]

    return out


def eval_post_rt_scenario(
    name: str,
    arms: Sequence[tuple[float, float, float, float]],
    *,
    c1_allo: float = 0.88,
    c1_univ: Optional[float] = None,
    c1_z: Optional[float] = None,
    phi_z_h1: float = 0.55,
    c2_inc: float = 0.87,
    c2_y: float = 0.87,
    c2_univ: float = 0.95,
    c2_d: float = 0.93,
    c2_z: Optional[float] = None,
    phi_z_h2: float = 0.80,
    c3_base: float = 0.89,
    c3_univ: Optional[float] = None,
    c3_z: Optional[float] = None,
    phi_z_h3: float = 0.80,
    c4_univ: float = 0.92,
    c4_pouch: float = 0.85,
    c4_r3p: float = 0.85,
    c4_r4: Optional[float] = None,
    phi_r4: float = 0.30,
    phi_yx: float = 0.75,
    phi_c: float = 0.25,
    phi_inc: float = 0.50,
    routes: Sequence[str],
    cell_classes: Sequence[str],
    compat_map: Mapping[str, FrozenSet[str]],
    compute_strassen: bool = True,
    compute_h23_lp: bool = False,
) -> dict[str, Any]:
    n = len(arms)
    _require(n in (5, 6), f"Expected 5 or 6 arms, got {n}")
    _require("R4" not in compat_map.get("INC", frozenset()), "GY-027(1) VIOLATION: unhardened INC (A/B/X) cannot use R4!")

    # H1
    if n == 5 or c1_univ is None:
        d1 = [0.0] * (1 << n)
        u_d = arms[4][0] / c1_allo
        if n == 5:
            d1[0] = 1.0 - c1_allo
            d1[15] = c1_allo * (1.0 - u_d)
            d1[31] = c1_allo * u_d
        else:
            u_z = arms[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:
        _require(c1_z is not None, "c1_z required when c1_univ is set")
        c1z = float(c1_z) if c1_z is not None else 0.0
        _require(0.0 < c1_allo <= c1_univ <= 1.0 and 0.0 < c1z <= c1_univ <= 1.0, "H1 cap domain error")
        sh = biv_bern(c1_allo / c1_univ, c1z / c1_univ, phi_z_h1)
        d1 = [0.0] * 64
        d1[0] = 1.0 - c1_univ
        u_d = arms[4][0] / c1_allo
        u_z = arms[5][0] / c1z
        for (s_sc, s_z), ws in sh.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 - sh[(0, 0)])

    # H2 (exact conditional m=0 mass conservation & coherent ceiling >= P(any H2 success))
    d2_5, c2_eff_5 = build_h2_5arm(
        arms[0][1], arms[1][1], arms[2][1], arms[3][1], arms[4][1],
        c2_inc=c2_inc, c2_y=c2_y, c2_univ=c2_univ, c2_auto=c2_d,
        phi_yx=phi_yx, phi_c=phi_c, phi_inc=phi_inc,
    )
    if n == 5:
        d2, c2_eff = d2_5, c2_eff_5
    elif c2_z is None:
        d2 = [0.0] * 64
        d2[0] = 1.0 - c2_univ
        sub5 = [d2_5[m] / c2_univ for m in range(32)]
        sub5[0] = (d2_5[0] - (1.0 - c2_univ)) / c2_univ
        _require(sub5[0] >= -1e-12, f"H2 sub5[0] negative: {sub5[0]}")
        sub5[0] = max(0.0, sub5[0])
        coupled = frechet_couple(sub5, arms[5][1] / c2_univ, phi_z_h2, lambda m: (m & 15) != 0)
        for k in range(64):
            d2[k] += c2_univ * coupled[k]
        c2_eff = max(c2_eff_5, 1.0 - d2[0])
    else:
        _require(0.0 < c2_z <= c2_univ <= 1.0, f"Invalid c2_z={c2_z}")
        sh2 = biv_bern(c2_eff_5 / c2_univ, c2_z / c2_univ, phi_z_h2)
        d2 = [0.0] * 64
        d2[0] = (1.0 - c2_univ) + c2_univ * sh2[(0, 0)]
        sub5_cond = [d2_5[m] / c2_eff_5 for m in range(32)]
        sub5_cond[0] = (d2_5[0] - (1.0 - c2_eff_5)) / c2_eff_5
        _require(sub5_cond[0] >= -1e-12, f"H2 sub5_cond[0] negative: {sub5_cond[0]}")
        sub5_cond[0] = max(0.0, sub5_cond[0])
        _require(abs(sum(sub5_cond) - 1.0) < 1e-12, f"sub5_cond sum={sum(sub5_cond)}")
        u_z2 = arms[5][1] / c2_z
        for m in range(32):
            frac = sub5_cond[m]
            d2[m] += c2_univ * sh2[(1, 0)] * frac
            d2[m] += c2_univ * sh2[(1, 1)] * frac * (1.0 - u_z2)
            d2[m | 32] += c2_univ * sh2[(1, 1)] * frac * u_z2
        d2[0] += c2_univ * sh2[(0, 1)] * (1.0 - u_z2)
        d2[32] += c2_univ * sh2[(0, 1)] * u_z2
        c2_eff = c2_univ * (1.0 - sh2[(0, 0)])

    # H3
    d3_5, c3_eff_5 = build_h3_5arm(
        arms[0][2], arms[1][2], arms[2][2], arms[3][2], arms[4][2],
        c3=c3_base, phi_yx=phi_yx, phi_c=phi_c, phi_inc=phi_inc,
    )
    if n == 5:
        d3, c3_eff = d3_5, c3_eff_5
    elif c3_univ is None or c3_z is None:
        d3 = [0.0] * 64
        d3[0] = 1.0 - c3_base
        sub5 = [d3_5[m] / c3_base for m in range(32)]
        sub5[0] = max(0.0, (d3_5[0] - (1.0 - c3_base)) / c3_base)
        coupled = frechet_couple(sub5, arms[5][2] / c3_base, phi_z_h3, lambda m: (m & 8) != 0)
        for k in range(64):
            d3[k] += c3_base * coupled[k]
        c3_eff = c3_base
    else:
        sh3 = biv_bern(c3_base / c3_univ, c3_z / c3_univ, phi_z_h3)
        d3 = [0.0] * 64
        d3[0] = (1.0 - c3_univ) + c3_univ * sh3[(0, 0)]
        sub5 = [d3_5[m] / c3_base for m in range(32)]
        sub5[0] = max(0.0, (d3_5[0] - (1.0 - c3_base)) / c3_base)
        u_z3 = arms[5][2] / c3_z
        for m in range(32):
            d3[m] += c3_univ * sh3[(1, 0)] * sub5[m]
            d3[m] += c3_univ * sh3[(1, 1)] * sub5[m] * (1.0 - u_z3)
            d3[m | 32] += c3_univ * sh3[(1, 1)] * sub5[m] * u_z3
        d3[0] += c3_univ * sh3[(0, 1)] * (1.0 - u_z3)
        d3[32] += c3_univ * sh3[(0, 1)] * u_z3
        c3_eff = c3_univ * (1.0 - sh3[(0, 0)])

    # H4
    if c4_r4 is None:
        sh4 = biv_bern(c4_pouch / c4_univ, c4_r3p / c4_univ, phi_c)
        d4 = [0.0] * (1 << n)
        d4[0] = 1.0 - c4_univ
        for (sp, s3), ws in sh4.items():
            wt = c4_univ * ws
            route_masks = []
            for rname, sflag, cr in [("R1", sp, c4_pouch), ("R2", sp, c4_pouch), ("R3P", s3, c4_r3p)]:
                idxs = [i for i in range(n) 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[(0, 0)])
    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_l = frechet_couple(d_sub, c4_r4 / c4_univ, phi_r4, lambda m: m != 0)
        d4 = [0.0] * (1 << n)