Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "StatsBase"
uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
version = "0.34.13"
version = "0.34.14"
authors = ["JuliaStats"]

[deps]
Expand Down
15 changes: 11 additions & 4 deletions src/sampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -379,15 +384,16 @@ 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

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
Expand Down Expand Up @@ -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]
Comment thread
devmotion marked this conversation as resolved.
Resolved
end
return x
end

seqsample_d!(a::AbstractArray, x::AbstractArray) = seqsample_d!(default_rng(), a, x)
Expand Down
43 changes: 43 additions & 0 deletions test/sampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Comment thread
devmotion marked this conversation as resolved.
Resolved
Comment thread
devmotion marked this conversation as resolved.
Resolved

@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)

Expand Down
Loading