code wiki / (root) / nx_estimator_diagnostics_test.nx

nx_estimator_diagnostics_test.nx source

↩ module page · 76 lines · 2379 B

1// nx_estimator_diagnostics_test.nx -- smoke for bias-variance primitive. 2 3import "syscalls.nx" 4import "nx_estimator_diagnostics.nx" 5 6func main() -> i64 { 7 let est: *i64 = sys_mmap(80) as *i64 8 let d: *EstimatorDiag = nx_diag_alloc() 9 10 // Test 1: unbiased low-variance estimator -- all estimates = theta. 11 let theta: i64 = 100 12 est[0] = 100 13 est[1] = 100 14 est[2] = 100 15 est[3] = 100 16 est[4] = 100 17 nx_diag_compute(d, est, 5, theta) 18 if d.mean != 100 { return 10 } 19 if d.bias != 0 { return 11 } 20 if d.variance != 0 { return 12 } 21 if d.mse != 0 { return 13 } 22 if d.max_abs_err != 0 { return 14 } 23 24 // Test 2: biased low-variance -- all estimates = theta + 10. 25 est[0] = 110 26 est[1] = 110 27 est[2] = 110 28 est[3] = 110 29 est[4] = 110 30 nx_diag_compute(d, est, 5, theta) 31 if d.mean != 110 { return 20 } 32 if d.bias != 10 { return 21 } 33 if d.variance != 0 { return 22 } 34 if d.mse != 100 { return 23 } // bias² 35 if d.max_abs_err != 10 { return 24 } 36 37 // Test 3: unbiased high-variance -- {90, 95, 100, 105, 110}, theta=100. 38 est[0] = 90 39 est[1] = 95 40 est[2] = 100 41 est[3] = 105 42 est[4] = 110 43 nx_diag_compute(d, est, 5, theta) 44 if d.mean != 100 { return 30 } 45 if d.bias != 0 { return 31 } 46 // variance = ((100+25+0+25+100) / 5) = 50 47 if d.variance != 50 { return 32 } 48 if d.mse != 50 { return 33 } 49 if d.max_abs_err != 10 { return 34 } 50 51 // Test 4: comparison -- ours (biased low-var) vs theirs (unbiased high-var). 52 let ours: *EstimatorDiag = nx_diag_alloc() 53 est[0] = 110 54 est[1] = 110 55 est[2] = 110 56 est[3] = 110 57 est[4] = 110 58 nx_diag_compute(ours, est, 5, theta) 59 let theirs: *EstimatorDiag = nx_diag_alloc() 60 est[0] = 90 61 est[1] = 95 62 est[2] = 100 63 est[3] = 105 64 est[4] = 110 65 nx_diag_compute(theirs, est, 5, theta) 66 67 // ours.bias=10 (|10|), theirs.bias=0 -> ours HIGHER BIAS. 68 // ours.variance=0, theirs.variance=50 -> ours LOWER VAR. 69 // Expected verdict: HIGHER_BIAS_LOWER_VAR. 70 let verdict: i64 = nx_diag_compare(ours, theirs) 71 if verdict != NX_DIAG_VERDICT_HIGHER_BIAS_LOWER_VAR { return 40 } 72 let rec: i64 = nx_diag_recommend(verdict) 73 if rec != NX_DIAG_FX_RECALIBRATE { return 41 } 74 75 return 0 76}