OpenIslet

scripts/verify_iip0043_wholegraft_gate0v_and_calibration.py

IIP-0043 / DEC-122 (CAND-034 / EXP-031 — Bucket 1 Closure, Stage S3-R2 Remediated)

script Release v0.8.0 gen-eb955f8266035aba 1008 KB · 76 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_iip0043_wholegraft_gate0v_and_calibration.py · SHA-256: 20706180ed1ed6a2…

        "test": "r2_counterexample3b_unit_cap_1p0_clamped_n1_vs_unclamped_n29",
        "reported_n": unit_res["min_n_pass"],
        "unclamped_ref": unit_res.get("unclamped_min_n_reference"),
        "boundary_strictly_verified": unit_res["boundary_strictly_verified"],
        "passed": ce3b_ok,
    })

    # 10. Counterexample #3c: narrow ambient exponent range Emin=-2, Emax=100 + Inexact trap on mixed CP!
    _clear_math_caches()
    narrow_trapped = False
    narrow_val = None
    with localcontext(Context(prec=4, rounding=ROUND_FLOOR, Emin=-2, Emax=100, traps=[decimal.Inexact])):
        try:
            narrow_val = exact_clopper_pearson_upper_general(1, 3, Decimal("0.05"), prec=80)
        except Exception:
            narrow_trapped = True
    ce3c_ok = bool((not narrow_trapped) and narrow_val is not None and Decimal(0) < narrow_val < Decimal(1))
    checks.append({
        "test": "r2_counterexample3c_mixed_cp_narrow_ambient_emin_neg2_inexact_trap_immunity",
        "trapped": narrow_trapped,
        "passed": ce3c_ok,
    })

    # 11. R06-B2 Exact component-probability ratio boundary:
    #     veh = 51/100 (alpha_veh = 0.025 -> s_veh_lower ≈ 0.4080363292), treated = 0/90402
    #     (alpha_treated = 0.025 -> u_ratio = 0.00010000201070... > 0.0001 MUST NOT PASS 0.0001,
    #     whereas 0/90404 clears <= 0.0001!).
    b2_fail_res = exact_paired_vehicle_ratio_bounds(
        0, 90402, 51, 100, alpha_treated=Decimal("0.025"), alpha_veh=Decimal("0.025"), prec=80
    )
    b2_pass_res = exact_paired_vehicle_ratio_bounds(
        0, 90404, 51, 100, alpha_treated=Decimal("0.025"), alpha_veh=Decimal("0.025"), prec=80
    )
    b2_ok = bool(
        abs(float(b2_fail_res["s_veh_lower_dec"]) - 0.4080363292) < 1e-9
        and abs(float(b2_fail_res["u_ratio_dec"]) - 0.00010000201070) < 1e-13
        and b2_fail_res["u_ratio_dec"] > Decimal("0.0001")
        and b2_pass_res["u_ratio_dec"] <= Decimal("0.0001")
    )
    checks.append({
        "test": "r06_b2_exact_component_ratio_boundary_veh_51of100_treated_0of90402_exceeds_1e4",
        "s_veh_lower_10dp": format(b2_fail_res["s_veh_lower_dec"], ".10f"),
        "u_ratio_at_90402_14dp": format(b2_fail_res["u_ratio_dec"], ".14f"),
        "u_ratio_at_90404_14dp": format(b2_pass_res["u_ratio_dec"], ".14f"),
        "exceeds_1e4_at_90402": bool(b2_fail_res["u_ratio_dec"] > Decimal("0.0001")),
        "clears_1e4_at_90404": bool(b2_pass_res["u_ratio_dec"] <= Decimal("0.0001")),
        "passed": b2_ok,
    })

    # 12. R08-M1: Exact paired ratio bounds (u_ratio and l_ratio) explicitly labeled as separate
    #     directional one-sided confidence statements at 1 - (alpha_treated + alpha_veh) each,
    #     not a simultaneous two-sided interval at that same unsplit alpha!
    m1_dir_ok = bool(
        b2_fail_res.get("confidence_statement_type") == "DIRECTIONAL_ONE_SIDED_SEPARATE_AT_1_MINUS_ALPHA_SUM"
        and b2_fail_res.get("directional_one_sided_confidence_lower_bound") == "0.950"
        and b2_fail_res.get("simultaneous_two_sided_at_unsplit_alpha_claimed") is False
        and b2_pass_res.get("confidence_statement_type") == "DIRECTIONAL_ONE_SIDED_SEPARATE_AT_1_MINUS_ALPHA_SUM"
    )
    checks.append({
        "test": "r08_m1_exact_paired_ratio_bounds_labeled_directional_one_sided_separate",
        "confidence_statement_type": b2_fail_res.get("confidence_statement_type"),
        "directional_one_sided_confidence_lower_bound": b2_fail_res.get("directional_one_sided_confidence_lower_bound"),
        "simultaneous_two_sided_at_unsplit_alpha_claimed": b2_fail_res.get("simultaneous_two_sided_at_unsplit_alpha_claimed"),
        "passed": m1_dir_ok,
    })

    # 13. R09-B2 (a) + R15-M1: Vehicle-normalized survival ratio domain [0, +infinity) — synthetic 90%
    #     (900/1,000) vs 70% (700/1,000) at deff=1 with component tails alpha_treated=0.025 and alpha_veh=0.025
    #     MUST NEVER clamp u_ratio to 1.0; guarantees 0 <= l_ratio <= point_est <= u_ratio!
    r90_70 = exact_paired_vehicle_ratio_bounds(
        900, 1000, 700, 1000, alpha_treated=Decimal("0.025"), alpha_veh=Decimal("0.025"), prec=80
    )
    pt_90_70 = Decimal(900) / Decimal(700)
    r90_70_ok = bool(
        Decimal(0) <= r90_70["l_ratio_dec"] <= pt_90_70 <= r90_70["u_ratio_dec"]
        and r90_70["u_ratio_dec"] == r90_70["u_ratio_unclamped_dec"]
        and r90_70["u_ratio_clamped_to_unity"] is False
        and r90_70["estimand_domain"] == "[0, +infinity)"
        and r90_70["bounds_ordered_l_le_u"] is True
        and r90_70["treated_survival_exceeds_vehicle_survival"] is True
        and r90_70["ratio_lower_limit_exceeds_unity"] is True
        and r90_70["ratio_upper_limit_exceeds_unity"] is True
        and abs(float(r90_70["l_ratio_dec"]) - 1.2079329473) < 1e-8
        and abs(float(r90_70["u_ratio_dec"]) - 1.3688921818) < 1e-8
    )
    checks.append({
        "test": "r09_b2_paired_ratio_90pct_vs_70pct_unclamped_domain_valid_l_le_pt_le_u",
        "treated_counts": "900/1000",
        "vehicle_counts": "700/1000",
        "alpha_treated": "0.025",
        "alpha_veh": "0.025",
        "l_ratio_10dp": format(r90_70["l_ratio_dec"], ".10f"),
        "point_est_10dp": format(pt_90_70, ".10f"),
        "u_ratio_10dp": format(r90_70["u_ratio_dec"], ".10f"),
        "u_ratio_clamped_to_unity": r90_70["u_ratio_clamped_to_unity"],
        "bounds_ordered_l_le_u": r90_70["bounds_ordered_l_le_u"],
        "passed": r90_70_ok,
    })

    # 14. R09-B2 (b): Full ratio-domain sweep (p_drug = 0, < p_veh, == p_veh, > p_veh, = 1,
    #     k_veh = 0 yielding u_ratio = +Infinity, and deff >= 1.0) guaranteeing 0 <= L <= U ALWAYS!
    sweep_cases = (
        (0, 1000, 700, 1000, Decimal("1.0")),
        (500, 1000, 700, 1000, Decimal("1.0")),
        (700, 1000, 700, 1000, Decimal("1.0")),
        (900, 1000, 700, 1000, Decimal("1.0")),
        (1000, 1000, 700, 1000, Decimal("1.0")),
        (900, 1000, 700, 1000, Decimal("2.0")),
        (10, 1000, 700, 1000, Decimal("2.5")),
        (500, 1000, 0, 1000, Decimal("1.0")),
        (0, 1000, 0, 1000, Decimal("1.0")),
    )
    sweep_ok = True
    for kt, nt, kv, nv, dff in sweep_cases:
        sw = exact_paired_vehicle_ratio_bounds(kt, nt, kv, nv, deff=dff, prec=80)
        if not (
            Decimal(0) <= sw["l_ratio_dec"] <= sw["u_ratio_dec"]
            and sw["bounds_ordered_l_le_u"] is True
            and sw["u_ratio_clamped_to_unity"] is False
            and sw["u_ratio_dec"] == sw["u_ratio_unclamped_dec"]
        ):
            sweep_ok = False
        if kv == 0:
            if not (sw["u_ratio_dec"] == Decimal("Infinity") and sw["u_ratio_80d"] == "Infinity"):
                sweep_ok = False
        elif dff == Decimal("1.0"):
            pt_sw = Decimal(kt * nv) / Decimal(nt * kv)
            if not (sw["l_ratio_dec"] <= pt_sw <= sw["u_ratio_dec"]):
                sweep_ok = False
    checks.append({
        "test": "r09_b2_paired_ratio_full_domain_sweep_l_le_u_including_zero_veh_and_deff",
        "cases_swept": len(sweep_cases),
        "all_ordered_and_unclamped": sweep_ok,
        "passed": sweep_ok,
    })

    # 15. R10-B3 (a): Exact Round-09 counterexample (200/299 vs 500/500, deff=100, alpha=0.49/0.025)
    #     Outward effective counts k_eff_lower = floor(k * n_eff / n) = 1, k_eff_upper = ceil(k * n_eff / n) = 2
    #     eliminate the floor(k/deff)/floor(n/deff) > k/n inward-bias bug and guarantee 0 <= L <= point <= U!
    r10_ce = exact_paired_vehicle_ratio_bounds(
        200,
        299,
        500,
        500,
        alpha_treated=Decimal("0.49"),
        alpha_veh=Decimal("0.025"),
        deff=Decimal("100"),
        prec=80,
    )
    r10_pt = Decimal(200) / Decimal(299)
    r10_ce_ok = bool(
        r10_ce["n_treated_eff"] == 2
        and r10_ce["k_treated_eff_lower"] == 1
        and r10_ce["k_treated_eff_upper"] == 2
        and Decimal(0) <= r10_ce["l_ratio_dec"] <= r10_pt <= r10_ce["u_ratio_dec"]
        and r10_ce["bounds_ordered_l_le_u"] is True
        and r10_ce["bounds_ordered_l_le_point_le_u"] is True
    )
    checks.append({
        "test": "r10_b3_counterexample_200of299_vs_500of500_deff100_alpha0p49_ordered_l_le_pt_le_u",
        "n_treated_eff": r10_ce["n_treated_eff"],
        "k_treated_eff_lower": r10_ce["k_treated_eff_lower"],
        "k_treated_eff_upper": r10_ce["k_treated_eff_upper"],
        "l_ratio_10dp": format(r10_ce["l_ratio_dec"], ".10f"),
        "point_est_10dp": format(r10_pt, ".10f"),
        "u_ratio_10dp": format(r10_ce["u_ratio_dec"], ".10f"),
        "passed": r10_ce_ok,
    })

    # 16. R10-B3 (b): Dense admitted-domain ordering sweep across n, k, deff (including deff > n),
    #     and alpha_treated, alpha_veh <= 0.5 (with alpha_treated + alpha_veh < 1) -> ZERO violations!
    dense_total = 0
    dense_violations = 0
    for nt_d in (1, 2, 3, 7, 17, 299):
        kt_vals = sorted(set((0, 1, nt_d // 3, nt_d // 2, (2 * nt_d) // 3, max(0, nt_d - 1), nt_d)))
        for kt_d in kt_vals:
            for dff_d in (Decimal("1"), Decimal("1.5"), Decimal("3"), Decimal("100"), Decimal("500")):
                for kv_d, nv_d in ((0, 10), (1, 10), (7, 10), (10, 10)):
                    for at_d, av_d in (
                        (Decimal("0.025"), Decimal("0.025")),
                        (Decimal("0.49"), Decimal("0.025")),
                        (Decimal("0.5"), Decimal("0.49")),
                        (Decimal("0.025"), Decimal("0.5")),
                    ):
                        dense_total += 1
                        res_d = exact_paired_vehicle_ratio_bounds(
                            kt_d, nt_d, kv_d, nv_d, alpha_treated=at_d, alpha_veh=av_d, deff=dff_d, prec=40
                        )
                        if res_d["empirical_ratio_point_estimate_dec"] is None:
                            if not (
                                res_d["bounds_ordered_l_le_u"] is True
                                and res_d["bounds_ordered_l_le_point_le_u"] is None
                                and Decimal(0) <= res_d["l_ratio_dec"] <= res_d["u_ratio_dec"]
                            ):
                                dense_violations += 1
                        elif not (
                            res_d["bounds_ordered_l_le_u"] is True
                            and res_d["bounds_ordered_l_le_point_le_u"] is True
                            and Decimal(0) <= res_d["l_ratio_dec"] <= res_d["empirical_ratio_point_estimate_dec"] <= res_d["u_ratio_dec"]
                        ):
                            dense_violations += 1
    checks.append({
        "test": "r10_b3_dense_admitted_domain_ordering_sweep_zero_violations",
        "grid_points_verified": dense_total,
        "ordering_violations": dense_violations,
        "passed": bool(dense_total >= 1000 and dense_violations == 0),
    })

    # 17. R11-B3: Sol's exact exchangeable binary count mixture counterexample (N=1000, P(K=0)=0.20,
    #     P(K=10)=0.80, true rate=0.008, true variance=16, true deff=125/62=2.0161290322580645,
    #     deterministic control=1.0): n_eff=496 gives u_ratio~0.00740994 < 0.008 (80% actual coverage
    #     for a nominal 95% upper bound!). Narrowed helper MUST set exact_directional_confidence_certified=False,
    #     confidence_scope="NON_AUTHORIZING_VARIANCE_ADJUSTED_DIAGNOSTIC_ONLY", release_authorizing_eligible=False,
    #     directional_one_sided_confidence_lower_bound=None, and adjudication_disposition=
    #     "HOLD_CLUSTERED_DEFF_GT_1_REQUIRES_CLUSTER_LEVEL_INFERENCE" (while deff==1.0 certifies True)!
    sol_deff = Decimal("2.0161290322580645")
    sol_u_only = exact_component_probability_ratio_upper(
        0, 1000, Decimal("0.025"), (Decimal("1.0"),), deff=sol_deff, prec=80
    )
    sol_paired = exact_paired_vehicle_ratio_bounds(
        0, 1000, 1_000_000, 1_000_000, alpha_treated=Decimal("0.025"), alpha_veh=Decimal("0.025"), deff=sol_deff, prec=80
    )
    indep_ref = exact_paired_vehicle_ratio_bounds(
        0, 1000, 1_000_000, 1_000_000, alpha_treated=Decimal("0.025"), alpha_veh=Decimal("0.025"), deff=Decimal("1.0"), prec=80
    )
    r11_b3_ok = bool(
        sol_u_only["n_treated_eff"] == 496
        and abs(float(sol_u_only["u_ratio_dec"]) - 0.00740994) < 1e-6
        and sol_u_only["u_ratio_dec"] < Decimal("0.008")
        and sol_u_only["exact_directional_confidence_certified"] is False
        and sol_u_only["confidence_scope"] == "NON_AUTHORIZING_VARIANCE_ADJUSTED_DIAGNOSTIC_ONLY"
        and sol_u_only["release_authorizing_eligible"] is False
        and sol_u_only["adjudication_disposition"] == "HOLD_CLUSTERED_DEFF_GT_1_REQUIRES_CLUSTER_LEVEL_INFERENCE"
        and sol_paired["n_treated_eff"] == 496
        and sol_paired["exact_directional_confidence_certified"] is False
        and sol_paired["confidence_scope"] == "NON_AUTHORIZING_VARIANCE_ADJUSTED_DIAGNOSTIC_ONLY"
        and sol_paired["release_authorizing_eligible"] is False
        and sol_paired["directional_one_sided_confidence_lower_bound"] is None
        and sol_paired["adjudication_disposition"] == "HOLD_CLUSTERED_DEFF_GT_1_REQUIRES_CLUSTER_LEVEL_INFERENCE"
        and sol_paired["bounds_ordered_l_le_point_le_u"] is True
        and indep_ref["exact_directional_confidence_certified"] is True
        and indep_ref["confidence_scope"] == "EXACT_INDEPENDENT_BINOMIAL_BONFERRONI_DEFF_EQ_1"
        and indep_ref["release_authorizing_eligible"] is True
        and indep_ref["directional_one_sided_confidence_lower_bound"] == "0.950"
    )
    checks.append({
        "test": "r11_b3_sol_exchangeable_mixture_deff_2p0161_non_authorizing_vs_deff_eq_1_certified",
        "n_treated_eff": sol_u_only["n_treated_eff"],
        "diagnostic_u_ratio_8dp": format(sol_u_only["u_ratio_dec"], ".8f"),
        "clustered_certified": sol_paired["exact_directional_confidence_certified"],