scripts/verify_iip0033_bilayer_and_resp1.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0033_bilayer_and_resp1.py · SHA-256: 45f1396930f3a6af…
s_min = (d_min_f ** 3) + (n - 3) * (y1 ** 3) + (y2 ** 3) + (d_max_f ** 3)
s_max = (d_min_f ** 3) + (y1 ** 3) + (n - 3) * (y2 ** 3) + (d_max_f ** 3)
return {
"sample_n": n,
"feasible": True,
"d_rms3_lb": float((s_min / float(n)) ** (1.0 / 3.0)),
"d_rms3_ub": float((s_max / float(n)) ** (1.0 / 3.0)),
}
# Case 4: N >= 21 with r05 == 0 (exact integer quantile indices: f05 == f95 == 0; 4 fixed anchors)
if r05 == 0:
s_min = i05 * (d_min_f ** 3) + (i95 - i05) * (p05_f ** 3) + (n - 1 - i95) * (p95_f ** 3) + (d_max_f ** 3)
s_max = (d_min_f ** 3) + i05 * (p05_f ** 3) + (i95 - i05) * (p95_f ** 3) + (n - 1 - i95) * (d_max_f ** 3)
return {
"sample_n": n,
"feasible": True,
"d_rms3_lb": float((s_min / float(n)) ** (1.0 / 3.0)),
"d_rms3_ub": float((s_max / float(n)) ** (1.0 / 3.0)),
}
# Case 5: N >= 22 with r05 > 0 (6 distinct anchors [0, i05, i05+1, i95, i95+1, n-1], 2 free variables (u, v) = (x[i05+1], x[i95]))
u_lo = p05_f
u_hi = min(p95_f, (p05_f - (1.0 - f05) * d_min_f) / f05)
v_lo = max(p05_f, (p95_f - f95 * d_max_f) / (1.0 - f95))
v_hi = p95_f
if u_lo > u_hi + tol or v_lo > v_hi + tol or u_lo > v_hi + tol:
return {"sample_n": n, "feasible": False, "d_rms3_lb": None, "d_rms3_ub": None}
u_hi = max(u_lo, u_hi)
v_lo = min(v_lo, v_hi)
w0_min, w1_min, w2_min, w3_min, w4_min, w5_min = (
i05, 1, i95 - (i05 + 1), 1, (n - 1) - (i95 + 1), 1
)
w0_max, w1_max, w2_max, w3_max, w4_max, w5_max = (
1, i05, 1, i95 - (i05 + 1), 1, (n - 1) - (i95 + 1)
)
def _y1(u_val: float) -> float:
return (p05_f - f05 * u_val) / (1.0 - f05)
def _y4(v_val: float) -> float:
return (p95_f - (1.0 - f95) * v_val) / f95
def _eval_min(u_val: float, v_val: float) -> float:
return (
w0_min * (d_min_f ** 3)
+ w1_min * (_y1(u_val) ** 3)
+ w2_min * (u_val ** 3)
+ w3_min * (v_val ** 3)
+ w4_min * (_y4(v_val) ** 3)
+ w5_min * (d_max_f ** 3)
)
def _eval_max(u_val: float, v_val: float) -> float:
return (
w0_max * (d_min_f ** 3)
+ w1_max * (_y1(u_val) ** 3)
+ w2_max * (u_val ** 3)
+ w3_max * (v_val ** 3)
+ w4_max * (_y4(v_val) ** 3)
+ w5_max * (d_max_f ** 3)
)
verts: List[Tuple[float, float]] = []
for u_cand, v_cand in (
(u_lo, v_lo),
(u_lo, v_hi),
(u_hi, v_hi),
(u_hi, v_lo),
(u_hi, u_hi),
(v_lo, v_lo),
(u_lo, u_lo),
(v_hi, v_hi),
):
if (
u_lo - tol <= u_cand <= u_hi + tol
and v_lo - tol <= v_cand <= v_hi + tol
and u_cand <= v_cand + tol
):
uc = min(max(u_cand, u_lo), u_hi)
vc = min(max(v_cand, v_lo), v_hi)
if uc > vc:
uc = vc = 0.5 * (uc + vc)
verts.append((uc, vc))
s_max = max(_eval_max(u_val, v_val) for u_val, v_val in verts)
rho_u = math.sqrt((w2_min * (1.0 - f05)) / (w1_min * f05))
u_star = p05_f / (f05 + (1.0 - f05) * rho_u)
rho_v = math.sqrt((w3_min * f95) / (w4_min * (1.0 - f95)))
v_star = p95_f / ((1.0 - f95) + f95 * rho_v)
u_c = min(max(u_star, u_lo), u_hi)
v_c = min(max(v_star, v_lo), v_hi)
min_cands: List[Tuple[float, float]] = list(verts)
if u_c <= v_c + tol:
uc_f, vc_f = u_c, v_c
if uc_f > vc_f:
uc_f = vc_f = 0.5 * (uc_f + vc_f)
min_cands.append((uc_f, vc_f))
for uc_e, vc_e in ((u_c, v_lo), (u_c, v_hi), (u_lo, v_c), (u_hi, v_c)):
if (
u_lo - tol <= uc_e <= u_hi + tol
and v_lo - tol <= vc_e <= v_hi + tol
and uc_e <= vc_e + tol
):
uc_cl = min(max(uc_e, u_lo), u_hi)
vc_cl = max(min(max(vc_e, v_lo), v_hi), uc_cl)
min_cands.append((uc_cl, vc_cl))
t_lo = max(u_lo, v_lo)
t_hi = min(u_hi, v_hi)
if t_lo <= t_hi + tol:
t_lo_c = min(t_lo, t_hi)
t_hi_c = max(t_lo, t_hi)
min_cands.append((t_lo_c, t_lo_c))
min_cands.append((t_hi_c, t_hi_c))
a1 = w1_min * f05 / ((1.0 - f05) ** 3)
a4 = w4_min * (1.0 - f95) / (f95 ** 3)
qa = (w2_min + w3_min) - a1 * (f05 ** 2) - a4 * ((1.0 - f95) ** 2)
qb = 2.0 * a1 * p05_f * f05 + 2.0 * a4 * p95_f * (1.0 - f95)
qc = -a1 * (p05_f ** 2) - a4 * (p95_f ** 2)
roots: List[float] = []
if abs(qa) <= 1e-15 * max(1.0, abs(qb), abs(qc)):
if abs(qb) > 1e-15:
roots.append(-qc / qb)
else:
disc = qb * qb - 4.0 * qa * qc
if disc >= 0.0:
sd = math.sqrt(disc)
q_term = -0.5 * (qb + math.copysign(sd, qb if qb != 0.0 else 1.0))
if abs(q_term) > 1e-30:
roots.append(q_term / qa)
roots.append(qc / q_term)
else:
roots.append(-qb / (2.0 * qa))
for rt in roots:
if t_lo_c - tol <= rt <= t_hi_c + tol:
rtc = min(max(rt, t_lo_c), t_hi_c)
min_cands.append((rtc, rtc))
s_min = min(_eval_min(u_val, v_val) for u_val, v_val in min_cands)
return {
"sample_n": n,
"feasible": True,
"d_rms3_lb": float((s_min / float(n)) ** (1.0 / 3.0)),
"d_rms3_ub": float((s_max / float(n)) ** (1.0 / 3.0)),
}
def _type7_quantile(vals: Sequence[float], p: float) -> float:
"""Exact pure-Python Hyndman-Fan Type 7 linear quantile (matches numpy.quantile(..., method='linear'))."""
s = sorted(float(x) for x in vals)
n = len(s)
assert n >= 1
if n == 1:
return s[0]
idx = p * (n - 1)
lo = int(math.floor(idx))
hi = int(math.ceil(idx))
frac = idx - lo
return s[lo] + frac * (s[hi] - s[lo])
def verify_type7_finite_sample_cubic_mean_bounds() -> Dict[str, Any]:
"""Verify exact joint-constraint finite-sample Hyndman-Fan Type-7 cubic-mean bounds over N = 2..500 including N=18 (17x10+1x300) and N=200 (58/65/65/68)."""
checked_cases = 0
for n in range(2, 501):
# Stress extreme right-skew (n-1 at 10, 1 at 300), left-skew (1 at 10, n-1 at 300),
# two-tail extreme, and smooth ramp samples across every finite N = 2..500
samples = [
[10.0] * (n - 1) + [300.0],
[10.0] + [300.0] * (n - 1),
[10.0] + [65.0] * max(0, n - 2) + [300.0],
[58.0 + 12.0 * (i / (n - 1)) for i in range(n)],
]
for arr in samples:
d_min = min(arr)
p05 = _type7_quantile(arr, 0.05)
p95 = _type7_quantile(arr, 0.95)
d_max = max(arr)
d_rms3 = (sum(x ** 3 for x in arr) / n) ** (1.0 / 3.0)
b = compute_type7_finite_sample_cubic_mean_bounds_iip0033(
d_min=d_min, p05=p05, p95=p95, d_max=d_max, sample_n=n
)
assert b["feasible"] is True
assert b["d_rms3_lb"] - 1e-9 <= d_rms3 <= b["d_rms3_ub"] + 1e-9, (
f"Finite-sample Type-7 bound failed at N={n}: {b['d_rms3_lb']} <= {d_rms3} <= {b['d_rms3_ub']}"
)
checked_cases += 1
# Explicit N = 18 counterexample (17x10 + 1x300): exact joint constraints force unique vector (lb == ub == 114.4954 um)
arr_18 = [10.0] * 17 + [300.0]
p05_18 = _type7_quantile(arr_18, 0.05)
p95_18 = _type7_quantile(arr_18, 0.95)
d_rms3_18 = (sum(x ** 3 for x in arr_18) / 18.0) ** (1.0 / 3.0)
b_18 = compute_type7_finite_sample_cubic_mean_bounds_iip0033(
d_min=10.0, p05=p05_18, p95=p95_18, d_max=300.0, sample_n=18
)
asymp_ub_18 = (0.05 * (p05_18 ** 3) + 0.90 * (p95_18 ** 3) + 0.05 * (300.0 ** 3)) ** (1.0 / 3.0)
assert abs(p05_18 - 10.0) < 1e-12 and abs(p95_18 - 53.5) < 1e-12
assert d_rms3_18 > asymp_ub_18 # proves why N-aware bound is required for small finite N
assert b_18["feasible"] is True
assert abs(b_18["d_rms3_lb"] - d_rms3_18) < 1e-9 and abs(b_18["d_rms3_ub"] - d_rms3_18) < 1e-9
# Explicit N = 200 pinned-extremum counterexample (min=p05=p95=58, max=70, claimed d_rms3=58.001 < exact lb=58.0732)
b_200_pin = compute_type7_finite_sample_cubic_mean_bounds_iip0033(
d_min=58.0, p05=58.0, p95=58.0, d_max=70.0, sample_n=200
)
exact_200_lb = ((199.0 * (58.0 ** 3) + 1.0 * (70.0 ** 3)) / 200.0) ** (1.0 / 3.0)
assert b_200_pin["feasible"] is True
assert abs(b_200_pin["d_rms3_lb"] - exact_200_lb) < 1e-9
assert b_200_pin["d_rms3_lb"] > 58.001
# Explicit N = 200 joint-constraint counterexample (min=58, p05=p95=65, max=68 -> x[9..190]=65, exact lb=64.7323, ub=65.1098)
b_200_joint = compute_type7_finite_sample_cubic_mean_bounds_iip0033(
d_min=58.0, p05=65.0, p95=65.0, d_max=68.0, sample_n=200
)
exact_200_joint_lb = ((9.0 * (58.0 ** 3) + 190.0 * (65.0 ** 3) + (68.0 ** 3)) / 200.0) ** (1.0 / 3.0)
exact_200_joint_ub = (((58.0 ** 3) + 190.0 * (65.0 ** 3) + 9.0 * (68.0 ** 3)) / 200.0) ** (1.0 / 3.0)
assert b_200_joint["feasible"] is True
assert abs(b_200_joint["d_rms3_lb"] - exact_200_joint_lb) < 1e-9
assert abs(b_200_joint["d_rms3_ub"] - exact_200_joint_ub) < 1e-9
assert 64.71 < b_200_joint["d_rms3_lb"] - 1e-9 and b_200_joint["d_rms3_lb"] <= 64.74 <= b_200_joint["d_rms3_ub"]
return {
"n_range_verified": [2, 500],
"stress_samples_verified_total": checked_cases,
"all_finite_sample_bounds_hold": True,
"n18_counterexample_17x10_1x300": {
"sample_n": 18,
"d_min_um": 10.0,
"type7_p05_um": round(p05_18, 4),
"type7_p95_um": round(p95_18, 4),
"d_max_um": 300.0,
"true_d_rms3_um": round(d_rms3_18, 4),
"asymptotic_0p05_ub_um": round(asymp_ub_18, 4),
"exact_finite_sample_lb_um": round(b_18["d_rms3_lb"], 4),
"exact_finite_sample_ub_um": round(b_18["d_rms3_ub"], 4),
"finite_sample_bound_holds": True,
},
"n200_pinned_extremum_counterexample_58_58_58_70": {
"sample_n": 200,
"d_min_um": 58.0,
"type7_p05_um": 58.0,
"type7_p95_um": 58.0,
"d_max_um": 70.0,
"claimed_impossible_d_rms3_um": 58.001,
"exact_pinned_extremum_lb_um": round(b_200_pin["d_rms3_lb"], 4),
"rejected_by_pinned_extremum_lb": bool(58.001 < b_200_pin["d_rms3_lb"] - 1e-9),