nx_fin_audit.nx source
↩ module page · 109 lines · 5281 B
1// nx_fin_audit.nx -- R5 of THE NISHI FINANCIAL ECOSYSTEM: medical-bill OVERBILLING / anomaly audit.
2// Dogfooded on the operator's multi-year cancer medical-overbilling crisis. Built on nx_money (exact
3// no-float cents). DATA-DRIVEN BY CONSTRUCTION (#6/#11/#25): the engine has ZERO hardcoded CPT codes,
4// thresholds, or multipliers -- every rule is a PACK passed in as data (the packs are grounded on the
5// fetched NCCI/MUE/Medicare corpus and, in R6, loaded from a sovereign rule-pack store). Each check is a
6// distinct LENS; a line can flag under more than one (reported per-check, never silently summed into one
7// number that double-counts). Every overcharge is exact integer cents.
8//
9// CHECKS (each returns a flag count, writes flagged line indices to out_idx[], total overcharge to out_oc[0]):
10// aud_duplicates -- same (code, date) billed more than once -> the repeat is the overcharge
11// aud_unbundle -- a comprehensive code billed WITH a component code that NCCI bundles into it ->
12// the component's charge is the overcharge (pack = ordered (comprehensive, component) pairs)
13// aud_mue -- units billed exceed a code's Medically-Unlikely-Edit max -> the excess-units charge
14// aud_fairprice -- billed exceeds (Medicare allowed * multiplier%) -> the amount above the fair ceiling
15// No hardware writes; pure analysis over caller arrays (never mutates input). license_tier: ORIGINAL
16import "nx_money.nx"
17
18// linear lookup: find `key` in keys[0..n); return the paired vals[i], or -1 if absent.
19func aud_lookup(keys: *i64, vals: *i64, n: i64, key: i64) -> i64 {
20 var i: i64 = 0
21 while i < n { if keys[i] == key { return vals[i] } i = i + 1 }
22 return 0 - 1
23}
24
25// DUPLICATES: a line i is a duplicate iff an earlier line j<i has the same (code, date). The duplicate's
26// billed amount is the overcharge (you should not pay the same service twice on the same date).
27// out_idx[k]=flagged line index, out_amt[k]=that finding's overcharge cents, out_oc[0]=total.
28func aud_duplicates(codes: *i64, dates: *i64, billed: *i64, n: i64, out_idx: *i64, out_amt: *i64, out_oc: *i64) -> i64 {
29 var cnt: i64 = 0
30 var oc: i64 = 0
31 var i: i64 = 0
32 while i < n {
33 var dup: i64 = 0
34 var j: i64 = 0
35 while j < i { if codes[j] == codes[i] { if dates[j] == dates[i] { dup = 1; j = i } } j = j + 1 }
36 if dup == 1 { out_idx[cnt] = i; out_amt[cnt] = billed[i]; cnt = cnt + 1; oc = mny_add(oc, billed[i]) }
37 i = i + 1
38 }
39 out_oc[0] = oc
40 return cnt
41}
42
43// UNBUNDLING: pack = ordered (comprehensive, component) code pairs. If BOTH the comprehensive and the
44// component appear on the bill, the component should have been bundled -> its charge is the overcharge.
45func aud_unbundle(codes: *i64, n: i64, pairA: *i64, pairB: *i64, np: i64, billed: *i64, out_idx: *i64, out_amt: *i64, out_oc: *i64) -> i64 {
46 var cnt: i64 = 0
47 var oc: i64 = 0
48 var p: i64 = 0
49 while p < np {
50 var hasA: i64 = 0
51 var idxB: i64 = 0 - 1
52 var i: i64 = 0
53 while i < n {
54 if codes[i] == pairA[p] { hasA = 1 }
55 if codes[i] == pairB[p] { if idxB < 0 { idxB = i } }
56 i = i + 1
57 }
58 if hasA == 1 { if idxB >= 0 {
59 out_idx[cnt] = idxB; out_amt[cnt] = billed[idxB]; cnt = cnt + 1
60 oc = mny_add(oc, billed[idxB])
61 } }
62 p = p + 1
63 }
64 out_oc[0] = oc
65 return cnt
66}
67
68// MUE: pack = (code, max-units). A line whose units exceed its code's max is flagged; the overcharge is
69// the excess-units share of the billed amount = round(billed * (units - max) / units), exact.
70func aud_mue(codes: *i64, units: *i64, billed: *i64, n: i64, mcode: *i64, mmax: *i64, nm: i64, out_idx: *i64, out_amt: *i64, out_oc: *i64) -> i64 {
71 var cnt: i64 = 0
72 var oc: i64 = 0
73 var i: i64 = 0
74 while i < n {
75 let mx: i64 = aud_lookup(mcode, mmax, nm, codes[i])
76 if mx >= 0 { if units[i] > mx {
77 let excess: i64 = units[i] - mx
78 let ocl: i64 = mny_div_round(billed[i] * excess, units[i], RND_HALF_EVEN)
79 out_idx[cnt] = i; out_amt[cnt] = ocl; cnt = cnt + 1
80 oc = mny_add(oc, ocl)
81 } }
82 i = i + 1
83 }
84 out_oc[0] = oc
85 return cnt
86}
87
88// FAIR-PRICE: pack = (code, Medicare-allowed cents). A line is flagged if billed exceeds the fair ceiling
89// = round(allowed * mult_pct / 100) (e.g. mult_pct=400 -> 4x Medicare). Overcharge = billed - ceiling.
90// Lines whose code has no benchmark are skipped (cannot assess). mult_pct is a data knob (#11), not magic.
91func aud_fairprice(codes: *i64, billed: *i64, n: i64, bcode: *i64, ballow: *i64, nb: i64, mult_pct: i64, out_idx: *i64, out_amt: *i64, out_oc: *i64) -> i64 {
92 var cnt: i64 = 0
93 var oc: i64 = 0
94 var i: i64 = 0
95 while i < n {
96 let allow: i64 = aud_lookup(bcode, ballow, nb, codes[i])
97 if allow >= 0 {
98 let ceiling: i64 = mny_div_round(allow * mult_pct, 100, RND_HALF_EVEN)
99 if billed[i] > ceiling {
100 let over: i64 = billed[i] - ceiling
101 out_idx[cnt] = i; out_amt[cnt] = over; cnt = cnt + 1
102 oc = mny_add(oc, over)
103 }
104 }
105 i = i + 1
106 }
107 out_oc[0] = oc
108 return cnt
109}