Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 130 additions & 2 deletions docs/saturation_study.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ records the cost of the sketch at the largest N measured, and finds the stream
length N* past which an exact answer costs 10x (100x) more than the sketch.

The study is `scripts/studies/study_saturation.py`, in phases: `accuracy`,
`cost`, `crossover`, `hydra` (Hydra grids, below) and `optimizer-cost` (the
cost table the optimizer in ASAPQuery reads). Around it,
`cost`, `crossover`, `hydra` (Hydra grids, below), `optimizer-cost` (the
cost table the optimizer in ASAPQuery reads) and `hll-tail` (the per-value
tail error of HLL for ASAPPlanner's measured accuracy table, below). Around it,
`scripts/studies/merge_study_parts.py` joins the outputs of runs split across
machines, `scripts/studies/recommend_config.py` turns data parameters into a
configuration, and four scripts draw the figures:
Expand Down Expand Up @@ -356,6 +357,133 @@ per grid; `measured_at` names the `dataset` and `schema_width`. Their accuracy
column is one pass's `err_max` at the default grouping; the optimizer reads
Hydra accuracy from `hydra_saturation.csv`.

## HLL per-value tail error (`--phase hll-tail`)

ASAPPlanner keeps a distinct count per group in an HLL state, merged over
the panes of a window, and needs a guarantee in the form "each value is
within relative error ε except with probability δ" (accuracy scope
`per_value`). HLL's analysis gives only a standard error (no failure
probability), so this phase measures the tail instead, on the exact state
the planner's executor builds, and writes it as the planner's measured
accuracy table (ASAPPlanner `MeasuredSketchAccuracy`,
`crates/accuracy/src/measured.rs`).

The trials run in `hll-tail-study/`, a separate crate (its own workspace) that
pins the `asap_sketchlib` commit the executor runs and builds the same type:
`HyperLogLogImpl<Classic, HllRegistersPn>` read with `estimate()` (an integer,
as the executor reads it), or with `--features sparse` the sparse-then-dense
`HyperLogLogSparse<HllRegistersPn>` once the executor keeps that. A point is
(precision `lg_k`, distinct count `n`, pane count `m`). A trial makes `n`
distinct 64-bit items, puts each in a uniformly random pane and, with
probability 1/2, also in a second random pane (one item seen in several
windows), builds one state per non-empty pane, merges them in pane order
into one state and records `|estimate − n| / n`. A trial's items do not depend
on `m` or `lg_k`, so every pane count and precision reads the same streams;
for a state whose merge equals the build of the union (both HLL states), every
`m` gives the same errors, which the study checks rather than assumes.

For each point the binary prints, per requested δ, the error exceeded in at
most `floor(δ · trials)` trials. The reduction takes, per precision and δ, the
worst over every count and pane count (`hll_tail_worst`) and writes:

- `hll_tail.jsonl` (raw, one line per point) and `hll_tail.csv`;
- `hll_tail_ranges.csv`: per count range, the worst error at
`--hll-target-delta` per precision and the least precision within
`--hll-target`;
- `hll_measured_accuracy.json`: one entry per precision, `statistic`
`Cardinality`, `accuracy_metric` `relative`, `trials`, and `error_at` for
every δ ≥ 3 / trials (the planner's validity rule).

Defaults: `lg_k` 12..18, counts 1..1e6 at 10 per decade (58 counts) plus
the switch band per precision (`hll_switch_counts`: steps of `0.01 m` over
`[2.25, 2.6] m` and `0.05 m` over `[2.0, 3.2] m`, one pane, 1,000 trials). Small
counts are included because below `1/ε` one register collision is already an
error above ε; counts go to 1e6 because the worst error sits where the
estimator switches from linear counting to the harmonic mean, near
`2.5 · 2^lg_k` (655,360 at `lg_k` 18);
panes 1, 6, 60, 360 (one 10 s pane, 1 min, 10 min and 1 h of 10 s panes),
2,000 trials per point, δ ∈ {0.005, 0.01, 0.02, 0.05, 0.1}, seed 1.

```sh
cargo build --release --manifest-path hll-tail-study/Cargo.toml # add --features sparse
python3 scripts/studies/study_saturation.py --phase hll-tail --hll-threads 32 --out out_hll
# Re-reduce hll_tail.jsonl (e.g. another --hll-target) without rerunning:
python3 scripts/studies/study_saturation.py --phase hll-tail --hll-reduce-only --out out_hll
```

Results (2026-12-04, `HyperLogLogSparse` at sketchlib `d28d5a6`). The decade
grid (1,624 points, 2,000 trials each) plus the switch band (53 counts per
precision at one pane, 1,000 trials each). In all 406 (lg_k, n) pairs of the
decade grid, every pane count gives the same errors, as the merge-equals-union
property predicts. Below `m/8` items the state is sparse and every error is 0.
Worst relative error per value (the table's entries, `trials` = 1,000):

| lg_k | δ 0.005 | δ 0.01 | δ 0.02 | δ 0.05 | δ 0.1 | worst at (δ 0.05) |
|---|---|---|---|---|---|---|
| 12 | 0.0635 | 0.0599 | 0.0556 | 0.0507 | 0.0449 | n = 9,789 (2.39 m) |
| 13 | 0.0508 | 0.0492 | 0.0464 | 0.0426 | 0.0388 | n = 19,743 (2.41 m) |
| 14 | 0.0437 | 0.0422 | 0.0399 | 0.0375 | 0.0353 | n = 39,649 (2.42 m) |
| 15 | 0.0390 | 0.0384 | 0.0376 | 0.0342 | 0.0327 | n = 79,299 (2.42 m) |
| 16 | 0.0356 | 0.0345 | 0.0334 | 0.0319 | 0.0309 | n = 159,252 (2.43 m) |
| 17 | 0.0331 | 0.0321 | 0.0317 | 0.0308 | 0.0301 | n = 318,505 (2.43 m) |
| 18 | 0.0310 | 0.0306 | 0.0303 | 0.0297 | 0.0291 | n = 637,010 (2.43 m) |

Every worst case sits in the switch band, about `0.05 m` wide, where the
estimate crosses `2.5 m` and jumps from linear counting to the harmonic mean.
The decade grid alone stepped over it and understated the error by up to 2×
(p16 0.0150, p18 0.0126), which is why the band is part of the default run.
For relative error 0.05 at δ 0.05 per value, the least passing precision is
13: p12 misses (0.0507), in the 1e3–1e4 range (`hll_tail_ranges.csv`). For
0.05 at δ 0.005 it is 14.

### UnivMon (`--phase univmon-tail`)

The same measurement for the UnivMon shapes ASAPPlanner lists (its
`UNIVMON_PARAMETERS`, reproduced in `univmon_shapes`), read for the distinct
count (`Cardinality`, relative error) and the frequency entropy
(`FrequencyEntropy`, absolute error in bits). The planner states no
theoretical bound for either. The total (`estimate_l1`) is the exact row
count, so it is not measured. `univmon-tail` (same crate) updates
`asap_sketchlib::UnivMon` as the executor does (one unit-weight update of the
sample's f64 bits per row). A trial streams `4n` rows over exactly `n` keys:
every key once, then `3n` more rows drawn uniformly (`uniform`) or from
Zipf(1.1) (`zipf1.1`). Each row goes to a random pane, and the pane states
are merged in pane order. Each entry is the worst over counts, panes and
both distributions. Defaults: counts 1..1e5 at 5 per decade, panes 1 and 6,
200 trials, δ ∈ {0.02, 0.05, 0.1}. Shapes whose state exceeds
`--um-max-state-mb` (64 MB, `univmon_state_bytes`) are left out and listed
on stderr. Outputs: `univmon_tail.jsonl`, `univmon_tail.csv` and
`univmon_measured_accuracy.json`.

```sh
python3 scripts/studies/study_saturation.py --phase univmon-tail --hll-threads 16 --out out_um
```

Results (2026-12-03, sketchlib `d28d5a6`, the defaults above; 16 of the 40
listed shapes fit 64 MB). Worst error per value at δ 0.05 (entropy in bits,
distinct count relative):

| shape (heap × rows × cols × layers) | entropy | distinct |
|---|---|---|
| 1 × 5 × {624 … 1,409,067} × 1 (L2 shapes) | 5.21 | 1.0 |
| 256 × 5 × 1,024 × 16 | 0.283 | 0.135 |
| 13,500 × 1 × 1 × 19 | 306 | 0.0085 |
| 54,000 × 1 × 1 × 19 | 261 | 0.0036 |
| 13,500 × 5 × 624 × 19 | 0.453 | 0.0087 |
| 13,500 × 5 × 2,367 × 19 | 0.227 | 0.0121 |
| 13,500 × 5 × 14,349 × 19 | 0.046 | 0.0108 |
| 13,500 × 5 × 56,817 × 19 | 0.020 | 0.0105 |
| 54,000 × 5 × 624 × 19 | 0.512 | 0.0023 |
| 54,000 × 5 × 2,367 × 19 | 0.231 | 0.0063 |
| 54,000 × 5 × 14,349 × 19 | 0.039 | 0.0051 |

The L2 shapes (one layer, heap 1) and the distinct shapes (one counter per
layer) are not built for entropy, and the table records that. Entropy within
0.1 bits at δ 0.05 per value needs 14,349 columns or more at 19 layers. The
worst cases often sit at the largest count (1e5), so the entries hold for
counts up to 1e5. Larger counts, more panes and the 24 shapes above 64 MB
are not measured.

## Results (config grid, N up to 1e7)

`--phase accuracy --n-max 1e7 --seeds 3 --jobs 16 --out out_grid_1e7`: 595
Expand Down
Loading