OpenIslet

scripts/verify_iip0055_hygiene_safecell_minifh_orthogonality.py

OpenIslet Wave-6 W6-C1 (Provisional IIP-0055 / DEC-136 / GATE-34 / GY-033;

script Release v0.8.0 gen-eb955f8266035aba 417 KB · 30 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_iip0055_hygiene_safecell_minifh_orthogonality.py · SHA-256: d5b98e2ba1310333…

    phi_r4: float = 0.30,
    phi_inc: float = 0.0,
    phi_yx: float = 0.75,
    phi_c: float = 0.25,
    routes: Sequence[str] = ("R1", "R2", "R1", "R3P", "R3P"),
    cell_classes: Sequence[str] = ("INC", "INC", "INC", "STAGED_HARDENED", "STAGED_HARDENED"),
    compat_map: Optional[Mapping[str, FrozenSet[str]]] = None,
) -> Dict[str, Any]:
    n = len(arms)
    require_cond(n in (5, 6), f"Expected 5 or 6 arms, got {n}")
    if compat_map is None:
        compat_map = {"INC": frozenset({"R1", "R2"}), "STAGED_HARDENED": frozenset({"R3P"})}
    require_cond("R4" not in compat_map.get("INC", frozenset()), "GY-027(1) violation: INC cannot use R4")
    require_cond(0.0 < c2_inc <= c2_y <= c2_univ <= 1.0, "Domain violation in c2_inc <= c2_y <= c2_univ")

    # H1
    if n == 5 or c1_univ is None:
        d1 = [0.0] * (1 << n)
        u_d = arms[4][0] / c1_allo
        d1[0] = 1.0 - c1_allo
        d1[15] = c1_allo * (1.0 - u_d)
        d1[31] = c1_allo * u_d
        c1_eff = c1_allo
    else:
        require_cond(c1_z is not None, "c1_z required for 6-arm H1")
        c1z = float(c1_z)
        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 (coherent ceiling >= P(any H2 success))
    d2_inc = frechet_joint([arms[0][1] / c2_inc, arms[1][1] / c2_inc, arms[2][1] / c2_inc], phi_inc)
    r2 = c2_inc / c2_y
    u_y2 = arms[3][1] / c2_y
    d2_both = frechet_couple(d2_inc, u_y2, phi_yx, lambda mi: bool(mi & 4))
    d2_allo4 = [r2 * d2_both[k] for k in range(16)]
    d2_allo4[0] += (1.0 - r2) * (1.0 - u_y2)
    d2_allo4[8] += (1.0 - r2) * u_y2
    biv2 = biv_bern(c2_y / c2_univ, c2_auto / c2_univ, phi_c)
    u_d2 = arms[4][1] / c2_auto
    d2_5 = [0.0] * 32
    d2_5[0] += 1.0 - c2_univ
    for (s0, s1), wp in biv2.items():
        w = c2_univ * wp
        if s0 == 0 and s1 == 0:
            d2_5[0] += w
        elif s0 == 1 and s1 == 0:
            for m4 in range(16):
                d2_5[m4] += w * d2_allo4[m4]
        elif s0 == 0 and s1 == 1:
            d2_5[0] += w * (1.0 - u_d2)
            d2_5[16] += w * u_d2
        else:
            for m4 in range(16):
                d2_5[m4] += w * d2_allo4[m4] * (1.0 - u_d2)
                d2_5[m4 | 16] += w * d2_allo4[m4] * u_d2
    c2_eff_5 = c2_univ * (1.0 - biv2[(0, 0)])
    if n == 5:
        d2, c2_eff = d2_5, c2_eff_5
    else:
        d2 = [0.0] * 64
        d2[0] = 1.0 - c2_univ
        sub5 = [d2_5[m] / c2_univ for m in range(32)]
        sub5[0] = max(0.0, (d2_5[0] - (1.0 - c2_univ)) / c2_univ)
        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])

    # H3
    d3_inc = frechet_joint([arms[0][2] / c3, arms[1][2] / c3, arms[2][2] / c3], phi_inc)
    d3_both = frechet_couple(d3_inc, arms[3][2] / c3, phi_yx, lambda mi: bool(mi & 4))
    d3_5cond = frechet_couple(d3_both, arms[4][2] / c3, phi_c, lambda m4: m4 != 0)
    d3_5 = [c3 * x for x in d3_5cond]
    d3_5[0] += 1.0 - c3
    if n == 5:
        d3, c3_eff = d3_5, c3
    else:
        d3 = [0.0] * 64
        d3[0] = 1.0 - c3
        sub5 = [d3_5[m] / c3 for m in range(32)]
        sub5[0] = max(0.0, (d3_5[0] - (1.0 - c3)) / c3)
        coupled = frechet_couple(sub5, arms[5][2] / c3, phi_z_h3, lambda m: (m & 8) != 0)
        for k in range(64):
            d3[k] += c3 * coupled[k]
        c3_eff = c3

    # 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)
        d4[0] = 1.0 - c4_univ
        for sm, ws in enumerate(sh4_l):
            wt = c4_univ * ws
            if wt == 0.0:
                continue
            sp, s3, s4 = (sm >> 0) & 1, (sm >> 1) & 1, (sm >> 2) & 1
            route_masks = []
            for rname, sflag, cr in (("R1", sp, c4_pouch), ("R2", sp, c4_pouch), ("R3P", s3, c4_r3p), ("R4", s4, c4_r4)):
                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_l[0])

    return eval_exact_n_arm(arms, [d1, d2, d3, d4], [c1_eff, c2_eff, c3_eff, c4_eff], routes, cell_classes, compat_map)


def compute_part_f_portfolio_verification(
    master_contract: Dict[str, Any],
    repo_root: Path | None,
) -> Dict[str, Any]:
    l0 = eval_3arm_core_l0()
    L1_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), (0.88, 0.84, 0.80, 0.72), (0.62, 0.92, 0.70, 0.70)]
    W5_ARMS = [(0.90, 0.78, 0.68, 0.745), (0.90, 0.79, 0.73, 0.775), (0.90, 0.82, 0.83, 0.745), (0.90, 0.84, 0.84, 0.735), (0.66, 0.92, 0.755, 0.715)]
    F4_ARMS = [(0.88, 0.78, 0.57, 0.69), (0.88, 0.80, 0.64, 0.70), (0.88, 0.83, 0.73, 0.69), (0.88, 0.85, 0.80, 0.72), (0.78, 0.92, 0.68, 0.70)]
    SIX_CENTRAL_ARMS = [
        (0.88, 0.78, 0.57, 0.69),  # A STRICTLY INVARIANT!
        (0.88, 0.80, 0.73, 0.70),
        (0.88, 0.83, 0.80, 0.69),
        (0.88, 0.85, 0.85, 0.72),
        (0.78, 0.92, 0.78, 0.80),
        (0.84, 0.83, 0.82, 0.80),
    ]
    six_routes = ("R1", "R2", "R1", "R3P", "R4", "R4")
    six_classes = ("INC", "INC", "INC", "STAGED_HARDENED", "STAGED_HARDENED", "Z_HARDENED")
    six_compat = {
        "INC": frozenset({"R1", "R2"}),
        "STAGED_HARDENED": frozenset({"R3P", "R4"}),
        "Z_HARDENED": frozenset({"R3P", "R4"}),
    }

    l1_ship = eval_5_or_6_arm_scenario(L1_ARMS, phi_inc=0.0)
    w5_l3b = eval_5_or_6_arm_scenario(W5_ARMS, c1_allo=0.90, c3=0.925, phi_inc=0.0)
    l1_honest = eval_5_or_6_arm_scenario(L1_ARMS, phi_inc=0.50)
    l1_phi25 = eval_5_or_6_arm_scenario(L1_ARMS, phi_inc=0.25)
    l1_phi75 = eval_5_or_6_arm_scenario(L1_ARMS, phi_inc=0.75)

    c1_phi25 = eval_5_or_6_arm_scenario(F4_ARMS, phi_inc=0.25)
    c1_phi50 = eval_5_or_6_arm_scenario(F4_ARMS, phi_inc=0.50)
    c1_phi75 = eval_5_or_6_arm_scenario(F4_ARMS, phi_inc=0.75)

    six_phi25 = eval_5_or_6_arm_scenario(
        SIX_CENTRAL_ARMS, c1_allo=0.88, c1_univ=0.925, c1_z=0.84, phi_z_h1=0.55,
        phi_z_h2=0.80, c3=0.942, phi_z_h3=0.80, c4_univ=0.950, c4_r4=0.870, phi_r4=0.30,
        phi_inc=0.25, routes=six_routes, cell_classes=six_classes, compat_map=six_compat,
    )
    six_phi50 = eval_5_or_6_arm_scenario(
        SIX_CENTRAL_ARMS, c1_allo=0.88, c1_univ=0.925, c1_z=0.84, phi_z_h1=0.55,
        phi_z_h2=0.80, c3=0.942, phi_z_h3=0.80, c4_univ=0.950, c4_r4=0.870, phi_r4=0.30,
        phi_inc=0.50, routes=six_routes, cell_classes=six_classes, compat_map=six_compat,
    )
    six_phi75 = eval_5_or_6_arm_scenario(
        SIX_CENTRAL_ARMS, c1_allo=0.88, c1_univ=0.925, c1_z=0.84, phi_z_h1=0.55,
        phi_z_h2=0.80, c3=0.942, phi_z_h3=0.80, c4_univ=0.950, c4_r4=0.870, phi_r4=0.30,
        phi_inc=0.75, routes=six_routes, cell_classes=six_classes, compat_map=six_compat,
    )

    # Diagnostic counter-factual: unpatched killed D1b-Lean-K1 when exogenous biologics fail (H3=0.15)
    k1_fail_arms = [(0.88, 0.78, 0.57, 0.69), (0.88, 0.80, 0.64, 0.70), (0.88, 0.83, 0.73, 0.69), (0.88, 0.85, 0.80, 0.72), (0.84, 0.92, 0.15, 0.70)]
    k1_twin_fail = eval_5_or_6_arm_scenario(k1_fail_arms, phi_inc=0.25)

    # Unrounded IEEE-754 float64 delta computation (preserving BOTH unrounded *_exact AND 4-dp *_pct!)
    h_ref = l1_honest["modular_exact"]
    for sc_obj in (c1_phi25, c1_phi50, c1_phi75, l1_phi25, l1_phi75, six_phi25, six_phi50, six_phi75):
        d_ex = sc_obj["modular_exact"] - h_ref
        sc_obj["delta_vs_honest_l1_exact_pp"] = d_ex
        sc_obj["delta_vs_honest_l1_pct_pp"] = round(d_ex, 4)

    # Fail-closed unrounded (< 1e-12) AND 4-decimal (== 0.0) verification against wave6_master_contract.json!
    mc_sc = master_contract["reachable_scenarios"]
    mc_sens = master_contract["mandatory_audit_sensitivities"]
    external_checks: List[Tuple[str, Dict[str, Any], Dict[str, Any]]] = [
        ("L0_core_3arm", l0, mc_sc["L0_core_3arm"]),
        ("L1_shipped_IIP0035_phi0", l1_ship, mc_sc["L1_shipped_IIP0035_phi0"]),
        ("Wave5_L3B_audited_caveated", w5_l3b, mc_sc["Wave5_L3B_audited_caveated"]),
        ("L1_honest_monoculture_phi_inc_0.50", l1_honest, mc_sc["L1_honest_monoculture_phi_inc_0.50"]),
        ("W6_C1_standalone_over_honest_L1", c1_phi25, mc_sc["W6_C1_standalone_over_honest_L1"]),
        ("Wave6_6Arm_Central_EnSC_H1_88_LOCKED", six_phi25, mc_sc["Wave6_6Arm_Central_EnSC_H1_88_LOCKED"]),
        ("Sens_C1_marginals_only_at_phi_inc_0.50", c1_phi50, mc_sens["Sens_C1_marginals_only_at_phi_inc_0.50"]),
        ("Sens_C1_marginals_only_at_phi_inc_0.75", c1_phi75, mc_sens["Sens_C1_marginals_only_at_phi_inc_0.75"]),
        ("Sens_6Arm_Central_at_phi_inc_0.50", six_phi50, mc_sens["Sens_6Arm_Central_at_phi_inc_0.50"]),
    ]
    exact_parity_errs: Dict[str, float] = {}
    display_parity_errs: Dict[str, float] = {}
    for key, actual, ref in external_checks:
        exact_diffs = [
            abs(actual["locked_exact"] - ref["locked_exact"]),
            abs(actual["modular_exact"] - ref["modular_exact"]),
            abs(actual["ceiling_exact"] - ref["ceiling_exact"]),
            max(abs(a - b) for a, b in zip(actual["hurdle_caps_exact_pct"], ref["hurdle_caps_exact_pct"])),
            max(abs(a - b) for a, b in zip(actual["hurdle_any_success_exact_pct"], ref["hurdle_any_success_exact_pct"])),
        ]
        if "strassen_h123_h4_exact" in ref:
            exact_diffs.append(max(abs(a - b) for a, b in zip(actual["strassen_h123_h4_exact"], ref["strassen_h123_h4_exact"])))