code wiki / (root) / nx_sketch_geomean.nx

nx_sketch_geomean.nx

buildroot/runtime/nx_sketch_geomean.nx

4862 B147 linesdepth 3pulls 3 transitivereach 0 importersview sourcekind sketch/demotopic sketch
docsdependenciesstructsconstsfunctions

about

sketch_geomean.nx -- streaming geometric mean via log-space accumulation. Geometric mean = (Π x_i)^(1/n) = 2^((Σ log_2 x_i) / n) Naive product overflows immediately (n=20, x=10 -> product ~ 10^20 >> i64). We accumulate in log_2 domain instead: log space is additive and bounded. USE CASES (where geometric mean beats arithmetic mean): - growth-rate averaging (CAGR / compound returns) - ratios (speedup factors, multiplicative gain) - normalized scores (e.g., averaging across scales) - magnitude data (sizes, populations, intensities) INTEGER LOG-BASE-2 (no f64): floor(log_2(x)) via bit_length(x) - 1 For finer fractional bits, mantissa interpolation could be added (queued for v2). SUM_LOG_2 fixed-point: track in PPM scale. Each insert contributes (bitlen - 1) * 1_000_000. Geometric mean recovered via 2^(sum/n_ppm). For 2^x where x is in PPM (fractional log_2): floor(x / 1_000_000) + linear interpolation on the fractional part. Returns approximate geometric mean. LOSSLESS-LANGUAGE DISCIPLINE: NX_ENV_REL_STDDEV with param_a reflecting quantization (use 50_000_000 ppb = 5% as conservative bound for floor-log estimator). ReferenceImpl tier.

dependencies 2 imports · 0 importers

nx_syscalls.nx nx_sketch_types.nx nx_sketch_geomean.nx

imports: nx_syscalls.nxnx_sketch_types.nx

imported by: nobody (leaf or entry point)

structs

39struct Geomean

consts

none

functions

47func nx_gm_alloc() -> *Geomean
called by 1: nx_gm_merge calls 1: sys_mmap
58func nx_gm_log2_floor(x: i64) -> i64
called by 1: nx_gm_add
74func nx_gm_add(g: *Geomean, value: i64) -> i64
92func nx_gm_pow2_floor(x_ppm: i64) -> i64
called by 1: nx_gm_value
110func nx_gm_value(g: *Geomean) -> i64
called by 1: nx_gm_query calls 1: nx_gm_pow2_floor
117func nx_gm_log_mean_ppm(g: *Geomean) -> i64
124func nx_gm_query(g: *Geomean) -> *ApproxI64
132func nx_gm_count(g: *Geomean) -> i64 { return g.n }
133func nx_gm_count_positive(g: *Geomean) -> i64 { return g.n_positive }
135func nx_gm_memory_bytes(g: *Geomean) -> i64
141func nx_gm_merge(a: *Geomean, b: *Geomean) -> *Geomean
calls 1: nx_gm_alloc