Skip to content

Studies: HLL and UnivMon per-value tail error for ASAPPlanner's measured accuracy table - #295

Draft
zzylol wants to merge 2 commits into
mainfrom
feat/hll-tail-study
Draft

zzylol wants to merge 2 commits into
mainfrom
feat/hll-tail-study

Conversation

@zzylol

@zzylol zzylol commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

ASAPPlanner had no failure probability for HLL's distinct count or for UnivMon's distinct count and entropy, so under an accuracy target it pruned them. This PR adds --phase hll-tail and --phase univmon-tail. They measure the per-value tail error (the error not exceeded except with probability δ, worst over counts and merged panes) on the exact state the planner's executor runs, and write it in the planner's MeasuredSketchAccuracy JSON format. Example: HLL lg_k 13 has worst relative error 0.0426 at δ 0.05 over distinct counts 1..1e6 (including the estimator's switch band) and 1–360 panes, so p13 is the least precision meeting rel 0.05 / δ 0.05 per value. p12 misses at 0.0507.

Review guide

  • Base main. Read first: hll-tail-study/src/main.rs (trial definition) and hll_tail / hll_tail_worst / measured_accuracy_table in scripts/studies/study_saturation.py.
  • Then: hll-tail-study/src/bin/univmon-tail.rs and univmon_tail. Docs: docs/saturation_study.md, "HLL per-value tail error".
  • hll-tail-study/ is its own workspace. It pins asap_sketchlib d28d5a6 (feat(hll): HyperLogLogSparse — exact hash set up to m/8, then classic registers asap_sketchlib#275, the sparse HLL the executor now keeps), not the benchmark's pin.

Behavior: before → after

cargo build --release --manifest-path hll-tail-study/Cargo.toml
python3 scripts/studies/study_saturation.py --phase hll-tail --hll-threads 16 --out out_hll
python3 scripts/studies/study_saturation.py --phase univmon-tail --hll-threads 16 --out out_um

Before: no such phase; the planner's HLL guarantee had an unknown failure probability. After: out_hll/hll_measured_accuracy.json (7 entries, lg_k 12..18) and out_um/univmon_measured_accuracy.json (32 entries). ASAPPlanner loads them with --measured-accuracy.

HLL, HyperLogLogSparse, worst relative error per value. Decade grid at 2,000 trials, plus the switch band at 1,000 trials. Merged and unmerged states give identical errors in all 406 decade-grid (lg_k, n) pairs; the error is 0 below m/8 items.

lg_k δ 0.005 δ 0.01 δ 0.02 δ 0.05 δ 0.1
12 0.0635 0.0599 0.0556 0.0507 0.0449
13 0.0508 0.0492 0.0464 0.0426 0.0388
14 0.0437 0.0422 0.0399 0.0375 0.0353
15 0.0390 0.0384 0.0376 0.0342 0.0327
16 0.0356 0.0345 0.0334 0.0319 0.0309
17 0.0331 0.0321 0.0317 0.0308 0.0301
18 0.0310 0.0306 0.0303 0.0297 0.0291

Every worst case is in the switch band near 2.4·2^lg_k, about 0.05·2^lg_k wide. Review finding (fixed in 87f3ce2): the first version used only the 10-per-decade grid, stepped over the band, and understated the error by up to 2× (p16 0.0150 → 0.0319). The least precision meeting rel 0.05 at δ 0.05 is 13.
UnivMon results are in the doc. Entropy within 0.1 bits at δ 0.05 needs 14,349 columns or more at 19 layers (13,500 × 5 × 56,817 × 19 gives 0.020 bits).

Interfaces

scripts/studies/study_saturation.py, hll-tail-study/

+# --phase hll-tail: run hll-tail-study, write hll_tail.jsonl/.csv, hll_tail_ranges.csv, hll_measured_accuracy.json
+--phase hll-tail  --hll-binary --hll-lg-ks --hll-n-max --hll-per-decade --hll-panes --hll-trials
+                  --hll-deltas --hll-seed --hll-threads --hll-target --hll-target-delta --hll-reduce-only
+# --phase univmon-tail: run univmon-tail over the planner's UnivMon shapes that fit --um-max-state-mb
+--phase univmon-tail  --um-binary --um-n-max --um-per-decade --um-panes --um-trials --um-deltas --um-max-state-mb
+# Per (precision, δ): the worst error over counts and panes, and where it was found.
+def hll_tail_worst(records, deltas)
+# The planner's MeasuredSketchAccuracy JSON: one entry per precision, δ ≥ 3/trials, non-increasing error.
+def measured_accuracy_table(worst, deltas)
+# Per count range: worst error per precision and the least precision within the target.
+def hll_tail_ranges(records, delta, target, ranges=HLL_TAIL_RANGES)
+# hll-tail-study's records, one per point (reruns deduplicated), sorted.
+def read_hll_tail(path)
+# Per precision: counts at 0.01·2^lg_k over [2.25, 2.6]·2^lg_k and 0.05·2^lg_k over [2.0, 3.2]·2^lg_k.
+def hll_switch_counts(lg_k)
+--hll-switch-band / --no-hll-switch-band (default on), --hll-switch-trials (default 1000)
+# The planner's 40 UnivMon shapes; the state size estimate used to leave the large ones out.
+def univmon_shapes()
+def univmon_state_bytes(shape)
+def univmon_tail_worst(records, deltas)
+def univmon_measured_accuracy_table(worst, deltas)
+# Binaries (one JSON line per point): hll-tail-study [--features dense], univmon-tail

Usage: see Behavior. The planner side is ProjectASAP/ASAPPlanner feat/hll-measured-accuracy: crates/accuracy/measured/{hll_distinct,univmon}.json and asap_planner --measured-accuracy.

Algorithm changes

Measured per-value bound. Problem: HLL's standard error states no failure probability. Steps:

  1. For each point (lg_k, n, panes m), run trials. A trial makes n distinct items; each goes to a random pane, and half also go to a second pane. Build one state per pane, merge in pane order, and read the estimate as the executor does.
  2. Sort the trial errors and record, for each δ, the error exceeded in at most ⌊δ·trials⌋ trials.
  3. For each precision and δ, take the max over n and m. Force the error to be non-increasing in δ.

Correctness: requests are only answered at the recorded δ ≥ 3/trials; the planner never extrapolates. Complexity: O(trials · Σ n · |panes|) updates. The HLL run took about 3 h on 16–32 threads. UnivMon's 16 shapes took about 25 min on 14 threads. Limits: the result is an empirical estimate, not a proof. Items are random 64-bit values hashed by the state.

Decisions and open questions

  • Decided (2026-12-03, user): measured route with per_value scope and δ 0.05. The paper calls this a measured per-value bound.
  • Needs reviewer decision: none.

Verification

scope result notes
python3 scripts/studies/test_study_saturation.py 56 passed new: HllTailTest (5), HllSwitchBandTest (1), UnivMonTailTest (2)
hll-tail-study cargo build --release, fmt, clippy clean
full hll-tail run (sparse, d28d5a6) 1,624 grid points + 371 band points table above
univmon-tail run 16 shapes × 2 dists × 26 counts × 2 panes doc table

Follow-ups (deferred)

  • UnivMon: the 24 listed shapes above 64 MB a state, counts above 1e5, and pane counts above 6 are not measured.
  • Tiny δ (e.g. 5e-6 per host for a ranked top-10 under a whole-result target) needs about 6e5 trials per point; not measured. The sparse state is exact up to m/8 items, which covers small groups deterministically (planner PR).

🤖 Generated with Claude Code

https://claude.ai/code/session_01K1M6boFpwkpMtRc1FnZuYi

… measured accuracy table

study_saturation.py --phase hll-tail runs hll-tail-study (a standalone crate
pinning the asap_sketchlib commit the planner's executor runs) over lg_k
12..18, distinct counts 1..1e6 (10 per decade) and states merged from 1, 6,
60 and 360 panes, 2,000 trials per point. It measures HyperLogLogSparse, read
as the executor reads it, and writes the error not exceeded except with
probability delta, worst over counts and panes, as ASAPPlanner's
MeasuredSketchAccuracy JSON. --phase univmon-tail does the same for the
planner's UnivMon shapes (distinct count and entropy) with univmon-tail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The 10-per-decade count grid stepped over the error spike near
n = 2.4 * 2^lg_k (the estimate crosses 2.5 * 2^lg_k and switches
estimators), so the table understated the worst error by up to 2x
(p16 0.0150 vs 0.0319). --phase hll-tail now also runs, per precision,
counts at 0.01 m steps over [2.25, 2.6] m and 0.05 m over [2.0, 3.2] m (one
pane: merging changes no state; 1,000 trials). New worst at delta 0.05:
p12 0.0507 ... p18 0.0297; the least precision meeting rel 0.05 is 13.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant