nx_var_lib.nx
buildroot/runtime/nx_var_lib.nx
about
nx_var_lib.nx -- F991 (risk core): VALUE-AT-RISK + EXPECTED SHORTFALL, integer-exact, fail-closed.
A risk number computed from too few observations is worse than no number -- it looks authoritative and
is meaningless, and that is how blow-ups happen. So the sovereign property here is that the tail is
only reported when the data can support it: a 99% VaR needs at least ~100 observations to have a single
point in the tail, and asking for one from 10 data points REFUSES (fail-closed) rather than inventing a
quantile. Everything is integer minor units, no float -- a reproducible risk number an auditor can redo.
Two measures:
Historical VaR -- the loss at the confidence quantile (the threshold a loss exceeds only (1-c) of the time)
Expected Shortfall (CVaR) -- the MEAN of the losses AT OR BEYOND the VaR quantile. This is the coherent,
sub-additive tail measure Basel III adopted over VaR; ES >= VaR always, and it sees
the shape of the tail VaR ignores. Reporting ES is the state of the art.
Convention: losses[] are POSITIVE numbers (a loss of 500 = 500); larger = worse.
SCALE ENVELOPE (declared): var_sort is selection sort O(n^2) on a COPY (never mutates the caller's array);
a merge/quick sort is the rung for very long series. DRY: composes nx_matter_lib helpers. license_tier: ORIGINAL LIB.
dependencies 1 imports · 1 importers
imports: nx_matter_lib.nx
imported by: nx_var_gate.nx
structs
| none |
consts
| 21 | const VAR_BAD: i64 = 0 - 1 // invalid confidence (<=0 or >=100) |
| 22 | const VAR_INSUFFICIENT: i64 = 0 - 2 // not enough observations for this confidence (fail-closed) |
functions
| 25 | func var_sort(a: *i64, n: i64) -> i64 called by 1: var_sorted_copy |
| 45 | func var_min_sample(confidence_pct: i64) -> i64 |
| 51 | func var_sorted_copy(losses: *i64, n: i64) -> *i64 |
| 60 | func var_index(n: i64, confidence_pct: i64) -> i64 |
| 68 | func var_historical(losses: *i64, n: i64, confidence_pct: i64) -> i64 |
| 78 | func var_expected_shortfall(losses: *i64, n: i64, confidence_pct: i64) -> i64 |