code wiki / (root) / sketch_ams.nx

sketch_ams.nx

buildroot/runtime/sketch_ams.nx

7695 B231 linesdepth 4pulls 4 transitivereach 3 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 · 3 importers

syscalls.nx sketch_types.nx sketch_ams.nx sketch_ams_test.nx sketch_ams_vs_naive_bench.nx sketch_observability_dashboard_ben

imports: syscalls.nxsketch_types.nx

imported by: sketch_ams_test.nxsketch_ams_vs_naive_bench.nxsketch_observability_dashboard_bench.nx

structs

35struct AMS {

consts

30const NX_AMS_MIN_D: i64 = 3
31const NX_AMS_MAX_D: i64 = 32
32const NX_AMS_MIN_S: i64 = 4
33const NX_AMS_MAX_S: i64 = 1024

functions

51func nx_ams_alloc(d: i64, s: i64, seed: i64) -> *AMS {
83func nx_ams_sign(a: *AMS, key: i64, j: i64, k: i64) -> i64 {
92func nx_ams_cell_idx(a: *AMS, j: i64, k: i64) -> i64 {
called by 1: nx_ams_f2
98func nx_ams_add(a: *AMS, key: i64, count: i64) -> i64 {
called by 3: mainmainmain
128func nx_ams_isqrt(x: i64) -> i64 {
145func nx_ams_f2(a: *AMS) -> i64 {
186func nx_ams_stderr_ppb(s: i64) -> i64 {
called by 1: nx_ams_query_f2 calls 1: nx_ams_isqrt
193func nx_ams_conf_ppb(d: i64) -> i64 {
called by 1: nx_ams_query_f2
202func nx_ams_query_f2(a: *AMS) -> *ApproxI64 {
214func nx_ams_merge(a: *AMS, b: *AMS) -> *AMS {
called by 1: main calls 1: nx_ams_alloc
229func nx_ams_memory_bytes(a: *AMS) -> i64 {