forked from SciML/DiffEqBayes.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabc.jl
65 lines (56 loc) · 2.04 KB
/
abc.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
55
56
57
58
59
60
61
62
63
64
65
using DiffEqBayes, OrdinaryDiffEq, ParameterizedFunctions, Distances, StatsBase, RecursiveArrayTools
using Test
# One parameter case
f1 = @ode_def begin
dx = a*x - x*y
dy = -3y + x*y
end a
u0 = [1.0,1.0]
tspan = (0.0,10.0)
prob1 = ODEProblem(f1,u0,tspan,[1.5])
sol = solve(prob1,Tsit5())
t = collect(range(1,stop=10,length=10))
randomized = VectorOfArray([(sol(t[i]) + .01randn(2)) for i in 1:length(t)])
data = convert(Array,randomized)
priors = [Normal(1.5,0.01)]
bayesian_result = abc_inference(prob1,Tsit5(),t,data,priors;
num_samples=500,ϵ = 0.001)
@test mean(bayesian_result.parameters, weights(bayesian_result.weights)) ≈ 1.5 atol=0.1
# custom distance-function
weights_ = ones(size(data)) # weighted data
for i = 1:3:length(data)
weights_[i] = 0
data[i] = 1e20 # to test that those points are indeed not used
end
distfn = function (d1, d2)
d = 0.0
for i=1:length(d1)
d += (d1[i] - d2[i])^2 * weights_[i]
end
return sqrt(d)
end
bayesian_result = abc_inference(prob1,Tsit5(),t,data,priors;
num_samples=500, ϵ = 0.001,
distancefunction = distfn)
@test mean(bayesian_result.parameters, weights(bayesian_result.weights)) ≈ 1.5 atol=0.1
# Four parameter case
f1 = @ode_def begin
dx = a*x - b*x*y
dy = -c*y + d*x*y
end a b c d
u0 = [1.0,1.0]
tspan = (0.0,10.0)
p = [1.5,1.0,3.0,1.0]
prob1 = ODEProblem(f1,u0,tspan,p)
sol = solve(prob1,Tsit5())
t = collect(range(1,stop=10,length=10))
randomized = VectorOfArray([(sol(t[i]) + .01randn(2)) for i in 1:length(t)])
data = convert(Array,randomized)
priors = [Truncated(Normal(1.5,1),0,2),Truncated(Normal(1.0,1),0,1.5),
Truncated(Normal(3.0,1),0,4),Truncated(Normal(1.0,1),0,2)]
bayesian_result = abc_inference(prob1,Tsit5(),t,data,priors; num_samples=500)
meanvals = mean(bayesian_result.parameters, weights(bayesian_result.weights), 1)
@test meanvals[1] ≈ 1.5 atol=3e-1
@test meanvals[2] ≈ 1.0 atol=3e-1
@test meanvals[3] ≈ 3.0 atol=3e-1
@test meanvals[4] ≈ 1.0 atol=3e-1