code wiki / (root) / nx_dcf_gate.nx

nx_dcf_gate.nx source

↩ module page · 102 lines · 4903 B

1// nx_dcf_gate.nx -- DISCOUNTED CASH FLOW GATE (intrinsic valuation). 2// Proves exact integer discounting, the Gordon terminal value, a full intrinsic valuation matching a 3// hand computation, the margin-of-safety verdict, and the fail-closed DCF trap (growth >= rate refuses). 4// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0 5 6import "nx_dcf_lib.nx" 7 8func dg_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 9func dg_putn(v: i64) -> i64 { 10 let t: *u8 = sys_mmap(32) 11 var o: i64 = 0 12 var m: i64 = v 13 if m < 0 { t[o] = 45 as u8; o = o + 1; m = 0 - m } 14 let d: *u8 = sys_mmap(32) 15 var k: i64 = 0 16 if m == 0 { d[0] = 48 as u8; k = 1 } 17 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 18 var i: i64 = 0 19 while i < k { t[o] = d[k - 1 - i]; o = o + 1; i = i + 1 } 20 sys_write(1, t, o) 21 return 0 22} 23func dg_ck(cnt: *i64, name: *u8, got: i64, want: i64) -> i64 { 24 if got == want { 25 cnt[0] = cnt[0] + 1 26 dg_puts(" PASS " as *u8); dg_puts(name); dg_puts(" = " as *u8); dg_putn(got); dg_puts("\n" as *u8) 27 return 1 28 } 29 cnt[1] = cnt[1] + 1 30 dg_puts(" FAIL " as *u8); dg_puts(name); dg_puts(" got " as *u8); dg_putn(got) 31 dg_puts(" want " as *u8); dg_putn(want); dg_puts("\n" as *u8) 32 return 0 33} 34func dg_ckstr(cnt: *i64, name: *u8, got: *u8, want: *u8) -> i64 { 35 if mt_streq(got, want) == 1 { 36 cnt[0] = cnt[0] + 1 37 dg_puts(" PASS " as *u8); dg_puts(name); dg_puts(" = " as *u8); dg_puts(got); dg_puts("\n" as *u8) 38 return 1 39 } 40 cnt[1] = cnt[1] + 1 41 dg_puts(" FAIL " as *u8); dg_puts(name); dg_puts(" got " as *u8); dg_puts(got); dg_puts("\n" as *u8) 42 return 0 43} 44 45func main(argc: i64, argv: *i64) -> i64 { 46 let cnt: *i64 = sys_mmap(16) as *i64 47 cnt[0] = 0 48 cnt[1] = 0 49 50 dg_puts("NISHI-DCF-GATE (discounted cash flow, intrinsic value, integer-exact, fail-closed on the trap)\n" as *u8) 51 52 // ---- D1: discount factors at 10% (rate_bp 1000), scaled x1e6 ---- 53 dg_ck(cnt, "D1 df period 0 = 1000000 (1.0)" as *u8, dcf_discount_factor(1000, 0), 1000000) 54 dg_ck(cnt, "D1a df period 1 at 10pct = 909090" as *u8, dcf_discount_factor(1000, 1), 909090) 55 dg_ck(cnt, "D1b df period 2 = 826445" as *u8, dcf_discount_factor(1000, 2), 826445) 56 dg_ck(cnt, "D1c df period 3 = 751313" as *u8, dcf_discount_factor(1000, 3), 751313) 57 58 // ---- D2: present value of a single cashflow ---- 59 dg_ck(cnt, "D2 PV of 100 at 10pct in 1yr = 90" as *u8, dcf_pv(100, 1000, 1), 90) 60 61 // ---- D3: Gordon terminal value. final 121, g 2pct (200bp), r 10pct (1000bp) -> 121*10200/800 = 1542 ---- 62 dg_ck(cnt, "D3 terminal value = 1542" as *u8, dcf_terminal(121, 200, 1000), 1542) 63 64 // ---- D4: THE TRAP. growth >= rate -> DCF_UNDEFINED, never a garbage valuation ---- 65 dg_ck(cnt, "D4 growth 1000 == rate 1000 -> DCF_UNDEFINED" as *u8, dcf_terminal(121, 1000, 1000), DCF_UNDEFINED) 66 dg_ck(cnt, "D4a growth 1200 > rate 1000 -> DCF_UNDEFINED" as *u8, dcf_terminal(121, 1200, 1000), DCF_UNDEFINED) 67 68 // ---- D5: FULL INTRINSIC VALUE. cashflows [100,110,121] at 10pct + terminal 1542 at period 3 ---- 69 // PV: 100->90, 110->90, 121->90, terminal 1542->1158. sum = 1428. (hand-verified integer truncation) 70 let cf: *i64 = sys_mmap(8 * 3) as *i64 71 cf[0] = 100 72 cf[1] = 110 73 cf[2] = 121 74 let tv: i64 = dcf_terminal(121, 200, 1000) 75 dg_ck(cnt, "D5 intrinsic value = 1428" as *u8, dcf_intrinsic(cf, 3, 1000, tv), 1428) 76 77 // ---- D6: the trap PROPAGATES -- an undefined terminal makes the whole intrinsic undefined ---- 78 dg_ck(cnt, "D6 undefined terminal -> intrinsic DCF_UNDEFINED" as *u8, 79 dcf_intrinsic(cf, 3, 1000, dcf_terminal(121, 1200, 1000)), DCF_UNDEFINED) 80 81 // ---- D7: MARGIN OF SAFETY + verdict ---- 82 let iv: i64 = 1428 83 dg_ck(cnt, "D7 margin of safety vs price 1000 = 4280 bp (42.8pct)" as *u8, dcf_margin_of_safety_bp(iv, 1000), 4280) 84 dg_ck(cnt, "D7a price 1000 -> UNDERVALUED (1)" as *u8, dcf_verdict(iv, 1000), 1) 85 dg_ckstr(cnt, "D7b verdict = UNDERVALUED" as *u8, dcf_verdict_str(dcf_verdict(iv, 1000)), "UNDERVALUED" as *u8) 86 dg_ck(cnt, "D7c price 1500 (within 15pct) -> FAIRLY-VALUED (0)" as *u8, dcf_verdict(iv, 1500), 0) 87 dg_ck(cnt, "D7d price 2000 -> OVERVALUED (-1)" as *u8, dcf_verdict(iv, 2000), 0 - 1) 88 89 // ---- D8: fail-closed on a bad price ---- 90 dg_ck(cnt, "D8 price 0 -> DCF_BAD" as *u8, dcf_verdict(iv, 0), DCF_BAD) 91 92 dg_puts("nx_dcf_gate: pass=" as *u8); dg_putn(cnt[0]) 93 dg_puts(" fail=" as *u8); dg_putn(cnt[1]); dg_puts("\n" as *u8) 94 if cnt[1] == 0 { 95 dg_puts("DCF nx_dcf: VERDICT=GREEN (exact intrinsic value + margin of safety; refuses the growth>=rate trap)\n" as *u8) 96 sys_exit(0) 97 return 0 98 } 99 dg_puts("DCF nx_dcf: VERDICT=RED\n" as *u8) 100 sys_exit(1) 101 return 1 102}