Repository navigation
Use the intended fallback threshold in Algorithm D - #1022
Merged
devmotion merged 4 commits intoNov 19, 2026
Merged
Conversation
Assisted-by: Codex
devmotion
force-pushed
the
codex/sequential-sampling-fallback
branch
from
November 10, 2026 22:17
e1b7267 to
51384eb
Compare
devmotion
reviewed
Nov 10, 2026
devmotion
left a comment
Member
There was a problem hiding this comment.
Thanks! The threshold fix seems correct, I checked and it matches Vitter's pseudocode in both papers:
- Vitter (1984), "Faster Methods for Random Sampling", CACM 27(7):703–718 (PDF). p. 715 says
alpha_inverse(≈ 1/α, with α ≈ 0.07) and thatthresholdis "always equal to alpha_inverse × n". p. 716 hasthreshold := alpha_inverse × n; while (n > 1) and (threshold < N) … threshold := threshold − alpha_inverse.seqsample_d!is a line-by-line port of that p. 716 version. - Vitter (1987), "An Efficient Algorithm for Sequential Random Sampling", ACM TOMS 13(1):58–67 (PDF). In Appendix A2 (p. 66):
negalphainv := −13; threshold := −negalphainv × n, with "we used α = 1/13". This is where the 13 comes from.
I also checked the sampler numerically with the PR's code. In the pure-D regime (N = 200, n = 3), the inclusion probabilities and the exact law of the first selected index match. In the mixed D→A regime, chi-square tests on inclusion counts are consistent with uniform. With dense samples from a Vector it's also faster, since A is used for the tail and the tail is no longer copied.
A few comments inline. The main one is that the existing statistical test for D no longer exercises D's rejection loop.
devmotion
reviewed
Nov 13, 2026
devmotion
approved these changes
Nov 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Algorithm D compared the remaining population with
n / 13. That kept it from switching to Algorithm A when the remaining sample was dense. This uses the intended13 * nthreshold and passes the remaining population as a view.This addresses the second suggestion in #998. The public algorithm selector is outside this change.
The latest cleanup clarifies which tests exercise D and separates the empty-sample RNG checks. All eight affected assertions passed on Julia 1.10.13. The boundary, handoff and full package tests passed before this test-only cleanup; the full suite was not rerun for it.
Developed with Codex and independently reviewed.