code wiki / (root) / sketch_zscore.nx

sketch_zscore.nx

buildroot/runtime/sketch_zscore.nx

4232 B128 linesdepth 5pulls 5 transitivereach 1 importersview sourcekind sketch/demotopic sketch
docsdependenciesstructsconstsfunctions

about

sketch_zscore.nx -- point z-score anomaly detector. Composes against sketch_stream_stats (running mean + stddev) to flag individual samples deviating from the streaming baseline: z = (x - mean) / stddev alarm if |z| > threshold (typical 2.0 ~ 95% conf, 3.0 ~ 99.7%) CAPABILITY: - O(1) state (mean + variance from StreamStats) - online: baseline updates with every sample - Returns Z in PPM ([-1e10, +1e10] practical range) - Sealed verdict: NORMAL / HIGH / LOW COMPLEMENTS THE ANOMALY FAMILY: - sketch_cusum cumulative shift over a window - sketch_mann_kendall monotonic trend test - sketch_zscore (this) point outlier vs running baseline CONFIGURATION: "warm-up" period: do not raise alarms until count >= min_count. Default: min_count = 30 (statistician's rule-of-thumb for CLT). LOSSLESS-LANGUAGE DISCIPLINE: returns z in PPM with NX_ENV_ABS, param_a = 1 (PPM quantization). Production tier; exact integer arithmetic over the running statistics.

dependencies 3 imports · 1 importers

syscalls.nx sketch_stream_stats.nx sketch_types.nx sketch_zscore.nx sketch_zscore_test.nx

imports: syscalls.nxsketch_stream_stats.nxsketch_types.nx

imported by: sketch_zscore_test.nx

structs

38struct ZScore {

consts

32const NX_ZS_VERDICT_NORMAL: i64 = 0
33const NX_ZS_VERDICT_HIGH: i64 = 1
34const NX_ZS_VERDICT_LOW: i64 = 2
36const NX_ZS_MIN_COUNT_DEFAULT: i64 = 30

functions

50func nx_zs_alloc(threshold_ppm: i64, min_count: i64) -> *ZScore {
called by 1: main calls 1: nx_stats_alloc
75func nx_zs_score_ppm(z: *ZScore, x: i64) -> i64 {
85func nx_zs_verdict(z: *ZScore, x: i64) -> i64 {
94func nx_zs_step(z: *ZScore, x: i64) -> i64 {
called by 1: main calls 2: nx_zs_verdictnx_stats_add
103func nx_zs_count(z: *ZScore) -> i64 {
called by 1: main calls 1: nx_stats_count
107func nx_zs_mean(z: *ZScore) -> i64 {
called by 1: main calls 1: nx_stats_mean
111func nx_zs_stddev(z: *ZScore) -> i64 {
calls 1: nx_stats_stddev
115func nx_zs_n_alarms(z: *ZScore) -> i64 {
called by 1: main
119func nx_zs_query(z: *ZScore, x: i64) -> *ApproxI64 {
called by 1: main calls 2: nx_zs_verdictnx_approx_new
126func nx_zs_memory_bytes(z: *ZScore) -> i64 {