code wiki / (root) / nx_sketch_ams.nx

nx_sketch_ams.nx

buildroot/runtime/nx_sketch_ams.nx

6453 B217 linesdepth 3pulls 3 transitivereach 0 importersview sourcekind sketch/demotopic sketch
docsdependenciesstructsconstsfunctions

about

sketch_ams.nx -- AMS sketch (Alon-Matias-Szegedy 1996). Estimates F_2 = Σ f_i² (second frequency moment) of a stream over implicit-keyed items. Used for: - self-join size estimation in databases - query-plan cardinality estimation - skew detection (high F_2 = skewed distribution) - L2 norm of frequency vector ALGORITHM: For each of d * s estimators, a random ±1 sign function ξ_jk. On (item x, count c): counter[j][k] += ξ_jk(x) * c for all (j,k). F_2 estimate per estimator: counter[j][k]² Within-group AVERAGE: F_2_j = mean of counter[j][k]² over k. Across-group MEDIAN: F_2 ≈ median(F_2_j) across j. Variance: average reduces variance by 1/s. Median over d independent estimates boosts confidence to 1 - 2^(-d/2). MEMORY: d * s * 8 bytes. d=5, s=64 -> 2560 bytes for F_2 with 12.5% relative error at 87.5% confidence. LOSSLESS-LANGUAGE DISCIPLINE: nx_ams_query returns ApproxI64 with NX_ENV_REL_STDDEV = 1/sqrt(s) per estimator (further tightened by median-of-d boost in practice). conf_ppb tracks (1 - 2^(-d/2)).

dependencies 2 imports · 0 importers

nx_syscalls.nx nx_sketch_types.nx nx_sketch_ams.nx

imports: nx_syscalls.nxnx_sketch_types.nx

imported by: nobody (leaf or entry point)

structs

41struct AMS

consts

36const NX_AMS_MIN_D: i64 = 3
37const NX_AMS_MAX_D: i64 = 32
38const NX_AMS_MIN_S: i64 = 4
39const NX_AMS_MAX_S: i64 = 1024

functions

51func nx_ams_alloc(d: i64, s: i64, seed: i64) -> *AMS
called by 1: nx_ams_merge calls 1: sys_mmap
77func nx_ams_sign(a: *AMS, key: i64, j: i64, k: i64) -> i64
called by 1: nx_ams_add
84func nx_ams_cell_idx(a: *AMS, j: i64, k: i64) -> i64
called by 2: nx_ams_addnx_ams_f2
90func nx_ams_add(a: *AMS, key: i64, count: i64) -> i64
113func nx_ams_isqrt(x: i64) -> i64
called by 1: nx_ams_stderr_ppb
130func nx_ams_f2(a: *AMS) -> i64
172func nx_ams_stderr_ppb(s: i64) -> i64
called by 1: nx_ams_query_f2 calls 1: nx_ams_isqrt
179func nx_ams_conf_ppb(d: i64) -> i64
called by 1: nx_ams_query_f2
188func nx_ams_query_f2(a: *AMS) -> *ApproxI64
200func nx_ams_merge(a: *AMS, b: *AMS) -> *AMS
calls 1: nx_ams_alloc
215func nx_ams_memory_bytes(a: *AMS) -> i64