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

Sampling with WeightVec and Float32

Open tomblaze opened this issue 10 years ago • 1 comments

I encountered a bug when wanting to sample from weight vec of single-precision floats. Below is a minimal working example.

prob_32 = rand(Float32, 1000);
prob_64 = rand(Float64, 1000);
sample(1:1000, WeightVec(prob_64), 31);
sample(1:1000, WeightVec(prob_32), 31);  #both work
sample(1:1000, WeightVec(prob_64), 32); #this works
sample(1:1000, WeightVec(prob_32), 32); #this throws error

ERROR: MethodError: `make_alias_table!` has no method matching make_alias_table!(::Array{Float32,1}, ::Float32, ::Array{Float64,1}, ::Array{Int64,1})
Closest candidates are:
  make_alias_table!(::AbstractArray{Float64,1}, ::Float64, ::AbstractArray{Float64,1}, ::AbstractArray{Int64,1})
 in alias_sample! at /HOME/.julia/v0.4/StatsBase/src/sampling.jl:424
 in sample! at /HOME/.julia/v0.4/StatsBase/src/sampling.jl:515

I can get around this problem by sampling one item at a time, but seems like a bug to fix.

tomblaze avatar May 04 '16 23:05 tomblaze

Historically, the functions in StatsBase and Distributions have mainly been defined for Float64. We are in a process of making the methods more generic with respect to element type but there is still much work to do. A PR to handle this would be appreciated.

andreasnoack avatar May 04 '16 23:05 andreasnoack