sketch_ewma.nx
buildroot/runtime/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 · 2 importers
imports: syscalls.nxsketch_types.nx
imported by: sketch_ewma_test.nxsketch_holt_vs_ewma_trended_bench.nx
structs
| 40 | struct Ewma { |
consts
| 36 | const NX_EWMA_ALPHA_MIN: i64 = 1 |
| 37 | const NX_EWMA_ALPHA_MAX: i64 = 1000000 |
| 38 | const NX_EWMA_SCALE: i64 = 1000000 |
| 67 | const NX_EWMA_SAFE_LIMIT: i64 = 4000000000000 // 4e12 |
functions
| 49 | func nx_ewma_alloc(alpha_ppm: i64) -> *Ewma { |
| 69 | func nx_ewma_safe_p(sample: i64) -> i64 { |
| 81 | func nx_ewma_add(e: *Ewma, sample: i64) -> i64 { |
| 102 | func nx_ewma_value(e: *Ewma) -> i64 { |
| 106 | func nx_ewma_count(e: *Ewma) -> i64 {
called by 1: main |
| 112 | func nx_ewma_effective_window(e: *Ewma) -> i64 {
called by 1: main |
| 122 | func nx_ewma_query(e: *Ewma) -> *ApproxI64 { |
| 129 | func nx_ewma_memory_bytes(e: *Ewma) -> i64 { |