Hello,
I am a new Julia user, moving from Stan. I am currently working on Bayesian epidemiological modeling. I have worked the @Storopoli tutorial and have successfully implemented posterior sampling with NUTS(), following the code on the page and replicating the posterior output. I have a couple of questions.
Question 1
I wanted to experiment with the arguments of NUTS(), described in the Turing.jl dev docs, namely the n_adapts (The number of samples to use with adaptation), the target acceptance rate for dual averaging and max_depth (Maximum doubling tree depth), so as to replicate the analysis I have performed in Stan.
I change
chain_sir = sample(model_sir, NUTS(), 2_000)
to
chain_sir = sample(model_sir, NUTS(600, .70, 19), 700)
But this throws the error:
Stacktrace:
[1] indexed_iterate(I::Int64, i::Int64, state::Nothing)
@ Base .\tuple.jl:98
[2] merge(a::NamedTuple{(), Tuple{}}, itr::Tuple{Int64, Float64, Int64})
@ Base .\namedtuple.jl:292
[3] (NUTS{Turing.Core.ForwardDiffAD{40}})(::Int64, ::Vararg{Any})
@ Turing.Inference C:\Users\lbour\.julia\packages\Turing\uAz5c\src\inference\hmc.jl:460
[4] NUTS(::Int64, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
@ Turing.Inference C:\Users\lbour\.julia\packages\Turing\uAz5c\src\inference\hmc.jl:414
[5] NUTS(::Int64, ::Vararg{Any})
@ Turing.Inference C:\Users\lbour\.julia\packages\Turing\uAz5c\src\inference\hmc.jl:414
[6] top-level scope
@ REPL[24]:1
Also, tried
chain_sir = sample(model_sir, NUTS(n_adapts = 600, target_accept_ratio = 0.7, max_depth=10),700)
, receiving the error
Closest candidates are:
(NUTS{AD})(::Float64; max_depth, Δ_max, init_ϵ, metricT) where AD at C:\Users\lbour\.julia\packages\Turing\uAz5c\src\inference\hmc.jl:449 got unsupported keyword arguments "n_adapts", "target_accept_ratio"
(NUTS{AD})(::Int64, ::Float64, ::Symbol...; max_depth, Δ_max, init_ϵ, metricT) where AD at C:\Users\lbour\.julia\packages\Turing\uAz5c\src\inference\hmc.jl:437 got unsupported keyword arguments "n_adapts", "target_accept_ratio"
(NUTS{AD})(::Int64, ::Float64, ::Tuple{}; kwargs...) where AD at C:\Users\lbour\.julia\packages\Turing\uAz5c\src\inference\hmc.jl:428
What would be the correct syntax? An online search did not help.
Question 2
I am currently implementing a more complex Bayesian compartmental model (I have previously asked a related question here), and I have translated my Stan code to Julia. I aim to implement my analysis via R, therefore I am using the JuliaConnectoR R library.
I define all functions and the model into a Julia module, then I import the module in R with JuliaConnectoR. Test data, the Julia module and a reproducible R code are available in this repo.
2.A. The following attempt to execute NUTS results in the error
nuts_fit_1 <- Turing$sample(model_sir, Turing$NUTS(0.7), 700)
Error: Evaluation in Julia failed.
Original Julia error message:
MethodError: no method matching sample(::DynamicPPL.Model{Main.Test_module.var"#1#2", (:y_deaths, :n_obs, :n_pop, :y_init, :ts, :left_t, :right_t, :eta0_sd, :eta1_sd, :gamma_shape, :gamma_scale, :sigmaBM_sd, :reciprocal_phi_scale, :sigmaBM_cp1, :sigmaBM_cp2, :I_D_rev), (), (), Tuple{Vector{Float64}, Int64, Float64, Vector{Float64}, Vector{Int64}, Vector{Int64}, Vector{Int64}, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Vector{Float64}}, Tuple{}}, ::Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}, ::Float64)
Closest candidates are:
sample(::AbstractMCMC.AbstractModel, !Matched::AbstractMCMC.AbstractSampler, ::Any; kwargs...) at C:\Users\lbour\.julia\packages\AbstractMCMC\oou1a\src\sample.jl:15
sample(::AbstractMCMC.AbstractModel, ::Turing.Inference.InferenceAlgorithm, !Matched::AbstractMCMC.AbstractMCMCParallel, !Matched::Integer, !Matched::Integer; kwargs...) at C:
2.B Attempting to change the user arguments results in:
nuts_fit_1 <- Turing$sample(model_sir, Turing$NUTS(600,0.7,10), 700)
Error: Evaluation in Julia failed.
Original Julia error message:
BoundsError: attempt to access Float64 at index [2]
Stacktrace:
[1] indexed_iterate(I::Float64, i::Int64, state::Nothing)
@ Base .\tuple.jl:98
[2] merge(a::NamedTuple{(), Tuple{}}, itr::Tuple{Float64, Float64, Float64})
@ Base .\namedtuple.jl:292
[3] (Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}})(::Float64, ::Vararg{Float64})
@ Turing.Inference C:\Users\lbour\.julia\packages\Turing\uAz5c\src\inference\hmc.jl:460
[4] Turing.Inference.NUTS(::Float64, ::Vararg{Float64}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
@ Turing.Inference C:\Users\lbour\.julia\packages\Turing\uAz5c\src\inference\hmc.jl:414
[5] Turing.Inference.NUTS(::Float64, ::Vararg{Float64})
@ Turing.Inference C:\Users\lbour\.julia\packages\Turing\uAz5c\src\inference\hmc.jl:414
[6] invokelatest(::Any, ::Any, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
Trying to debug 2.A., I suspect that the issue arises from my @model , but I cannot understand the Error output in order to fix it. Point 2.B. is more related to Question 1.
Thanks.
Versions:
WIndows 10, laptop machine
Julia 1.7.0
Turing v0.15.1
DifferentialEquations v6.18.0
LazyArrays v0.21.20
R 4.1.0
JuliaConnectoR 1.1.0
RStudio 1.4.11