Skip to content
Merged
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
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions crates/accuracy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,5 @@ asap_sketchlib = { workspace = true }
asap-types = { path = "../types" }
serde_json = "1"

[features]
# Exposes `test_support`: estimator, sizing, composition and propagation
# helpers other crates' tests call.
test-support = []

[dev-dependencies]
asap-types = { path = "../types", features = ["test-support"] }
42 changes: 4 additions & 38 deletions crates/accuracy/src/guarantee/cardinality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@
//!
//! Neither bound depends on `N`, so no distinct bound is needed: the
//! failure probability ([`kmv_failure_probability`]) is their sum, per
//! output row (each group's estimate). Sizing ([`kmv_k`]) takes the
//! smallest `k` with that sum at most `δ`, about
//! output row (each group's estimate). Sizing (test-only
//! `sizing_oracle::kmv_k`) takes the smallest `k` with that sum at most
//! `δ`, about
//! `1 + (1+ε)(2+ε)·ln(2/δ)/ε²` (below `3·ln(2/δ)/ε² + 1` for `ε ≤ 1`).
use super::*;

/// The smallest `k` `asap_sketchlib`'s KMV estimate `(k-1)/U` is defined for.
const KMV_MIN_K: u32 = 2;
/// The largest `k` sizing returns.
#[cfg(test)]
const KMV_MAX_K: u32 = 1 << 26;
pub(super) const KMV_MIN_K: u32 = 2;

pub(super) fn guarantee(
algorithm: &SketchAlgorithm,
Expand Down Expand Up @@ -79,38 +77,6 @@ pub(crate) fn kmv_failure_probability(k: u32, eps: f64) -> f64 {
((over + under) * (1.0 + 1e-12)).min(1.0)
}

/// The smallest `k` whose KMV failure probability at relative error `eps`
/// is at most `delta` ([`kmv_failure_probability`]), up to `2^26`; a
/// degenerate `eps` or `delta` saturates to `2^26`.
#[cfg(test)]
pub(crate) fn kmv_k(eps: f64, delta: f64) -> u32 {
if !(eps.is_finite() && eps > 0.0 && delta.is_finite() && delta > 0.0) {
return KMV_MAX_K;
}
// The overestimate term alone at most `δ` bounds `k` below; both terms
// at most `δ/2` bound it above.
let k_at = |ln_inverse: f64| {
saturating_ceil(
1.0 + (1.0 + eps) * (2.0 + eps) * ln_inverse / (eps * eps),
KMV_MIN_K,
KMV_MAX_K,
)
};
let (mut lo, mut hi) = (k_at((1.0 / delta).ln()), k_at((2.0 / delta).ln()));
if kmv_failure_probability(hi, eps) > delta {
return KMV_MAX_K;
}
while lo < hi {
let mid = lo + (hi - lo) / 2;
if kmv_failure_probability(mid, eps) <= delta {
hi = mid;
} else {
lo = mid + 1;
}
}
hi
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
31 changes: 2 additions & 29 deletions crates/accuracy/src/guarantee/cms.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Count-Min Sketch L1 frequency error and parameter sizing.
//! Count-Min Sketch L1 frequency error.
use super::*;

pub(super) fn guarantee(
Expand All @@ -24,25 +24,13 @@ pub(super) fn guarantee(
))
}

/// CMS: over-count ≤ ε·N with width `w = ⌈e/ε⌉` columns.
#[cfg(test)]
pub(crate) fn cms_width(eps: f64) -> u32 {
saturating_ceil(std::f64::consts::E / eps, 2, 1 << 26)
}

/// CMS: failure probability ≤ δ with depth `d = ⌈ln(1/δ)⌉` rows.
/// δ = 0.01 → depth 5.
pub(crate) fn cms_depth(delta: f64) -> u32 {
saturating_ceil((1.0 / delta).ln(), 1, 32)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn local_guarantee_inverts_frequency_sizing() {
use crate::guarantee::size_params;
use crate::guarantee::sizing_oracle::size_params;
let c = asap_types::ir::operator::agg_intent::default_cardinality();
let params = size_params(SketchAlgorithm::Cms, &c, 0.01, 0.001);
let g = sketch_guarantee(
Expand Down Expand Up @@ -76,19 +64,4 @@ mod tests {
AccuracyMetric::ABSOLUTE_STREAM_L1
);
}

/// A top-k heap holds `max(k, ⌈1/ε⌉)` entries over a CMS sized as for counts.
#[test]
fn topk_heap_capacity_respects_accuracy_and_output_count() {
use crate::guarantee::size_params;
let intent = AggIntent::TopK { k: 25 };
assert_eq!(
size_params(SketchAlgorithm::CmsWithHeap, &intent, 0.01, 0.01),
SketchParams::CmsWithHeap {
width: 272, // ⌈e/0.01⌉
depth: 5,
heap_size: 100,
}
);
}
}
73 changes: 4 additions & 69 deletions crates/accuracy/src/guarantee/count_sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub(super) fn guarantee(
/// probability at most `q/ε² = q w/3` (Chebyshev, `Var ≤ q F2`), with `q`
/// the column collision probability ([`column_collision`]): `1/3` to
/// within `w/(3 · 2^32)`.
fn row_failure_probability(width: u32) -> f64 {
pub(super) fn row_failure_probability(width: u32) -> f64 {
column_collision(width) * f64::from(width.max(1)) / 3.0
}

Expand All @@ -45,7 +45,7 @@ fn column_collision(cols: u32) -> f64 {

/// The median of an odd `depth` of independent rows misses only when at
/// least `(depth + 1)/2` rows miss: `P(Binomial(depth, p) ≥ (depth+1)/2)`.
fn count_sketch_failure_probability(width: u32, depth: u32) -> Option<f64> {
pub(super) fn count_sketch_failure_probability(width: u32, depth: u32) -> Option<f64> {
if depth == 0 || depth.is_multiple_of(2) {
return None;
}
Expand All @@ -56,47 +56,10 @@ fn count_sketch_failure_probability(width: u32, depth: u32) -> Option<f64> {
))
}

/// CountSketch `L2` point-query width: ε = sqrt(3/w).
#[cfg(test)]
pub(crate) fn count_sketch_width(eps: f64) -> u32 {
saturating_ceil(3.0 / (eps * eps), 2, 1 << 26)
}

/// The smallest positive odd depth whose binomial median failure
/// probability at `width` is at most `delta`, capped at 255.
#[cfg(test)]
pub(crate) fn count_sketch_depth(width: u32, delta: f64) -> u32 {
if !(delta.is_finite() && delta > 0.0 && delta < 1.0) {
return 255;
}
(1..=255)
.step_by(2)
.find(|&depth| {
count_sketch_failure_probability(width, depth).is_some_and(|tail| tail <= delta)
})
.unwrap_or(255)
}

#[cfg(test)]
mod tests {
use super::*;

/// The executor's `CountSketch<_, RegularPath>` takes a column as the
/// low 32 hash bits modulo the width, so a row fails with probability
/// `1/3` (to within `2^-32`) at any width, not `3/8` off powers of two:
/// ε = 0.05 (width 1200) needs depth 47 at δ = 0.01, 81 at 1e-3 and
/// 117 at 1e-4, not 85, 149 and 215.
#[test]
fn depth_follows_the_libraries_modulo_column_hash() {
let width = count_sketch_width(0.05);
assert_eq!(width, 1200);
assert!((row_failure_probability(width) - 1.0 / 3.0).abs() < 1e-6);
assert_eq!(
[0.01, 1e-3, 1e-4].map(|delta| count_sketch_depth(width, delta)),
[47, 81, 117]
);
}

/// Measured on the library's own `CountSketch<_, RegularPath>`: two
/// keys share a column of a 1536-wide row about `1/1536` of the time.
/// A `⌈log2 w⌉`-bit hash slice modulo `w` would give `1.125/1536`, 6
Expand Down Expand Up @@ -124,6 +87,7 @@ mod tests {
/// most a δ share of keys missing.
#[test]
fn sized_sketch_meets_its_guarantee_on_a_seeded_stream() {
use crate::guarantee::sizing_oracle::{count_sketch_depth, count_sketch_width};
use asap_sketchlib::{CountSketch, DataInput, RegularPath, Vector2D};
let (eps, delta) = (0.05, 0.01);
let width = count_sketch_width(eps);
Expand Down Expand Up @@ -157,7 +121,7 @@ mod tests {

#[test]
fn count_sketch_uses_an_l2_guarantee() {
use crate::guarantee::size_params;
use crate::guarantee::sizing_oracle::size_params;
use asap_types::ir::operator::agg_intent::default_cardinality;
let intent = default_cardinality();
let count_sketch = size_params(SketchAlgorithm::CountSketch, &intent, 0.01, 0.01);
Expand All @@ -176,33 +140,4 @@ mod tests {
);
assert!(meets(&guarantee, 0.01, Some(0.01)));
}

/// The depth is the smallest odd one whose binomial tail meets δ. Each
/// is at most Hoeffding's `ln(1/δ) / (2 (1/2 − p)²)` at the row's
/// failure probability `p ≈ 1/3`: `18 ln(1/δ)` at any width.
#[test]
fn count_sketch_depth_is_the_smallest_meeting_the_binomial_tail() {
for width in [1024, 3000, 30_000] {
let hoeffding = 18.0 * (1.0 + 1e-4);
for delta in [0.1, 0.01, 0.001] {
let depth = count_sketch_depth(width, delta);
assert!(depth % 2 == 1);
assert!(count_sketch_failure_probability(width, depth).unwrap() <= delta);
if depth > 1 {
assert!(count_sketch_failure_probability(width, depth - 2).unwrap() > delta);
}
assert!(
f64::from(depth) <= hoeffding * (1.0 / delta).ln(),
"{width} {delta}"
);
}
}
// Any width fails a row with probability 1/3 (to within 2^-32 w/3):
// P(Bin(3, 1/3) ≥ 2) = 7/27.
for width in [1024, 1000] {
let tail = count_sketch_failure_probability(width, 3).unwrap();
assert!((tail - 7.0 / 27.0).abs() < 1e-6, "{width}");
}
assert_eq!(count_sketch_failure_probability(1000, 4), None);
}
}
5 changes: 0 additions & 5 deletions crates/accuracy/src/guarantee/ddsketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,3 @@ pub(super) fn guarantee(
"ddsketch_relative_error_alpha_v1",
))
}

#[cfg(test)]
pub(super) fn size_params(epsilon: f64) -> SketchParams {
SketchParams::DDSketch { alpha: epsilon }
}
Loading
Loading