Open ClaudMor opened 3 years ago
I don't think I'll be getting to this any time soon, but maybe @tpapp would like to chime in if he has an idea. The initialization runs an MAP first so that could be something, but I don't see how that could get to such a bad starting point so I am pretty confused by this behavior as well.
I will look into it.
Do you have any update? I would be very interested to know.
Apologies for the delay (I am working through a large backlog) and thanks for the ping.
Conceptually, the q
is a point in ββΏ
(unconstrained), after the transformation. So you take your initial parameters, tag on a sigma, and then transform back; or transform back and tag on a sigma which will be transformed with asββ
. I chose the latter, and would do it like this:
variable_transformation = as(Vector, asββ, length(variable_parameters_priors))
q0 = vcat(inverse(variable_transformation, π«), fill(0.0, size(data', 1) ))
mcmc_kwargs = (initialization = (q = q0,),
warmup_stages = default_warmup_stages(; local_optimization = nothing))
bayesian_result_dynamic = dynamichmc_inference(problem, Tsit5(), t,data', variable_parameters_priors,
variable_transformation;
save_idxs = [4*4+1],
Ο_priors = fill(InverseGamma(2, 3), size(data', 1)),
mcmc_kwargs = mcmc_kwargs)
This skips the initial optimization, which fails. I imagine that this is happening because of a singularity that is far from the typical set, but breaks MAP. But this should be fine since we have an initial point.
Let me know if I can help with anything else.
Hello @tpapp ,
thanks for answering and sorry for replying so late.
I implemented your fix, and it properly works for both simple models like the above one and more complex models, the only problem being that it throws an error when the initialization q0
is "too good".
ERROR: Reached maximum number of iterations while bisecting interval for Ο΅.
I explain myself better in the following MWE ( partially taken from the first comment) that reproduces this behaviour. So with your fix, now this works fine:
using DynamicHMC, Turing, DifferentialEquations, DiffEqBayes, RecursiveArrayTools, TransformVariables, Optim, DiffEqParamEstim, Plots
# Model parameters
Ξ² = 0.01# infection rate
Ξ»_R = 0.05 # inverse of transition time from infected to recovered
Ξ»_D = 0.83 # inverse of transition time from infected to dead
P = vcat([Ξ², Ξ»_R, Ξ»_D]...)
# regional contact matrix and regional population
## regional contact matrix
regional_all_contact_matrix = [3.45536 0.485314 0.506389 0.123002 ; 0.597721 2.11738 0.911374 0.323385 ; 0.906231 1.35041 1.60756 0.67411 ; 0.237902 0.432631 0.726488 0.979258] # 4x4 contact matrix
## regional population stratified by age
N= [723208 , 874150, 1330993, 1411928] # array of 4 elements, each of which representing the absolute amount of population in the corresponding age class.
# model fixed parameters ( transitions)
Ξ΄β, Ξ΄β, Ξ΄β, Ξ΄β = [0.003/100, 0.004/100, (0.015+0.030+0.064+0.213+0.718)/(5*100), (2.384+8.466+12.497+1.117)/(4*100)]
Ξ΄ = vcat(repeat([Ξ΄β],1),repeat([Ξ΄β],1),repeat([Ξ΄β],1),repeat([Ξ΄β],4-1-1-1))
# Initial conditions
iβ = 0.075 # fraction of initial infected people in every age class
Iβ = repeat([iβ],4)
Sβ = N.-Iβ
Rβ = [0.0 for n in 1:length(N)]
Dβ = [0.0 for n in 1:length(N)]
D_totβ = [0.0]
β¬ = vcat([Sβ, Iβ, Rβ, Dβ, D_totβ]...)
# Time
final_time = 20
π― = (1.0,final_time);
t = collect(1.0:20.0)
# data used for calibration
data = [0,2,4,5,5,13,17,21,26,46,59,81,111,133,154,175,209,238,283,315]
function SIRD!(du,u,p,t)
# Parameters to be calibrated
Ξ², Ξ»_R, Ξ»_D = p
# initialize this parameter (death probability stratified by age, taken from literature)
C = regional_all_contact_matrix
# State variables
S = @view u[4*0+1:4*1]
I = @view u[4*1+1:4*2]
R = @view u[4*2+1:4*3]
D = @view u[4*3+1:4*4]
D_tot = @view u[4*4+1]
# Differentials
dS = @view du[4*0+1:4*1]
dI = @view du[4*1+1:4*2]
dR = @view du[4*2+1:4*3]
dD = @view du[4*3+1:4*4]
dD_tot = @view du[4*4+1]
# Force of infection
Ξ = Ξ²*[sum([C[i,j]*I[j]/N[j] for j in 1:size(C)[1]]) for i in 1:size(C)[2]]
# System of equations
@. dS = -Ξ*S
@. dI = Ξ*S - ((1-Ξ΄)*Ξ»_R + Ξ΄*Ξ»_D)*I
@. dR = Ξ»_R*(1-Ξ΄)*I
@. dD = Ξ»_D*Ξ΄*I
@. dD_tot = dD[1]+dD[2]+dD[3]+dD[4]
end;
# the problem builds and solves correctly
problem = ODEProblem(SIRD!,β¬, π―, P )
solution = solve(problem, Tsit5(), saveat = 1:20);
# build a faster problem
fast_problem = ODEProblem(modelingtoolkitize(problem),β¬, π―, P)
fast_solution = solve(problem, Tsit5(), saveat = 1:20);
# define priors
const variable_parameters_priors = [Uniform(0.0,1.0),Uniform(0.0,1.0),Uniform(0.0,1.0)]
# define transoformations
variable_transformation = as(Vector, asββ, length(variable_parameters_priors))
# define initial values and kwargs
q0 = vcat(inverse(variable_transformation, P), fill(0.0, size(data', 1) ))
mcmc_kwargs = (initialization = (q = q0,),
warmup_stages = default_warmup_stages(; local_optimization = nothing))
# run DynamicHMC
bayesian_result_dynamic = dynamichmc_inference(fast_problem, Tsit5(), t,data', variable_parameters_priors,
variable_transformation;
save_idxs = [4*4+1],
Ο_priors = fill(InverseGamma(2, 3), size(data', 1)),
mcmc_kwargs = mcmc_kwargs, num_samples = 100)
β Warning: Interrupted. Larger maxiters is needed.
β @ DiffEqBase C:\Users\claud\.julia\packages\DiffEqBase\F3qfC\src\integrator_interface.jl:328
β Info: found initial stepsize
β Ο΅ = 0.00315
β Info: Starting MCMC
β total_steps = 75
β tuning = "stepsize"
β Info: MCMC progress
β step = 1
β seconds_per_step = 0.0016
β estimated_seconds_left = 0.12
β Ο΅ = 0.00315
β Info: Starting MCMC
β total_steps = 25
β tuning = "stepsize and LinearAlgebra.Diagonal metric"
β Info: MCMC progress
β step = 1
β seconds_per_step = 0.00021
β estimated_seconds_left = 0.0051
β Ο΅ = 0.0749
β Info: adaptation finished
β adapted_kinetic_energy = Gaussian kinetic energy (Diagonal), βdiag(Mβ»ΒΉ): [0.40236231155083, 0.31137659812546126, 0.3492919363452165, 0.23136714942917988]
β Info: Starting MCMC
β total_steps = 50
β tuning = "stepsize and LinearAlgebra.Diagonal metric"
β Info: MCMC progress
β step = 1
β seconds_per_step = 0.00085
β estimated_seconds_left = 0.042
β Ο΅ = 0.0533
β Info: adaptation finished
β adapted_kinetic_energy = Gaussian kinetic energy (Diagonal), βdiag(Mβ»ΒΉ): [0.25375311569626813, 0.6560920328559817, 0.3752689270387867, 0.31201639993723373]
β Info: Starting MCMC
β total_steps = 100
β tuning = "stepsize and LinearAlgebra.Diagonal metric"
β Info: MCMC progress
β step = 1
β seconds_per_step = 0.002
β estimated_seconds_left = 0.2
β Ο΅ = 0.0211
β Info: adaptation finished
β adapted_kinetic_energy = Gaussian kinetic energy (Diagonal), βdiag(Mβ»ΒΉ): [0.23349928137563844, 0.49562480846134777, 0.6331264780774535, 0.18102869444738545]
β Info: Starting MCMC
β total_steps = 200
β tuning = "stepsize and LinearAlgebra.Diagonal metric"
β Info: MCMC progress
β step = 1
β seconds_per_step = 0.00018
β estimated_seconds_left = 0.035
β Ο΅ = 0.0242
β Info: MCMC progress
β step = 101
β seconds_per_step = 0.0031
β estimated_seconds_left = 0.31
β Ο΅ = 0.0733
β Info: adaptation finished
β adapted_kinetic_energy = Gaussian kinetic energy (Diagonal), βdiag(Mβ»ΒΉ): [0.20678108418406718, 0.5267738626279858, 0.4572416283983815, 0.1447869395318676]
β Info: Starting MCMC
β total_steps = 400
β tuning = "stepsize and LinearAlgebra.Diagonal metric"
β Info: MCMC progress
β step = 1
β seconds_per_step = 0.00014
β estimated_seconds_left = 0.055
β Ο΅ = 0.0242
β Info: MCMC progress
β step = 101
β seconds_per_step = 0.0044
β estimated_seconds_left = 1.3
β Ο΅ = 0.0228
β Info: MCMC progress
β step = 201
β seconds_per_step = 0.0021
β estimated_seconds_left = 0.43
β Ο΅ = 0.0267
β Info: MCMC progress
β step = 301
β seconds_per_step = 0.0035
β estimated_seconds_left = 0.34
β Ο΅ = 0.0366
β Info: adaptation finished
β adapted_kinetic_energy = Gaussian kinetic energy (Diagonal), βdiag(Mβ»ΒΉ): [0.24400473754408097, 0.7548261634208205, 0.5741360148155081, 0.20164617282342642]
β Info: Starting MCMC
β total_steps = 50
β tuning = "stepsize"
β Info: MCMC progress
β step = 1
β seconds_per_step = 9.3e-5
β estimated_seconds_left = 0.0046
β Ο΅ = 0.0309
β Info: Starting MCMC
β total_steps = 100
β Info: MCMC progress
β step = 1
β seconds_per_step = 0.0011
β estimated_seconds_left = 0.11
But if I then try to better initialize the parameters using Optim:
# obtain optim-compatible data
const optim_data = convert(Array, VectorOfArray(data))
# define cost function
cost_function = build_loss_objective(fast_problem, Tsit5(), L2Loss(t, optim_data'), maxiters=1000000, verbose=true, save_idxs=[4*4+1])
# define bounds that mimic the priors above
const lower_bounds = [0.01, 0.01, 0.01]
const upper_bounds = [1.0, 1.0, 1.0]
# run optim
result_optim = Optim.optimize(cost_function, lower_bounds, upper_bounds, P, Optim.Fminbox(BFGS()))
# get the calibrated parameters
P_optim = result_optim.minimizer
# check that the parameters coming from optim correspond to a kind of good fit
calibrated_solution = solve(fast_problem, Tsit5(); saveat=t, u0=β¬, p=P_optim)
total_predicted_deaths = [slice[end] for slice in calibrated_solution.u]
p = plot(calibrated_solution.t, total_predicted_deaths)
plot!(calibrated_solution.t, data,seriestype=:scatter)
p
# initilialize DynamicHMC with the parameters values coming from optim
q0 = vcat(inverse(variable_transformation, P_optim), fill(0.0, size(data', 1) ))
mcmc_kwargs = (initialization = (q = q0,),
warmup_stages = default_warmup_stages(; local_optimization = nothing))
I get:
bayesian_result_dynamic = dynamichmc_inference(fast_problem, Tsit5(), t,data', variable_parameters_priors,
variable_transformation;
save_idxs = [4*4+1],
Ο_priors = fill(InverseGamma(2, 3), size(data', 1)),
mcmc_kwargs = mcmc_kwargs, num_samples = 10000)
β Warning: Automatic dt set the starting dt as NaN, causing instability.
β @ OrdinaryDiffEq C:\Users\claud\.julia\packages\OrdinaryDiffEq\ZgbOP\src\solve.jl:479
β Warning: NaN dt detected. Likely a NaN value in the state, parameters, or derivative value caused this outcome.
β @ DiffEqBase C:\Users\claud\.julia\packages\DiffEqBase\F3qfC\src\integrator_interface.jl:322
β Warning: Automatic dt set the starting dt as NaN, causing instability.
β @ OrdinaryDiffEq C:\Users\claud\.julia\packages\OrdinaryDiffEq\ZgbOP\src\solve.jl:479
β Warning: NaN dt detected. Likely a NaN value in the state, parameters, or derivative value caused this outcome.
β @ DiffEqBase C:\Users\claud\.julia\packages\DiffEqBase\F3qfC\src\integrator_interface.jl:322
β Warning: Instability detected. Aborting
β @ DiffEqBase C:\Users\claud\.julia\packages\DiffEqBase\F3qfC\src\integrator_interface.jl:348
β Warning: dt <= dtmin. Aborting. There is either an error in your model specification or the true solution is unstable.
β @ DiffEqBase C:\Users\claud\.julia\packages\DiffEqBase\F3qfC\src\integrator_interface.jl:342
ERROR: Reached maximum number of iterations while bisecting interval for Ο΅.
Stacktrace:
[1] error(::String) at .\error.jl:33
[2] bisect_stepsize(::InitialStepsizeSearch, ::DynamicHMC.var"#3#4"{DynamicHMC.Hamiltonian{GaussianKineticEnergy{LinearAlgebra.Diagonal{Float64,Array{Float64,1}},LinearAlgebra.Diagonal{Float64,Array{Float64,1}}},LogDensityProblems.ForwardDiffLogDensity{LogDensityProblems.TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:parameters, :Ο),Tuple{TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1},TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1}}}},DiffEqBayes.DynamicHMCPosterior{Tsit5,ODEProblem{Array{Float64,1},Tuple{Float64,Float64},true,Array{Float64,1},ODEFunction{true,typeof(SIRD!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},LinearAlgebra.Adjoint{Int64,Array{Int64,1}},Array{Float64,1},Array{Uniform{Float64},1},Array{InverseGamma{Float64},1},Tuple{},Array{Int64,1}}},ForwardDiff.GradientConfig{ForwardDiff.Tag{LogDensityProblems.var"#28#29"{LogDensityProblems.TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:parameters, :Ο),Tuple{TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1},TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1}}}},DiffEqBayes.DynamicHMCPosterior{Tsit5,ODEProblem{Array{Float64,1},Tuple{Float64,Float64},true,Array{Float64,1},ODEFunction{true,typeof(SIRD!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},LinearAlgebra.Adjoint{Int64,Array{Int64,1}},Array{Float64,1},Array{Uniform{Float64},1},Array{InverseGamma{Float64},1},Tuple{},Array{Int64,1}}}},Float64},Float64,4,Array{ForwardDiff.Dual{ForwardDiff.Tag{LogDensityProblems.var"#28#29"{LogDensityProblems.TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:parameters, :Ο),Tuple{TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1},TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1}}}},DiffEqBayes.DynamicHMCPosterior{Tsit5,ODEProblem{Array{Float64,1},Tuple{Float64,Float64},true,Array{Float64,1},ODEFunction{true,typeof(SIRD!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},LinearAlgebra.Adjoint{Int64,Array{Int64,1}},Array{Float64,1},Array{Uniform{Float64},1},Array{InverseGamma{Float64},1},Tuple{},Array{Int64,1}}}},Float64},Float64,4},1}}}},DynamicHMC.PhasePoint{DynamicHMC.EvaluatedLogDensity{Array{Float64,1},Float64},Array{Float64,1}},Float64}, ::Float64, ::Float64, ::Float64, ::Float64) at C:\Users\claud\.julia\packages\DynamicHMC\n1LsS\src\stepsize.jl:125
[3] find_initial_stepsize(::InitialStepsizeSearch, ::DynamicHMC.var"#3#4"{DynamicHMC.Hamiltonian{GaussianKineticEnergy{LinearAlgebra.Diagonal{Float64,Array{Float64,1}},LinearAlgebra.Diagonal{Float64,Array{Float64,1}}},LogDensityProblems.ForwardDiffLogDensity{LogDensityProblems.TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:parameters, :Ο),Tuple{TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1},TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1}}}},DiffEqBayes.DynamicHMCPosterior{Tsit5,ODEProblem{Array{Float64,1},Tuple{Float64,Float64},true,Array{Float64,1},ODEFunction{true,typeof(SIRD!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},LinearAlgebra.Adjoint{Int64,Array{Int64,1}},Array{Float64,1},Array{Uniform{Float64},1},Array{InverseGamma{Float64},1},Tuple{},Array{Int64,1}}},ForwardDiff.GradientConfig{ForwardDiff.Tag{LogDensityProblems.var"#28#29"{LogDensityProblems.TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:parameters, :Ο),Tuple{TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1},TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1}}}},DiffEqBayes.DynamicHMCPosterior{Tsit5,ODEProblem{Array{Float64,1},Tuple{Float64,Float64},true,Array{Float64,1},ODEFunction{true,typeof(SIRD!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},LinearAlgebra.Adjoint{Int64,Array{Int64,1}},Array{Float64,1},Array{Uniform{Float64},1},Array{InverseGamma{Float64},1},Tuple{},Array{Int64,1}}}},Float64},Float64,4,Array{ForwardDiff.Dual{ForwardDiff.Tag{LogDensityProblems.var"#28#29"{LogDensityProblems.TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:parameters, :Ο),Tuple{TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1},TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1}}}},DiffEqBayes.DynamicHMCPosterior{Tsit5,ODEProblem{Array{Float64,1},Tuple{Float64,Float64},true,Array{Float64,1},ODEFunction{true,typeof(SIRD!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},LinearAlgebra.Adjoint{Int64,Array{Int64,1}},Array{Float64,1},Array{Uniform{Float64},1},Array{InverseGamma{Float64},1},Tuple{},Array{Int64,1}}}},Float64},Float64,4},1}}}},DynamicHMC.PhasePoint{DynamicHMC.EvaluatedLogDensity{Array{Float64,1},Float64},Array{Float64,1}},Float64}) at C:\Users\claud\.julia\packages\DynamicHMC\n1LsS\src\stepsize.jl:149
[4] warmup at C:\Users\claud\.julia\packages\DynamicHMC\n1LsS\src\mcmc.jl:162 [inlined]
[5] #36 at C:\Users\claud\.julia\packages\DynamicHMC\n1LsS\src\mcmc.jl:446 [inlined]
[6] BottomRF at .\reduce.jl:81 [inlined]
[7] afoldl at .\operators.jl:526 [inlined] (repeats 2 times)
[8] _foldl_impl at .\tuple.jl:207 [inlined]
[9] foldl_impl(::Base.BottomRF{DynamicHMC.var"#36#37"{DynamicHMC.SamplingLogDensity{Random._GLOBAL_RNG,LogDensityProblems.ForwardDiffLogDensity{LogDensityProblems.TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:parameters, :Ο),Tuple{TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1},TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1}}}},DiffEqBayes.DynamicHMCPosterior{Tsit5,ODEProblem{Array{Float64,1},Tuple{Float64,Float64},true,Array{Float64,1},ODEFunction{true,typeof(SIRD!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},LinearAlgebra.Adjoint{Int64,Array{Int64,1}},Array{Float64,1},Array{Uniform{Float64},1},Array{InverseGamma{Float64},1},Tuple{},Array{Int64,1}}},ForwardDiff.GradientConfig{ForwardDiff.Tag{LogDensityProblems.var"#28#29"{LogDensityProblems.TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:parameters, :Ο),Tuple{TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1},TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1}}}},DiffEqBayes.DynamicHMCPosterior{Tsit5,ODEProblem{Array{Float64,1},Tuple{Float64,Float64},true,Array{Float64,1},ODEFunction{true,typeof(SIRD!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},LinearAlgebra.Adjoint{Int64,Array{Int64,1}},Array{Float64,1},Array{Uniform{Float64},1},Array{InverseGamma{Float64},1},Tuple{},Array{Int64,1}}}},Float64},Float64,4,Array{ForwardDiff.Dual{ForwardDiff.Tag{LogDensityProblems.var"#28#29"{LogDensityProblems.TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:parameters, :Ο),Tuple{TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1},TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1}}}},DiffEqBayes.DynamicHMCPosterior{Tsit5,ODEProblem{Array{Float64,1},Tuple{Float64,Float64},true,Array{Float64,1},ODEFunction{true,typeof(SIRD!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},LinearAlgebra.Adjoint{Int64,Array{Int64,1}},Array{Float64,1},Array{Uniform{Float64},1},Array{InverseGamma{Float64},1},Tuple{},Array{Int64,1}}}},Float64},Float64,4},1}}},DynamicHMC.NUTS{Val{:generalized}},LogProgressReport{Nothing}}}}, ::NamedTuple{(:init,),Tuple{Tuple{Tuple{},DynamicHMC.WarmupState{DynamicHMC.EvaluatedLogDensity{Array{Float64,1},Float64},GaussianKineticEnergy{LinearAlgebra.Diagonal{Float64,Array{Float64,1}},LinearAlgebra.Diagonal{Float64,Array{Float64,1}}},Nothing}}}}, ::Tuple{Nothing,InitialStepsizeSearch,TuningNUTS{Nothing,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{Nothing,DualAveraging{Float64}}}) at .\reduce.jl:48
[10] mapfoldl_impl(::typeof(identity), ::DynamicHMC.var"#36#37"{DynamicHMC.SamplingLogDensity{Random._GLOBAL_RNG,LogDensityProblems.ForwardDiffLogDensity{LogDensityProblems.TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:parameters, :Ο),Tuple{TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1},TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1}}}},DiffEqBayes.DynamicHMCPosterior{Tsit5,ODEProblem{Array{Float64,1},Tuple{Float64,Float64},true,Array{Float64,1},ODEFunction{true,typeof(SIRD!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},LinearAlgebra.Adjoint{Int64,Array{Int64,1}},Array{Float64,1},Array{Uniform{Float64},1},Array{InverseGamma{Float64},1},Tuple{},Array{Int64,1}}},ForwardDiff.GradientConfig{ForwardDiff.Tag{LogDensityProblems.var"#28#29"{LogDensityProblems.TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:parameters, :Ο),Tuple{TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1},TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1}}}},DiffEqBayes.DynamicHMCPosterior{Tsit5,ODEProblem{Array{Float64,1},Tuple{Float64,Float64},true,Array{Float64,1},ODEFunction{true,typeof(SIRD!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},LinearAlgebra.Adjoint{Int64,Array{Int64,1}},Array{Float64,1},Array{Uniform{Float64},1},Array{InverseGamma{Float64},1},Tuple{},Array{Int64,1}}}},Float64},Float64,4,Array{ForwardDiff.Dual{ForwardDiff.Tag{LogDensityProblems.var"#28#29"{LogDensityProblems.TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:parameters, :Ο),Tuple{TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1},TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1}}}},DiffEqBayes.DynamicHMCPosterior{Tsit5,ODEProblem{Array{Float64,1},Tuple{Float64,Float64},true,Array{Float64,1},ODEFunction{true,typeof(SIRD!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},LinearAlgebra.Adjoint{Int64,Array{Int64,1}},Array{Float64,1},Array{Uniform{Float64},1},Array{InverseGamma{Float64},1},Tuple{},Array{Int64,1}}}},Float64},Float64,4},1}}},DynamicHMC.NUTS{Val{:generalized}},LogProgressReport{Nothing}}}, ::NamedTuple{(:init,),Tuple{Tuple{Tuple{},DynamicHMC.WarmupState{DynamicHMC.EvaluatedLogDensity{Array{Float64,1},Float64},GaussianKineticEnergy{LinearAlgebra.Diagonal{Float64,Array{Float64,1}},LinearAlgebra.Diagonal{Float64,Array{Float64,1}}},Nothing}}}}, ::Tuple{Nothing,InitialStepsizeSearch,TuningNUTS{Nothing,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{Nothing,DualAveraging{Float64}}}) at .\reduce.jl:44
[11] mapfoldl(::Function, ::Function, ::Tuple{Nothing,InitialStepsizeSearch,TuningNUTS{Nothing,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{Nothing,DualAveraging{Float64}}}; kw::Base.Iterators.Pairs{Symbol,Tuple{Tuple{},DynamicHMC.WarmupState{DynamicHMC.EvaluatedLogDensity{Array{Float64,1},Float64},GaussianKineticEnergy{LinearAlgebra.Diagonal{Float64,Array{Float64,1}},LinearAlgebra.Diagonal{Float64,Array{Float64,1}}},Nothing}},Tuple{Symbol},NamedTuple{(:init,),Tuple{Tuple{Tuple{},DynamicHMC.WarmupState{DynamicHMC.EvaluatedLogDensity{Array{Float64,1},Float64},GaussianKineticEnergy{LinearAlgebra.Diagonal{Float64,Array{Float64,1}},LinearAlgebra.Diagonal{Float64,Array{Float64,1}}},Nothing}}}}}) at .\reduce.jl:160
[12] #foldl#205 at .\reduce.jl:178 [inlined]
[13] _warmup at C:\Users\claud\.julia\packages\DynamicHMC\n1LsS\src\mcmc.jl:444 [inlined]
[14] mcmc_keep_warmup(::Random._GLOBAL_RNG, ::LogDensityProblems.ForwardDiffLogDensity{LogDensityProblems.TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:parameters, :Ο),Tuple{TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1},TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1}}}},DiffEqBayes.DynamicHMCPosterior{Tsit5,ODEProblem{Array{Float64,1},Tuple{Float64,Float64},true,Array{Float64,1},ODEFunction{true,typeof(SIRD!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},LinearAlgebra.Adjoint{Int64,Array{Int64,1}},Array{Float64,1},Array{Uniform{Float64},1},Array{InverseGamma{Float64},1},Tuple{},Array{Int64,1}}},ForwardDiff.GradientConfig{ForwardDiff.Tag{LogDensityProblems.var"#28#29"{LogDensityProblems.TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:parameters, :Ο),Tuple{TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1},TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1}}}},DiffEqBayes.DynamicHMCPosterior{Tsit5,ODEProblem{Array{Float64,1},Tuple{Float64,Float64},true,Array{Float64,1},ODEFunction{true,typeof(SIRD!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},LinearAlgebra.Adjoint{Int64,Array{Int64,1}},Array{Float64,1},Array{Uniform{Float64},1},Array{InverseGamma{Float64},1},Tuple{},Array{Int64,1}}}},Float64},Float64,4,Array{ForwardDiff.Dual{ForwardDiff.Tag{LogDensityProblems.var"#28#29"{LogDensityProblems.TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:parameters, :Ο),Tuple{TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1},TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1}}}},DiffEqBayes.DynamicHMCPosterior{Tsit5,ODEProblem{Array{Float64,1},Tuple{Float64,Float64},true,Array{Float64,1},ODEFunction{true,typeof(SIRD!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},LinearAlgebra.Adjoint{Int64,Array{Int64,1}},Array{Float64,1},Array{Uniform{Float64},1},Array{InverseGamma{Float64},1},Tuple{},Array{Int64,1}}}},Float64},Float64,4},1}}}, ::Int64; initialization::NamedTuple{(:q,),Tuple{Array{Float64,1}}}, warmup_stages::Tuple{Nothing,InitialStepsizeSearch,TuningNUTS{Nothing,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{Nothing,DualAveraging{Float64}}}, algorithm::DynamicHMC.NUTS{Val{:generalized}}, reporter::LogProgressReport{Nothing}) at C:\Users\claud\.julia\packages\DynamicHMC\n1LsS\src\mcmc.jl:520
[15] macro expansion at C:\Users\claud\.julia\packages\UnPack\EkESO\src\UnPack.jl:100 [inlined]
[16] mcmc_with_warmup(::Random._GLOBAL_RNG, ::LogDensityProblems.ForwardDiffLogDensity{LogDensityProblems.TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:parameters, :Ο),Tuple{TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1},TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1}}}},DiffEqBayes.DynamicHMCPosterior{Tsit5,ODEProblem{Array{Float64,1},Tuple{Float64,Float64},true,Array{Float64,1},ODEFunction{true,typeof(SIRD!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},LinearAlgebra.Adjoint{Int64,Array{Int64,1}},Array{Float64,1},Array{Uniform{Float64},1},Array{InverseGamma{Float64},1},Tuple{},Array{Int64,1}}},ForwardDiff.GradientConfig{ForwardDiff.Tag{LogDensityProblems.var"#28#29"{LogDensityProblems.TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:parameters, :Ο),Tuple{TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1},TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1}}}},DiffEqBayes.DynamicHMCPosterior{Tsit5,ODEProblem{Array{Float64,1},Tuple{Float64,Float64},true,Array{Float64,1},ODEFunction{true,typeof(SIRD!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},LinearAlgebra.Adjoint{Int64,Array{Int64,1}},Array{Float64,1},Array{Uniform{Float64},1},Array{InverseGamma{Float64},1},Tuple{},Array{Int64,1}}}},Float64},Float64,4,Array{ForwardDiff.Dual{ForwardDiff.Tag{LogDensityProblems.var"#28#29"{LogDensityProblems.TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:parameters, :Ο),Tuple{TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1},TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1}}}},DiffEqBayes.DynamicHMCPosterior{Tsit5,ODEProblem{Array{Float64,1},Tuple{Float64,Float64},true,Array{Float64,1},ODEFunction{true,typeof(SIRD!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},LinearAlgebra.Adjoint{Int64,Array{Int64,1}},Array{Float64,1},Array{Uniform{Float64},1},Array{InverseGamma{Float64},1},Tuple{},Array{Int64,1}}}},Float64},Float64,4},1}}}, ::Int64; initialization::NamedTuple{(:q,),Tuple{Array{Float64,1}}}, warmup_stages::Tuple{Nothing,InitialStepsizeSearch,TuningNUTS{Nothing,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{Nothing,DualAveraging{Float64}}}, algorithm::DynamicHMC.NUTS{Val{:generalized}}, reporter::LogProgressReport{Nothing}) at C:\Users\claud\.julia\packages\DynamicHMC\n1LsS\src\mcmc.jl:578
[17] dynamichmc_inference(::ODEProblem{Array{Float64,1},Tuple{Float64,Float64},true,Array{Float64,1},ODEFunction{true,typeof(SIRD!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}, ::Tsit5, ::Array{Float64,1}, ::LinearAlgebra.Adjoint{Int64,Array{Int64,1}}, ::Array{Uniform{Float64},1}, ::TransformVariables.ArrayTransform{TransformVariables.ShiftedExp{true,Float64},1}; Ο_priors::Array{InverseGamma{Float64},1}, sample_u0::Bool, rng::Random._GLOBAL_RNG, num_samples::Int64, AD_gradient_kind::Val{:ForwardDiff}, save_idxs::Array{Int64,1}, solve_kwargs::Tuple{}, mcmc_kwargs::NamedTuple{(:initialization, :warmup_stages),Tuple{NamedTuple{(:q,),Tuple{Array{Float64,1}}},Tuple{Nothing,InitialStepsizeSearch,TuningNUTS{Nothing,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{LinearAlgebra.Diagonal,DualAveraging{Float64}},TuningNUTS{Nothing,DualAveraging{Float64}}}}}) at C:\Users\claud\.julia\packages\DiffEqBayes\d58SB\src\dynamichmc_inference.jl:105
[18] top-level scope at REPL[119]:1
Increasing num_samples
to 1000 and 10000 does not solve the issue. It also happens with more complex models.
How may I get around this?
Thank you very much
@ClaudMor: I am not an expert on this model family, but are you sure parameters near 1 actually make sense? For Optim.optimize
, you actually constrain to 1
, so I am wondering if you want asπ
instead of asββ
in the transformation.
Also, a quick fix like
q0 = vcat(inverse(variable_transformation, P_optim), fill(0.0, size(data', 1) )) .- 0.001
makes it work.
Cf boundary values are problematic in the Stan manual.
Incidentally, I recently removed local optimization from DynamicHMC.jl (tpapp/DynamicHMC.jl#146). It adds very little to well-behaved posteriors (the adaptation restarts multiple times anyway, so the starting point should not matter much), and often creates problems like this.
Hello,
I'm using DynamicHMC through DiffEqBayes.jl, but I'm experiencing an error I'm not able to overcome. This issue is similar to this and this, and as such it could be related more to DynamicHMC.jl than to DiffEqBayes.jl, so I'll be happy to move it if necessary.
So my goal is to calibrate an epidemiological model using MCMC samplers. Here is a simplified version of the model.
If I try to calibrate the three parameters using
dynamic_inference
, I get the following error:I don't understand why does DynamicHMC use negative starting point, when the trasformations and the priors require that they are positive.
Curiously, if I use DynamicHMC via Turing via DiffEqBayes, it properly works:
I'm interested in using the DynamicHMC interface since it seems to allow for parameter initialization.
Thanks in advance