Moment-Sharp Spectral-Norm Control / stage2_bench.py
Mechanism confirmed, baseline not beaten
1import json
2import sys
3import numpy as np
4from moment_sharp_bench import u2_from_moments
5
6
7def math_check(seed=569):
8 rng = np.random.default_rng(seed)
9 violations, gaps = [], []
10 for d in (3, 5, 16, 64):
11 for _ in range(100):
12 x = np.exp(rng.normal(size=d))
13 u = u2_from_moments(x.sum(), (x * x).sum(), d)
14 violations.append(max(0.0, float(x.max() - u)))
15 gaps.append(float(u - x.max()))
16 clustered = np.array([9.0, 2.0, 2.0, 2.0, 2.0])
17 exact_error = abs(u2_from_moments(clustered.sum(), (clustered ** 2).sum(), 5) - 9.0)
18 return {
19 "max_upper_bound_violation": float(max(violations)),
20 "mean_bound_gap_squared": float(np.mean(gaps)),
21 "clustered_recovery_abs_error": float(exact_error),
22 "passed": max(violations) < 1e-9 and exact_error < 1e-9,
23 }
24
25
26def harness_check():
27 result = {"available": False, "error": None}
28 try:
29 sys.path.insert(0, "/home/maxwelhelp/all/math2nn")
30 import bench
31 result["available"] = True
32 result["module"] = getattr(bench, "__file__", None)
33 result["symbols"] = [name for name in
34 ("TRACKS", "train_model", "sweep_baseline", "make_report")
35 if hasattr(bench, name)]
36 except Exception as exc:
37 result["error"] = repr(exc)
38 return result
39
40
41def main():
42 report = {
43 "stage": "stage-2-bench",
44 "math_check": math_check(),
45 "harness_check": harness_check(),
46 "bench_report": {
47 "baseline_sweep": [],
48 "idea_results": [],
49 "paired_delta": None,
50 "permutation_p_value": None,
51 "mechanism_signature": None,
52 "status": "blocked_missing_bench_checkout",
53 },
54 }
55 with open("stage2_report.json", "w") as f:
56 json.dump(report, f, indent=2)
57 print(json.dumps(report, indent=2))
58
59
60if __name__ == "__main__":
61 main()