JuliaStats / Klara.jl

MCMC inference in Julia
Other
167 stars 38 forks source link

MethodError: Cannot `convert` an object of type Array{Float64,1} to an object of type Float64 #160

Closed serdar- closed 7 years ago

serdar- commented 7 years ago

Dear all,

I try to do sampling using MALA sampler but I get the following error:

MethodError: Cannot `convert` an object of type Array{Float64,1} to an object of type Float64
This may have arisen from a call to the constructor Float64(...),
since type constructors fall back to convert methods.

 in ##lowlevel_variable_method#610 at C:\Users\Serdar\.julia\v0.5\Klara\src\codegen.jl:67 [inlined]
 in ##closure#612(::Klara.BasicContMuvParameterState{Float64}) at C:\Users\Serdar\.julia\v0.5\Klara\src\variables/parameters\parameters.jl:40
 in ##uptotarget_closures#614(::Klara.BasicContMuvParameterState{Float64}) at C:\Users\Serdar\.julia\v0.5\Klara\src\variables/parameters\parameters.jl:89
 in initialize!(::Klara.BasicContMuvParameterState{Float64}, ::Klara.BasicContMuvParameter, ::Klara.MALA, ::Dict{Symbol,Any}) at C:\Users\Serdar\.julia\v0.5\Klara\src\samplers\MALA.jl:68
 in Klara.BasicMCJob(::Klara.GenericModel, ::Int64, ::Klara.MALA, ::Klara.VanillaMCTuner, ::Klara.BasicMCRange{Int64}, ::Array{Klara.VariableState,1}, ::Dict{Symbol,Any}, ::Bool, ::Bool, ::Bool, ::Bool) at C:\Users\Serdar\.julia\v0.5\Klara\src\jobs\BasicMCJob.jl:85
 in (::Core.#kw#Type)(::Array{Any,1}, ::Type{Klara.BasicMCJob}, ::Klara.GenericModel, ::Klara.MALA, ::Klara.BasicMCRange{Int64}, ::Array{Any,1}) at .\<missing>:0
 in #BasicMCJob#188(::Int64, ::Klara.VanillaMCTuner, ::Dict{Symbol,Any}, ::Bool, ::Bool, ::Bool, ::Bool, ::Type{T}, ::Klara.GenericModel, ::Klara.MALA, ::Klara.BasicMCRange{Int64}, ::Dict{Symbol,Array{Float64,1}}) at C:\Users\Serdar\.julia\v0.5\Klara\src\jobs\BasicMCJob.jl:186
 in (::Core.#kw#Type)(::Array{Any,1}, ::Type{Klara.BasicMCJob}, ::Klara.GenericModel, ::Klara.MALA, ::Klara.BasicMCRange{Int64}, ::Dict{Symbol,Array{Float64,1}}) at .\<missing>:0

Here is my code:

fl(v::Vector{Float64}) = S*v

df(v::Vector{Float64}) = sum(S,2)

v_int = (vu-vl).*rand(95) - vu

prob = BasicContMuvParameter(:prob, logtarget=fl,gradlogtarget=df)

samp_model = likelihood_model(prob, false)

sampler = MALA(0.9)

mcrange = BasicMCRange(nsteps=1000, burnin=100)

v0 = Dict(:prob=>v_int)

outopts = Dict{Symbol, Any}(:monitor=>[:value, :logtarget, :gradlogtarget], :diagnostics=>[:accept])

job = BasicMCJob(samp_model, sampler, mcrange, v0, outopts=outopts)

run(job)

What could be the problem?

papamarkou commented 7 years ago

I tried to run your code but vu, vl and S are not defined in your snippet. How did you define these? My first guess is that there may be a breakage depending on the version of Julia you are using, as the most recent ones have changed the way they handle arrays. You may send me your full snippet and I can check it, possibly with some delay, as I am back to my base from 11 July onwards.

serdar- commented 7 years ago

Ok I think i sorted out the problem. Apparently the function fl has to return one floating point value rather than an array. Thus my S*v returns an array. I want to sample from a system of linear equations, is it possible for me to do that?

papamarkou commented 7 years ago

I will close this issue @serdar, as you found out the answer to the question of your example. As for the last question about solving a system of linear equations, it is rather generic and open-ended for me to answer. If you have any more specific queries, ideally with an attempted solution too, then you may open an issue along with accompanying code and I can look at it.