code wiki / (root) / nx_sketch_ewma.nx

nx_sketch_ewma.nx

buildroot/runtime/nx_sketch_ewma.nx

4334 B137 linesdepth 3pulls 3 transitivereach 0 importersview sourcekind sketch/demotopic sketch
docsdependenciesstructsconstsfunctions

about

sketch_ewma.nx -- exponentially-weighted moving average (EWMA). Single-pass time-series smoother: x_new = alpha * sample + (1 - alpha) * x_old Smaller alpha = more smoothing (longer memory). alpha=1 means "no smoothing" -- track current sample only. alpha near 0 means "very long memory." PARAMETERIZATION: alpha is expressed in parts-per-million (PPM). Valid range [1, 1_000_000]. alpha_ppm=10000 = 0.01 = "Q1 weight of 1% per sample" ~ effective window ~100 samples. INTEGER ARITHMETIC (no f64 in sibling): x_new = (alpha_ppm * sample + (1e6 - alpha_ppm) * x_old) / 1e6 OVERFLOW BUDGET: |sample|, |x_old| < 2^32 to avoid overflow in alpha * sample. For |sample| up to 2^43 (~8.8e12) safe given alpha_ppm <= 1e6 (2^20). USE CASES: - SRE latency smoothing (oncall dashboards) - financial price tracking (EMA in technical analysis) - sensor smoothing (IoT temperature/humidity) LOSSLESS-LANGUAGE DISCIPLINE: EWMA's "approximation" is the systematic bias toward recent samples -- a feature, not loss. We declare envelope_kind = NX_ENV_ABS, param_a = quantization error per step (<= 1). MaturityClass = Production (deterministic recurrence, no probabilistic bounds).

dependencies 2 imports · 0 importers

nx_syscalls.nx nx_sketch_types.nx nx_sketch_ewma.nx

imports: nx_syscalls.nxnx_sketch_types.nx

imported by: nobody (leaf or entry point)

structs

46struct Ewma

consts

42const NX_EWMA_ALPHA_MIN: i64 = 1
43const NX_EWMA_ALPHA_MAX: i64 = 1000000
44const NX_EWMA_SCALE: i64 = 1000000
73const NX_EWMA_SAFE_LIMIT: i64 = 4000000000000 // 4e12

functions

55func nx_ewma_alloc(alpha_ppm: i64) -> *Ewma
calls 1: sys_mmap
75func nx_ewma_safe_p(sample: i64) -> i64
called by 1: nx_ewma_add
87func nx_ewma_add(e: *Ewma, sample: i64) -> i64
calls 1: nx_ewma_safe_p
108func nx_ewma_value(e: *Ewma) -> i64
112func nx_ewma_count(e: *Ewma) -> i64
118func nx_ewma_effective_window(e: *Ewma) -> i64
128func nx_ewma_query(e: *Ewma) -> *ApproxI64
calls 1: nx_approx_new
135func nx_ewma_memory_bytes(e: *Ewma) -> i64