scripts/verify_iip0042_master_multilayer_portfolio_crosswalk.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0042_master_multilayer_portfolio_crosswalk.py · SHA-256: 16964a158458b5cf…
d2[16] += w * u_d2
else:
for m4 in range(16):
d2[m4] += w * d2_allo4[m4] * (1.0 - u_d2)
d2[m4 | 16] += w * d2_allo4[m4] * u_d2
c2_eff = c2_univ * (1.0 - biv2[(0, 0)])
return d2, c2_eff
def build_h3_dist_5arm(
pa3: float, pb3: float, px3: float, py3: float, pd3: float,
*, c3_a: float, c3_bx: float, c3_y: float, c3_d: float,
phi_yx: float, phi_c: float,
) -> tuple[list[float], float]:
"""Exact nested variance-preserving 5-arm H3 distribution (uniform OR drug-free Design A firewall)."""
if abs(c3_a - c3_bx) <= 1e-12 and abs(c3_d - c3_y) <= 1e-12 and c3_bx <= c3_y + 1e-12:
d3_inc = _frechet_joint([pa3 / c3_bx, pb3 / c3_bx, px3 / c3_bx], 0.0)
r3 = c3_bx / c3_y
u_y3 = py3 / c3_y
d3_both = _frechet_couple(d3_inc, u_y3, phi_yx, lambda mi: bool(mi & 4))
d3_allo4 = [0.0] * 16
for k in range(16):
d3_allo4[k] += r3 * d3_both[k]
d3_allo4[0] += (1.0 - r3) * (1.0 - u_y3)
d3_allo4[8] += (1.0 - r3) * u_y3
d3_5cond = _frechet_couple(d3_allo4, pd3 / c3_y, phi_c, lambda m4: m4 != 0)
d3 = [0.0] * 32
d3[0] += 1.0 - c3_y
for k in range(32):
d3[k] += c3_y * d3_5cond[k]
return d3, c3_y
if c3_a < c3_bx and abs(c3_bx - c3_y) <= 1e-12 and abs(c3_d - c3_y) <= 1e-12:
c3_reset = c3_y
r_a = c3_a / c3_reset
u_a = pa3 / c3_a
u_b = pb3 / c3_reset
u_x = px3 / c3_reset
u_y = py3 / c3_reset
u_d = pd3 / c3_reset
d_inc_all = _frechet_joint([u_a, u_b, u_x], 0.0)
d_allo4_all = _frechet_couple(d_inc_all, u_y, phi_yx, lambda mi: bool(mi & 4))
d_5_all = _frechet_couple(d_allo4_all, u_d, phi_c, lambda m4: m4 != 0)
d_inc_no_a = _frechet_joint([0.0, u_b, u_x], 0.0)
d_allo4_no_a = _frechet_couple(d_inc_no_a, u_y, phi_yx, lambda mi: bool(mi & 4))
d_5_no_a = _frechet_couple(d_allo4_no_a, u_d, phi_c, lambda m4: m4 != 0)
d3 = [0.0] * 32
d3[0] += 1.0 - c3_reset
w_all = c3_reset * r_a
w_no_a = c3_reset * (1.0 - r_a)
for k in range(32):
d3[k] += w_all * d_5_all[k] + w_no_a * d_5_no_a[k]
return d3, c3_reset
raise ValueError(f"Unsupported H3 ceiling configuration: {(c3_a, c3_bx, c3_y, c3_d)}")
def build_h4_dist_5arm(
pa4: float, pb4: float, px4: float, py4: float, pd4: float,
*, h4_topology: str, c4_univ: float,
c4_pouch: float = 0.85, c4_r3p: float = 0.85, c4_r3b: float = 0.85,
phi_c: float,
) -> tuple[list[float], float, tuple[str, ...]]:
u_a = pa4 / c4_pouch
u_b = pb4 / c4_pouch
u_x = px4 / c4_pouch
d_ax = _frechet_joint([u_a, u_x], 1.0)
d4_inc = [0.0] * 8
for m_ax in range(4):
bit_a = m_ax & 1
bit_x = (m_ax >> 1) & 1
base_m = bit_a | (bit_x << 2)
d4_inc[base_m] += d_ax[m_ax] * (1.0 - u_b)
d4_inc[base_m | 2] += d_ax[m_ax] * u_b
if h4_topology == "TWO_PARADIGM_WU3":
biv4 = _bivariate(c4_pouch / c4_univ, c4_r3p / c4_univ, phi_c)
d4_r3 = _frechet_joint([py4 / c4_r3p, pd4 / c4_r3p], 1.0)
d4 = [0.0] * 32
d4[0] += 1.0 - c4_univ
for (s0, s1), wp in biv4.items():
w = c4_univ * wp
if s0 == 0 and s1 == 0:
d4[0] += w
elif s0 == 1 and s1 == 0:
for m3 in range(8):
d4[m3] += w * d4_inc[m3]
elif s0 == 0 and s1 == 1:
for mr in range(4):
m5 = ((mr & 1) << 3) | (((mr >> 1) & 1) << 4)
d4[m5] += w * d4_r3[mr]
else:
for m3 in range(8):
for mr in range(4):
m5 = m3 | ((mr & 1) << 3) | (((mr >> 1) & 1) << 4)
d4[m5] += w * d4_inc[m3] * d4_r3[mr]
c4_eff = c4_univ * (1.0 - biv4[(0, 0)])
return d4, c4_eff, ("R1", "R2", "R1", "R3P", "R3P")
if h4_topology == "THREE_PARADIGM_D1_ON_R3B":
if pd4 > c4_r3b + 1e-12:
raise ValueError(f"pd4={pd4} exceeds c4_r3b={c4_r3b}")
u_y4 = py4 / c4_r3p
u_d4 = pd4 / c4_r3b
triv4 = _frechet_joint([c4_pouch / c4_univ, c4_r3p / c4_univ, c4_r3b / c4_univ], phi_c)
d4 = [0.0] * 32
d4[0] += 1.0 - c4_univ
for s_mask in range(8):
w = c4_univ * triv4[s_mask]
if w == 0.0:
continue
inc_dist = d4_inc if (s_mask & 1) else [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
y_dist = [(1.0 - u_y4), u_y4] if (s_mask & 2) else [1.0, 0.0]
b_dist = [(1.0 - u_d4), u_d4] if (s_mask & 4) else [1.0, 0.0]
for m3, w3 in enumerate(inc_dist):
if w3 == 0.0:
continue
for my, wy in enumerate(y_dist):
if wy == 0.0:
continue
for mb, wb in enumerate(b_dist):
if wb == 0.0:
continue
d4[m3 | (my << 3) | (mb << 4)] += w * w3 * wy * wb
c4_eff = c4_univ * (1.0 - triv4[0])
return d4, c4_eff, ("R1", "R2", "R1", "R3P", "R3B")
raise ValueError(f"Unknown h4_topology={h4_topology!r}")
def evaluate_5arm_exact_2pow20(
*,
layer_id: str,
label: str,
epistemic_tier: str,
drug_status: str,
arms: list[tuple[float, float, float, float]],
dists: list[list[float]],
eff_ceils: list[float],
routes: tuple[str, ...],
compat_counterfactual: dict[str, frozenset[str]] | None,
compat_hypo_transfer: dict[str, frozenset[str]] | None,
cell_classes: tuple[str, ...],
l0_locked: float,
l0_mod100: float,
l1_locked: float,
l1_mod100: float,
check_4096_incumbent_parity: bool = False,
return_state_laws: bool = False,
) -> dict[str, Any]:
n_states = 32
d_inc3 = []
for h in range(4):
dh = dists[h]
if len(dh) != n_states or min(dh) < -1e-12 or abs(sum(dh) - 1.0) > 1e-12:
raise RuntimeError(f"Invalid probability distribution in {layer_id} H{h+1}")
for i in range(5):
rec = sum(dh[m] for m in range(n_states) if (m & (1 << i)))
if abs(rec - arms[i][h]) > 1e-11:
raise RuntimeError(
f"Marginal drift {layer_id} arm {ARM_NAMES[i]} H{h+1}: {rec:.12f} vs {arms[i][h]:.12f}"
)
di = [0.0] * 8
for m in range(n_states):
di[m & 7] += dh[m]
d_inc3.append(di)
max_4096_err = None
if check_4096_incumbent_parity:
canon = _live_canon_inc()
max_4096_err = 0.0
for m0 in range(8):
p0a, p0b = d_inc3[0][m0], canon[0][m0]
for m1 in range(8):
p1a, p1b = p0a * d_inc3[1][m1], p0b * canon[1][m1]
for m2 in range(8):
p2a, p2b = p1a * d_inc3[2][m2], p1b * canon[2][m2]
for m3 in range(8):
err = abs(p2a * d_inc3[3][m3] - p2b * canon[3][m3])
if err > max_4096_err:
max_4096_err = err
if max_4096_err > 1e-12:
raise RuntimeError(f"4,096-state parity failed on {layer_id}: {max_4096_err}")
v = [0.0] * n_states
v[n_states - 1] = 1.0
v_h123: list[float] = []
for h in range(4):
dh = dists[h]
vn = [0.0] * n_states
for s, vs in enumerate(v):
if vs == 0.0:
continue
for m, w in enumerate(dh):
if w != 0.0:
vn[s & m] += vs * w
v = vn
if h == 2:
v_h123 = list(v)
# Strict live-registered mask (ALWAYS masks unregistered R3B!)
strict_mask = 0
has_unreg = False
for i in range(5):
if routes[i] in compat_strict_live[cell_classes[i]]:
strict_mask |= (1 << i)
else:
has_unreg = True
strict_locked = sum(v[s] for s in range(1, n_states) if (s & strict_mask) != 0)
dh4 = dists[3]
def _calc_mod(cmap: dict[str, frozenset[str]]) -> float:
acc = 0.0
for m_cell in range(1, n_states):
wc = v_h123[m_cell]
if wc == 0.0:
continue
allowed_r: set[str] = set()
for i in range(5):
if m_cell & (1 << i):
allowed_r |= cmap[cell_classes[i]]
for m_h4 in range(1, n_states):
wh = dh4[m_h4]
if wh != 0.0 and any(routes[j] in allowed_r for j in range(5) if (m_h4 & (1 << j))):
acc += wc * wh
return acc
strict_mod100 = _calc_mod(compat_strict_live)
strict_mod90 = 0.90 * strict_mod100 + 0.10 * strict_locked
if has_unreg:
if compat_counterfactual is None:
raise RuntimeError(f"Unregistered route in {layer_id} without explicit counterfactual map")
cf_mask = 0
for i in range(5):
if routes[i] in compat_counterfactual[cell_classes[i]]:
cf_mask |= (1 << i)
cf_locked = sum(v[s] for s in range(1, n_states) if (s & cf_mask) != 0)
cf_mod100 = _calc_mod(compat_counterfactual)
cf_mod90 = 0.90 * cf_mod100 + 0.10 * cf_locked
cf_mod100_hypo = _calc_mod(compat_hypo_transfer) if compat_hypo_transfer is not None else cf_mod100
else:
cf_locked = strict_locked
cf_mod100 = strict_mod100
cf_mod90 = strict_mod90
cf_mod100_hypo = strict_mod100
ceil = math.prod(eff_ceils)