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)
values = first.(fromDistribution)
andwts = last.(fromDistribution)
instead of the loop. – DNF