StatsBase.jl
StatsBase.jl copied to clipboard
MethodError from sample with Float32 weights
I think this code should work:
julia> using StatsBase
julia> sample(1:3, StatsBase.Weights(rand(Float32,3)), replace=true)
ERROR: MethodError: no method matching sample(::UnitRange{Int64}, ::Weights{Float32, Float32, Vector{Float32}}; replace=true)
Closest candidates are:
sample(::AbstractArray, ::AbstractWeights) at /home/juliohm/.julia/packages/StatsBase/DU1bT/src/sampling.jl:561 got unsupported keyword argument "replace"
sample(::AbstractArray, ::AbstractWeights, ::Integer; replace, ordered) at /home/juliohm/.julia/packages/StatsBase/DU1bT/src/sampling.jl:932
sample(::AbstractArray, ::AbstractWeights, ::Tuple{Vararg{Int64, N}} where N; replace, ordered) at /home/juliohm/.julia/packages/StatsBase/DU1bT/src/sampling.jl:939
It works fine with Float64.
It doesn't seem to work with Float64
either. AFAICT you need to pass a third positional argument giving the number of values you want if you pass replace
.
Sorry I copied the wrong line of code. This is the code that triggers the bug:
using StatsBase
n = 5030
N = 141230
sample(1:n, StatsBase.Weights(rand(Float32,n)), N, replace=true)
If you change to Float64 it works fine.
OK. Looking at the problematic make_alias_table
function, it seems that its signature could be relaxed without creating issues (this code is quite old).
This bug is affecting me again in other projects. The make_alias_table
function is causing a lot of issues downstream it seems.
Copying the error message:
ERROR: MethodError: no method matching make_alias_table!(::Weights{Float32, Float32, Vector{Float32}}, ::Float32, ::Vector{Float64}, ::Vector{Int64})
Closest candidates are:
make_alias_table!(::AbstractVector{Float64}, ::Float64, ::AbstractVector{Float64}, ::AbstractVector{Int64}) at /home/juliohm/.julia/packages/StatsBase/DWW4r/src/sampling.jl:587
Stacktrace:
[1] alias_sample!(rng::Random._GLOBAL_RNG, a::UnitRange{Int64}, wv::Weights{Float32, Float32, Vector{Float32}}, x::Vector{Int64})
@ StatsBase ~/.julia/packages/StatsBase/DWW4r/src/sampling.jl:666
[2] sample!(rng::Random._GLOBAL_RNG, a::UnitRange{Int64}, wv::Weights{Float32, Float32, Vector{Float32}}, x::Vector{Int64}; replace::Bool, ordered::Bool)
@ StatsBase ~/.julia/packages/StatsBase/DWW4r/src/sampling.jl:914
[3] #sample#203
@ ~/.julia/packages/StatsBase/DWW4r/src/sampling.jl:929 [inlined]
[4] #sample#204
@ ~/.julia/packages/StatsBase/DWW4r/src/sampling.jl:932 [inlined]
[5] top-level scope
@ REPL[10]:1
Can you investigate whether widening the type signature seems to be OK?
This bug is no longer present.
julia> using StatsBase
julia> n = 5030
5030
julia> N = 141230
141230
julia> sample(1:n, StatsBase.Weights(rand(Float32,n)), N, replace=true)
141230-element Vector{Int64}:
247
862
2144
⋮
3452
1185
This issue was fixed by https://github.com/JuliaStats/StatsBase.jl/pull/499.