s3_vals = [v[1] for v in pair_results.values()] return { "proposal_id": "IIP-0024", "decision_id": "DEC-103", "phi_donor": round(phi_d, 4), "baseline_phi_donor_0_shared_ax_locked_pct": round(100.0 * base_tree["locked_2route_shared_ax_pouch_exact"], 3), "baseline_phi_donor_0_three_slot_locked_pct": round(100.0 * base_tree["locked_3slot_2pow12_exact"], 3), "two_bank_shared_ax_locked_range_pct": [round(100.0 * min(sh_vals), 2), round(100.0 * max(sh_vals), 2)], "two_bank_three_slot_locked_range_pct": [round(100.0 * min(s3_vals), 2), round(100.0 * max(s3_vals), 2)], "two_bank_pairings_exact": { k: { "shared_ax_locked_pct": round(100.0 * v[0], 3), "three_slot_locked_pct": round(100.0 * v[1], 3), } for k, v in pair_results.items() }, "headline_probabilities_invariant": True, } def _betacf_iip_0028(a: float, b: float, x: float) -> float: max_it = 300 eps = 3.0e-15 fpmin = 1.0e-300 qab = a + b qap = a + 1.0 qam = a - 1.0 c = 1.0 d = 1.0 - qab * x / qap if abs(d) < fpmin: d = fpmin d = 1.0 / d h = d for m in range(1, max_it + 1): m2 = 2 * m aa = m * (b - m) * x / ((qam + m2) * (a + m2)) d = 1.0 + aa * d if abs(d) < fpmin: d = fpmin c = 1.0 + aa / c if abs(c) < fpmin: c = fpmin d = 1.0 / d h *= d * c aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2)) d = 1.0 + aa * d if abs(d) < fpmin: d = fpmin c = 1.0 + aa / c if abs(c) < fpmin: c = fpmin d = 1.0 / d del_h = d * c h *= del_h if abs(del_h - 1.0) < eps: break return h def _reg_inc_beta_iip_0028(a: float, b: float, x: float) -> float: if x <= 0.0: return 0.0 if x >= 1.0: return 1.0 lbeta = math.lgamma(a) + math.lgamma(b) - math.lgamma(a + b) bt = math.exp(a * math.log(x) + b * math.log(1.0 - x) - lbeta) if x < (a + 1.0) / (a + b + 2.0): return bt * _betacf_iip_0028(a, b, x) / a return 1.0 - bt * _betacf_iip_0028(b, a, 1.0 - x) / b def clopper_pearson_u95_iip_0028(k: int, n: int, alpha: float = 0.05) -> float: """Exact one-sided Clopper-Pearson binomial upper bound for one prespecified donor-level event (IIP-0028; DEC-106).""" if isinstance(k, bool) or isinstance(n, bool) or not isinstance(k, int) or not isinstance(n, int): raise TypeError("Binomial counts must be strict non-boolean integers (IIP-0028; DEC-106)") if k < 0 or n <= 0 or k > n: raise ValueError("Invalid binomial counts (IIP-0028; DEC-106)") alp = _strict_prob_float_iip0024(alpha, "alpha") if not (0.0 < alp < 1.0): raise ValueError("alpha must lie in (0, 1)") if k == n: return 1.0 if k == 0: return 1.0 - (alp ** (1.0 / n)) lo, hi = 0.0, 1.0 for _ in range(100): mid = 0.5 * (lo + hi) if _reg_inc_beta_iip_0028(float(n - k), float(k + 1), 1.0 - mid) > alp: lo = mid else: hi = mid return 0.5 * (lo + hi) def exact_mcnemar_one_sided_pvalue_iip_0028(n01_b_rescues_a: int, n10_a_rescues_b: int) -> float: """Exact conditional upper-tail McNemar p-value for H1: p01 > p10 where 01 = Design A fails, B2 Base clears (IIP-0028; DEC-106).""" if isinstance(n01_b_rescues_a, bool) or isinstance(n10_a_rescues_b, bool) or not isinstance(n01_b_rescues_a, int) or not isinstance(n10_a_rescues_b, int): raise TypeError("Discordant counts must be strict integers (IIP-0028; DEC-106)") if n01_b_rescues_a < 0 or n10_a_rescues_b < 0: raise ValueError("Discordant counts must be non-negative") m = n01_b_rescues_a + n10_a_rescues_b if m == 0 or n01_b_rescues_a <= 0: return 1.0 return _reg_inc_beta_iip_0028(float(n01_b_rescues_a), float(m - n01_b_rescues_a + 1), 0.5) def exact_mcnemar_two_sided_pvalue_iip_0028(n01_b_rescues_a: int, n10_a_rescues_b: int) -> float: if isinstance(n01_b_rescues_a, bool) or isinstance(n10_a_rescues_b, bool) or not isinstance(n01_b_rescues_a, int) or not isinstance(n10_a_rescues_b, int): raise TypeError("Discordant counts must be strict integers (IIP-0028; DEC-106)") if n01_b_rescues_a < 0 or n10_a_rescues_b < 0: raise ValueError("Discordant counts must be non-negative") m = n01_b_rescues_a + n10_a_rescues_b if m == 0: return 1.0 k = max(n01_b_rescues_a, n10_a_rescues_b) return min(1.0, 2.0 * _reg_inc_beta_iip_0028(float(k), float(m - k + 1), 0.5)) def exact_unconditional_mcnemar_power_iip_0028( n_pairs: int, p01_b_rescues_a: float, p10_a_rescues_b: float, alpha: float = 0.05, ) -> dict: """ Exact unconditional operating characteristics of the conditional McNemar test summed over the trinomial distribution of discordant/concordant donor blocks (IIP-0028; DEC-106). """ if isinstance(n_pairs, bool) or not isinstance(n_pairs, int) or n_pairs <= 0: raise ValueError("n_pairs must be a positive integer (IIP-0028; DEC-106)") p01 = _strict_prob_float_iip0024(p01_b_rescues_a, "p01_b_rescues_a") p10 = _strict_prob_float_iip0024(p10_a_rescues_b, "p10_a_rescues_b") alp = _strict_prob_float_iip0024(alpha, "alpha") if p01 < 0.0 or p10 < 0.0 or (p01 + p10) > 1.0 or not (0.0 < alp < 1.0): raise ValueError("Invalid discordant probability cell or alpha (IIP-0028; DEC-106)") p_conc = max(0.0, 1.0 - p01 - p10) lg_n = math.lgamma(n_pairs + 1) power_one = 0.0 power_two = 0.0 type1_matched = 0.0 q_null = p01 + p10 for n01 in range(n_pairs + 1): for n10 in range(n_pairs - n01 + 1): n_c = n_pairs - n01 - n10 log_mult = lg_n - math.lgamma(n01 + 1) - math.lgamma(n10 + 1) - math.lgamma(n_c + 1) t1 = n01 * math.log(p01) if n01 > 0 else 0.0 t2 = n10 * math.log(p10) if n10 > 0 else 0.0 t3 = n_c * math.log(p_conc) if n_c > 0 else 0.0 has_alt = (n01 == 0 or p01 > 0.0) and (n10 == 0 or p10 > 0.0) and (n_c == 0 or p_conc > 0.0) p_cell = math.exp(log_mult + t1 + t2 + t3) if has_alt else 0.0 if exact_mcnemar_one_sided_pvalue_iip_0028(n01, n10) <= alp: power_one += p_cell p_half = 0.5 * q_null p_c0 = 1.0 - q_null if (n01 + n10 == 0 or p_half > 0.0) and (n_c == 0 or p_c0 > 0.0): tn12 = (n01 + n10) * math.log(p_half) if (n01 + n10) > 0 else 0.0 tn3 = n_c * math.log(p_c0) if n_c > 0 else 0.0 type1_matched += math.exp(log_mult + tn12 + tn3) if exact_mcnemar_two_sided_pvalue_iip_0028(n01, n10) <= alp: power_two += p_cell psi = p01 + p10 diff = p01 - p10 z_alpha = 1.6448536269514722 asymp = ( 0.5 * math.erfc(-((math.sqrt(n_pairs) * diff - z_alpha * math.sqrt(psi)) / math.sqrt(psi - diff * diff)) / math.sqrt(2.0)) if psi > diff * diff else (1.0 if diff > 0.0 else 0.0) ) return { "n_pairs": n_pairs, "p01_b_rescues_a": p01, "p10_a_rescues_b": p10, "exact_one_sided_power": round(power_one, 6), "exact_two_sided_power": round(power_two, 6), "connor_asymptotic_one_sided_power": round(asymp, 6), "asymptotic_minus_exact_pp": round(100.0 * (asymp - power_one), 4), "matched_null_exact_type1_rate": round(type1_matched, 6), } def compute_mcnemar_nuisance_size_profile_iip_0028(n_pairs: int = 24, alpha: float = 0.05) -> dict: """ Exact unconditional one-sided McNemar Type-I size over total discordance q = p01 + p10 in [0, 1] (with symmetric null per-direction probability p01 = p10 = q / 2; IIP-0028; DEC-106). Explicitly separates total discordance q = 0.96335 from per-direction 0.48167 at n = 24. """ if isinstance(n_pairs, bool) or not isinstance(n_pairs, int) or n_pairs <= 0: raise ValueError("n_pairs must be a positive integer (IIP-0028; DEC-106)") alp = _strict_prob_float_iip0024(alpha, "alpha") lg_n = math.lgamma(n_pairs + 1) rej_by_m = [0.0] * (n_pairs + 1) for m in range(1, n_pairs + 1): lg_m = math.lgamma(m + 1) rej_m = 0.0 for a in range(m + 1): if exact_mcnemar_one_sided_pvalue_iip_0028(a, m - a) <= alp: rej_m += math.exp(lg_m - math.lgamma(a + 1) - math.lgamma(m - a + 1) - m * math.log(2.0)) rej_by_m[m] = rej_m def _size_at_q(q_total: float) -> float: if q_total <= 0.0: return 0.0 if q_total >= 1.0: return rej_by_m[n_pairs] tot = 0.0 for m in range(1, n_pairs + 1): if rej_by_m[m] == 0.0: continue bm = math.exp( lg_n - math.lgamma(m + 1) - math.lgamma(n_pairs - m + 1) + m * math.log(q_total) + ((n_pairs - m) * math.log(1.0 - q_total) if (n_pairs - m) > 0 else 0.0) ) tot += bm * rej_by_m[m] return tot def _dsize_dq(q_total: float) -> float: deriv = 0.0 for m in range(1, n_pairs + 1): if rej_by_m[m] == 0.0: continue bm = math.exp( lg_n - math.lgamma(m + 1) - math.lgamma(n_pairs - m + 1) + m * math.log(q_total) + ((n_pairs - m) * math.log(1.0 - q_total) if (n_pairs - m) > 0 else 0.0) ) deriv += bm * rej_by_m[m] * ((m / q_total) - ((n_pairs - m) / (1.0 - q_total))) return deriv # Coarse 1,000-point bracket + exact golden-section / derivative refinement on [0, 1]: best_q = 1.0 best_sz = rej_by_m[n_pairs] for step in range(1, 1000): qv = step / 1000.0 sv = _size_at_q(qv) if sv > best_sz: best_sz = sv best_q = qv if 0.0 < best_q < 1.0: lo_q = max(1e-6, best_q - 0.002) hi_q = min(1.0 - 1e-6, best_q + 0.002) if _dsize_dq(lo_q) > 0.0 and _dsize_dq(hi_q) < 0.0: for _ in range(70): mid_q = 0.5 * (lo_q + hi_q) if _dsize_dq(mid_q) > 0.0: lo_q = mid_q