nx_sketch_ewma.nx
buildroot/runtime/nx_sketch_ewma.nx
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
imports: nx_syscalls.nxnx_sketch_types.nx
imported by: nobody (leaf or entry point)
structs
| 46 | struct Ewma |
consts
| 42 | const NX_EWMA_ALPHA_MIN: i64 = 1 |
| 43 | const NX_EWMA_ALPHA_MAX: i64 = 1000000 |
| 44 | const NX_EWMA_SCALE: i64 = 1000000 |
| 73 | const NX_EWMA_SAFE_LIMIT: i64 = 4000000000000 // 4e12 |
functions
| 55 | func nx_ewma_alloc(alpha_ppm: i64) -> *Ewma calls 1: sys_mmap |
| 75 | func nx_ewma_safe_p(sample: i64) -> i64 called by 1: nx_ewma_add |
| 87 | func nx_ewma_add(e: *Ewma, sample: i64) -> i64 calls 1: nx_ewma_safe_p |
| 108 | func nx_ewma_value(e: *Ewma) -> i64 |
| 112 | func nx_ewma_count(e: *Ewma) -> i64 |
| 118 | func nx_ewma_effective_window(e: *Ewma) -> i64 |
| 128 | func nx_ewma_query(e: *Ewma) -> *ApproxI64 calls 1: nx_approx_new |
| 135 | func nx_ewma_memory_bytes(e: *Ewma) -> i64 |