else: hi_q = mid_q best_q = 0.5 * (lo_q + hi_q) best_sz = _size_at_q(best_q) sz_q1 = rej_by_m[n_pairs] return { "n_pairs": n_pairs, "alpha": alp, "global_worst_case_size": round(best_sz, 6), "global_worst_case_size_pct": round(100.0 * best_sz, 4), "argmax_total_discordance_q": round(best_q, 5), "argmax_per_direction_null_p01_eq_p10": round(0.5 * best_q, 5), "size_at_total_discordance_q_1p0": round(sz_q1, 6), "size_at_total_discordance_q_1p0_pct": round(100.0 * sz_q1, 4), } def compute_ab_frechet_dependence_2pow12_iip_0028( pa_vec: list[float] | tuple[float, ...], pb_vec: list[float] | tuple[float, ...], px_vec: list[float] | tuple[float, ...], c_vec: list[float] | tuple[float, ...], phi_ab: float = 0.0, ) -> dict: """ Evaluate H2/H3 conditional Design A-B Frechet upper-bound coupling weight phi_AB in [0, 1] across the 4,096-state (2^12) common-shock tree while holding X conditionally independent and verifying exact 12-marginal recovery (IIP-0028; DEC-106). """ if not all(isinstance(v, (list, tuple)) and len(v) == 4 for v in (pa_vec, pb_vec, px_vec, c_vec)): raise ValueError("All hurdle and ceiling vectors must be 4-element lists or tuples (IIP-0028; DEC-106)") phi = _strict_prob_float_iip0024(phi_ab, "phi_ab") if not (0.0 <= phi <= 1.0): raise ValueError(f"phi_ab={phi} must lie in [0, 1] (IIP-0028; DEC-106)") pa = [_strict_prob_float_iip0024(pa_vec[i], f"pa[{i}]") for i in range(4)] pb = [_strict_prob_float_iip0024(pb_vec[i], f"pb[{i}]") for i in range(4)] px = [_strict_prob_float_iip0024(px_vec[i], f"px[{i}]") for i in range(4)] cc = [_strict_prob_float_iip0024(c_vec[i], f"c[{i}]") for i in range(4)] # Validate marginals & ceilings via baseline generator first compute_common_shock_2pow12_portfolio_tree(pa, pb, px, cc) ua = [pa[i] / cc[i] for i in range(4)] ub = [pb[i] / cc[i] for i in range(4)] ux = [px[i] / cc[i] for i in range(4)] def _eval_mode(shared_ax_h4: bool) -> tuple[float, float]: dists: list[list[float]] = [] for h in range(4): ch = cc[h] if h == 0: d = [0.0] * 8 d[0] = 1.0 - ch d[7] = ch dists.append(d) continue qa, qb, qx = ua[h], ub[h], ux[h] d = [0.0] * 8 d[0] += 1.0 - ch for ah in (0, 1): for bh in (0, 1): for xh in (0, 1): mask = ah | (bh << 1) | (xh << 2) if h in (1, 2) and phi > 0.0: p11 = (1.0 - phi) * (qa * qb) + phi * min(qa, qb) pab = {(1, 1): p11, (1, 0): qa - p11, (0, 1): qb - p11, (0, 0): 1.0 - qa - qb + p11}[(ah, bh)] pg = pab * (qx if xh else 1.0 - qx) elif h == 3 and shared_ax_h4: p11 = min(qa, qx) pax = {(1, 1): p11, (1, 0): max(0.0, qa - qx), (0, 1): max(0.0, qx - qa), (0, 0): 1.0 - max(qa, qx)}[(ah, xh)] pg = pax * (qb if bh else 1.0 - qb) else: pg = (qa if ah else 1.0 - qa) * (qb if bh else 1.0 - qb) * (qx if xh else 1.0 - qx) d[mask] += ch * pg dists.append(d) locked = 0.0 any_h123 = 0.0 mass = 0.0 margs = [[0.0, 0.0, 0.0] for _ in range(4)] for m0 in range(8): for m1 in range(8): for m2 in range(8): s2 = m0 & m1 & m2 p012 = dists[0][m0] * dists[1][m1] * dists[2][m2] for m3 in range(8): w = p012 * dists[3][m3] mass += w if s2 != 0: any_h123 += w if (s2 & m3) != 0: locked += w for hi, mh in enumerate((m0, m1, m2, m3)): if mh & 1: margs[hi][0] += w if mh & 2: margs[hi][1] += w if mh & 4: margs[hi][2] += w if abs(mass - 1.0) > 1e-12: raise ValueError(f"2^12 phi_AB mass drift: {mass}") for hi in range(4): if ( abs(margs[hi][0] - pa[hi]) > 1e-12 or abs(margs[hi][1] - pb[hi]) > 1e-12 or abs(margs[hi][2] - px[hi]) > 1e-12 ): raise ValueError("2^12 phi_AB marginal recovery drift") pouch_union = ( cc[3] * (1.0 - (1.0 - max(ua[3], ux[3])) * (1.0 - ub[3])) if shared_ax_h4 else cc[3] * (1.0 - (1.0 - ua[3]) * (1.0 - ub[3]) * (1.0 - ux[3])) ) return locked, any_h123 * pouch_union def _cond_corr_and_disc(h_idx: int) -> tuple[float, float]: qa, qb = ua[h_idx], ub[h_idx] p11 = (1.0 - phi) * (qa * qb) + phi * min(qa, qb) denom = math.sqrt(qa * (1.0 - qa) * qb * (1.0 - qb)) r_val = (p11 - qa * qb) / denom if denom > 0.0 else 0.0 disc = (qa - p11) + (qb - p11) return r_val, disc sh_lock, sh_mod = _eval_mode(shared_ax_h4=True) r3_lock, r3_mod = _eval_mode(shared_ax_h4=False) r_h2, disc_h2 = _cond_corr_and_disc(1) r_h3, disc_h3 = _cond_corr_and_disc(2) return { "phi_ab": round(phi, 4), "single_A_pct": round(100.0 * pa[0] * pa[1] * pa[2] * pa[3], 6), "single_B_pct": round(100.0 * pb[0] * pb[1] * pb[2] * pb[3], 6), "single_X_pct": round(100.0 * px[0] * px[1] * px[2] * px[3], 6), "conditional_pearson_r_h2": round(r_h2, 6), "conditional_pearson_r_h3": round(r_h3, 6), "conditional_ab_discordance_h2_pct": round(100.0 * disc_h2, 6), "conditional_ab_discordance_h3_pct": round(100.0 * disc_h3, 6), "shared_ax_locked_pct": round(100.0 * sh_lock, 6), "shared_ax_modular_pct": round(100.0 * sh_mod, 6), "three_route_locked_pct": round(100.0 * r3_lock, 6), "three_route_modular_pct": round(100.0 * r3_mod, 6), } def bridge_2x2_concordance_to_conditional_phi_ab_iip_0028( n11_both_clear: int, n10_a_only_clears: int, n01_b2_only_clears: int, n00_both_fail: int, *, common_shock_ceiling_c: float, b2_to_scored_b_hurdle_bridge_verified: bool = False, ) -> dict: """ Bridge an observed Donor x (Design A vs ISLET1_B2_5LOCUS_BASE) 2x2 concordance table to conditional Frechet dependence weight phi_AB (IIP-0028; DEC-106). Explicitly separates (1) direct assay concordance & exact McNemar p-values from (2) conditional hurdle coupling phi_AB, which requires BOTH a fixed/identified common-shock ceiling c_h AND a verified biological bridge from B2 Base to scored Design B on H2/H3. """ for lbl, val in ( ("n11_both_clear", n11_both_clear), ("n10_a_only_clears", n10_a_only_clears), ("n01_b2_only_clears", n01_b2_only_clears), ("n00_both_fail", n00_both_fail), ): if isinstance(val, bool) or not isinstance(val, int) or val < 0: raise ValueError(f"{lbl} must be a non-negative integer (IIP-0028; DEC-106)") if type(b2_to_scored_b_hurdle_bridge_verified) is not bool: raise TypeError("b2_to_scored_b_hurdle_bridge_verified must be a boolean") n_tot = n11_both_clear + n10_a_only_clears + n01_b2_only_clears + n00_both_fail if n_tot < 6: raise ValueError("Paired 2x2 donor concordance table requires N >= 6 independent donor blocks (IIP-0028; DEC-106)") ch = _strict_prob_float_iip0024(common_shock_ceiling_c, "common_shock_ceiling_c") if not (0.0 < ch <= 1.0): raise ValueError("common_shock_ceiling_c must lie in (0, 1]") p11 = n11_both_clear / float(n_tot) p10 = n10_a_only_clears / float(n_tot) p01 = n01_b2_only_clears / float(n_tot) p00 = n00_both_fail / float(n_tot) pa_hat = p11 + p10 pb_hat = p11 + p01 p_one = exact_mcnemar_one_sided_pvalue_iip_0028(n01_b2_only_clears, n10_a_only_clears) p_two = exact_mcnemar_two_sided_pvalue_iip_0028(n01_b2_only_clears, n10_a_only_clears) if pa_hat > ch + 1e-12 or pb_hat > ch + 1e-12: return { "status": "INCOMPATIBLE_COMMON_SHOCK_CEILING_BELOW_OBSERVED_MARGINAL", "n_total_blocks": n_tot, "observed_2x2_proportions": {"p11": round(p11, 6), "p10": round(p10, 6), "p01": round(p01, 6), "p00": round(p00, 6)}, "exact_mcnemar_one_sided_p_01_gt_10": round(p_one, 6), "exact_mcnemar_two_sided_p": round(p_two, 6), "conditional_phi_ab_hat_clipped_0_1": None, } ua_hat = pa_hat / ch ub_hat = pb_hat / ch u11_hat = p11 / ch frechet_span = min(ua_hat, ub_hat) - (ua_hat * ub_hat) raw_phi = ((u11_hat - ua_hat * ub_hat) / frechet_span) if frechet_span > 1e-12 else 0.0 clipped_phi = min(1.0, max(0.0, raw_phi)) return { "status": ( "BRIDGED_TO_CONDITIONAL_HURDLE_PHI_AB" if b2_to_scored_b_hurdle_bridge_verified else "ASSAY_CONCORDANCE_ONLY_BRIDGE_TO_SCORED_B_UNVERIFIED" ), "n_total_blocks": n_tot, "observed_2x2_counts": { "n11_both_clear": n11_both_clear, "n10_a_only_clears": n10_a_only_clears, "n01_b2_only_clears": n01_b2_only_clears, "n00_both_fail": n00_both_fail, }, "observed_2x2_proportions": { "p11": round(p11, 6), "p10": round(p10, 6), "p01": round(p01, 6), "p00": round(p00, 6), }, "assay_concordance_fraction": round(p11 + p00, 6), "assay_discordance_fraction": round(p10 + p01, 6), "exact_mcnemar_one_sided_p_01_gt_10": round(p_one, 6), "exact_mcnemar_two_sided_p": round(p_two, 6), "assumed_common_shock_ceiling_c": ch, "b2_to_scored_b_hurdle_bridge_verified": b2_to_scored_b_hurdle_bridge_verified, "unclipped_conditional_phi_ab_plug_in": round(raw_phi, 6) if b2_to_scored_b_hurdle_bridge_verified else None, "conditional_phi_ab_hat_clipped_0_1": round(clipped_phi, 6) if b2_to_scored_b_hurdle_bridge_verified else None, } def compute_paired_mcnemar_and_phi_ab_concordance_audit_iip_0028() -> dict: """ Deterministic operating-characteristic and 2^12 concordance-bridge audit for the crossed Donor x (ISLET1_A4_ARM1B vs ISLET1_B2_5LOCUS_BASE) Stage-0 study (IIP-0028; DEC-106). Leaves all 2,160 stack scores/ranks and 27% / 31% / 36% / 53% headline odds 100% invariant. """ pa = [0.88, 0.78, 0.57, 0.69] pb = [0.88, 0.79, 0.64, 0.70] px = [0.88, 0.82, 0.73, 0.69] cv = [0.88, 0.87, 0.89, 0.85] corners = ((0.40, 0.05), (0.40, 0.02), (0.45, 0.02), (0.45, 0.05), (0.50, 0.02), (0.50, 0.05)) sample_sizes = (6, 16, 20, 24, 29) grid_30 = [ exact_unconditional_mcnemar_power_iip_0028(n_p, p01, p10, alpha=0.05) for n_p in sample_sizes for (p01, p10) in corners ] by_key = {(r["n_pairs"], r["p01_b_rescues_a"], r["p10_a_rescues_b"]): r for r in grid_30}