nx_sketch_geomean.nx
buildroot/runtime/nx_sketch_geomean.nx
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
imports: nx_syscalls.nxnx_sketch_types.nx
imported by: nobody (leaf or entry point)
structs
| 39 | struct Geomean |
consts
| none |
functions
| 47 | func nx_gm_alloc() -> *Geomean |
| 58 | func nx_gm_log2_floor(x: i64) -> i64 called by 1: nx_gm_add |
| 74 | func nx_gm_add(g: *Geomean, value: i64) -> i64 calls 1: nx_gm_log2_floor |
| 92 | func nx_gm_pow2_floor(x_ppm: i64) -> i64 called by 1: nx_gm_value |
| 110 | func nx_gm_value(g: *Geomean) -> i64 |
| 117 | func nx_gm_log_mean_ppm(g: *Geomean) -> i64 |
| 124 | func nx_gm_query(g: *Geomean) -> *ApproxI64 |
| 132 | func nx_gm_count(g: *Geomean) -> i64 { return g.n } |
| 133 | func nx_gm_count_positive(g: *Geomean) -> i64 { return g.n_positive } |
| 135 | func nx_gm_memory_bytes(g: *Geomean) -> i64 |
| 141 | func nx_gm_merge(a: *Geomean, b: *Geomean) -> *Geomean calls 1: nx_gm_alloc |