Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update turing_inference #46 #68

Merged
merged 2 commits into from
Feb 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update turing_inference #46
  • Loading branch information
Vaibhavdixit02 committed Feb 9, 2019
commit 61da1eacf660ec4aa455590e02e6cd3a9801ca0c
54 changes: 16 additions & 38 deletions src/turing_inference.jl
Original file line number Diff line number Diff line change
@@ -1,54 +1,32 @@
function turing_inference(prob::DiffEqBase.DEProblem,alg,t,data,priors = nothing;
num_samples=1000, delta=0.65, kwargs...)

function bif(vi, sampler, x=data)
_lp = 0.0
N = length(priors)
_theta = Vector(undef,N)
num_samples=1000, delta=0.65, kwargs...)

N = length(priors)
_theta = Vector{Real}(undef, N)
@model bif(x) = begin
for i in 1:length(priors)
_theta[i], __lp = Turing.assume(sampler,
priors[i],
Turing.VarName(vi, [:bif, Symbol("theta$i")], ""),
vi)
_lp += __lp
_theta[i] ~ priors[i]
end
σ ~ InverseGamma(2, 3)

theta = convert(Array{typeof(first(_theta))},_theta)

σ, __lp = Turing.assume(sampler,
InverseGamma(2, 3),
Turing.VarName(vi, [:bif, :σ], ""),
vi)
_lp += __lp
theta = convert(Array{typeof(first(_theta))}, _theta)
p_tmp = remake(prob, u0 = convert.(eltype(theta), (prob.u0)), p = theta)
sol_tmp = solve(p_tmp, alg; saveat = t, kwargs...)
fill_length = length(t) - length(sol_tmp.u)

p_tmp = remake(prob, u0=convert.(eltype(theta),(prob.u0)),p=theta)
sol_tmp = solve(p_tmp,alg;saveat=t,kwargs...)
fill_length = length(t)-length(sol_tmp.u)
for i in 1:fill_length
if eltype(sol_tmp.u) <: Number
push!(sol_tmp.u,Inf)
push!(sol_tmp.u, Inf)
else
push!(sol_tmp.u,fill(Inf,size(sol_tmp[1])))
push!(sol_tmp.u, fill(Inf, size(sol_tmp[1])))
end
end
for i = 1:length(t)
res = sol_tmp.u[i]
# x[:,i] ~ MvNormal(res, σ*ones(2))
__lp = Turing.observe(
sampler,
MvNormal(res, σ*ones(length(prob.u0))), # Distribution
x[:,i], # Data point
vi
)
_lp += __lp
x[:,i] ~ MvNormal(res, σ*ones(length(prob.u0)))
end

vi.logp = _lp
vi
end

bif() = bif(Turing.VarInfo(), nothing)

chn = sample(bif, Turing.NUTS(num_samples, delta))
end
model = bif(data)
chn = sample(model, Turing.IS(num_samples))
end
17 changes: 7 additions & 10 deletions test/turing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ priors = [Normal(1.5,0.01)]

bayesian_result = turing_inference(prob1,Tsit5(),t,data,priors;num_samples=500)

@show mean(bayesian_result[:theta1][50:end])
@show mean(bayesian_result[:_theta][50:end])

@test mean(bayesian_result[:theta1][50:end]) ≈ 1.5 atol=0.1
@test mean(bayesian_result[:_theta1][50:end])[1] ≈ 1.5 atol=0.1

println("Four parameter case")
f1 = @ode_def begin
Expand All @@ -38,12 +38,9 @@ priors = [Truncated(Normal(1.5,0.01),0,2),Truncated(Normal(1.0,0.01),0,1.5),

bayesian_result = turing_inference(prob1,Tsit5(),t,data,priors;num_samples=500)

@show mean(bayesian_result[:theta1][50:end])
@show mean(bayesian_result[:theta2][50:end])
@show mean(bayesian_result[:theta3][50:end])
@show mean(bayesian_result[:theta4][50:end])
@show mean(bayesian_result[:_theta][50:end])

@test mean(bayesian_result[:theta1][50:end]) ≈ 1.5 atol=3e-1
@test mean(bayesian_result[:theta2][50:end]) ≈ 1.0 atol=3e-1
@test mean(bayesian_result[:theta3][50:end]) ≈ 3.0 atol=3e-1
@test mean(bayesian_result[:theta4][50:end]) ≈ 1.0 atol=3e-1
@test mean(bayesian_result[:_theta][50:end])[1] ≈ 1.5 atol=3e-1
@test mean(bayesian_result[:_theta][50:end])[2] ≈ 1.0 atol=3e-1
@test mean(bayesian_result[:_theta][50:end])[3] ≈ 3.0 atol=3e-1
@test mean(bayesian_result[:_theta][50:end])[4] ≈ 1.0 atol=3e-1