Skip to content

Add sort-first and partition-based weighted quantile tables - #389

Merged
jmh530 merged 3 commits into
libmir:masterfrom
jmh530:codex/weighted-table-sort-first
Nov 30, 2026
Merged

jmh530 merged 3 commits into
libmir:masterfrom
jmh530:codex/weighted-table-sort-first

Conversation

@jmh530

@jmh530 jmh530 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Callers who query the same observations several times can now choose when to do the ordering work. weightedQuantileTableSortFirst prepares sorted rows and cumulative weights at construction for read-only queries. The existing weightedQuantileTable forwards to it and preserves its return type and behavior.

The partition factories copy positive-weight rows and discover partitions as queries arrive. Copies of a table share that work. A single positive-weight row needs no cache. Larger tables retain up to 64 partitions and continue selecting from unresolved rows when the cache fills.

  • For frequency counts, weightedQuantileTableByPartition!(WeightInterpretation.frequency) supports type 7 and type 8. Zero observations with the same positive count must have the same sign.
  • For relative weights, tryWeightedQuantileTableByPartition supports inverse CDF and averaged inverse CDF. It returns an empty Nullable before allocating if it cannot guarantee exact partition sums or if positive-weight observations contain both signs of zero. Rounding at a cumulative boundary can change which observation is selected, so callers choose explicitly whether to use sorted accumulation.

For example, a caller can use partition selection when it preserves the sorted median and otherwise choose SortFirst:

auto candidate = tryWeightedQuantileTableByPartition(values, weights);
auto median = candidate.isNull
    ? weightedQuantileTableSortFirst(values, weights).median
    : candidate.get.median;

All three factory families support GC, reference-counted, and custom allocation. Custom-allocated tables are disposed once with the original allocator after all copies are finished.

Tests cover cumulative boundaries, signed zeros, wide-integer interpolation, shared query state, cache exhaustion, allocation-free declines, and allocator cleanup. Local validation passed all 113 modules with LDC 1.43.0 and DIP1000 on a snapshot including upstream 1529f280f87a. The DMD 2.114.0 documentation check also passed.

AI-Assisted-By: Codex (GPT-6.2 Astra Medium)

Add weightedQuantileTableSortFirst, rcWeightedQuantileTableSortFirst,
and makeWeightedQuantileTableSortFirst so callers can explicitly prepare
sorted observations and cumulative weights before the first query.

Have the existing factories forward to their sort-first counterparts.
Both frequency and relative weights retain their existing preparation,
allocation policy, and return type. Add a documented example that builds
a reference-counted table from unordered observations and queries it
through a const handle.

AI-Assisted-By: Codex (GPT-6.2 Astra Medium)
@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Add weightedQuantileTableByPartition and its RC and custom-allocator
variants for type 7 and type 8 queries. Tables own the positive-count
rows and share discovered partitions across queries and copies.

Reserve up to one cache node per positive-count row, capped at 64.
A single row needs no cache. Once the cache fills, queries continue
partitioning the unresolved rows without sorting the table.

Use raw counts to resolve ties when row boundaries affect rounding or
the sign of a zero result. Zero observations with the same positive
count must have the same sign.

Test agreement with SortFirst for signed zeros and wide integers,
queries after the cache fills, and custom allocation and disposal.

AI-Assisted-By: Codex (GPT-6.2 Astra Medium)
Add tryWeightedQuantileTableByPartition and its RC and custom-allocator
variants for inverse CDF and averaged inverse CDF. Reuse the frequency
table's bounded partition cache for repeated queries.

Check during validation that partial weight sums are exact in any
partition order. Otherwise, rounding can move a probability across a
cumulative boundary and change the selected observation. Return null
before allocating when the check fails or positive-weight observations
contain both signs of zero. Callers can then choose SortFirst.

Test agreement with SortFirst at and around cumulative boundaries,
shared table ownership, allocation-free declines, and allocator cleanup.

AI-Assisted-By: Codex (GPT-6.2 Astra Medium)
@jmh530
jmh530 force-pushed the codex/weighted-table-sort-first branch from b8c76ae to b6d509e Compare November 30, 2026 16:14
@jmh530
jmh530 merged commit 84e46be into libmir:master Nov 30, 2026
23 checks passed
@jmh530
jmh530 deleted the codex/weighted-table-sort-first branch November 30, 2026 16:29
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.

1 participant