StatsBase.jl icon indicating copy to clipboard operation
StatsBase.jl copied to clipboard

Better error message when attempting to return an ordered matrix sample

Open CameronBieganek opened this issue 5 years ago • 2 comments

This came up on Discourse.

a = [1, 2]
wv = weights([0.4, 0.6])
x = sample(a, wv, (2, 2), ordered=true)
julia> x = sample(a, wv, (3, 4), ordered=true)
ERROR: UndefKeywordError: keyword argument dims not assigned
Stacktrace:
 [1] sort!(::Array{Int64,2}) at ./sort.jl:1105
 [2] sample!(::Random._GLOBAL_RNG, ::Array{Int64,1}, ::Weights{Float64,Float64,Array{Float64,1}}, ::Array{Int64,2}; replace::Bool, ordered::Bool) at /Users/bieganek/.julia/packages/StatsBase/EA8Mh/src/sampling.jl:844
 [3] #sample#171 at /Users/bieganek/.julia/packages/StatsBase/EA8Mh/src/sampling.jl:878 [inlined]
 [4] #sample#172 at /Users/bieganek/.julia/packages/StatsBase/EA8Mh/src/sampling.jl:881 [inlined]
 [5] top-level scope at REPL[4]:1

CameronBieganek avatar Mar 23 '21 16:03 CameronBieganek

It seems reasonable to support this via iteration order of x, i.e. column-major order.

After all, that's the ordering that is used if a is multidimensional:

julia> sample([17 3; 4 1], 2, replace=false, ordered=true)
2-element Array{Int64,1}:
  4
  3

stevengj avatar Mar 23 '21 16:03 stevengj

(It won't give correct results for replace=true until #675 is fixed, however.)

stevengj avatar Mar 23 '21 16:03 stevengj