code wiki / (root) / sketch_ewma.nx

sketch_ewma.nx

buildroot/runtime/sketch_ewma.nx

4208 B131 linesdepth 4pulls 4 transitivereach 2 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 · 2 importers

syscalls.nx sketch_types.nx sketch_ewma.nx sketch_ewma_test.nx sketch_holt_vs_ewma_trended_bench.

imports: syscalls.nxsketch_types.nx

imported by: sketch_ewma_test.nxsketch_holt_vs_ewma_trended_bench.nx

structs

40struct Ewma {

consts

36const NX_EWMA_ALPHA_MIN: i64 = 1
37const NX_EWMA_ALPHA_MAX: i64 = 1000000
38const NX_EWMA_SCALE: i64 = 1000000
67const NX_EWMA_SAFE_LIMIT: i64 = 4000000000000 // 4e12

functions

49func nx_ewma_alloc(alpha_ppm: i64) -> *Ewma {
called by 2: mainmain
69func nx_ewma_safe_p(sample: i64) -> i64 {
called by 2: nx_ewma_addmain
81func nx_ewma_add(e: *Ewma, sample: i64) -> i64 {
called by 2: mainmain calls 1: nx_ewma_safe_p
102func nx_ewma_value(e: *Ewma) -> i64 {
called by 2: mainmain
106func nx_ewma_count(e: *Ewma) -> i64 {
called by 1: main
112func nx_ewma_effective_window(e: *Ewma) -> i64 {
called by 1: main
122func nx_ewma_query(e: *Ewma) -> *ApproxI64 {
called by 1: main calls 1: nx_approx_new
129func nx_ewma_memory_bytes(e: *Ewma) -> i64 {