nx_sketch_ams.nx
buildroot/runtime/nx_sketch_ams.nx
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
imports: nx_syscalls.nxnx_sketch_types.nx
imported by: nobody (leaf or entry point)
structs
| 41 | struct AMS |
consts
| 36 | const NX_AMS_MIN_D: i64 = 3 |
| 37 | const NX_AMS_MAX_D: i64 = 32 |
| 38 | const NX_AMS_MIN_S: i64 = 4 |
| 39 | const NX_AMS_MAX_S: i64 = 1024 |
functions
| 51 | func nx_ams_alloc(d: i64, s: i64, seed: i64) -> *AMS |
| 77 | func nx_ams_sign(a: *AMS, key: i64, j: i64, k: i64) -> i64 called by 1: nx_ams_add |
| 84 | func nx_ams_cell_idx(a: *AMS, j: i64, k: i64) -> i64 |
| 90 | func nx_ams_add(a: *AMS, key: i64, count: i64) -> i64 |
| 113 | func nx_ams_isqrt(x: i64) -> i64 called by 1: nx_ams_stderr_ppb |
| 130 | func nx_ams_f2(a: *AMS) -> i64 |
| 172 | func nx_ams_stderr_ppb(s: i64) -> i64 |
| 179 | func nx_ams_conf_ppb(d: i64) -> i64 called by 1: nx_ams_query_f2 |
| 188 | func nx_ams_query_f2(a: *AMS) -> *ApproxI64 |
| 200 | func nx_ams_merge(a: *AMS, b: *AMS) -> *AMS calls 1: nx_ams_alloc |
| 215 | func nx_ams_memory_bytes(a: *AMS) -> i64 |