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] diff --git a/src/sampling.jl b/src/sampling.jl index e045b912e..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 - alpha = 1 / 13 # choose alpha value - threshold = 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,15 +446,16 @@ function seqsample_d!(rng::AbstractRNG, a::AbstractArray, x::AbstractArray) n -= 1 q1 -= s q2 = q1 / N - threshold -= alpha + threshold -= alpha_inv 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] 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 e25a8f9b3..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)) @@ -186,6 +187,48 @@ 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 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) + 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), 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 + +@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 ((100, 1), (100, 3), (40, 3), (10, 10)) + rng = MersenneTwister(1) + x = zeros(Int, k) + @test seqsample_d!(rng, 1:N, x) === x + end +end + a = sample(3:12, 5; replace=false) check_sample_norep(a, (3, 12), 0; ordered=false)