Skip to content

Closed form approximations#136

Merged
aneubeck merged 3 commits into
mainfrom
aneubeck/faster_func
Jul 7, 2026
Merged

Closed form approximations#136
aneubeck merged 3 commits into
mainfrom
aneubeck/faster_func

Conversation

@aneubeck

@aneubeck aneubeck commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

This PR introduces a closed form approximation of the size estimation.
For the inverse, it was simpler to use Newton than trying to find another approximating function.
Note: these functions are used instead of the LUT table, since the LUT only works for a certain number of one bits.

The PR also adds documentation how the error rate increases for the metric when the set size goes up and a guideline when to use it.

@aneubeck aneubeck requested a review from a team as a code owner July 7, 2026 13:55
Copilot AI review requested due to automatic review settings July 7, 2026 13:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR replaces the diff-count one-bit calibration lookup/newton-on-summation path with a closed-form approximation (and a cheap Newton refinement on that approximation) to estimate set size from observed one-bit counts. It also documents how the metric’s error grows with larger set differences and provides guidance on configuration choice.

Changes:

  • Add closed-form forward approximation for expected_diff_buckets and a corresponding inverse (estimate_count_approx) with Newton refinement.
  • Switch OnesMetric::{to_f32, from_f32} to use the new approximations instead of the exact-model inversion.
  • Expand documentation on precision/error growth and recommended configs (b >= 10) in both rustdoc and README.
Show a summary per file
File Description
crates/geo_filters/src/diff_count/metric.rs Uses the new closed-form estimation functions for metric calibration and adds guidance in rustdoc.
crates/geo_filters/src/diff_count/config.rs Implements the closed-form forward approximation and inverse (Newton refinement) plus tests comparing against the exact model.
crates/geo_filters/README.md Adds detailed explanation of relative error growth and configuration guidance for nearest-neighbor ranking.

Review details

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 3/3 changed files
  • Comments generated: 2
  • Review effort level: Low

Comment on lines +162 to +176
let s = 0.5 / -phi.ln();
let a = 2.0 * (1.0 - phi);
// Invert the large-N asymptote `m ≈ S (gamma + ln(a N)) + 1/4` for the initial guess.
let mut items = ((ones - 0.25) / s - GAMMA).exp() / a;
if !items.is_finite() || items <= 0.0 {
items = ones;
}
for _ in 0..3 {
let (m, dm) = expected_diff_buckets_approx(phi, items);
items -= (m - ones) / dm;
if items < f64::MIN_POSITIVE {
items = f64::MIN_POSITIVE;
}
}
items
Comment on lines +124 to +126
/// reorder candidates whose true distances are within roughly a factor of two of each other,
/// returning an approximate rather than exact nearest neighbor; for an exact result, shortlist with
/// this metric and re-rank the top candidates with [`Count::size_with_sketch`](crate::Count).

@itsibitzi itsibitzi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's give it a go.

@aneubeck aneubeck merged commit fa1c18e into main Jul 7, 2026
8 checks passed
@aneubeck aneubeck deleted the aneubeck/faster_func branch July 7, 2026 14:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants