code wiki / _hdl_build / nx_dr_fuse_gate.nx

nx_dr_fuse_gate.nx source

↩ module page · 99 lines · 4462 B

1// nx_dr_fuse_gate.nx -- KAT + neg-controls for the learned signal combiner (DR-12). 2// Synthetic set where the right answer is KNOWN: the "semantic" feature separates insights from 3// distractors, the "lexical" feature is deliberately ANTI-correlated (a lexical-only weighting 4// scores 0), and the third feature is constant noise. A fitter that works must discover the 5// informative feature and ignore the misleading one. 6// Proves in particular: (1) weights fitted on a TRAINING task range generalise to a DISJOINT 7// held-out task, and (2) the NEG-CONTROL -- on a task whose insights and distractors are 8// IDENTICAL, recall at matched precision is 0, so the metric cannot be gamed by any weighting. 9import "nx_dr_fuse.nx" 10import "nx_gate_verdict.nx" 11 12// write one record: [task,label,lex,sem,idf] 13func fg_put(r: *i64, i: i64, t: i64, lab: i64, sem: i64) -> i64 { 14 let b: i64 = i * 5 15 r[b] = t 16 r[b+1] = lab 17 if lab == 1 { r[b+2] = 300 } else { r[b+2] = 700 } // lexical: ANTI-correlated on purpose 18 r[b+3] = sem // semantic: the informative signal 19 r[b+4] = 500 // third feature: constant noise 20 return 0 21} 22 23func main() -> i64 { 24 let ctr: *i64 = gv_ctr() 25 gv_head("nx_dr_fuse -- learned signal combiner, fitted on train / scored on held-out (DR-12)") 26 27 let n: i64 = 16 28 let r: *i64 = sys_mmap(n * 5 * 8) as *i64 29 // tasks 0..2 : separable (insights sem=800, distractors sem=200) 30 var t: i64 = 0 31 var i: i64 = 0 32 while t < 3 { 33 fg_put(r, i, t, 1, 800); i = i + 1 34 fg_put(r, i, t, 1, 800); i = i + 1 35 fg_put(r, i, t, 0, 200); i = i + 1 36 fg_put(r, i, t, 0, 200); i = i + 1 37 t = t + 1 38 } 39 // task 3 : NEG-CONTROL -- insights and distractors are IDENTICAL on every feature 40 fg_put(r, i, 3, 1, 500); r[i*5+2] = 500; i = i + 1 41 fg_put(r, i, 3, 1, 500); r[i*5+2] = 500; i = i + 1 42 fg_put(r, i, 3, 0, 500); r[i*5+2] = 500; i = i + 1 43 fg_put(r, i, 3, 0, 500); r[i*5+2] = 500; i = i + 1 44 45 // T1 scoring is the weighted sum 46 let w1: *i64 = sys_mmap(3 * 8) as *i64 47 w1[0] = 1; w1[1] = 2; w1[2] = 0 48 var ok1: i64 = 0 49 if fu_score(r, 0, w1) == (300 + 2 * 800) { ok1 = 1 } 50 gv_check("T1 score is the weighted sum", ok1, ctr) 51 52 // T2 the informative feature alone orders every pair correctly 53 let wsem: *i64 = sys_mmap(3 * 8) as *i64 54 wsem[0] = 0; wsem[1] = 1; wsem[2] = 0 55 var ok2: i64 = 0 56 if fu_pairacc(r, n, wsem, 0, 3) == 1000 { ok2 = 1 } 57 gv_check("T2 informative feature ranks all pairs correctly", ok2, ctr) 58 59 // T3 the misleading feature alone gets EVERY pair wrong (the trap is real) 60 let wlex: *i64 = sys_mmap(3 * 8) as *i64 61 wlex[0] = 1; wlex[1] = 0; wlex[2] = 0 62 var ok3: i64 = 0 63 if fu_pairacc(r, n, wlex, 0, 3) == 0 { ok3 = 1 } 64 gv_check("T3 anti-correlated feature ranks all pairs wrong", ok3, ctr) 65 66 // T4 the fitter recovers a perfect ranking on the TRAINING range 67 let w: *i64 = sys_mmap(3 * 8) as *i64 68 let acc: i64 = fu_fit(r, n, w, 0, 2) 69 var ok4: i64 = 0 70 if acc == 1000 { ok4 = 1 } 71 gv_check("T4 fitter reaches perfect train pairwise accuracy", ok4, ctr) 72 73 // T5 it learned to prefer the informative feature over the misleading one 74 var ok5: i64 = 0 75 if w[1] > w[0] { ok5 = 1 } 76 gv_check("T5 fitted weights prefer informative over misleading", ok5, ctr) 77 78 // T6 HELD-OUT: weights fitted on tasks [0,2) score perfectly on unseen task 2 79 var ok6: i64 = 0 80 if fu_recall(r, n, w, 2, 3) == 1000 { ok6 = 1 } 81 gv_check("T6 fitted weights generalise to a held-out task", ok6, ctr) 82 83 // T7 NEG-CONTROL: on a task where insights and distractors are IDENTICAL, no weighting can 84 // separate them -- recall at matched precision must be 0. 85 var ok7: i64 = 0 86 if fu_recall(r, n, w, 3, 4) == 0 { if fu_recall(r, n, wsem, 3, 4) == 0 { ok7 = 1 } } 87 gv_check("T7 neg-control identical candidates recall 0", ok7, ctr) 88 89 // T8 the fit is DETERMINISTIC (same data -> same weights, bit-for-bit) 90 let w2: *i64 = sys_mmap(3 * 8) as *i64 91 fu_fit(r, n, w2, 0, 2) 92 var ok8: i64 = 0 93 if w2[0] == w[0] { if w2[1] == w[1] { if w2[2] == w[2] { ok8 = 1 } } } 94 gv_check("T8 fit is deterministic", ok8, ctr) 95 96 let rc: i64 = gv_verdict("DR-FUSE", ctr, "learned combiner: train/held-out separation, misleading-feature trap, neg-control") 97 sys_exit(rc) 98 return rc 99}