I am new to Turing and in the process of translating a complex dynamical stochastic model from Stan to Turing. Instead of declaring individual variables (many of them arrays), I would like to include them in a dictionary, and here I am running into issues using the right type. How do I initialize dictionary and array using the correct type?
This short piece of code demonstrates what I would like to achieve without having to declare x
:
using Turing
@model function mwe_model()
# unnecessary declaration of an extra variable to get type
x ~ Normal(0.0, 1.0)
T = typeof(x)
stoch = Dict{String, T}();
stoch["param1"] = Array{T}(undef, 1)
stoch["param1"][1] ~ Normal(0.0, 1.0)
end
PS: I have tried setting the type explicitly which is less flexible and sometimes results in errors:
import ForwardDiff
using Turing
@model function mwe_model()
T = ForwardDiff.Dual :: Type
stoch = Dict{String, T}();
# this creates an error
stoch["param1"] = Array{T}(undef, 1)
stoch["param1"][1] ~ Normal(0.0, 1.0)
# this works:
#stoch["param2"] = Array{T}(undef, 1, 1)
#stoch["param2"][1, 1] ~ Normal(0.0, 1.0)
end
display(sample(mwe_model(), NUTS(), 1_000))
The code above terminates with an error: LoadError: UndefRefError: access to undefined reference
. However, it appears to work when using the 2-dimensional array stoch["param2"]
instead of stoch["param1"]
. This is probably a bug, but I am most interested in a more flexible way to obtain the type without having to hard-code it.