-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsde_fitting_example.jl
54 lines (46 loc) · 1.38 KB
/
sde_fitting_example.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using DifferentialEquations
using Plots
using StatsPlots
using DifferentialEquations
using Turing
u0 = [1.0,1.0]
tspan = (0.0,10.0)
function multiplicative_noise!(du,u,p,t)
x,y = u
du[1] = p[5]*x
du[2] = p[6]*y
end
p = [1.5,1.0,3.0,1.0,0.1,0.1]
function lotka_volterra!(du,u,p,t)
α,β,γ,δ = p
du[1] = dx = α*x - β*x*y
du[2] = dy = δ*x*y - γ*y
end
prob_sde = SDEProblem(lotka_volterra!,multiplicative_noise!,u0,tspan,p)
ensembleprob = EnsembleProblem(prob_sde)
@time data = solve(ensembleprob,SOSRI(),saveat=0.1,trajectories=1000)
plot(EnsembleSummary(data))
Turing.setadbackend(:forwarddiff)
@model function fitlv(data, prob)
σ ~ InverseGamma(2,3)
α ~ truncated(Normal(1.3,0.5),0.5,2.5)
β ~ truncated(Normal(1.2,0.25),0.5,2)
γ ~ truncated(Normal(3.2,0.25),2.2,4.0)
δ ~ truncated(Normal(1.2,0.25),0.5,2.0)
ϕ1 ~ truncated(Normal(0.12,0.3),0.05,0.25)
ϕ2 ~ truncated(Normal(0.12,0.3),0.05,0.25)
p = [α,β,γ,δ,ϕ1,ϕ2]
prob = remake(prob, p=p)
predicted = solve(prob,SOSRI(),saveat=0.1)
if predicted.retcode != :Success
Turing.acclogp!(_varinfo, -Inf)
end
for j in 1:length(data)
for i = 1:length(predicted)
data[j][i] ~ MvNormal(predicted[i],σ)
end
end
end
model = fitlv(data, prob_sde)
chain = sample(model, NUTS(0.25), 5000, init_theta = [1.5,1.3,1.2,2.7,1.2,0.12,0.12])
plot(chain)