code wiki / _hdl_build / nx_flip_hazard.nx
nx_flip_hazard.nx source
↩ module page · 155 lines · 6921 B
1// nx_flip_hazard.nx -- THE WEAR-AND-TEAR MEMBER of the FlipOrgan family.
2// nx_flip_hazard <class> <comp> <usage_now> <hold> <repair_cost> [plane-prefix-root]
3//
4// WHY IT EXISTS (measured on the LIVE engine 2026-08-08, not assumed): nx_flip_score decides with
5// `margin = lift - repair`, verified exactly across all 8 parts of the FOCUS13A deal, and the
6// findings plane's `cond` column appears in ZERO decisions. So a part the engine declines to fix
7// carries a RISK OF FAILING that is priced at ZERO. For the DPS6 dual-clutch that unpriced risk
8// is 29.3%-51.9% of the entire p10 downside margin.
9//
10// It reads observations ITSELF from <root>obs- (id|class|comp|usage|source) -- the caller never
11// types a failure rate, exactly as nx_flip_score reads parts rather than being handed a repair
12// total. Thresholds come from <root>hzcfg- (class|min_obs|min_r2_bps|note), never from code.
13//
14// ASSET-AGNOSTIC: `usage` is miles for a car, trading days for an FX drawdown, cycles for a
15// battery. PROVEN on both -- DPS6 beta=1.7274 (WEAR-OUT) vs EUR/USD drawdowns beta=0.6226
16// (PERSISTENT). Opposite regimes from one estimator; car logic applied to FX would be backwards.
17//
18// PROVENANCE CEILING, printed on every run because it bounds every number downstream: NHTSA
19// complaints are SELF-REPORTED with no exposure denominator, so this is the hazard of COMPLAINT
20// ARRIVAL, not of failure. Real evidence about shape and ordering; NOT a fleet-calibrated census.
21// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
22import "nx_flip_lib.nx"
23import "nx_hazard_lib.nx"
24import "nx_syscalls.nx"
25
26const FH_ARGC_MIN: i64 = 6
27const FH_ARG_PFX: i64 = 6
28const FH_OBS_CLASS: i64 = 1
29const FH_OBS_COMP: i64 = 2
30const FH_OBS_USAGE: i64 = 3
31const FH_CFG_MINOBS: i64 = 1
32const FH_CFG_MINR2: i64 = 2
33const FH_MAXOBS: i64 = 4096
34const FH_SLOTS: i64 = 8
35
36func main(argc: i64, argv: *i64) -> i64 {
37 if argc < FH_ARGC_MIN {
38 fx_puts("usage: nx_flip_hazard <class> <comp> <usage_now> <hold> <repair_cost> [plane-prefix-root]\n" as *u8)
39 sys_exit(FX_EXIT_USAGE)
40 return FX_EXIT_USAGE
41 }
42 let cls: *u8 = argv[1] as *u8
43 let comp: *u8 = argv[2] as *u8
44 let usage_now: i64 = fx_atoi(argv[3] as *u8)
45 let hold: i64 = fx_atoi(argv[4] as *u8)
46 let repair: i64 = fx_atoi(argv[5] as *u8)
47 var root: *u8 = "flip-" as *u8
48 if argc > FH_ARG_PFX { root = argv[FH_ARG_PFX] as *u8 }
49
50 let nop: *i64 = sts_mm(FX_SCRATCH) as *i64
51 let ncp: *i64 = sts_mm(FX_SCRATCH) as *i64
52 let bo: *u8 = fx_plane(root, "obs-" as *u8, nop)
53 let bc: *u8 = fx_plane(root, "hzcfg-" as *u8, ncp)
54 let no: i64 = nop[0]
55 let nc: i64 = ncp[0]
56 let sp: *i64 = sts_mm(FX_SCRATCH) as *i64
57 let rw: *i64 = sts_mm(FX_SCRATCH) as *i64
58 let out: *i64 = sts_mm(FH_SLOTS * HZ_I64) as *i64
59
60 // THRESHOLDS FROM THE PLANE (contract rule 3). An absent class row is REFUSED, never
61 // defaulted -- a silent default is how an unvetted asset class acquires a confident number.
62 if nc == 0 {
63 fx_puts("FLIP-RED reason=hzcfg-plane-empty\n" as *u8)
64 sys_exit(FX_EXIT_REFUSED)
65 return FX_EXIT_REFUSED
66 }
67 if fx_row_by_id(bc, nc, cls, rw, sp) == 0 {
68 fx_puts("FLIP-RED reason=no-hazard-config-for-class class=" as *u8)
69 fx_puts(cls); fx_puts("\n" as *u8)
70 sys_exit(FX_EXIT_REFUSED)
71 return FX_EXIT_REFUSED
72 }
73 let min_obs: i64 = fx_col_atoi(bc, rw[0], rw[1], FH_CFG_MINOBS, sp)
74 let min_r2: i64 = fx_col_atoi(bc, rw[0], rw[1], FH_CFG_MINR2, sp)
75
76 // Gather this (class, comp)'s observations. The ENGINE reads the parts, not the caller.
77 let ts: *i64 = sts_mm(FH_MAXOBS * HZ_I64) as *i64
78 var n: i64 = 0
79 var i: i64 = 0
80 while i < no {
81 let le: i64 = fx_line_end(bo, no, i)
82 if le > i {
83 if fx_col(bo, i, le, FH_OBS_CLASS, sp) == 1 {
84 if fx_slice_eqs(bo, sp[0], sp[1], cls) == 1 {
85 if fx_col(bo, i, le, FH_OBS_COMP, sp) == 1 {
86 if fx_slice_eqs(bo, sp[0], sp[1], comp) == 1 {
87 if n < FH_MAXOBS {
88 ts[n] = fx_col_atoi(bo, i, le, FH_OBS_USAGE, sp)
89 n = n + 1
90 }
91 }
92 }
93 }
94 }
95 }
96 i = le + 1
97 }
98
99 fx_puts("FLIP-HAZARD\n" as *u8)
100 fx_puts("class=" as *u8); fx_puts(cls)
101 fx_puts(" comp=" as *u8); fx_puts(comp); fx_puts("\n" as *u8)
102
103 if n == 0 {
104 fx_puts("verdict=REFUSED n_obs=0 rule=absent-observations-are-not-a-clean-bill-of-health\n" as *u8)
105 sys_exit(FX_EXIT_REFUSED)
106 return FX_EXIT_REFUSED
107 }
108 if hz_wb_fit(ts, n, min_obs, min_r2, out) == 0 {
109 // Name WHICH precondition failed. A bare REFUSED sends the reader to guess, and the two
110 // causes have opposite remedies: gather more data vs distrust the data you have.
111 fx_puts("verdict=REFUSED n_obs=" as *u8); fx_putn(n)
112 fx_puts(" min_obs=" as *u8); fx_putn(min_obs)
113 fx_puts(" min_r2_bps=" as *u8); fx_putn(min_r2)
114 if n < min_obs {
115 fx_puts(" rule=too-few-observations-to-fit-a-curve-anyone-should-bid-on\n" as *u8)
116 } else {
117 fx_puts(" rule=fit-quality-below-bar-or-degenerate-spread\n" as *u8)
118 }
119 sys_exit(FX_EXIT_REFUSED)
120 return FX_EXIT_REFUSED
121 }
122
123 let beta_fp: i64 = out[0]
124 let eta: i64 = out[1]
125 let pf: i64 = hz_cond_pfail_bps(usage_now, usage_now + hold, beta_fp, eta)
126 if pf < 0 {
127 fx_puts("verdict=REFUSED rule=invalid-hazard-parameters\n" as *u8)
128 sys_exit(FX_EXIT_REFUSED)
129 return FX_EXIT_REFUSED
130 }
131 let ec: i64 = hz_expected_cost(pf, repair)
132
133 // beta IS the wear regime, and naming it is the point: a defect-class part and a worn-out
134 // part demand opposite decisions (chase the campaign vs discount the price).
135 var regime: *u8 = "MEMORYLESS-age-carries-no-information" as *u8
136 if beta_fp > HZ_FP + HZ_FP / 8 { regime = "WEAR-OUT-risk-rises-with-usage" as *u8 }
137 if beta_fp < HZ_FP - HZ_FP / 8 { regime = "INFANT-MORTALITY-or-PERSISTENT-risk-falls-with-usage" as *u8 }
138
139 fx_puts("verdict=FITTED regime=" as *u8); fx_puts(regime)
140 fx_puts(" p_fail_bps=" as *u8); fx_putn(pf)
141 fx_puts(" expected_cost=" as *u8); fx_putn(ec)
142 fx_puts(" beta_bps=" as *u8); fx_putn(out[2])
143 fx_puts(" eta=" as *u8); fx_putn(eta)
144 fx_puts(" r2_bps=" as *u8); fx_putn(out[4])
145 fx_puts(" n_obs=" as *u8); fx_putn(n)
146 fx_puts("\n" as *u8)
147 fx_kv("usage_now" as *u8, usage_now)
148 fx_kv("hold" as *u8, hold)
149 fx_kv("repair_cost" as *u8, repair)
150 fx_kv("min_obs" as *u8, min_obs)
151 fx_kv("min_r2_bps" as *u8, min_r2)
152 fx_puts("provenance=self-reported-complaint-arrival-hazard-no-exposure-denominator-not-a-census\n" as *u8)
153 sys_exit(0)
154 return 0
155}