nx_targetfit_gate.nx source
↩ module page · 141 lines · 10072 B
1// nx_targetfit_gate.nx -- proves the target-conformance ruler: bands, normalisation, abstention, drift.
2//
3// EVERY EXPECTED VALUE BELOW IS HAND arithmetic OVER THE PUBLISHED pg_bound TABLE, so a tooth that
4// fails is either a ruler bug or a DELIBERATE bounds change -- and going RED on a bounds change is a
5// feature, not brittleness: the plausible envelopes are supposed to move only by measurement, and a
6// conformance ruler whose normalisation base moved deserves a red until someone re-adjudicates it.
7//
8// THE ONE TOOTH THAT JUSTIFIES THE WHOLE ORGAN IS T4: the SAME 55 mm miss reads 100 permil on total
9// height and 647 permil on breast projection -- a shrug on one axis, a different body on the other.
10// A percent-of-target ruler calls those equal. That is the commensurability the envelope
11// normalisation buys, proven in one line of arithmetic.
12import "nx_syscalls.nx"
13import "nx_canon_proportions.nx"
14import "nx_proportion_guard.nx"
15import "nx_targetfit.nx"
16import "nx_gate_verdict.nx"
17
18// ---- fixture values, PURPOSE-named (rule 11) ------------------------------------------------------
19// Envelopes transcribed from pg_bound (HUMAN): height 1400..1950 -> 550, breast projection 25..110
20// -> 85, hip width 280..460 -> 180, shoulder width 300..470 -> 170.
21const TG_ENV_HEIGHT: i64 = 550
22const TG_ENV_BPROJ: i64 = 85
23const TG_ENV_HIP: i64 = 180
24const TG_ENV_SHOULDER: i64 = 170
25const TG_TGT_HEIGHT: i64 = 1700
26const TG_TGT_HIP: i64 = 370
27const TG_TGT_SHOULDER: i64 = 380
28const TG_TGT_BPROJ: i64 = 60
29// misses chosen so the permil lands EXACTLY ON each band edge, pinning the inclusive-boundary contract
30const TG_MISS_TIGHT: i64 = 11 // 11*1000/550 = 20 -> TIGHT
31const TG_MISS_SLIGHT: i64 = 55 // 55*1000/550 = 100 -> SLIGHT
32const TG_MISS_HIP_EDGE: i64 = 27 // 27*1000/180 = 150 -> SLIGHT, exactly on the edge
33const TG_MISS_SH_EDGE: i64 = 51 // 51*1000/170 = 300 -> MODERATE, exactly on the edge
34const TG_MISS_SH_OUT: i64 = 120 // 120*1000/170 = 705 -> OUTSIDE
35const TG_AXIS_UNKNOWN: i64 = 999 // no pg_bound row exists for this id
36const TG_N4: i64 = 4
37
38func tg_arr(n: i64) -> *i64 { return sys_mmap(n * 8) as *i64 }
39func tg_eq(a: i64, b: i64) -> i64 { if a == b { return 1 } return 0 }
40func tg_zero(a: i64) -> i64 { if a == 0 { return 1 } return 0 }
41func tg_pos(a: i64) -> i64 { if a > 0 { return 1 } return 0 }
42func tg_eq2(a: i64, b: i64, c: i64, d: i64) -> i64 { if a == b { if c == d { return 1 } } return 0 }
43func tg_eq3(a: i64, b: i64, c: i64, d: i64, e: i64, f: i64) -> i64 {
44 if a == b { if c == d { if e == f { return 1 } } } return 0
45}
46
47func main() -> i64 {
48 let ctr: *i64 = gv_ctr()
49 gv_head("nx_targetfit_gate -- the conformance ruler: envelope normalisation, band grading, abstention, drift" as *u8)
50
51 // ---- the normalisation base itself ----------------------------------------------------------
52 gv_check("T1 envelopes derive from pg_bound, the estate's ONE plausibility table -- height 550, projection 85" as *u8,
53 tg_eq2(tf_envelope(NX_AXIS_TOTAL_HEIGHT, PG_HUMAN), TG_ENV_HEIGHT,
54 tf_envelope(NX_AXIS_BREAST_PROJECTION, PG_HUMAN), TG_ENV_BPROJ), ctr)
55 gv_check("neg-control-an-axis-with-NO-bound-row-has-envelope-minus-one-because-missing-is-not-permission" as *u8,
56 tg_eq(tf_envelope(TG_AXIS_UNKNOWN, PG_HUMAN), 0 - 1), ctr)
57
58 // ---- T4: THE COMMENSURABILITY TOOTH ---------------------------------------------------------
59 let vh: i64 = tf_variance_permil(NX_AXIS_TOTAL_HEIGHT, PG_HUMAN, TG_TGT_HEIGHT, TG_TGT_HEIGHT + TG_MISS_SLIGHT)
60 let vb: i64 = tf_variance_permil(NX_AXIS_BREAST_PROJECTION, PG_HUMAN, TG_TGT_BPROJ, TG_TGT_BPROJ + TG_MISS_SLIGHT)
61 gv_check("T3 a 55mm miss on height reads 100 permil of its envelope" as *u8, tg_eq(vh, 100), ctr)
62 gv_check("T4 THE SAME 55mm on breast projection reads 647 permil -- same millimetres, different body: the normalisation IS the ruler" as *u8,
63 tg_eq(vb, 647), ctr)
64 gv_check("T5 direction does not matter: a miss below the target grades identically to one above" as *u8,
65 tg_eq(tf_variance_permil(NX_AXIS_TOTAL_HEIGHT, PG_HUMAN, TG_TGT_HEIGHT, TG_TGT_HEIGHT - TG_MISS_SLIGHT), vh), ctr)
66
67 // ---- band edges are INCLUSIVE, pinned exactly -----------------------------------------------
68 gv_check("T6 20 permil is TIGHT, 100 is SLIGHT, and the 150 and 300 edges grade INTO the band, not out of it" as *u8,
69 tg_eq3(tf_grade(20, TF_TIGHT_PERMIL_DEFAULT, TF_SLIGHT_PERMIL_DEFAULT, TF_MODERATE_PERMIL_DEFAULT), TF_TIGHT,
70 tf_grade(150, TF_TIGHT_PERMIL_DEFAULT, TF_SLIGHT_PERMIL_DEFAULT, TF_MODERATE_PERMIL_DEFAULT), TF_SLIGHT,
71 tf_grade(300, TF_TIGHT_PERMIL_DEFAULT, TF_SLIGHT_PERMIL_DEFAULT, TF_MODERATE_PERMIL_DEFAULT), TF_MODERATE), ctr)
72 gv_check("T7 301 permil is OUTSIDE and a negative variance is UNBOUNDED -- the two refusal shapes stay distinct" as *u8,
73 tg_eq2(tf_grade(301, TF_TIGHT_PERMIL_DEFAULT, TF_SLIGHT_PERMIL_DEFAULT, TF_MODERATE_PERMIL_DEFAULT), TF_OUTSIDE,
74 tf_grade(0 - 1, TF_TIGHT_PERMIL_DEFAULT, TF_SLIGHT_PERMIL_DEFAULT, TF_MODERATE_PERMIL_DEFAULT), TF_UNBOUNDED), ctr)
75
76 // ---- the multi-axis subject verdict ---------------------------------------------------------
77 // Four axes: one lands in each band, plus one unbounded. WITHIN because nothing is OUTSIDE, and
78 // the partition must sum: 1+1+1+0+1 == 4.
79 let axes: *i64 = tg_arr(TG_N4)
80 let tgts: *i64 = tg_arr(TG_N4)
81 let meas: *i64 = tg_arr(TG_N4)
82 let grades: *i64 = tg_arr(TG_N4)
83 let o: *i64 = tg_arr(TF_O_SLOTS)
84 axes[0] = NX_AXIS_TOTAL_HEIGHT; tgts[0] = TG_TGT_HEIGHT; meas[0] = TG_TGT_HEIGHT + TG_MISS_TIGHT
85 axes[1] = NX_AXIS_HIP_WIDTH; tgts[1] = TG_TGT_HIP; meas[1] = TG_TGT_HIP + TG_MISS_HIP_EDGE
86 axes[2] = NX_AXIS_SHOULDER_WIDTH; tgts[2] = TG_TGT_SHOULDER; meas[2] = TG_TGT_SHOULDER + TG_MISS_SH_EDGE
87 axes[3] = TG_AXIS_UNKNOWN; tgts[3] = 100; meas[3] = 100
88 let vin: i64 = tf_fit(axes, tgts, meas, TG_N4, PG_HUMAN,
89 TF_TIGHT_PERMIL_DEFAULT, TF_SLIGHT_PERMIL_DEFAULT, TF_MODERATE_PERMIL_DEFAULT, o, grades)
90 gv_subjects("axes in the WITHIN fixture" as *u8, o[TF_O_AXES], ctr)
91 gv_check("T8 a subject with tight, slight and moderate misses and no outside reads WITHIN" as *u8,
92 tg_eq(vin, TF_WITHIN), ctr)
93 gv_check("T9 the partition sums: one axis in each band, one unbounded, and tf_reconciles proves it" as *u8,
94 tg_eq3(o[TF_O_TIGHT] + o[TF_O_SLIGHT] + o[TF_O_MODERATE], 3, o[TF_O_UNBOUNDED], 1, tf_reconciles(o), 1), ctr)
95 gv_check("T10 per-axis grades are a WORKLIST, not a count: each axis carries its own band" as *u8,
96 tg_eq3(grades[0], TF_TIGHT, grades[1], TF_SLIGHT, grades[2], TF_MODERATE), ctr)
97 gv_check("T11 the worst axis is NAMED (shoulder at 300 permil), and the mean is over MEASURABLE axes only: (20+150+300)/3 = 156" as *u8,
98 tg_eq3(o[TF_O_WORST], 300, o[TF_O_WORSTAXIS], NX_AXIS_SHOULDER_WIDTH, o[TF_O_MEANVAR], 156), ctr)
99 gv_check("T12 the verdict CARRIES ITS OWN BAR: the band edges used are echoed in the result" as *u8,
100 tg_eq3(o[TF_O_BANDT], TF_TIGHT_PERMIL_DEFAULT, o[TF_O_BANDS], TF_SLIGHT_PERMIL_DEFAULT, o[TF_O_BANDM], TF_MODERATE_PERMIL_DEFAULT), ctr)
101
102 // ---- the OUT fixture, and the bite ----------------------------------------------------------
103 let meas2: *i64 = tg_arr(TG_N4)
104 let grades2: *i64 = tg_arr(TG_N4)
105 let o2: *i64 = tg_arr(TF_O_SLOTS)
106 meas2[0] = meas[0]; meas2[1] = meas[1]; meas2[3] = meas[3]
107 meas2[2] = TG_TGT_SHOULDER + TG_MISS_SH_OUT
108 let vout: i64 = tf_fit(axes, tgts, meas2, TG_N4, PG_HUMAN,
109 TF_TIGHT_PERMIL_DEFAULT, TF_SLIGHT_PERMIL_DEFAULT, TF_MODERATE_PERMIL_DEFAULT, o2, grades2)
110 gv_check("T13 one axis beyond moderate flips the subject to OUT and names shoulder at 705 permil" as *u8,
111 tg_eq3(vout, TF_OUT, o2[TF_O_WORST], 705, o2[TF_O_WORSTAXIS], NX_AXIS_SHOULDER_WIDTH), ctr)
112 gv_bite("T14 outside-band detection" as *u8, tg_eq(vout, TF_OUT), tg_eq(vin, TF_OUT), ctr)
113
114 // ---- abstention: the two shapes of nothing-to-measure ---------------------------------------
115 let oe: *i64 = tg_arr(TF_O_SLOTS)
116 let ge: *i64 = tg_arr(1)
117 gv_check("neg-control-ZERO-axes-is-UNMEASURABLE-never-WITHIN-because-a-comparison-that-did-not-happen-cannot-pass" as *u8,
118 tg_eq(tf_fit(axes, tgts, meas, 0, PG_HUMAN, TF_TIGHT_PERMIL_DEFAULT, TF_SLIGHT_PERMIL_DEFAULT, TF_MODERATE_PERMIL_DEFAULT, oe, ge), TF_UNMEASURABLE), ctr)
119 let axu: *i64 = tg_arr(1)
120 let tgu: *i64 = tg_arr(1)
121 let meu: *i64 = tg_arr(1)
122 let gru: *i64 = tg_arr(1)
123 let ou: *i64 = tg_arr(TF_O_SLOTS)
124 axu[0] = TG_AXIS_UNKNOWN; tgu[0] = 100; meu[0] = 100
125 gv_check("neg-control-a-subject-whose-EVERY-axis-is-unbounded-is-UNMEASURABLE-so-the-uncharacterised-axes-cannot-be-the-ones-that-always-agree" as *u8,
126 tg_eq(tf_fit(axu, tgu, meu, 1, PG_HUMAN, TF_TIGHT_PERMIL_DEFAULT, TF_SLIGHT_PERMIL_DEFAULT, TF_MODERATE_PERMIL_DEFAULT, ou, gru), TF_UNMEASURABLE), ctr)
127
128 // ---- drift: the signal BEFORE the failure ---------------------------------------------------
129 // Same generator, same target, second run worse: mean 156 -> higher. tf_drift is positive-means-
130 // worse, and a pass/fail ruler is structurally blind to it until the failure has already shipped.
131 let d: i64 = tf_drift(o, o2)
132 gv_check("T17 drift between the two runs is POSITIVE (worse) and exactly the mean difference" as *u8,
133 tg_eq(d, o2[TF_O_MEANVAR] - o[TF_O_MEANVAR]), ctr)
134 gv_bite("T18 drift detection" as *u8, tg_pos(d), tg_pos(tf_drift(o, o)), ctr)
135
136 let rc: i64 = gv_verdict("TARGETFIT-GATE" as *u8, ctr,
137 "variance is commensurable across axes because it is normalised by each axis's own plausible envelope, the bands grade inclusively and are echoed in the verdict, unmeasurable never passes, and drift is visible before failure" as *u8)
138 gv_journal("nx_targetfit_gate" as *u8, ctr[0], ctr[1], tg_zero(rc))
139 sys_exit(rc)
140 return rc
141}