OpenIslet

scripts/rank_islet1_stacks.py

Step 5 & 6: Project Islet-1 Sign-Sensitive, Endpoint-Oriented & Locus-Penalized Evaluator (v0.6.9).

script Release v0.8.0 gen-eb955f8266035aba 638 KB · 42 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/rank_islet1_stacks.py · SHA-256: c05d5fe606219e88…

        c0,
        stage0_status=stage0_status,
        cell_classes=("UNHARDENED_A_B_X", "UNHARDENED_A_B_X", "UNHARDENED_A_B_X", fourth_arm_cell_class),
        route_ids=routes,
    )


def _eval_5arm_unified_iip0035(
    label: str,
    p_y: tuple[float, float, float, float],
    p_d1: tuple[float, float, float, float],
    *,
    pa: tuple[float, float, float, float],
    pb: tuple[float, float, float, float],
    px: tuple[float, float, float, float],
    c2_y: float,
    c3_y: float,
    phi_yx: float,
    phi_c: float,
    d1_on_r3: bool,
    c4_univ: float,
    c4_r3: float,
    p0_l: float,
    p0_m: float,
    c0: float,
    stage0_status: str,
) -> dict:
    c2_univ, c2_auto = 0.95, 0.93
    u_d1 = p_d1[0] / 0.88
    d1 = [0.0] * 32
    d1[0] = 0.12
    d1[15] = 0.88 * (1.0 - u_d1)
    d1[31] = 0.88 * u_d1

    d2_inc = _frechet_bernoulli_joint_iip0034([pa[1] / 0.87, pb[1] / 0.87, px[1] / 0.87], 0.0)
    r2 = 0.87 / c2_y
    u_y2 = p_y[1] / c2_y
    d2_both = _frechet_couple_new_arm_iip0034(d2_inc, u_y2, phi_yx, lambda mi: bool(mi & 4))
    d2_allo4 = [0.0] * 16
    for k in range(16):
        d2_allo4[k] += r2 * d2_both[k]
    d2_allo4[0] += (1.0 - r2) * (1.0 - u_y2)
    d2_allo4[8] += (1.0 - r2) * u_y2

    biv2 = _bivariate_bernoulli_iip0034(c2_y / c2_univ, c2_auto / c2_univ, phi_c)
    if p_d1[1] > c2_auto + 1e-12:
        raise ValueError(f"Autologous H2 probability {p_d1[1]} exceeds ceiling c2_auto={c2_auto} in {label}")
    u_d2 = p_d1[1] / c2_auto
    d2 = [0.0] * 32
    d2[0] += 1.0 - c2_univ
    for (s0, s1), wp in biv2.items():
        w = c2_univ * wp
        if s0 == 0 and s1 == 0:
            d2[0] += w
        elif s0 == 1 and s1 == 0:
            for m4 in range(16):
                d2[m4] += w * d2_allo4[m4]
        elif s0 == 0 and s1 == 1:
            d2[0] += w * (1.0 - u_d2)
            d2[16] += w * u_d2
        else:
            for m4 in range(16):
                d2[m4] += w * d2_allo4[m4] * (1.0 - u_d2)
                d2[m4 | 16] += w * d2_allo4[m4] * u_d2
    c2_e = c2_univ * (1.0 - biv2[(0, 0)])

    d3_inc = _frechet_bernoulli_joint_iip0034([pa[2] / 0.89, pb[2] / 0.89, px[2] / 0.89], 0.0)
    r3 = 0.89 / c3_y
    u_y3 = p_y[2] / c3_y
    d3_both = _frechet_couple_new_arm_iip0034(d3_inc, u_y3, phi_yx, lambda mi: bool(mi & 4))
    d3_allo4 = [0.0] * 16
    for k in range(16):
        d3_allo4[k] += r3 * d3_both[k]
    d3_allo4[0] += (1.0 - r3) * (1.0 - u_y3)
    d3_allo4[8] += (1.0 - r3) * u_y3
    d3_5cond = _frechet_couple_new_arm_iip0034(d3_allo4, p_d1[2] / c3_y, phi_c, lambda m4: m4 != 0)
    d3 = [0.0] * 32
    d3[0] += 1.0 - c3_y
    for k in range(32):
        d3[k] += c3_y * d3_5cond[k]
    c3_e = c3_y

    d4_inc = _live_incumbent_h4_cond_iip0034()
    biv4 = _bivariate_bernoulli_iip0034(0.85 / c4_univ, c4_r3 / c4_univ, phi_c)
    d4 = [0.0] * 32
    d4[0] += 1.0 - c4_univ
    if d1_on_r3:
        d4_r3 = _frechet_bernoulli_joint_iip0034([p_y[3] / c4_r3, p_d1[3] / c4_r3], 1.0)
        for (s0, s1), wp in biv4.items():
            w = c4_univ * wp
            if s0 == 0 and s1 == 0:
                d4[0] += w
            elif s0 == 1 and s1 == 0:
                for m3 in range(8):
                    d4[m3] += w * d4_inc[m3]
            elif s0 == 0 and s1 == 1:
                for mr in range(4):
                    m5 = ((mr & 1) << 3) | (((mr >> 1) & 1) << 4)
                    d4[m5] += w * d4_r3[mr]
            else:
                for m3 in range(8):
                    for mr in range(4):
                        m5 = m3 | ((mr & 1) << 3) | (((mr >> 1) & 1) << 4)
                        d4[m5] += w * d4_inc[m3] * d4_r3[mr]
        routes = ("R1", "R2", "R1", "R3P", "R3P")
    else:
        d4_pouch_d1 = _frechet_couple_new_arm_iip0034(d4_inc, p_d1[3] / 0.85, 1.0, lambda m3: bool(m3 & 2))
        d4_p0 = [0.0] * 32
        for m4 in range(16):
            d4_p0[(m4 & 7) | (((m4 >> 3) & 1) << 4)] = d4_pouch_d1[m4]
        u_y4 = p_y[3] / c4_r3
        for (s0, s1), wp in biv4.items():
            w = c4_univ * wp
            if s0 == 0 and s1 == 0:
                d4[0] += w
            elif s0 == 1 and s1 == 0:
                for m5 in range(32):
                    if d4_p0[m5]:
                        d4[m5] += w * d4_p0[m5]
            elif s0 == 0 and s1 == 1:
                d4[0] += w * (1.0 - u_y4)
                d4[8] += w * u_y4
            else:
                for m5 in range(32):
                    if d4_p0[m5]:
                        d4[m5] += w * d4_p0[m5] * (1.0 - u_y4)
                        d4[m5 | 8] += w * d4_p0[m5] * u_y4
        routes = ("R1", "R2", "R1", "R3P", "R2")
    c4_e = c4_univ * (1.0 - biv4[(0, 0)])
    return _run_dp_and_verify_parity_iip0034(
        label,
        [pa, pb, px, p_y, p_d1],
        [d1, d2, d3, d4],
        [0.88, c2_e, c3_e, c4_e],
        p0_l,
        p0_m,
        c0,
        stage0_status=stage0_status,
        cell_classes=("UNHARDENED_A_B_X", "UNHARDENED_A_B_X", "UNHARDENED_A_B_X", "DESIGN_Y_FULLSTACK_V2", "D1B_AUTO_C_V2"),
        route_ids=routes,
    )


def _solve_exact_breakeven_p1_iip0035(
    *,
    pa: tuple[float, float, float, float],
    pb: tuple[float, float, float, float],
    px: tuple[float, float, float, float],
    phi_cross: float,
    c0: float,
) -> float:
    """Exact continuous affine break-even p1* where 4-arm D1b@R3P locked odds equal the live shared ceiling c0."""
    target = c0 * 100.0
    d2_inc = _frechet_bernoulli_joint_iip0034([pa[1] / 0.87, pb[1] / 0.87, px[1] / 0.87], 0.0)
    biv2 = _bivariate_bernoulli_iip0034(0.87 / 0.95, 0.93 / 0.95, phi_cross)
    u_d2 = 0.92 / 0.93
    d2 = [0.0] * 16
    d2[0] += 0.05
    for (s0, s1), wp in biv2.items():
        w = 0.95 * wp
        if s0 == 0 and s1 == 0:
            d2[0] += w
        elif s0 == 1 and s1 == 0:
            for mi in range(8):
                d2[mi] += w * d2_inc[mi]
        elif s0 == 0 and s1 == 1:
            d2[0] += w * (1.0 - u_d2)
            d2[8] += w * u_d2
        else:
            for mi in range(8):
                d2[mi] += w * d2_inc[mi] * (1.0 - u_d2)
                d2[mi | 8] += w * d2_inc[mi] * u_d2
    d3_inc = _frechet_bernoulli_joint_iip0034([pa[2] / 0.89, pb[2] / 0.89, px[2] / 0.89], 0.0)
    d3_cond = _frechet_couple_new_arm_iip0034(d3_inc, 0.70 / 0.89, phi_cross, lambda mi: mi != 0)
    d3 = [0.0] * 16
    d3[0] += 0.11
    for k in range(16):
        d3[k] += 0.89 * d3_cond[k]
    d4_inc = _live_incumbent_h4_cond_iip0034()
    biv4 = _bivariate_bernoulli_iip0034(0.85 / 0.92, 0.85 / 0.92, phi_cross)
    u_d4 = 0.70 / 0.85
    d4 = [0.0] * 16
    d4[0] += 0.08
    for (s0, s1), wp in biv4.items():
        w = 0.92 * wp
        if s0 == 0 and s1 == 0:
            d4[0] += w
        elif s0 == 1 and s1 == 0:
            for mi in range(8):
                d4[mi] += w * d4_inc[mi]
        elif s0 == 0 and s1 == 1:
            d4[0] += w * (1.0 - u_d4)
            d4[8] += w * u_d4
        else:
            for mi in range(8):
                d4[mi] += w * d4_inc[mi] * (1.0 - u_d4)
                d4[mi | 8] += w * d4_inc[mi] * u_d4

    def _eval_locked_pct(p1_val: float) -> float:
        u_d1 = p1_val / 0.88
        d1 = [0.0] * 16
        d1[0] = 0.12
        d1[7] = 0.88 * (1.0 - u_d1)
        d1[15] = 0.88 * u_d1
        dp = [0.0] * 16
        dp[15] = 1.0
        for d in (d1, d2, d3, d4):
            nxt = [0.0] * 16
            for a_mask, p_a in enumerate(dp):
                if p_a:
                    for b_mask, p_b in enumerate(d):
                        if p_b:
                            nxt[a_mask & b_mask] += p_a * p_b
            dp = nxt
        return (1.0 - dp[0]) * 100.0

    lo, hi = 0.45, 0.85
    f_lo = _eval_locked_pct(lo)
    f_hi = _eval_locked_pct(hi)
    if not (math.isfinite(f_lo) and math.isfinite(f_hi)) or f_hi <= f_lo:
        raise ValueError(f"Non-increasing or non-finite break-even bracket for phi_cross={phi_cross}: f({lo})={f_lo}, f({hi})={f_hi}")
    if target < f_lo - 1e-12 or target > f_hi + 1e-12:
        raise ValueError(
            f"Target ceiling {target:.6f}% lies outside bracket [{f_lo:.6f}%, {f_hi:.6f}%] on p1 in [{lo}, {hi}] for phi_cross={phi_cross}"
        )
    p1_star = lo + (hi - lo) * (target - f_lo) / (f_hi - f_lo)
    residual = abs(_eval_locked_pct(p1_star) - target)
    if not math.isfinite(residual) or residual > 1e-11:
        raise ValueError(f"Exact affine break-even residual {residual:.3e} exceeded 1e-11 at p1*={p1_star}")
    return round(p1_star, 6)


def _max_abs_full_joint_state_diff_iip0035(
    dists_a: tuple[tuple[float, ...], ...],
    dists_b: tuple[tuple[float, ...], ...],
) -> float:
    """Exact max_{s in 2^(4N)} |P_a(s) - P_b(s)| across ALL unrounded 2^(4N) joint states (up to 1,048,576)."""
    if len(dists_a) != 4 or len(dists_b) != 4:
        raise ValueError("Expected 4 stage distributions")
    for side_label, dists in (("dists_a", dists_a), ("dists_b", dists_b)):
        for st_idx, dist in enumerate(dists):
            for entry in dist:
                if isinstance(entry, bool) or not isinstance(entry, (int, float)) or not math.isfinite(float(entry)):
                    raise ValueError(
                        f"Non-finite probability encountered in unrounded 2^(4N) scenario law ({side_label}[{st_idx}]={entry!r})"
                    )
    if any(len(a) != len(b) for a, b in zip(dists_a, dists_b)):
        return float("inf")
    a0, a1, a2, a3 = dists_a
    b0, b1, b2, b3 = dists_b