code wiki / (root) / nx_shapley.nx

nx_shapley.nx

buildroot/runtime/nx_shapley.nx

16341 B474 linesdepth 4pulls 5 transitivereach 0 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

about

nx_shapley.nx -- game-theoretic feature attribution. Shapley 1953 ("A Value for n-Person Games"): the UNIQUE fair allocation of a coalition's value back to its members. The substrate's third attribution primitive after nx_attribution (top-K of W @ x) and nx_patch_attribution (Cohen's d per axis). phi_i = sum over S subset N\{i} of |S|! * (N-|S|-1)! / N! * [ f(S union {i}) - f(S) ] Closes the WORLD-CLASS attribution gap named in nxc2/docs/TOPDOWN_BOTTOMUP_GAP_ANALYSIS.md. No existing image-gen / LLM stack ships Shapley in typed bits-up form; they all defer to SHAP / KernelSHAP Python libs that hide per-coalition evaluation under floating-point opacity. Two modes: EXACT -- N <= 10 features; full 2^N coalition enumeration. SAMPLED -- N > 10; Castro/Gomez/Tejada 2009 permutation sampling estimator. Unbiased. Caller supplies f(active_mask, n_features, ctx) returning a scalar value in Q10. Substrate computes the contribution vector phi[] (Q10 each) such that sum(phi) + baseline = f(all_on). genealogy_id: shapley_1953_castro_2009 lineage_id: substrate_attribution_v3 5W+H+GLP linkage: each phi[i] is the WHY answer for "feature i contributed how much to output f(all_on)". Caller emits one Nx5whRecord with what=ATTRIBUTION per non-trivial phi[i].

dependencies 4 imports · 0 importers

nx_syscalls.nx nx_runtime.nx nx_tier.nx nx_random.nx nx_shapley.nx

imports: nx_syscalls.nxnx_runtime.nxnx_tier.nxnx_random.nx

imported by: nobody (leaf or entry point)

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main nx_shapley_compute_exact sys_mmap _shapley_popcount _shapley_bit_test _shapley_bit_set _shapley_weight_q10 _shapley_factorial nx_shapley_efficiency_resi nx_shapley_compute_sampled sys_mmap ↻ nx_rng_new nx_rng_splitmix _shapley_permute nx_rng_below nx_rng_next nx_rng_rotl _shapley_bit_set ↻ sys_mmap ↻ nx_shapley_topk sys_mmap ↻

structs

69struct NxShapleyResult

consts

43const NX_MAGIC_5040: i64 = 5040
44const NX_MAGIC_40320: i64 = 40320
45const NX_MAGIC_362880: i64 = 362880
46const NX_MAGIC_3628800: i64 = 3628800
47const NX_MAGIC_4096: i64 = 4096
51const NX_SHAPLEY_MODE_EXACT: nx_int = 0
52const NX_SHAPLEY_MODE_SAMPLED: nx_int = 1
55const NX_SHAPLEY_MAX_EXACT_N: nx_int = 10
58const NX_SHAPLEY_MAX_FEATURES: nx_int = 256
61const NX_SHAPLEY_DEFAULT_SAMPLES: nx_int = 2048
62const NX_SHAPLEY_MAX_SAMPLES: nx_int = 65536
65const NX_SHAPLEY_Q10: nx_int = 1024
78const NX_SHAPLEY_RESULT_BYTES: nx_size = 48

functions

86func _shapley_factorial(n: nx_int) -> nx_int
called by 1: _shapley_weight_q10
106func _shapley_weight_q10(s: nx_int, n: nx_int) -> nx_int
120func _shapley_popcount(mask: nx_int) -> nx_int
134func _shapley_bit_set(mask: nx_int, bit: nx_int) -> nx_int
142func _shapley_bit_test(mask: nx_int, bit: nx_int) -> nx_int
156func nx_shapley_compute_exact(
232func _shapley_permute(perm: *nx_int, n: nx_int, rng: *NxRng) -> nx_int
251func nx_shapley_compute_sampled(
327func nx_shapley_efficiency_residual(
called by 1: main
349func nx_shapley_topk(
called by 1: main calls 1: sys_mmap
397func _test_fn_additive(active_mask: nx_int, n_features: nx_int, ctx: *u8) -> nx_int
407func _test_fn_and_gate(active_mask: nx_int, n_features: nx_int, ctx: *u8) -> nx_int
418func main() -> nx_int