I’m new to the Julia language and to modeling, so I may ask a strange question.
When estimating parameters in Turing.jl, is it possible to conditionally branch for one parameter with another variable?
For example, in the code below, I want to branch the value of the parameter γ depending on the value of var1.
In such a case, I don’t know where and how to write the code.
Please let me know if there is a better way.
I would appreciate your advice.
Thank you for your help.
using Turing, Distributions, DataFrames, DifferentialEquations, DiffEqSensitivity
using MCMCChains, Plots, StatsPlots
using Random
Random.seed!(12);
function lotka_volterra(du,u,p,t)
x, y = u
α, β, δ, γ = p
du[1] = dx = (α - β*y)x
du[2] = dy = (δ*x - γ)y
end
p = [1.5, 1.0, 3.0, 1.0]
u0 = [1.0,1.0]
prob = ODEProblem(lotka_volterra,u0,(0.0,10.0),p)
sol = solve(prob,Tsit5())
plot(sol)
odedata1 = Array(solve(prob,Tsit5(),saveat=0.1))
odedata2 = odedata1 .+ rand()
odedata3 = odedata1 .+ rand()
odedata = zeros(Float64, 2, 101, 3)
odedata[:,:,1] = odedata1
odedata[:,:,2] = odedata2
odedata[:,:,3] = odedata3
# I don’t know where and how to write the following code.
# var1 = [80, 40, 70]
# if var1 > 60
# γ = 4
# else
# γ = 1.5
# end
Turing.setadbackend(:forwarddiff)
@model function fitlv(data)
σ ~ InverseGamma(2, 3)
α ~ truncated(Normal(1.5,0.5),0.5,2.5)
β ~ truncated(Normal(1.2,0.5),0,2)
γ ~ truncated(Normal(3.0,0.5),1,4)
δ ~ truncated(Normal(1.0,0.5),0,2)
p = [α,β,γ,δ]
prob = ODEProblem(lotka_volterra,u0,(0.0,10.0),p)
predicted = solve(prob,Tsit5(),saveat=0.1)
for k in 1:ndims(data)
for i = 1:length(predicted)
data[:,i,k] ~ MvNormal(predicted[i], σ)
end
end
end
model = fitlv(odedata)
chain = sample(model, NUTS(.65),1000)
plot(chain)