code wiki / _hdl_build / nx_mdl_overfit_gate.nx

nx_mdl_overfit_gate.nx source

↩ module page · 83 lines · 7316 B

1import "nx_gate_gn.nx" 2import "nx_gate_base.nx" 3// nx_mdl_overfit_gate.nx -- WIGNER OPERATIONALIZED: the MDL/Occam prior that rejects an overfit (operator: Wigner's 4// unreasonable effectiveness of math should guide; mechanistic, no LLM). The sharpest test of "nature's laws are 5// SIMPLE": given NOISY observations of a simple law (y = 2x + noise), a COMPLEX model can fit the noisy points EXACTLY 6// (residual 0) but it fit the NOISE -- it fails on held-out. The SIMPLE model has nonzero training residual (it can't 7// fit noise) but GENERALIZES. The MDL score = training-residual + lambda*complexity picks the SIMPLE law -- because 8// simplicity, per Wigner, IS physical truth. No LLM: this is least-squares + a description-length count. 9// T0 NOISY DATA: y ~= 2x + small noise (a simple law, observed imperfectly). 10// T1 SIMPLE (linear y=a*x): fits a~=2.04, small nonzero residual (can't fit noise), captures the law. 11// T2 COMPLEX (quadratic a+bx+cx^2): fits the 3 noisy points EXACTLY (residual 0) -- but it fit the noise. 12// T3 MDL/OCCAM: MDL(simple) < MDL(complex) -- the complexity penalty outweighs the complex model's lower residual. 13// T4 GENERALIZES: held-out x=4 -> SIMPLE predicts ~8 (true), COMPLEX predicts ~9 (overfit, wrong). Simplicity wins on truth. 14// T5 = Wigner mechanized: the simplest law that fits is the physical one; MDL rejects the noise-fitter. No LLM. 15// license_tier: ORIGINAL 16import "nx_f32_hw.nx" 17import "nx_syscalls.nx" 18 19func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 20" as *u8); return ok } 21func gm(x: i64) -> i64 { return gn(f32_int(f32_mul(x, f32_of(1000)))) } 22func f32_le(x: i64, y: i64) -> i64 { let d: i64=f32_sub(x,y) & 0xFFFFFFFF; if ((d>>31)&1)==1 { return 1 } if (d & 0x7FFFFFFF)==0 { return 1 } return 0 } 23func f32_abs(x: i64) -> i64 { return x & 0x7FFFFFFF } 24 25func main() -> i64 { 26 gw("=== nx_mdl_overfit_gate: WIGNER mechanized -- MDL/Occam rejects the overfit, the simple law generalizes (no LLM) ===\n" as *u8) 27 var pass: i64=0; var total: i64=0 28 // NOISY observations of the simple law y = 2x: x=1,2,3 -> y = [2.1, 3.9, 6.2] (true [2,4,6], noise [+.1,-.1,+.2]). 29 let x0: i64=f32_of(1); let x1: i64=f32_of(2); let x2: i64=f32_of(3) 30 let y0: i64=f32_div(f32_of(21),f32_of(10)); let y1: i64=f32_div(f32_of(39),f32_of(10)); let y2: i64=f32_div(f32_of(62),f32_of(10)) 31 32 // T0. 33 total=total+1; pass=pass+1 34 gw(" [PASS] T0 NOISY DATA: y=[" as *u8); gm(y0); gw("," as *u8); gm(y1); gw("," as *u8); gm(y2); gw("]m ~= 2x + noise (true 2x=[2000,4000,6000]m)\n" as *u8) 35 36 // T1 SIMPLE: linear y=a*x. a = sum(xy)/sum(x^2). 37 let sxy: i64=f32_add(f32_add(f32_mul(x0,y0),f32_mul(x1,y1)),f32_mul(x2,y2)) 38 let sxx: i64=f32_add(f32_add(f32_mul(x0,x0),f32_mul(x1,x1)),f32_mul(x2,x2)) 39 let a: i64=f32_div(sxy,sxx) 40 let r0: i64=f32_sub(y0,f32_mul(a,x0)); let r1: i64=f32_sub(y1,f32_mul(a,x1)); let r2: i64=f32_sub(y2,f32_mul(a,x2)) 41 let res_s: i64=f32_add(f32_add(f32_mul(r0,r0),f32_mul(r1,r1)),f32_mul(r2,r2)) 42 total=total+1; if f32_int(f32_mul(a,f32_of(1000)))>=2000 { if f32_int(f32_mul(a,f32_of(1000)))<=2080 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } 43 gw("T1 SIMPLE (linear y=a*x): a=" as *u8); gm(a); gw("m (~2.04, captures the law), training residual=" as *u8); gn(f32_int(f32_mul(res_s,f32_of(1000)))); gw("m (small, can't fit noise)\n" as *u8) 44 45 // T2 COMPLEX: quadratic a2 + b2*x + c2*x^2 EXACT through 3 points (finite differences). residual 0. 46 let c2: i64=f32_div(f32_sub(f32_add(y0,y2),f32_mul(f32_of(2),y1)), f32_of(2)) // (y0-2y1+y2)/2 47 let b2: i64=f32_sub(f32_sub(y1,y0), f32_mul(f32_of(3),c2)) // (y1-y0)-3c 48 let a2: i64=f32_sub(f32_sub(y0,b2),c2) // y0-b-c 49 // residual on training (should be ~0). 50 let q0: i64=f32_sub(y0, f32_add(f32_add(a2, f32_mul(b2,x0)), f32_mul(c2,f32_mul(x0,x0)))) 51 let q2v: i64=f32_sub(y2, f32_add(f32_add(a2, f32_mul(b2,x2)), f32_mul(c2,f32_mul(x2,x2)))) 52 let res_c: i64=f32_add(f32_mul(q0,q0),f32_mul(q2v,q2v)) 53 total=total+1; if f32_int(f32_mul(res_c,f32_of(1000)))<=2 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 54 gw("T2 COMPLEX (quadratic): a=" as *u8); gm(a2); gw("m b=" as *u8); gm(b2); gw("m c=" as *u8); gm(c2); gw("m, training residual=" as *u8); gn(f32_int(f32_mul(res_c,f32_of(1000)))); gw("m (~0, EXACT -- it fit the noise)\n" as *u8) 55 56 // T3 MDL/OCCAM: MDL = residual + lambda*params. lambda=1; simple params=1, complex params=3. 57 let lam: i64=f32_of(1) 58 let mdl_s: i64=f32_add(res_s, f32_mul(lam,f32_of(1))) 59 let mdl_c: i64=f32_add(res_c, f32_mul(lam,f32_of(3))) 60 total=total+1; if f32_le(mdl_s,mdl_c)==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 61 gw("T3 MDL/OCCAM: MDL(simple)=" as *u8); gm(mdl_s); gw("m < MDL(complex)=" as *u8); gm(mdl_c); gw("m -- complexity penalty outweighs the complex model's lower residual (Wigner: pick simple)\n" as *u8) 62 63 // T4 GENERALIZES: held-out x=4, true y=8. simple a*4; complex a2+b2*4+c2*16. 64 let x4: i64=f32_of(4); let truev: i64=f32_of(8) 65 let pred_s: i64=f32_mul(a,x4) 66 let pred_c: i64=f32_add(f32_add(a2, f32_mul(b2,x4)), f32_mul(c2,f32_mul(x4,x4))) 67 let err_s: i64=f32_abs(f32_sub(pred_s,truev)); let err_c: i64=f32_abs(f32_sub(pred_c,truev)) 68 total=total+1; if f32_le(err_s,err_c)==1 { if f32_le(err_s,f32_div(f32_of(3),f32_of(10)))==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } 69 gw("T4 GENERALIZES: held-out x=4 (true 8000m) -> SIMPLE=" as *u8); gm(pred_s); gw("m (err " as *u8); gm(err_s); gw("m) vs COMPLEX=" as *u8); gm(pred_c); gw("m (err " as *u8); gm(err_c); gw("m) -- the noise-fitter OVERSHOOTS\n" as *u8) 70 71 // T5. 72 total=total+1; if f32_le(mdl_s,mdl_c)==1 { if f32_le(err_s,err_c)==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } 73 gw("T5 WIGNER MECHANIZED: MDL chose the SIMPLE law (lower description length) AND it generalized -- simplicity = physical truth, no LLM\n" as *u8) 74 75 gw("\n WIGNER OPERATIONALIZED: a complex model fit the NOISY data exactly (residual 0) but it fit the noise -> it overshot on held-out.\n" as *u8) 76 gw(" The MDL/Occam prior -- training-fit + a complexity (description-length) penalty -- preferred the SIMPLE linear law, which\n" as *u8) 77 gw(" generalized. This is WHY the machine scientist finds physical laws, not curve-fits: per Wigner, nature's laws are SIMPLE math,\n" as *u8) 78 gw(" so the minimum-description-length hypothesis is the physical one. Pure least-squares + counting -- NO LLM. Composes with\n" as *u8) 79 gw(" nx_machine_scientist (search) + nx_evo_synth (evolution) -> the full mechanistic machine scientist, Wigner-guided.\n" as *u8) 80 gw("MDL-OVERFIT verdict=" as *u8) 81 if pass==total { gw("GREEN passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw(" -- MDL rejects the overfit, the simple law generalizes (Wigner mechanized, no LLM)\n" as *u8); sys_exit(0); return 0 } 82 gw("RED passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw("\n" as *u8); sys_exit(1); return 1 83}