cross-scenario-physics-code-transfer / code /_compute_perm_test.py
physics-code-transfer-bench's picture
Initial anonymous release for NeurIPS 2026 E&D submission
189f45b verified
"""Permutation test on the 5-vs-5 high/low PosDis band overlap (R2 ROI fix).
Tests: under random reassignment of cross-scenario accuracies to PosDis values,
how often do we see the observed flatness (top-5 mean - bot-5 mean) or larger
in absolute value? If the empirical |top5 - bot5| is similar to random, the
sufficiency claim is statistically defensible (no signal).
Also computes: probability of observing the observed band-overlap (range of top5
intersecting range of bot5) under random reshuffling.
"""
import numpy as np
from scipy import stats
# 24 configs from the paper
configs = [
# (name, posdis, cross192)
("disc_L2_V5", 0.20, 43.9),
("disc_L2_V10", 0.25, 41.7),
("disc_L3_V5", 0.13, 42.8),
("disc_L3_V10", 0.12, 45.6),
("disc_L4_V5", 0.10, 42.2),
("disc_L4_V10", 0.08, 45.0),
("disc_L5_V5", 0.07, 43.9),
("cont_dim2", 0.15, 54.4),
("cont_dim3", 0.15, 41.1),
("cont_dim5", 0.06, 43.9),
("cont_dim10", 0.04, 48.3),
("cont_dim20", 0.02, 55.0),
("disc_multi_L3_V5", 0.51, 46.1),
("disc_multi_L4_V10", 0.48, 50.6),
("cont_multi_dim3", 0.40, 55.0),
("disc_multi5_L2_V5", 0.82, 52.2),
("disc_multi5_L3_V5", 0.83, 46.1),
("disc_multi5_L4_V5", 0.70, 47.8),
("disc_multi5_L2_V10_e250", 0.70, 55.6),
("disc_multi5_L3_V10_e250", 0.81, 43.3),
("disc_multi5_L4_V10_e250", 0.70, 41.7),
("disc_multi5_L2_V5_e200", 0.83, 51.1),
("disc_multi5_L4_V5_e250", 0.91, 46.7),
("disc_multi_L5_V5_3cls", 0.73, 42.2),
]
posdis = np.array([c[1] for c in configs])
cross = np.array([c[2] for c in configs])
# Sort by PosDis
order = np.argsort(posdis)
top5_idx = order[-5:]
bot5_idx = order[:5]
top5_pd = posdis[top5_idx]; top5_cr = cross[top5_idx]
bot5_pd = posdis[bot5_idx]; bot5_cr = cross[bot5_idx]
print(f"Top-5 PosDis: {sorted(top5_pd.tolist())}, cross: {sorted(top5_cr.tolist())}")
print(f"Bot-5 PosDis: {sorted(bot5_pd.tolist())}, cross: {sorted(bot5_cr.tolist())}")
obs_diff_means = abs(top5_cr.mean() - bot5_cr.mean())
obs_top5_range = (top5_cr.min(), top5_cr.max())
obs_bot5_range = (bot5_cr.min(), bot5_cr.max())
obs_overlap = max(0, min(obs_top5_range[1], obs_bot5_range[1]) - max(obs_top5_range[0], obs_bot5_range[0]))
print(f"\nObserved |mean(top5) - mean(bot5)| = {obs_diff_means:.2f} pp")
print(f" top5 mean = {top5_cr.mean():.2f}, bot5 mean = {bot5_cr.mean():.2f}")
print(f" top5 range = [{obs_top5_range[0]:.1f}, {obs_top5_range[1]:.1f}]")
print(f" bot5 range = [{obs_bot5_range[0]:.1f}, {obs_bot5_range[1]:.1f}]")
print(f" observed band overlap = {obs_overlap:.2f} pp")
# Permutation: shuffle cross values, recompute top5 vs bot5 by FIXED PosDis ranks
n_perm = 100000
rng = np.random.default_rng(42)
diffs = []
overlaps = []
for _ in range(n_perm):
cross_perm = rng.permutation(cross)
t = cross_perm[top5_idx]; b = cross_perm[bot5_idx]
diffs.append(abs(t.mean() - b.mean()))
ov = max(0, min(t.max(), b.max()) - max(t.min(), b.min()))
overlaps.append(ov)
diffs = np.array(diffs); overlaps = np.array(overlaps)
p_diff = float(np.mean(diffs >= obs_diff_means))
p_overlap = float(np.mean(overlaps >= obs_overlap))
print(f"\nPermutation test (n_perm={n_perm}):")
print(f" p(|mean(top5) - mean(bot5)| >= observed {obs_diff_means:.2f}) = {p_diff:.3f}")
print(f" p(band overlap >= observed {obs_overlap:.2f}) = {p_overlap:.3f}")
print(f"\n null mean of |mean diff|: {diffs.mean():.2f} pp")
print(f" null 95th percentile of |mean diff|: {np.percentile(diffs, 95):.2f} pp")
# Same direction (low PosDis -> high cross): proper one-sided test
# Test: is top5 cross significantly LOWER than bot5 cross?
obs_signed_diff = top5_cr.mean() - bot5_cr.mean()
signed_diffs = []
for _ in range(n_perm):
cross_perm = rng.permutation(cross)
t = cross_perm[top5_idx]; b = cross_perm[bot5_idx]
signed_diffs.append(t.mean() - b.mean())
signed_diffs = np.array(signed_diffs)
p_lower = float(np.mean(signed_diffs <= obs_signed_diff))
print(f"\n observed signed mean(top5) - mean(bot5) = {obs_signed_diff:+.2f} pp")
print(f" p(top5 mean <= observed under null) = {p_lower:.3f}")
# k-robustness: same permutation test for k in {3..8} to address "5-vs-5 cherry pick" critique
print("\n=== k-robustness sweep: top-k vs bot-k (PosDis vs cross @N=192) ===")
print(f"{'k':>3s} | {'obs |diff|':>11s} | {'null mean':>10s} | {'null 95th':>10s} | {'p (two-sided)':>14s} | {'p (one-sided lower)':>20s}")
print("-" * 88)
k_results = []
for k in range(3, 9):
top_k = order[-k:]; bot_k = order[:k]
top_k_cr = cross[top_k]; bot_k_cr = cross[bot_k]
obs_abs = abs(top_k_cr.mean() - bot_k_cr.mean())
obs_signed = top_k_cr.mean() - bot_k_cr.mean()
null_abs = []; null_signed = []
for _ in range(n_perm):
cp = rng.permutation(cross)
null_abs.append(abs(cp[top_k].mean() - cp[bot_k].mean()))
null_signed.append(cp[top_k].mean() - cp[bot_k].mean())
null_abs = np.array(null_abs); null_signed = np.array(null_signed)
p_two = float(np.mean(null_abs >= obs_abs))
p_one_lower = float(np.mean(null_signed <= obs_signed))
k_results.append((k, obs_abs, null_abs.mean(), np.percentile(null_abs, 95), p_two, p_one_lower))
print(f"{k:>3d} | {obs_abs:>10.2f} | {null_abs.mean():>10.2f} | {np.percentile(null_abs, 95):>10.2f} | {p_two:>14.3f} | {p_one_lower:>20.3f}")
print("\nInterpretation: across all k in {3..8}, two-sided p > 0.5 (observed |diff| smaller than null mean)")
print("means top-k mean is no further from bot-k mean than random reshuffling produces.")