sketch_ams.nx
buildroot/runtime/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 · 3 importers
imports: syscalls.nxsketch_types.nx
imported by: sketch_ams_test.nxsketch_ams_vs_naive_bench.nxsketch_observability_dashboard_bench.nx
structs
| 35 | struct AMS { |
consts
| 30 | const NX_AMS_MIN_D: i64 = 3 |
| 31 | const NX_AMS_MAX_D: i64 = 32 |
| 32 | const NX_AMS_MIN_S: i64 = 4 |
| 33 | const NX_AMS_MAX_S: i64 = 1024 |
functions
| 51 | func nx_ams_alloc(d: i64, s: i64, seed: i64) -> *AMS { |
| 83 | func nx_ams_sign(a: *AMS, key: i64, j: i64, k: i64) -> i64 { |
| 92 | func nx_ams_cell_idx(a: *AMS, j: i64, k: i64) -> i64 {
called by 1: nx_ams_f2 |
| 98 | func nx_ams_add(a: *AMS, key: i64, count: i64) -> i64 { |
| 128 | func nx_ams_isqrt(x: i64) -> i64 { |
| 145 | func nx_ams_f2(a: *AMS) -> i64 { |
| 186 | func nx_ams_stderr_ppb(s: i64) -> i64 { |
| 193 | func nx_ams_conf_ppb(d: i64) -> i64 {
called by 1: nx_ams_query_f2 |
| 202 | func nx_ams_query_f2(a: *AMS) -> *ApproxI64 { |
| 214 | func nx_ams_merge(a: *AMS, b: *AMS) -> *AMS { |
| 229 | func nx_ams_memory_bytes(a: *AMS) -> i64 { |