sketch_zscore.nx
buildroot/runtime/sketch_zscore.nx
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
imports: syscalls.nxsketch_stream_stats.nxsketch_types.nx
imported by: sketch_zscore_test.nx
structs
| 38 | struct ZScore { |
consts
| 32 | const NX_ZS_VERDICT_NORMAL: i64 = 0 |
| 33 | const NX_ZS_VERDICT_HIGH: i64 = 1 |
| 34 | const NX_ZS_VERDICT_LOW: i64 = 2 |
| 36 | const NX_ZS_MIN_COUNT_DEFAULT: i64 = 30 |
functions
| 50 | func nx_zs_alloc(threshold_ppm: i64, min_count: i64) -> *ZScore { |
| 75 | func nx_zs_score_ppm(z: *ZScore, x: i64) -> i64 { |
| 85 | func nx_zs_verdict(z: *ZScore, x: i64) -> i64 { |
| 94 | func nx_zs_step(z: *ZScore, x: i64) -> i64 { |
| 103 | func nx_zs_count(z: *ZScore) -> i64 { |
| 107 | func nx_zs_mean(z: *ZScore) -> i64 { |
| 111 | func nx_zs_stddev(z: *ZScore) -> i64 {
calls 1: nx_stats_stddev |
| 115 | func nx_zs_n_alarms(z: *ZScore) -> i64 {
called by 1: main |
| 119 | func nx_zs_query(z: *ZScore, x: i64) -> *ApproxI64 { |
| 126 | func nx_zs_memory_bytes(z: *ZScore) -> i64 {
calls 1: nx_stats_memory_bytes |