# Clear variable?

I am trying to sample a Turing model with this Gibbs sampler:

sampler = Gibbs(PG(200, :toss), NUTS(.65, :a, :b, :θ, :σ, :μ, :temp))


Unfortunately, while making sure some of my code was correct, I have set a, b, \mu to some values. When I try to run this line of code, I get the error

MethodError: Cannot convert an object of type Float64 to an object of type Symbol


I tried setting these variables to the keyword nothing. But that didn’t help. I tried clear!(:a) but that is throwing up an error.

I’m on Julia 1.4.1. Please help, am I reading this error correctly? How do I fix it, short of closing my workspace and starting the kernel again?

I don’t think the issue is what you think (though I haven’t used Turing much, so not 100% sure). Here’s where I usually see such an error:

julia> a = [:a, :b]
2-element Array{Symbol,1}:
:a
:b

julia> push!(a, 1.)
ERROR: MethodError: Cannot convert an object of type Float64 to an object of type Symbol
Closest candidates are:
convert(::Type{T}, ::T) where T at essentials.jl:167
Symbol(::Any...) at strings/basic.jl:206


Because the Vector is of Symbols, and Julia won’t implicity convert a Float to a symbol. I don’t know enough about the functions there to know which might be causing the problem, but you can probably dig into the stack trace and figure it out.

Note: is usually helpful to answering questions if you can include a minimal example of code that causes the error and the entire stack trace. See here for more tips

2 Likes

Here is a MWE

g=Gibbs(NUTS(:u),PG(20, :s))


This single line throws the error

MethodError: no method matching iterate(::Symbol)
Closest candidates are:
iterate(!Matched::Core.SimpleVector) at essentials.jl:603
iterate(!Matched::Core.SimpleVector, !Matched::Any) at essentials.jl:603
iterate(!Matched::ExponentialBackOff) at error.jl:253
...

Stacktrace:
[1] indexed_iterate(::Symbol, ::Int64) at .\tuple.jl:84
[2] merge(::NamedTuple{(),Tuple{}}, ::Tuple{Symbol}) at .\namedtuple.jl:255
[4] NUTS(::Symbol; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at C:\Users\Manoj\.julia\packages\Turing\GMBTf\src\inference\hmc.jl:313
[5] NUTS(::Symbol) at C:\Users\Manoj\.julia\packages\Turing\GMBTf\src\inference\hmc.jl:313
[6] top-level scope at In[113]:1


If I change to

g=Gibbs(NUTS(.65,:u),PG(20, :s))


I get the error

MethodError: Cannot convert an object of type Float64 to an object of type Symbol
Closest candidates are:
convert(::Type{T}, !Matched::T) where T at essentials.jl:171
Symbol(::Any...) at strings/basic.jl:207

Stacktrace:
[1] merge(::NamedTuple{(),Tuple{}}, ::Tuple{Float64,Symbol}) at .\namedtuple.jl:255
[3] NUTS(::Float64, ::Vararg{Any,N} where N; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at C:\Users\Manoj\.julia\packages\Turing\GMBTf\src\inference\hmc.jl:313
[4] NUTS(::Float64, ::Vararg{Any,N} where N) at C:\Users\Manoj\.julia\packages\Turing\GMBTf\src\inference\hmc.jl:313
[5] top-level scope at In[114]:1


Finally if I change to

g=Gibbs(NUTS(1000,.65,:u),PG(20, :s))


it doesn’t give an error.

Can someone explain to me why I need to explicitly give the parameters to NUTS here? I can get away in sample() with giving NUTS only one parameter. The documentation says I can give no parameters, and it will pick up default values. Also what does the parameter

n_adapts::Int : The number of samples to use with adaptation.


That is a bug. Looks like we didn’t implement the constructors for: NUTS(.65,:u) and NUTS(:u).
NUTS is not a valid Gibbs component. Use HMC.