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 #48

Merged
merged 2 commits into from
Jul 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ DiffEqBase 1.7.0
Mamba
Stan
Distributions
Turing
Turing 0.4.3
MacroTools
Optim
RecursiveArrayTools
Expand Down
16 changes: 13 additions & 3 deletions src/turing_inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,41 @@ function turing_inference(prob::DEProblem,alg,t,data,priors = nothing;
num_samples=1000, epsilon = 0.02, tau = 4, kwargs...)

bif(vi, sampler, x=data) = begin
_lp = 0.0
N = length(priors)
_theta = Vector(N)

for i in 1:length(priors)
_theta[i] = Turing.assume(sampler,
_theta[i], __lp = Turing.assume(sampler,
priors[i],
Turing.VarName(vi, [:bif, Symbol("theta$i")], ""),
vi)
_lp += __lp
end

theta = convert(Array{typeof(first(_theta))},_theta)
σ = Turing.assume(sampler,

σ, __lp = Turing.assume(sampler,
InverseGamma(2, 3),
Turing.VarName(vi, [:bif, :σ], ""),
vi)
_lp += __lp

p_tmp = problem_new_parameters(prob, theta); sol_tmp = solve(p_tmp,alg;saveat=t,kwargs...)

for i = 1:length(t)
res = sol_tmp.u[i]
# x[:,i] ~ MvNormal(res, σ*ones(2))
Turing.observe(
__lp = Turing.observe(
sampler,
MvNormal(res, σ*ones(length(prob.u0))), # Distribution
x[:,i], # Data point
vi
)
_lp += __lp
end

vi.logp = _lp
vi
end

Expand Down