From 51384eb7cd01029fa06c07fa9a35fa2c38919304 Mon Sep 17 00:00:00 2001 From: Brad <1676751+Sinkleberg@users.noreply.github.com> Date: Fri, 6 Nov 2026 08:05:08 -0500 Subject: [PATCH 1/4] Use the intended fallback threshold in Algorithm D Assisted-by: Codex --- src/sampling.jl | 8 ++++---- test/sampling.jl | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/sampling.jl b/src/sampling.jl index e045b912e..09f0cadb9 100644 --- a/src/sampling.jl +++ b/src/sampling.jl @@ -386,8 +386,8 @@ function seqsample_d!(rng::AbstractRNG, a::AbstractArray, x::AbstractArray) vprime = exp(-randexp(rng)/n) q1 = N - n + 1 q2 = q1 / N - alpha = 1 / 13 # choose alpha value - threshold = alpha * n + inv_alpha = 13.0 # Switch to algorithm A when N <= n / alpha. + threshold = inv_alpha * n while n > 1 && threshold < N while true @@ -440,11 +440,11 @@ function seqsample_d!(rng::AbstractRNG, a::AbstractArray, x::AbstractArray) n -= 1 q1 -= s q2 = q1 / N - threshold -= alpha + threshold -= inv_alpha end if n > 1 - seqsample_a!(rng, a[i+1:end], @view x[j+1:end]) + seqsample_a!(rng, @view(a[i+1:end]), @view(x[j+1:end])) else s = trunc(Int, N * vprime) x[j+=1] = a[i+=s+1] diff --git a/test/sampling.jl b/test/sampling.jl index e25a8f9b3..a0c902870 100644 --- a/test/sampling.jl +++ b/test/sampling.jl @@ -186,6 +186,43 @@ check_sample_norep(a, (3, 12), 5.0e-3; ordered=true) test_rng_use(seqsample_d!, 1:10, zeros(Int, 6)) +# Track when algorithm D hands its remaining population to algorithm A. +struct SamplingTailProbe <: AbstractVector{Int} + population::UnitRange{Int} + tails::Vector{UnitRange{Int}} +end +Base.size(a::SamplingTailProbe) = size(a.population) +Base.getindex(a::SamplingTailProbe, i::Int) = a.population[i] +Base.getindex(::SamplingTailProbe, ::UnitRange{Int}) = error("copied the population tail") +function Base.view(a::SamplingTailProbe, indices::UnitRange{Int}) + push!(a.tails, indices) + return view(a.population, indices) +end + +@testset "algorithm D fallback" begin + for N in (25, 26, 27) + a = SamplingTailProbe(1:N, UnitRange{Int}[]) + x = zeros(Int, 2) + seqsample_d!(MersenneTwister(1), a, x) + @test 1 <= x[1] < x[2] <= N + @test a.tails == (N <= 26 ? [1:N] : UnitRange{Int}[]) + end + + # With three draws, D starts first. Its first selection determines + # whether the remaining population is dense enough for A. + switched = Bool[] + for seed in 1:32 + a = SamplingTailProbe(1:40, UnitRange{Int}[]) + x = zeros(Int, 3) + seqsample_d!(MersenneTwister(seed), a, x) + @test 1 <= x[1] < x[2] < x[3] <= 40 + switch = 40 - x[1] <= 13 * 2 + push!(switched, switch) + @test a.tails == (switch ? [(x[1] + 1):40] : UnitRange{Int}[]) + end + @test any(switched) && !all(switched) +end + a = sample(3:12, 5; replace=false) check_sample_norep(a, (3, 12), 0; ordered=false) From c7b000f6d4d4682f008fbdc61362f486f918cedb Mon Sep 17 00:00:00 2001 From: Brad <1676751+Sinkleberg@users.noreply.github.com> Date: Tue, 10 Nov 2026 19:45:53 -0500 Subject: [PATCH 2/4] Return the sample buffer and cover the Algorithm D handoff --- src/sampling.jl | 13 ++++++++--- test/sampling.jl | 58 ++++++++++++++++++++++++------------------------ 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/src/sampling.jl b/src/sampling.jl index 09f0cadb9..040c1286c 100644 --- a/src/sampling.jl +++ b/src/sampling.jl @@ -368,6 +368,11 @@ Random subsequence sampling using algorithm D described in the following paper ( Jeffrey Scott Vitter. "Faster Methods for Random Sampling". Communications of the ACM, 27 (7), July 1984. +When the remaining population has at most 13 times the remaining sample size, +this switches to algorithm A, using ``α = 1/13`` from: +Jeffrey Scott Vitter. "An Efficient Algorithm for Sequential Random Sampling". +ACM Transactions on Mathematical Software, 13 (1), March 1987. + This algorithm consumes ``O(k)`` random numbers, with `k=length(x)`. The outputs are ordered. """ @@ -379,6 +384,7 @@ function seqsample_d!(rng::AbstractRNG, a::AbstractArray, x::AbstractArray) N = length(a) n = length(x) n <= N || error("length(x) should not exceed length(a)") + n == 0 && return x i = 0 j = 0 @@ -386,8 +392,8 @@ function seqsample_d!(rng::AbstractRNG, a::AbstractArray, x::AbstractArray) vprime = exp(-randexp(rng)/n) q1 = N - n + 1 q2 = q1 / N - inv_alpha = 13.0 # Switch to algorithm A when N <= n / alpha. - threshold = inv_alpha * n + alpha_inv = 13.0 # 1/α with α = 1/13 as in Vitter (1987). + threshold = alpha_inv * n while n > 1 && threshold < N while true @@ -440,7 +446,7 @@ function seqsample_d!(rng::AbstractRNG, a::AbstractArray, x::AbstractArray) n -= 1 q1 -= s q2 = q1 / N - threshold -= inv_alpha + threshold -= alpha_inv end if n > 1 @@ -449,6 +455,7 @@ function seqsample_d!(rng::AbstractRNG, a::AbstractArray, x::AbstractArray) s = trunc(Int, N * vprime) x[j+=1] = a[i+=s+1] end + return x end seqsample_d!(a::AbstractArray, x::AbstractArray) = seqsample_d!(default_rng(), a, x) diff --git a/test/sampling.jl b/test/sampling.jl index a0c902870..ef6a474c1 100644 --- a/test/sampling.jl +++ b/test/sampling.jl @@ -186,41 +186,41 @@ check_sample_norep(a, (3, 12), 5.0e-3; ordered=true) test_rng_use(seqsample_d!, 1:10, zeros(Int, 6)) -# Track when algorithm D hands its remaining population to algorithm A. -struct SamplingTailProbe <: AbstractVector{Int} - population::UnitRange{Int} - tails::Vector{UnitRange{Int}} -end -Base.size(a::SamplingTailProbe) = size(a.population) -Base.getindex(a::SamplingTailProbe, i::Int) = a.population[i] -Base.getindex(::SamplingTailProbe, ::UnitRange{Int}) = error("copied the population tail") -function Base.view(a::SamplingTailProbe, indices::UnitRange{Int}) - push!(a.tails, indices) - return view(a.population, indices) +@testset "algorithm D sampling" begin + # Exercise D's rejection loop and the mixed D → A regime. + for (N, k) in ((100, 3), (40, 3)) + a = zeros(Int, k, n) + for j = 1:size(a,2) + seqsample_d!(1:N, view(a,:,j)) + end + check_sample_norep(a, (1, N), 5.0e-3; ordered=true) + end end +test_rng_use(seqsample_d!, 1:100, zeros(Int, 3)) + @testset "algorithm D fallback" begin - for N in (25, 26, 27) - a = SamplingTailProbe(1:N, UnitRange{Int}[]) - x = zeros(Int, 2) - seqsample_d!(MersenneTwister(1), a, x) - @test 1 <= x[1] < x[2] <= N - @test a.tails == (N <= 26 ? [1:N] : UnitRange{Int}[]) + for N in (25, 26, 27), seed in 1:5 + rng_d = MersenneTwister(seed) + x_d = zeros(Int, 2) + @test seqsample_d!(rng_d, 1:N, x_d) === x_d + rng_a = MersenneTwister(seed) + randexp(rng_a) + x_a = seqsample_a!(rng_a, 1:N, zeros(Int, 2)) + @test (x_d == x_a && rng_d == rng_a) == (N <= 26) end +end - # With three draws, D starts first. Its first selection determines - # whether the remaining population is dense enough for A. - switched = Bool[] - for seed in 1:32 - a = SamplingTailProbe(1:40, UnitRange{Int}[]) - x = zeros(Int, 3) - seqsample_d!(MersenneTwister(seed), a, x) - @test 1 <= x[1] < x[2] < x[3] <= 40 - switch = 40 - x[1] <= 13 * 2 - push!(switched, switch) - @test a.tails == (switch ? [(x[1] + 1):40] : UnitRange{Int}[]) +@testset "algorithm D return value" begin + for (N, k) in ((0, 0), (10, 0), (100, 1), (100, 3), (40, 3), (10, 10)) + rng = MersenneTwister(1) + previous_rng = copy(rng) + x = zeros(Int, k) + @test seqsample_d!(rng, 1:N, x) === x + if k == 0 + @test rng == previous_rng + end end - @test any(switched) && !all(switched) end a = sample(3:12, 5; replace=false) From 06a2025810bae0b108fffe3e5353b7fdc92bcd9f Mon Sep 17 00:00:00 2001 From: Brad <1676751+Sinkleberg@users.noreply.github.com> Date: Thu, 19 Nov 2026 03:07:04 -0500 Subject: [PATCH 3/4] Clarify algorithm D test coverage and isolate empty samples --- test/sampling.jl | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/test/sampling.jl b/test/sampling.jl index ef6a474c1..3d61de422 100644 --- a/test/sampling.jl +++ b/test/sampling.jl @@ -178,6 +178,7 @@ check_sample_norep(a, (3, 12), 5.0e-3; ordered=true) test_rng_use(seqsample_c!, 1:10, zeros(Int, 6)) +# These dense cases exercise the immediate handoff to A, not D's main loop. a = zeros(Int, 5, n) for j = 1:size(a,2) seqsample_d!(3:12, view(a,:,j)) @@ -187,7 +188,7 @@ check_sample_norep(a, (3, 12), 5.0e-3; ordered=true) test_rng_use(seqsample_d!, 1:10, zeros(Int, 6)) @testset "algorithm D sampling" begin - # Exercise D's rejection loop and the mixed D → A regime. + # Exercise D's main loop (N > 13n) and the mixed D → A regime. for (N, k) in ((100, 3), (40, 3)) a = zeros(Int, k, n) for j = 1:size(a,2) @@ -211,15 +212,20 @@ test_rng_use(seqsample_d!, 1:100, zeros(Int, 3)) end end +@testset "algorithm D empty sample" begin + for N in (0, 10) + rng = MersenneTwister(1) + x = Int[] + @test seqsample_d!(rng, 1:N, x) === x + @test rng == MersenneTwister(1) + end +end + @testset "algorithm D return value" begin - for (N, k) in ((0, 0), (10, 0), (100, 1), (100, 3), (40, 3), (10, 10)) + for (N, k) in ((100, 1), (100, 3), (40, 3), (10, 10)) rng = MersenneTwister(1) - previous_rng = copy(rng) x = zeros(Int, k) @test seqsample_d!(rng, 1:N, x) === x - if k == 0 - @test rng == previous_rng - end end end From a886dd2a8ecbbd902e4b20ac134d49b56ff06eec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=BCller-Widmann?= Date: Thu, 19 Nov 2026 12:00:15 +0100 Subject: [PATCH 4/4] Bump version from 0.34.13 to 0.34.14 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index dcf79d5ba..ab2f037a6 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "StatsBase" uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" -version = "0.34.13" +version = "0.34.14" authors = ["JuliaStats"] [deps]