nx_finhealth_gate.nx source
↩ module page · 119 lines · 6414 B
1// nx_finhealth_gate.nx -- COMPOSITE FINANCIAL HEALTH GATE.
2// Proves the whole finance pipeline composes into one verdict: it computes a REAL Altman Z (via az_zscore)
3// and a REAL Piotroski F-score (via pio_fscore) and judges them together, and it proves the two
4// fail-closed gates -- INCOMPLETE dominates on a missing fact, FLAGGED dominates when the books do not
5// balance no matter how good the scores are.
6// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
7
8import "nx_finhealth_lib.nx"
9
10func hg_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
11func hg_putn(v: i64) -> i64 {
12 let t: *u8 = sys_mmap(32)
13 var o: i64 = 0
14 var m: i64 = v
15 if m < 0 { t[o] = 45 as u8; o = o + 1; m = 0 - m }
16 let d: *u8 = sys_mmap(32)
17 var k: i64 = 0
18 if m == 0 { d[0] = 48 as u8; k = 1 }
19 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
20 var i: i64 = 0
21 while i < k { t[o] = d[k - 1 - i]; o = o + 1; i = i + 1 }
22 sys_write(1, t, o)
23 return 0
24}
25func hg_ck(cnt: *i64, name: *u8, got: i64, want: i64) -> i64 {
26 if got == want {
27 cnt[0] = cnt[0] + 1
28 hg_puts(" PASS " as *u8); hg_puts(name); hg_puts(" = " as *u8); hg_putn(got); hg_puts("\n" as *u8)
29 return 1
30 }
31 cnt[1] = cnt[1] + 1
32 hg_puts(" FAIL " as *u8); hg_puts(name); hg_puts(" got " as *u8); hg_putn(got)
33 hg_puts(" want " as *u8); hg_putn(want); hg_puts("\n" as *u8)
34 return 0
35}
36func hg_ckstr(cnt: *i64, name: *u8, got: *u8, want: *u8) -> i64 {
37 if mt_streq(got, want) == 1 {
38 cnt[0] = cnt[0] + 1
39 hg_puts(" PASS " as *u8); hg_puts(name); hg_puts(" = " as *u8); hg_puts(got); hg_puts("\n" as *u8)
40 return 1
41 }
42 cnt[1] = cnt[1] + 1
43 hg_puts(" FAIL " as *u8); hg_puts(name); hg_puts(" got " as *u8); hg_puts(got); hg_puts("\n" as *u8)
44 return 0
45}
46
47func hg_fill(m: *i64, ni: i64, roa: i64, roap: i64, cfo: i64, ltd: i64, ltdp: i64, cur: i64, curp: i64, sh: i64, shp: i64, gm: i64, gmp: i64, at: i64, atp: i64) -> i64 {
48 m[0] = ni; m[1] = roa; m[2] = roap; m[3] = cfo; m[4] = ltd; m[5] = ltdp; m[6] = cur
49 m[7] = curp; m[8] = sh; m[9] = shp; m[10] = gm; m[11] = gmp; m[12] = at; m[13] = atp
50 return 0
51}
52
53func main(argc: i64, argv: *i64) -> i64 {
54 let cnt: *i64 = sys_mmap(16) as *i64
55 cnt[0] = 0
56 cnt[1] = 0
57
58 hg_puts("NISHI-FINHEALTH-GATE (composite: Altman Z + Piotroski F + identity gate, one verdict)\n" as *u8)
59
60 let m: *i64 = sys_mmap(8 * PIO_N) as *i64
61
62 // ---- verify the sub-organs compose: a healthy firm produces a SAFE Altman zone and an F-score of 8 ----
63 // Altman inputs -> Z ~ 4480 (SAFE). Balance sheet: assets 1000 = liab 200 + equity 800.
64 let z: i64 = az_zscore(300, 400, 200, 800, 200, 500, 1000)
65 hg_ck(cnt, "C0 Altman Z (real compute) SAFE zone (2=SAFE)" as *u8, az_zone(z), AZ_SAFE)
66 hg_fill(m, 1000, 800, 600, 1200, 2000, 2500, 15000, 14000, 500, 500, 4200, 4000, 9000, 8500)
67 let f8: i64 = pio_fscore(m) // this firm's 9 tests -> 8 (dilution test aside; here shares equal so 9)
68 // (shares 500<=500 passes T7, so this actually scores 9)
69 hg_ck(cnt, "C0a Piotroski F (real compute) = 9" as *u8, f8, 9)
70
71 // ---- H1: STRONG -- SAFE zone + high F-score + balanced books ----
72 hg_ck(cnt, "H1 healthy firm -> FH_STRONG (2)" as *u8,
73 fh_analyze(300, 400, 200, 800, 200, 500, 1000, f8, 1000, 200, 800), FH_STRONG)
74 hg_ckstr(cnt, "H1a verdict = STRONG" as *u8, fh_verdict_str(fh_analyze(300, 400, 200, 800, 200, 500, 1000, f8, 1000, 200, 800)), "STRONG" as *u8)
75
76 // ---- H2: WEAK -- a distressed firm (DISTRESS zone) with a low F-score ----
77 let zbad: i64 = az_zscore(0 - 100, 0 - 200, 0 - 50, 100, 800, 200, 1000)
78 hg_ck(cnt, "H2pre Altman DISTRESS zone (0)" as *u8, az_zone(zbad), AZ_DISTRESS)
79 hg_fill(m, 0 - 100, 0 - 300, 100, 0 - 200, 3000, 2500, 13000, 14000, 600, 500, 3800, 4000, 8000, 8500)
80 let f0: i64 = pio_fscore(m) // -> 0
81 hg_ck(cnt, "H2 distressed firm -> FH_WEAK (0)" as *u8,
82 fh_analyze(0 - 100, 0 - 200, 0 - 50, 100, 800, 200, 1000, f0, 1000, 800, 200), FH_WEAK)
83
84 // ---- H3: THE STRUCTURAL GATE. great scores BUT the books do not balance -> FLAGGED, not STRONG ----
85 // assets 1000 != liabilities 200 + equity 700 (off by 100).
86 hg_ck(cnt, "H3 unbalanced books override good scores -> FH_FLAGGED (3)" as *u8,
87 fh_analyze(300, 400, 200, 800, 200, 500, 1000, f8, 1000, 200, 700), FH_FLAGGED)
88 hg_ckstr(cnt, "H3a verdict = FLAGGED (do not trust the scores)" as *u8,
89 fh_verdict_str(fh_analyze(300, 400, 200, 800, 200, 500, 1000, f8, 1000, 200, 700)), "FLAGGED" as *u8)
90
91 // ---- H4: THE MISSING-DATA GATE. an INCOMPLETE F-score dominates everything -> INCOMPLETE ----
92 // (even with a SAFE zone and balanced books).
93 hg_fill(m, 1000, 800, 600, 1200, 2000, 2500, 15000, 14000, 500, 500, 4200, 4000, 9000, 8500)
94 m[6] = PIO_NA // a fact could not be parsed
95 let finc: i64 = pio_fscore(m) // -> PIO_INCOMPLETE (-1)
96 hg_ck(cnt, "H4 missing fact -> FH_INCOMPLETE (-1), overrides SAFE+balanced" as *u8,
97 fh_analyze(300, 400, 200, 800, 200, 500, 1000, finc, 1000, 200, 800), FH_INCOMPLETE)
98 hg_ckstr(cnt, "H4a verdict = INCOMPLETE (a health call on partial facts is refused)" as *u8,
99 fh_verdict_str(fh_analyze(300, 400, 200, 800, 200, 500, 1000, finc, 1000, 200, 800)), "INCOMPLETE" as *u8)
100
101 // ---- H5: MODERATE -- SAFE zone but a middling F-score of 5, balanced ----
102 hg_ck(cnt, "H5 safe zone + mid F-score 5 -> FH_MODERATE (1)" as *u8,
103 fh_analyze(300, 400, 200, 800, 200, 500, 1000, 5, 1000, 200, 800), FH_MODERATE)
104
105 // ---- H6: priority -- INCOMPLETE beats FLAGGED (missing data is checked first) ----
106 hg_ck(cnt, "H6 missing data + unbalanced books -> INCOMPLETE (checked first)" as *u8,
107 fh_analyze(300, 400, 200, 800, 200, 500, 1000, 0 - 1, 1000, 200, 700), FH_INCOMPLETE)
108
109 hg_puts("nx_finhealth_gate: pass=" as *u8); hg_putn(cnt[0])
110 hg_puts(" fail=" as *u8); hg_putn(cnt[1]); hg_puts("\n" as *u8)
111 if cnt[1] == 0 {
112 hg_puts("FINHEALTH nx_finhealth: VERDICT=GREEN (composes Altman+Piotroski+identity; missing-data and unbalanced-books gate the verdict)\n" as *u8)
113 sys_exit(0)
114 return 0
115 }
116 hg_puts("FINHEALTH nx_finhealth: VERDICT=RED\n" as *u8)
117 sys_exit(1)
118 return 1
119}