import json import math from pathlib import Path import numpy as np SEED = 46 rng = np.random.default_rng(SEED) def mixture(a, lambdas, weights): a = np.asarray(a, dtype=float) lam = np.asarray(lambdas, dtype=float) w = np.asarray(weights, dtype=float) terms = w * np.exp(-np.outer(np.atleast_1d(a), lam)) g = terms.sum(axis=1) pi = terms / g[:, None] m = pi @ lam return g, m, pi def math_verification(): # Three predictions from the mechanism: # P1: finite-difference m'(a) equals - tilted variance. # P2: m(0)=E[Lambda], and m(a) approaches min(lambda) at large pressure. # P3: q'(a)/q(a)=-m(a), i.e. relative suppression per pressure is the hazard. lam = np.array([0.25, 1.0, 4.0, 16.0]) w = np.ones(4) / 4 grid = np.linspace(0, 20, 401) g, m, pi = mixture(grid, lam, w) var = (pi @ (lam ** 2)) - m**2 # Small centered differences test the claimed m'(a)=-Var_a(lambda). hdiff = 1e-5 _, mp_fd, _ = mixture(grid + hdiff, lam, w) _, mm_fd, _ = mixture(np.maximum(grid - hdiff, 0), lam, w) dm_fd = (mp_fd - mm_fd) / (2 * hdiff) interior = grid > hdiff p1_err = float(np.max(np.abs(dm_fd[interior] + var[interior]))) p1_scale = float(np.max(np.abs(var[interior]))) mean0 = float(m[0]) min_lam = float(lam.min()) large_pressure = float(m[-1]) p2_initial_error = abs(mean0 - float(w @ lam)) # predicted crossover to within 1% of the least susceptible component; # solve by a sweep and compare with a direct pairwise bound estimate. target = min_lam * 1.01 observed_idx = np.where(m <= target)[0] observed_cross = float(grid[observed_idx[0]]) if len(observed_idx) else float("inf") # For this mixture, at large a the next component ratio is exp(-(1-.25)a); # a conservative 1% dominance estimate uses equal weights and ratio <= .01. # Quantitative prediction obtained directly from the asymptotic target: # solve m(a)=1.01*min(lambda), using monotonicity of m. lo, hi = 0.0, 100.0 for _ in range(80): mid = (lo + hi) / 2 if mixture(np.array([mid]), lam, w)[1][0] <= target: hi = mid else: lo = mid predicted_cross = hi # P3 across several pressure values, with centered finite differences. h = 1e-4 gp, mp, _ = mixture(grid + h, lam, w) gm, mm, _ = mixture(np.maximum(grid - h, 0), lam, w) # avoid boundary and use derivative of log q valid = grid > 0 log_deriv = (np.log(gp) - np.log(gm)) / (2*h) p3_err = float(np.max(np.abs(log_deriv[valid] + m[valid]))) # Parameter sweep confirms stronger heterogeneity gives larger early curvature: sweep = [] for high in [1.0, 2.0, 4.0, 8.0, 16.0, 32.0]: ll = np.array([0.25, high]) ww = np.array([.5, .5]) _, mm, pp = mixture(np.array([0.0, 0.5]), ll, ww) vv0 = float(pp[0] @ (ll**2) - mm[0]**2) vv05 = float(pp[1] @ (ll**2) - mm[1]**2) sweep.append({"high_lambda": high, "m0": float(mm[0]), "curvature_at_0": vv0, "curvature_at_0.5": vv05}) return { "p1_derivative_variance_max_abs_error": p1_err, "p1_max_variance_scale": p1_scale, "p2_m_at_zero": mean0, "p2_weighted_mean_prediction": float(w @ lam), "p2_initial_abs_error": p2_initial_error, "p2_min_lambda": min_lam, "p2_m_at_pressure_20": large_pressure, "p2_observed_within_1pct_pressure": observed_cross, "p2_pairwise_dominance_prediction": predicted_cross, "p3_log_q_derivative_hazard_max_abs_error": p3_err, "heterogeneity_sweep": sweep, } def softmax(x): y = x - x.max(axis=1, keepdims=True) e = np.exp(y) return e / e.sum(axis=1, keepdims=True) def routing_trial(kind, rounds=100, B=512, E=8, beta=.90, alpha=4.0): # Fixed token preferences plus a deliberately collapsed expert bias. local_rng = np.random.default_rng(SEED) logits = local_rng.normal(0, .65, size=(B, E)) logits[:, 0] += 2.3 logits[:, 1] += 1.0 p = np.zeros(E) loads, cvs, overflows, entropies = [], [], [], [] lam = np.array([.25, 1., 4., 16.]) w = np.ones(4) / 4 for _ in range(rounds): u = softmax(logits).mean(axis=0) p = beta*p + (1-beta)*u if kind == "mixture": q, _, _ = mixture(p, lam, w) adjusted = logits + alpha*np.log(q + 1e-12)[None, :] elif kind == "single": adjusted = logits + alpha*np.log(np.exp(-2.0*p))[None, :] elif kind == "linear": adjusted = logits - alpha*p[None, :] else: adjusted = logits chosen = np.argmax(adjusted, axis=1) count = np.bincount(chosen, minlength=E) cap = int(math.ceil(1.10*B/E)) loads.append(count) cvs.append(float(count.std()/(count.mean()+1e-12))) overflows.append(float(np.maximum(count-cap, 0).sum()/B)) probs = softmax(adjusted) entropies.append(float((-probs*np.log(probs+1e-12)).sum(axis=1).mean())) loads = np.asarray(loads) return { "mean_cv_last_50": float(np.mean(cvs[-50:])), "mean_overflow_last_50": float(np.mean(overflows[-50:])), "mean_router_entropy_last_50": float(np.mean(entropies[-50:])), "final_load": loads[-1].tolist(), "max_load_last_50": float(loads[-50:].max()), } def main(): math_results = math_verification() routing = {k: routing_trial(k) for k in ["baseline", "mixture", "single", "linear"]} result = {"seed": SEED, "math_verification": math_results, "routing": routing} Path("results.json").write_text(json.dumps(result, indent=2)) print(json.dumps(result, indent=2)) if __name__ == "__main__": main()