code wiki / (root) / sketch_geomean.nx

sketch_geomean.nx

buildroot/runtime/sketch_geomean.nx

4746 B141 linesdepth 4pulls 4 transitivereach 1 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 · 1 importers

syscalls.nx sketch_types.nx sketch_geomean.nx sketch_geomean_test.nx

imports: syscalls.nxsketch_types.nx

imported by: sketch_geomean_test.nx

structs

33struct Geomean {

consts

none

functions

41func nx_gm_alloc() -> *Geomean {
called by 2: nx_gm_mergemain
52func nx_gm_log2_floor(x: i64) -> i64 {
called by 1: nx_gm_add
68func nx_gm_add(g: *Geomean, value: i64) -> i64 {
called by 1: main calls 1: nx_gm_log2_floor
86func nx_gm_pow2_floor(x_ppm: i64) -> i64 {
called by 1: nx_gm_value
104func nx_gm_value(g: *Geomean) -> i64 {
called by 2: nx_gm_querymain calls 1: nx_gm_pow2_floor
111func nx_gm_log_mean_ppm(g: *Geomean) -> i64 {
called by 1: main
118func nx_gm_query(g: *Geomean) -> *ApproxI64 {
called by 1: main calls 2: nx_gm_valuenx_approx_new
126func nx_gm_count(g: *Geomean) -> i64 { return g.n }
called by 1: main
127func nx_gm_count_positive(g: *Geomean) -> i64 { return g.n_positive }
called by 1: main
129func nx_gm_memory_bytes(g: *Geomean) -> i64 {
135func nx_gm_merge(a: *Geomean, b: *Geomean) -> *Geomean {
called by 1: main calls 1: nx_gm_alloc