scripts/verify_iip0035_d1b_auto_and_portfolio.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0035_d1b_auto_and_portfolio.py · SHA-256: fedb990aeaedd116…
or not (Decimal(0) < a_obs_d <= Decimal("0.05"))
or not (Decimal(0) <= a_eta_d <= Decimal("0.10"))
):
return {
"gate_id": gate_id,
"status": "HOLD_INSUFFICIENT_BONFERRONI_ALPHA_ALLOCATION",
"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) and a_eta_d < Decimal("0.05"):
return {
"gate_id": gate_id,
"status": "HOLD_INSUFFICIENT_BONFERRONI_ALPHA_ALLOCATION",
"reason": "ETA_RECOVERY_L95_REQUIRES_ALPHA_ETA_AT_LEAST_0P05",
"empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INVALID_INPUT",
"sampling_independence_provenance": sampling_independence_provenance,
"calibration_sources": calibration_sources,
"cap_fraction_str": cap_str,
}
else:
a_obs_d = _safe_exact_decimal(alpha_obs)
a_f_d = _safe_exact_decimal(alpha_f_eligible)
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 (
a_obs_d is None
or a_f_d is None
or a_eta_d is None
or not (Decimal(0) < a_obs_d <= Decimal("0.05"))
or not (Decimal(0) < a_f_d < Decimal("0.10"))
or not (Decimal(0) <= a_eta_d <= Decimal("0.10"))
):
return {
"gate_id": gate_id,
"status": "HOLD_INSUFFICIENT_BONFERRONI_ALPHA_ALLOCATION",
"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) and a_eta_d < Decimal("0.05"):
return {
"gate_id": gate_id,
"status": "HOLD_INSUFFICIENT_BONFERRONI_ALPHA_ALLOCATION",
"reason": "ETA_RECOVERY_L95_REQUIRES_ALPHA_ETA_AT_LEAST_0P05",
"empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INVALID_INPUT",
"sampling_independence_provenance": sampling_independence_provenance,
"calibration_sources": calibration_sources,
"cap_fraction_str": cap_str,
}
# Never award a calibrated bound or substitute unit default 1.0 when caller allocates alpha or declares an estimated bound!
if a_eta_d > Decimal(0) and (
eta_d >= Decimal(1)
or (isinstance(calibration_sources, dict) and calibration_sources.get("eta_recovery_l95") == "default")
):
return {
"gate_id": gate_id,
"status": "HOLD_MISSING_OR_UNIT_ETA_RECOVERY_FOR_ALPHA_ALLOCATION",
"reason": "ALPHA_ETA_RECOVERY_ALLOCATION_REQUIRES_EXPLICIT_ESTIMATE_LT_1",
"empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INVALID_INPUT",
"sampling_independence_provenance": sampling_independence_provenance,
"calibration_sources": calibration_sources,
"cap_fraction_str": cap_str,
}
if (a_f_d > Decimal(0) or f_eligible_provenance == "INDEPENDENTLY_ESTIMATED_WITH_ALPHA_ALLOCATION") and (
f_d >= Decimal(1)
or (isinstance(calibration_sources, dict) and calibration_sources.get("f_eligible") == "default")
):
return {
"gate_id": gate_id,
"status": "HOLD_MISSING_OR_UNIT_F_ELIGIBLE_FOR_ALPHA_ALLOCATION",
"reason": "ESTIMATED_F_ELIGIBLE_PROVENANCE_OR_ALPHA_REQUIRES_EXPLICIT_ESTIMATE_LT_1",
"empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INVALID_INPUT",
"sampling_independence_provenance": sampling_independence_provenance,
"calibration_sources": calibration_sources,
"cap_fraction_str": cap_str,
}
# Directed interval + resource-bounded exact aligned-integer Bonferroni sum check (zero false-reject on exact 0.10!)
with _clean_dec_ctx(80, ROUND_FLOOR):
_alpha_sum_lower = a_obs_d + a_eta_d + a_f_d
with _clean_dec_ctx(80, ROUND_CEILING):
_alpha_sum_upper = a_obs_d + a_eta_d + a_f_d
if _alpha_sum_lower > Decimal("0.10"):
return {
"gate_id": gate_id,
"status": "HOLD_INSUFFICIENT_BONFERRONI_ALPHA_ALLOCATION",
"reason": "BONFERRONI_ALPHA_SUM_EXCEEDS_0P10_FOR_90PCT_JOINT_COVERAGE",
"empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INVALID_INPUT",
"sampling_independence_provenance": sampling_independence_provenance,
"calibration_sources": calibration_sources,
"cap_fraction_str": cap_str,
}
if _alpha_sum_upper > Decimal("0.10"):
# Bracket straddles 0.10: resolve via exact bounded Fraction sum before deciding
_alpha_bits = max(
_dec_frac_bit_estimate(a_obs_d),
_dec_frac_bit_estimate(a_eta_d),
_dec_frac_bit_estimate(a_f_d),
)
if _alpha_bits <= 40_000:
exact_joint_alpha_frac = Fraction(a_obs_d) + Fraction(a_eta_d) + Fraction(a_f_d)
if exact_joint_alpha_frac > Fraction(1, 10):
return {
"gate_id": gate_id,
"status": "HOLD_INSUFFICIENT_BONFERRONI_ALPHA_ALLOCATION",
"reason": "BONFERRONI_ALPHA_SUM_EXCEEDS_0P10_FOR_90PCT_JOINT_COVERAGE",
"empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INVALID_INPUT",
"sampling_independence_provenance": sampling_independence_provenance,
"calibration_sources": calibration_sources,
"cap_fraction_str": cap_str,
}
else:
return {
"gate_id": gate_id,
"status": "HOLD_BONFERRONI_ALPHA_SUM_UNRESOLVED_WITHIN_BUDGET",
"reason": "BONFERRONI_ALPHA_SUM_STRADDLES_0P10_BEYOND_EXACT_BUDGET",
"empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INVALID_INPUT",
"sampling_independence_provenance": sampling_independence_provenance,
"calibration_sources": calibration_sources,
"cap_fraction_str": cap_str,
}
# 4. Exact 80-digit sound outward Decimal upper & lower bounds (fresh trap-free Context; paired Taylor -expm1 for tiny x)
with _clean_dec_ctx(80, ROUND_FLOOR) as ctx:
eff_lower = f_d * eta_d
if eff_lower <= Decimal(0):
return {
"gate_id": gate_id,
"status": "HOLD_UNDERFLOW_OR_INVALID_DETECTION_FACTOR",
"empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_INVALID_INPUT",
"sampling_independence_provenance": sampling_independence_provenance,
"calibration_sources": calibration_sources,
"cap_fraction_str": cap_str,
}
ctx.rounding = ROUND_CEILING
eff_upper = f_d * eta_d
ctx.rounding = ROUND_FLOOR
ln_alpha_lo = a_obs_d.ln().next_minus()
ctx.rounding = ROUND_CEILING
ln_alpha_hi = a_obs_d.ln().next_plus()
n_d = Decimal(n_raw)
ctx.rounding = ROUND_FLOOR
x_lo = (-ln_alpha_hi) / n_d
ctx.rounding = ROUND_CEILING
x_hi = (-ln_alpha_lo) / n_d
p_raw_lower, p_raw_upper = _sound_one_minus_exp_neg(x_lo, x_hi, 80)
ctx.rounding = ROUND_CEILING
u95_upper = p_raw_upper / eff_lower
if u95_upper > Decimal(1):
u95_upper = Decimal(1)
ctx.rounding = ROUND_FLOOR
u95_lower = p_raw_lower / eff_upper
if u95_lower > Decimal(1):
u95_lower = Decimal(1)
u95_lower_pct = u95_lower * Decimal(100)
u95_math_lower_pct_str = format(u95_lower_pct, "e") if (u95_lower_pct > Decimal(0) and u95_lower_pct < Decimal("1e-6")) else format(u95_lower_pct, "f")
ctx.rounding = ROUND_CEILING
u95_upper_pct_exact = u95_upper * Decimal(100)
u95_math_upper_pct_exact_str = format(u95_upper_pct_exact, "e") if (u95_upper_pct_exact > Decimal(0) and u95_upper_pct_exact < Decimal("1e-6")) else format(u95_upper_pct_exact, "f")
# Sound upward-rounded (ROUND_CEILING) serialization so exported strings NEVER round below u95_upper
if u95_upper == Decimal(0):
u95_str = "0.0000000000000000e+00"
else:
exp_adj = u95_upper.adjusted()
mant_ceil = u95_upper.scaleb(-exp_adj).quantize(Decimal("1e-16"), rounding=ROUND_CEILING)
if mant_ceil >= Decimal(10):
mant_ceil = (mant_ceil / Decimal(10)).quantize(Decimal("1e-16"), rounding=ROUND_CEILING)
exp_adj += 1
u95_str = f"{mant_ceil:.16f}e{exp_adj:+03d}"
u95_pct_ceil = (u95_upper * Decimal(100)).quantize(Decimal("0.00000001"), rounding=ROUND_CEILING)
u95_pct_str = f"{u95_pct_ceil:.8f}"
cap_cmp = _zero_event_u95_compare(
a_obs_d, n_raw, f_d, eta_d, cap_d, u95_lower=u95_lower, u95_upper=u95_upper
)
passed = (cap_cmp is True)
rep_meets_exact: bool | None = None
if rep_pct_d is not None:
if rep_pct_d <= Decimal(0):
rep_meets_exact = False
elif rep_pct_d < u95_lower_pct:
# Compare in percent units FIRST so extreme subnormal exponents like MIN_ETINY never throw!
rep_meets_exact = False
elif u95_upper_pct_exact <= rep_pct_d:
rep_meets_exact = True
else:
rep_frac_d = _exact_pct_to_frac(rep_pct_d)
if rep_frac_d is None:
rep_meets_exact = None
else:
rep_meets_exact = _zero_event_u95_compare(
a_obs_d,
n_raw,
f_d,
eta_d,
rep_frac_d,
u95_lower=u95_lower,
u95_upper=u95_upper,
)
has_bonferroni_alpha = (a_eta_d > Decimal(0)) or (a_f_d > Decimal(0))
has_nonunit_factor = (eta_d < Decimal(1)) or (f_d < Decimal(1))
_cal_src = calibration_sources if isinstance(calibration_sources, dict) else {}
default_cal_assumptions: List[str] = []
if eta_d == Decimal(1):
default_cal_assumptions.append(
"DEFAULT_UNIT_RECOVERY_ASSUMED(eta=1.0)"
if _cal_src.get("eta_recovery_l95", "default") == "default"
else "UNIT_RECOVERY_ASSUMED(eta=1.0)"
)
if f_d == Decimal(1):
default_cal_assumptions.append(
"DEFAULT_UNIT_ELIGIBILITY_ASSUMED(f_eligible=1.0)"
if _cal_src.get("f_eligible", "default") == "default"
else "UNIT_ELIGIBILITY_ASSUMED(f_eligible=1.0)"
)
elif _cal_src.get("f_eligible_provenance") == "default" and a_f_d == Decimal(0):
default_cal_assumptions.append("DEFAULT_KNOWN_ELIGIBILITY_PROVENANCE_ASSUMED")
# Directed 80-digit sound Bonferroni coverage bound (exact when resolved via Fraction, else directed ROUND_CEILING)
with _clean_dec_ctx(80, ROUND_CEILING) as ctx_cov:
if exact_joint_alpha_frac is not None:
joint_alpha_upper = Decimal(exact_joint_alpha_frac.numerator) / Decimal(exact_joint_alpha_frac.denominator)
else:
joint_alpha_upper = a_obs_d + a_eta_d + a_f_d
ctx_cov.rounding = ROUND_FLOOR
cov_pct_lower = (Decimal(1) - joint_alpha_upper) * Decimal(100)
cov_floor = cov_pct_lower.quantize(Decimal("0.01"), rounding=ROUND_FLOOR)
ctx_cov.rounding = ROUND_CEILING
ja_ceil_6 = joint_alpha_upper.quantize(Decimal("0.000001"), rounding=ROUND_CEILING)
with _clean_dec_ctx(80, ROUND_HALF_EVEN):
joint_alpha_str = format(joint_alpha_upper, ".6g") if ja_ceil_6 == joint_alpha_upper else f"{ja_ceil_6:.6f}"
alpha_obs_fmt = format(a_obs_d, ".6g")
eff_det_fmt = format(eff_lower, ".12g")
if has_bonferroni_alpha:
cov_regime = f"BONFERRONI_JOINT_COVERAGE_GE_{cov_floor:.2f}PCT_CALIBRATED"
cov_cat = "CALIBRATED"
elif has_nonunit_factor:
cov_regime = (
f"OBSERVATION_LEVEL_GE_{cov_floor:.2f}PCT_PARTIAL_FACTOR_WITH_DEFAULT_ASSUMPTIONS"
if default_cal_assumptions
else f"OBSERVATION_LEVEL_GE_{cov_floor:.2f}PCT_KNOWN_FACTORS_OBSERVATION_ALPHA_ONLY"
)
cov_cat = "PARTIAL_FACTOR_WITH_DEFAULTS" if default_cal_assumptions else "KNOWN_FACTORS_NO_BONFERRONI"