Skip to content

Separate frequency-count and relative-weight quantile APIs - #391

Merged
jmh530 merged 3 commits into
libmir:masterfrom
jmh530:codex/frequency-quantile-api
Nov 30, 2026
Merged

jmh530 merged 3 commits into
libmir:masterfrom
jmh530:codex/frequency-quantile-api

Conversation

@jmh530

@jmh530 jmh530 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

A weight of 3 can mean that an observation occurred three times, or that one observation carries three times the relative weight of another. Those meanings can produce different quantiles even when all weights are integers. This PR makes the choice visible in the function name.

const int[2] values = [0, 10], weights = [1, 3];

assert(frequencyQuantile(values, weights, 0.25) == 7.5);
assert(weightedQuantile(values, weights, 0.25) == 0);

frequencyQuantile treats the rows as the sample [0, 10, 10, 10] and computes its type 7 first quartile without expanding the counts. weightedQuantile uses the inverse CDF by default. The observation 0 carries one quarter of the total mass, so it is the first value to reach the requested probability.

Migration

This is a breaking change for quantile calls that explicitly pass WeightInterpretation.

Previous call Replacement
weightedQuantile!(WeightInterpretation.frequency)(x, counts, p) frequencyQuantile(x, counts, p)
weightedQuantile!(WeightInterpretation.frequency, WeightedQuantileAlgo.type8)(x, counts, p) frequencyQuantile!(WeightedQuantileAlgo.type8)(x, counts, p)
weightedQuantile!(WeightInterpretation.relative)(x, weights, p) weightedQuantile(x, weights, p)
weightedQuantileTableByPartition!(WeightInterpretation.frequency)(x, counts) frequencyQuantileTableByPartition(x, counts)

Existing weightedQuantile(x, weights, p) calls keep their relative-weight behavior. Histogram factories and other statistics that accept WeightInterpretation continue to expose that option.

Scope

The first commit adds the frequency family for scalar and batch quantiles, median, interquartile range, and median absolute deviation. It covers sort-first and assume-sorted calls, caller-provided storage, and GC, reference-counted, and custom allocation.

The second commit adds frequency table factories and separates the two partition queries. A frequency query locates a rank in the represented sample. A relative-weight query locates a cumulative-mass boundary. Both reuse the same partition search and cache.

The third commit removes the quantile selector and migrates callers, tests, benchmarks, and documentation. The sorted and partitioned tables have concrete public types so generic callers can infer their template arguments from factory results.

Validation

On Windows, the final tree passed the focused DMD 2.114.0 DIP1000 run across 49 modules. An external caller that relies on table type inference compiled and ran with DMD 2.114.0 and LDC 1.43.0. Ddoc passed with warnings treated as errors, and the generated documentation contains method anchors for all four public table types.

The full LDC DIP1000 suite passed 113 modules before the final review fixes. The benchmark runner also checked its results for frequency counts, integer relative weights, and fractional relative weights.

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

Add frequencyQuantile for observations paired with occurrence counts.
It defaults to type 7 and also supports type 8. Both use the ranks of
the represented sample without expanding it. For values [0, 10] and
counts [1, 3], the type 7 first quartile is 7.5, as for [0, 10, 10, 10].

Provide sort-first, assume-sorted, and caller-storage variants, along
with frequency median, interquartile range, and median absolute
deviation. Cover scalar and batch results, sorted observation/count
pairs, and the GC, reference-counted, and custom allocation paths.

AI-Assisted-By: Codex (GPT-6.2 Astra Medium)
Add frequencyQuantileTable, frequencyQuantileTableSortFirst, and
frequencyQuantileTableByPartition for repeated queries over counts.
Expose distinct table types for counts and relative weights across
the GC, reference-counted, and custom allocator factories.

Separate the partition query calculations. A frequency query locates
a rank in the represented sample and finds the next rank when
interpolation requires it. A relative query locates a cumulative-mass
boundary and finds its successor for averaged inverse CDF. Both reuse
the same partition search and cache.

AI-Assisted-By: Codex (GPT-6.2 Astra Medium)
Select the meaning of the weights by function name. Callers using
weightedQuantile!(WeightInterpretation.frequency) switch to
frequencyQuantile. Callers explicitly passing
WeightInterpretation.relative drop that argument. Existing calls to
weightedQuantile without a selector keep their relative-weight behavior.

Apply the same change to sorted queries, caller storage, tables, and
summary statistics. Update histogram factories to call the appropriate
quantile family while preserving their public interpretation option.

Migrate the benchmark and examples, test that the old quantile calls
are rejected, and document how to choose between the two families.

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!

@jmh530
jmh530 merged commit cf117a7 into libmir:master Nov 30, 2026
23 checks passed
@jmh530
jmh530 deleted the codex/frequency-quantile-api branch November 30, 2026 19:52
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