Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MethodError from sample with Float32 weights #695

Closed
juliohm opened this issue Jun 1, 2021 · 8 comments
Closed

MethodError from sample with Float32 weights #695

juliohm opened this issue Jun 1, 2021 · 8 comments
Labels

Comments

@juliohm
Copy link

juliohm commented Jun 1, 2021

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.

@ararslan ararslan changed the title [BUG] sample crashes with Float32 MethodError from sample with Float32 weights Jun 1, 2021
@nalimilan
Copy link
Member

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.

@juliohm
Copy link
Author

juliohm commented Jun 3, 2021

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.

@nalimilan
Copy link
Member

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

@juliohm
Copy link
Author

juliohm commented Oct 10, 2021

This bug is affecting me again in other projects. The make_alias_table function is causing a lot of issues downstream it seems.

@juliohm
Copy link
Author

juliohm commented Oct 10, 2021

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

@nalimilan
Copy link
Member

Can you investigate whether widening the type signature seems to be OK?

@LilithHafner
Copy link
Contributor

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

@devmotion
Copy link
Member

This issue was fixed by #499.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants