u_hi_260 = p_hi_260 / eff_lo_hi if u_lo_260 > Decimal(1): u_lo_260 = Decimal(1) if u_hi_260 > Decimal(1): u_hi_260 = Decimal(1) if u_hi_260 <= target_frac: return True if target_frac < u_lo_260: return False return None def _zero_event_u95_le_target( alpha_d: Decimal, n_raw: int, f_d: Decimal, eta_d: Decimal, target_frac: Decimal, *, u95_lower: Decimal, u95_upper: Decimal, ) -> bool: """Fail-closed conservative decider for true U95 <= target_frac (returns True iff provably <= target_frac).""" return _zero_event_u95_compare( alpha_d, n_raw, f_d, eta_d, target_frac, u95_lower=u95_lower, u95_upper=u95_upper ) is True def evaluate_sound_decimal_clopper_pearson_zero_event( *, gate_id: str, k: Any, n_raw: Any, max_u95_fraction: Any, eta_recovery_l95: Any = Decimal(1), f_eligible: Any = Decimal(1), f_eligible_provenance: str = "KNOWN_OR_CONSERVATIVELY_ESTABLISHED", alpha_obs: Any = None, alpha_eta_recovery: Any = None, alpha_f_eligible: Any = None, sampling_independence_model: Any = "INDEPENDENT_BIOLOGICAL_OPPORTUNITIES", clustered_sampling: Any = False, sampling_independence_conflict: bool = False, sampling_malformed: bool = False, sampling_independence_provenance: str = "UNSPECIFIED_DEFAULT_ASSUMED_INDEPENDENT", calibration_hold_reason: str | None = None, calibration_sources: Dict[str, str] | None = None, reported_u95_pct: Decimal | None = None, ) -> Dict[str, Any]: """Strictly sound outward Decimal rare-event evaluator for zero-event Clopper-Pearson bounds. Semantics (pre-empting ambiguous 'k==0 veto' shorthand): - Any integer k >= 1 IMMEDIATELY returns FAIL_HARD_STOP (unconditional hard stop). - When k == 0, bounds the zero-event upper limit by directed Decimal arithmetic using logarithm and exponential enclosures at ordinary arguments and a paired alternating Taylor enclosure at small arguments (x < 1e-6). The final upper bound is >= the exact capped upper limit (clamped inclusively at 1). Comparisons that remain unresolved within the 2,000,000-bit exact rational and 260-digit outward enclosure proof budget HOLD explicitly rather than misclassifying. - Whenever eta_recovery_l95 < 1.0, requires alpha_eta_recovery >= 0.05 and Bonferroni sum <= 0.10. - Always stamps empirical_clearance_status as mathematical count subgate arithmetic ONLY, conditional on validated recovery/eligible-fraction calibration, supplying no empirical clearance. """ cap_d = _safe_exact_decimal(max_u95_fraction) cap_valid = cap_d is not None and Decimal(0) < cap_d <= Decimal(1) with _clean_dec_ctx(80, ROUND_HALF_EVEN): cap_str = format(cap_d, ".16e") if cap_valid and cap_d is not None else None is_int_k = isinstance(k, int) and not isinstance(k, bool) is_int_n = isinstance(n_raw, int) and not isinstance(n_raw, bool) # 1. Explicit k >= 1 is ALWAYS FAIL_HARD_STOP (independent of denominator, calibration, or cap validity) if is_int_k and k >= 1: return { "gate_id": gate_id, "status": "FAIL_HARD_STOP", "reason": "POSITIVE_EVENT_COUNT_K_GE_1_HARD_STOP", "empirical_clearance_status": "FAIL_HARD_STOP_POSITIVE_EVENT_DETECTED", "sampling_independence_provenance": sampling_independence_provenance, "calibration_sources": calibration_sources, "k": k, "n_raw": n_raw if is_int_n else None, "cap_fraction_str": cap_str, } # 2. Validate cap, integer k == 0, and n_raw >= 1 if not cap_valid or cap_d is None: return { "gate_id": gate_id, "status": "HOLD_INVALID_CAP_FRACTION", "empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INVALID_INPUT", "sampling_independence_provenance": sampling_independence_provenance, "calibration_sources": calibration_sources, "cap_fraction_str": cap_str, } if k is None or n_raw is None: return { "gate_id": gate_id, "status": "HOLD_MISSING_ATOMIC_INTEGER_K_OR_N", "empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INCOMPLETE", "sampling_independence_provenance": sampling_independence_provenance, "calibration_sources": calibration_sources, "cap_fraction_str": cap_str, } if not is_int_k or not is_int_n: return { "gate_id": gate_id, "status": "HOLD_NON_INTEGER_COUNT_OR_DENOMINATOR_REJECTED", "empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INVALID_INPUT", "sampling_independence_provenance": sampling_independence_provenance, "calibration_sources": calibration_sources, "cap_fraction_str": cap_str, } if n_raw <= 0 or k < 0 or k > n_raw: return { "gate_id": gate_id, "status": "HOLD_INVALID_COUNT_BOUNDS", "empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INVALID_INPUT", "sampling_independence_provenance": sampling_independence_provenance, "calibration_sources": calibration_sources, "cap_fraction_str": cap_str, } # 2b. Reject contradictory, malformed, or explicitly clustered/non-independent sampling units (GY-018 / DEC-103 parity) if sampling_independence_conflict: return { "gate_id": gate_id, "status": "HOLD_CONFLICTING_SAMPLING_INDEPENDENCE_DECLARATIONS", "reason": "CONTRADICTORY_PANEL_AND_ASSAY_SAMPLING_INDEPENDENCE_DECLARATIONS", "empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INVALID_INPUT", "sampling_independence_provenance": sampling_independence_provenance, "calibration_sources": calibration_sources, "cap_fraction_str": cap_str, } if sampling_malformed: return { "gate_id": gate_id, "status": "HOLD_MALFORMED_SAMPLING_INDEPENDENCE_DECLARATION", "reason": "MALFORMED_OR_NON_ASCII_SAMPLING_INDEPENDENCE_DECLARATION", "empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INVALID_INPUT", "sampling_independence_provenance": sampling_independence_provenance, "calibration_sources": calibration_sources, "cap_fraction_str": cap_str, } if ( not isinstance(sampling_independence_model, str) or sampling_independence_model != "INDEPENDENT_BIOLOGICAL_OPPORTUNITIES" or clustered_sampling is not False ): return { "gate_id": gate_id, "status": "HOLD_NON_INDEPENDENT_OR_CLUSTERED_SAMPLING_REJECTED", "reason": "REQUIRES_INDEPENDENT_BIOLOGICAL_OPPORTUNITIES_AND_CLUSTERED_SAMPLING_FALSE", "empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INVALID_INPUT", "sampling_independence_provenance": sampling_independence_provenance, "calibration_sources": calibration_sources, "cap_fraction_str": cap_str, } # 2c. Reject cross-scope calibration conflicts, explicit-null overrides, or split bound groups if calibration_hold_reason is not None: return { "gate_id": gate_id, "status": calibration_hold_reason, "reason": calibration_hold_reason, "empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INVALID_INPUT", "sampling_independence_provenance": sampling_independence_provenance, "calibration_sources": calibration_sources, "cap_fraction_str": cap_str, } # 3. Validate calibration factors & provenance (alpha_eta >= 0.05 whenever eta < 1.0) eta_d = _safe_exact_decimal(eta_recovery_l95) f_d = _safe_exact_decimal(f_eligible) if eta_d is None or f_d is None: return { "gate_id": gate_id, "status": "HOLD_NONFINITE_RECOVERY_OR_ELIGIBLE_FACTOR", "empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INVALID_INPUT", "sampling_independence_provenance": sampling_independence_provenance, "calibration_sources": calibration_sources, "cap_fraction_str": cap_str, } if not (Decimal(0) < eta_d <= Decimal(1)) or not (Decimal(0) < f_d <= Decimal(1)): return { "gate_id": gate_id, "status": "HOLD_OUT_OF_RANGE_RECOVERY_OR_ELIGIBLE_FACTOR", "empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INVALID_INPUT", "sampling_independence_provenance": sampling_independence_provenance, "calibration_sources": calibration_sources, "cap_fraction_str": cap_str, } if (eta_d < Decimal(1) or f_d < Decimal(1)) and eta_d < Decimal("0.30"): return { "gate_id": gate_id, "status": "HOLD_RECOVERY_BELOW_0P30_QUALIFICATION_FLOOR", "empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INVALID_INPUT", "sampling_independence_provenance": sampling_independence_provenance, "calibration_sources": calibration_sources, "cap_fraction_str": cap_str, } if not isinstance(f_eligible_provenance, str) or f_eligible_provenance not in VALID_F_ELIGIBLE_PROVENANCE: return { "gate_id": gate_id, "status": "HOLD_MISSING_OR_INVALID_ELIGIBLE_PROVENANCE", "empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INVALID_INPUT", "sampling_independence_provenance": sampling_independence_provenance, "calibration_sources": calibration_sources, "cap_fraction_str": cap_str, } # Validate reported_u95_pct finiteness BEFORE arithmetic if supplied rep_pct_d: Decimal | None = None if reported_u95_pct is not None: rep_pct_d = _safe_exact_decimal(reported_u95_pct) if rep_pct_d is None: return { "gate_id": gate_id, "status": "HOLD_INVALID_REPORTED_U95_PCT", "reason": "NONFINITE_OR_MALFORMED_REPORTED_U95_PCT", "empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INVALID_INPUT", "sampling_independence_provenance": sampling_independence_provenance, "calibration_sources": calibration_sources, "cap_fraction_str": cap_str, } exact_joint_alpha_frac: Fraction | None = None with _clean_dec_ctx(80, ROUND_CEILING) as ctx: if f_eligible_provenance == "KNOWN_OR_CONSERVATIVELY_ESTABLISHED": a_obs_d = Decimal("0.05") if alpha_obs is None else _safe_exact_decimal(alpha_obs) a_eta_d = ( (Decimal("0.05") if eta_d < Decimal(1) else Decimal("0")) if alpha_eta_recovery is None else _safe_exact_decimal(alpha_eta_recovery) ) if alpha_f_eligible is not None: _af_chk = _safe_exact_decimal(alpha_f_eligible) if _af_chk is None or _af_chk != Decimal("0"): return { "gate_id": gate_id, "status": "HOLD_CONFLICTING_F_ELIGIBLE_PROVENANCE_AND_ALPHA", "reason": "NONZERO_OR_INVALID_ALPHA_F_ELIGIBLE_INCOMPATIBLE_WITH_KNOWN_OR_CONSERVATIVELY_ESTABLISHED_PROVENANCE", "empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INVALID_INPUT", "sampling_independence_provenance": sampling_independence_provenance, "calibration_sources": calibration_sources, "cap_fraction_str": cap_str, } a_f_d = Decimal("0") if ( a_obs_d is None or a_eta_d is None