code wiki / (root) / sketch_count_sketch.nx

sketch_count_sketch.nx

buildroot/runtime/sketch_count_sketch.nx

8067 B230 linesdepth 4pulls 4 transitivereach 7 importersview sourcekind sketch/demotopic sketch
docsdependenciesstructsconstsfunctions

about

sketch_count_sketch.nx -- CountSketch (Charikar-Chen-Farach-Colton 2002). Frequency-counting variant that gives UNBIASED estimates via ±1 sign hashing. Each (key, count) update adds s_j(key) * count to row j column h_j(key), where s_j ∈ {-1, +1} is a sign hash. Query: median of (s_j(key) * counter[j][h_j(key)]) across rows. COMPLEMENTS CMS (runtime/sketch_cms.nx): - CMS: always OVERESTIMATES (false positives via positive collisions). Use when "upper bound" matters. - CountSketch: UNBIASED. Use when expectation matters (statistical summaries, F_2 estimation, sparse-approximation literature). CAPABILITY STOMP: shipping both means callers pick by error semantics. DataSketches ships CMS as "FrequentLongs"; CountSketch is queued in their docs but not implemented. ERROR BOUND: |estimate(x) - f(x)| <= ε · ||f||_2 / sqrt(d) where ||f||_2 = sqrt(Σ f_i²) is the L2 norm of the frequency vector. For w = O(1/ε²) and d = O(log(1/δ)) rows: confidence 1-δ. Default params: d=5 rows, w=2048 columns -> conf >99%, ε ~ 0.022.

dependencies 2 imports · 7 importers

syscalls.nx sketch_types.nx sketch_count_sketch.nx _a2a_count_subst.nx _a2a_cs_x100.nx _py_freq_substrate.nx sketch_count_sketch_negative_bench sketch_count_sketch_test.nx sketch_count_sketch_vs_cms_bias_be sketch_observability_dashboard_ben

imports: syscalls.nxsketch_types.nx

imported by: _a2a_count_subst.nx_a2a_cs_x100.nx_py_freq_substrate.nxsketch_count_sketch_negative_bench.nxsketch_count_sketch_test.nxsketch_count_sketch_vs_cms_bias_bench.nxsketch_observability_dashboard_bench.nx

structs

33struct CountSketch {

consts

28const NX_CS_MIN_D: i64 = 3
29const NX_CS_MAX_D: i64 = 32
30const NX_CS_MIN_W: i64 = 64
31const NX_CS_MAX_W: i64 = 65536

functions

46func nx_cs_alloc(d: i64, w: i64, seed: i64) -> *CountSketch {
76func nx_cs_h1(c: *CountSketch, key: i64) -> i64 {
81func nx_cs_h2(c: *CountSketch, key: i64) -> i64 {
86func nx_cs_column(c: *CountSketch, key: i64, j: i64) -> i64 {
92func nx_cs_sign(c: *CountSketch, key: i64, j: i64) -> i64 {
100func nx_cs_cell_idx(c: *CountSketch, j: i64, col: i64) -> i64 {
106func nx_cs_add(c: *CountSketch, key: i64, count: i64) -> i64 {
131func nx_cs_estimate(c: *CountSketch, key: i64) -> i64 {
174func nx_cs_isqrt(x: i64) -> i64 {
called by 1: nx_cs_query
192func nx_cs_conf_ppb(d: i64) -> i64 {
called by 1: nx_cs_query
200func nx_cs_query(c: *CountSketch, key: i64) -> *ApproxI64 {
213func nx_cs_merge(a: *CountSketch, b: *CountSketch) -> *CountSketch {
called by 1: main calls 1: nx_cs_alloc
228func nx_cs_memory_bytes(c: *CountSketch) -> i64 {
called by 1: main