1
votes

I am playing a bit with Julia.

Consider this function:

function drawValues(fromDistribution, byCount)

#=
inputs:
fromDistribution : 
    A 2D array
    Each element is an array with two elements
    The first one is a value, and the second one is the probability of that value
    We will draw a value out of this distribution from a random number generator
    
byCount :
    An integer
    We draw that many values from the source distribution

=#



values = []
wts    = []

for i = 1:length(fromDistribution)
    
    push!(values, fromDistribution[i][1])
    push!(wts   , fromDistribution[i][2])
    
end


w = Weights(wts)

res = []

for i = 1:byCount
    
    r = sample(values, w)
    push!(res, r)
    
end


plot(values, wts)
print(res)


end

This throws the error :

ERROR: MethodError: no method matching Weights(::Array{Any,1}, ::Float64) Closest candidates are: Weights(::var"#18#V", ::var"#16#S") where {var"#16#S"<:Real, var"#17#T"<:Real, var"#18#V"<:AbstractArray{var"#17#T",1}} at /home/hedgehog/.julia/packages/StatsBase/EA8Mh/src/weights.jl:13
Weights(::Any) at /home/hedgehog/.julia/packages/StatsBase/EA8Mh/src/weights.jl:16 Stacktrace: [1] Weights(::Array{Any,1}) at /home/hedgehog/.julia/packages/StatsBase/EA8Mh/src/weights.jl:16 [2] drawValues(::Array{Array{Float64,1},1}, ::Int64) at /home/hedgehog/LASER.jl:51 [3] top-level scope at REPL[13]:1 [4] run_repl(::REPL.AbstractREPL, ::Any) at /build/julia/src/julia-1.5.3/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:288

It seems, that the second definition ( Weights(::Array{Any,1})) whould fit. But somehow Julia sees two input arguments?

Please help.

Version details :

Julia Version 1.5.3 Commit 788b2c77c1* (2020-11-09 13:37 UTC) Platform Info:
OS: Linux (x86_64-pc-linux-gnu)
CPU: AMD Ryzen 7 3700X 8-Core Processor
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-10.0.1 (ORCJIT, znver2)

1
Write values = first.(fromDistribution) and wts = last.(fromDistribution) instead of the loop.DNF
This is the same post as on the Julia Discourse. Please don't cross-post, or at least give links to avoid wasting people's time.Milan Bouchet-Valat

1 Answers

1
votes

Your Vectors have elements of type any.

It should be:

wts = Float64[]

When you write wts=[] it is an equivalent of wts=Any[].

Have a look at the weight methods:

julia> methods(weights)
# 3 methods for generic function "weights":
[1] weights(vs::AbstractArray{T,1} where T<:Real) in StatsBase at c:\JuliaPkg\Julia1.5.3\packages\StatsBase\EA8Mh\src\weights.jl:76
[2] weights(vs::AbstractArray{T,N} where N where T<:Real) in StatsBase at c:\JuliaPkg\Julia1.5.3\packages\StatsBase\EA8Mh\src\weights.jl:77
[3] weights(model::StatisticalModel) in StatsBase at c:\JuliaPkg\Julia1.5.3\packages\StatsBase\EA8Mh\src\statmodels.jl:143

A container having elements of subtype of Real is required.

Similarly for the other containers providing the types is recommended as well:

value = Float64[]
res = Float64[]  # or maybe Int[] depending on what your code does