code wiki / (root) / nx_sketch_ams.nx

nx_sketch_ams.nx

buildroot/runtime/nx_sketch_ams.nx

6159 B203 linesdepth 3pulls 5 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 3 imports · 0 importers

nx_syscalls.nx nx_sketch_types.nx nx_vecmath.nx nx_sketch_ams.nx

imports: nx_syscalls.nxnx_sketch_types.nxnx_vecmath.nx

imported by: nobody (leaf or entry point)

structs

42struct AMS

consts

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

functions

52func nx_ams_alloc(d: i64, s: i64, seed: i64) -> *AMS
called by 1: nx_ams_merge calls 1: sys_mmap
78func nx_ams_sign(a: *AMS, key: i64, j: i64, k: i64) -> i64
called by 1: nx_ams_add
85func nx_ams_cell_idx(a: *AMS, j: i64, k: i64) -> i64
called by 2: nx_ams_addnx_ams_f2
91func nx_ams_add(a: *AMS, key: i64, count: i64) -> i64
114func nx_ams_isqrt(x: i64) -> i64 { return vm_isqrt(x) }
called by 1: nx_ams_stderr_ppb calls 1: vm_isqrt
116func nx_ams_f2(a: *AMS) -> i64
158func nx_ams_stderr_ppb(s: i64) -> i64
called by 1: nx_ams_query_f2 calls 1: nx_ams_isqrt
165func nx_ams_conf_ppb(d: i64) -> i64
called by 1: nx_ams_query_f2
174func nx_ams_query_f2(a: *AMS) -> *ApproxI64
186func nx_ams_merge(a: *AMS, b: *AMS) -> *AMS
calls 1: nx_ams_alloc
201func nx_ams_memory_bytes(a: *AMS) -> i64