Skip to content

Commit 3faddf0

Browse files
Merge pull request #1160 from ParamThakkar123/ParameterEstimation
Updating ParameterEstimation Benchmarks
2 parents 4584973 + 107eab3 commit 3faddf0

File tree

5 files changed

+1559
-714
lines changed

5 files changed

+1559
-714
lines changed

benchmarks/ParameterEstimation/FitzHughNagumoParameterEstimation.jmd

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ author: Vaibhav Dixit, Chris Rackauckas
88
```julia
99
using ParameterizedFunctions, OrdinaryDiffEq, DiffEqParamEstim, Optimization
1010
using OptimizationBBO, OptimizationNLopt, ForwardDiff, Plots, BenchmarkTools
11+
using ModelingToolkit: t_nounits as t, D_nounits as D
1112
gr(fmt=:png)
1213
```
1314

@@ -19,10 +20,24 @@ glo_init = [2.5,2.5,2.5,2.5]
1920
```
2021

2122
```julia
22-
fitz = @ode_def FitzhughNagumo begin
23-
dv = v - v^3/3 -w + l
24-
dw = τinv*(v + a - b*w)
25-
end a b τinv l
23+
@mtkmodel FitzHughNagumo begin
24+
@parameters begin
25+
a = 0.7 # Parameter for excitability
26+
b = 0.8 # Recovery rate parameter
27+
τinv = 0.08 # Inverse of the time constant
28+
l = 0.5 # External stimulus
29+
end
30+
@variables begin
31+
v(t) = 1.0 # Membrane potential with initial condition
32+
w(t) = 1.0 # Recovery variable with initial condition
33+
end
34+
@equations begin
35+
D(v) ~ v - v^3 / 3 - w + l
36+
D(w) ~ τinv * (v + a - b * w)
37+
end
38+
end
39+
40+
@mtkbuild fitz = FitzHughNagumo()
2641
```
2742

2843
```julia
@@ -38,7 +53,7 @@ prob_short = ODEProblem(fitz, r0, tspan2,p)
3853
dt = 30.0/3000
3954
tf = 30.0
4055
tinterval = 0:dt:tf
41-
t = collect(tinterval)
56+
time_points = collect(tinterval)
4257
```
4358

4459
```julia
@@ -54,7 +69,7 @@ t_short = collect(tinterval_short)
5469
#Generate Data
5570
data_sol_short = solve(prob_short,Vern9(),saveat=t_short,reltol=1e-9,abstol=1e-9)
5671
data_short = convert(Array, data_sol_short) # This operation produces column major dataset obs as columns, equations as rows
57-
data_sol = solve(prob,Vern9(),saveat=t,reltol=1e-9,abstol=1e-9)
72+
data_sol = solve(prob,Vern9(),saveat=time_points,reltol=1e-9,abstol=1e-9)
5873
data = convert(Array, data_sol)
5974
```
6075

@@ -177,7 +192,7 @@ Vern9 solver with reltol=1e-9 and abstol=1e-9 is used and the dataset is increas
177192

178193

179194
```julia
180-
obj = build_loss_objective(prob,Vern9(),L2Loss(t,data),Optimization.AutoForwardDiff(),tstops=t,reltol=1e-9,abstol=1e-9)
195+
obj = build_loss_objective(prob,Vern9(),L2Loss(time_points,data),Optimization.AutoForwardDiff(),tstops=time_points,reltol=1e-9,abstol=1e-9)
181196
optprob = OptimizationProblem(obj, glo_init, lb = first.(glo_bounds), ub = last.(glo_bounds))
182197
@btime res1 = solve(optprob, BBO_adaptive_de_rand_1_bin(), maxiters = 4e3)
183198
```

benchmarks/ParameterEstimation/LorenzParameterEstimation.jmd

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ author: finmod, Chris Rackauckas, Vaibhav Dixit
55

66
# Estimate the parameters of the Lorenz system from the dataset
77

8-
9-
108
Note: If data is generated with a fixed time step method and then is tested against with the same time step, there is a biased introduced since it's no longer about hitting the true solution, rather it's just about retrieving the same values that the ODE was first generated by! Thus this version uses adaptive timestepping for all portions so that way tests are against the true solution.
119

1210
```julia
1311
using ParameterizedFunctions, OrdinaryDiffEq, DiffEqParamEstim, Optimization
1412
using OptimizationBBO, OptimizationNLopt, Plots, ForwardDiff, BenchmarkTools
13+
using ModelingToolkit: t_nounits as t, D_nounits as D
1514
gr(fmt=:png)
1615
```
1716

@@ -25,11 +24,25 @@ LocIniPar = [9.0, 20.0, 2.0] # for local optimization
2524
```
2625

2726
```julia
28-
g1 = @ode_def LorenzExample begin
29-
dx = σ*(y-x)
30-
dy = x*(ρ-z) - y
31-
dz = x*y - β*z
32-
end σ ρ β
27+
@mtkmodel LorenzExample begin
28+
@parameters begin
29+
σ = 10.0 # Parameter: Prandtl number
30+
ρ = 28.0 # Parameter: Rayleigh number
31+
β = 8/3 # Parameter: Geometric factor
32+
end
33+
@variables begin
34+
x(t) = 1.0 # Initial condition for x
35+
y(t) = 1.0 # Initial condition for y
36+
z(t) = 1.0 # Initial condition for z
37+
end
38+
@equations begin
39+
D(x) ~ σ * (y - x)
40+
D(y) ~ x * (ρ - z) - y
41+
D(z) ~ x * y - β * z
42+
end
43+
end
44+
45+
@mtkbuild g1 = LorenzExample()
3346
p = [10.0,28.0,2.66] # Parameters used to construct the dataset
3447
r0 = [1.0; 0.0; 0.0] #[-11.8,-5.1,37.5] PODES Initial values of the system in space # [0.1, 0.0, 0.0]
3548
tspan = (0.0, 30.0) # PODES sample of 3000 observations over the (0,30) timespan
@@ -42,7 +55,7 @@ prob_short = ODEProblem(g1, r0, tspan2,p)
4255
dt = 30.0/3000
4356
tf = 30.0
4457
tinterval = 0:dt:tf
45-
t = collect(tinterval)
58+
time_points = collect(tinterval)
4659
```
4760

4861
```julia
@@ -58,7 +71,7 @@ t_short = collect(tinterval_short)
5871
# Generate Data
5972
data_sol_short = solve(prob_short,Vern9(),saveat=t_short,reltol=1e-9,abstol=1e-9)
6073
data_short = convert(Array, data_sol_short) # This operation produces column major dataset obs as columns, equations as rows
61-
data_sol = solve(prob,Vern9(),saveat=t,reltol=1e-9,abstol=1e-9)
74+
data_sol = solve(prob,Vern9(),saveat=time_points,reltol=1e-9,abstol=1e-9)
6275
data = convert(Array, data_sol)
6376
```
6477

@@ -204,7 +217,7 @@ Vern9 solver with reltol=1e-9 and abstol=1e-9 has been established to be accurat
204217

205218
```julia
206219
# BB with Vern9 converges very slowly. The final values are within the NarrowBounds.
207-
obj = build_loss_objective(prob,Vern9(),L2Loss(t,data),tstops=t,reltol=1e-9,abstol=1e-9)
220+
obj = build_loss_objective(prob,Vern9(),L2Loss(time_points,data),tstops=time_points,reltol=1e-9,abstol=1e-9)
208221
optprob = OptimizationProblem(obj, GloIniPar, lb = first.(LooserBounds), ub = last.(LooserBounds))
209222
@btime res1 = solve(optprob, BBO_adaptive_de_rand_1_bin(); maxiters = 4e3) # Default adaptive_de_rand_1_bin_radiuslimited 33 sec [10.2183, 24.6711, 2.28969]
210223
#@btime res1 = bboptimize(obj;SearchRange = LooserBounds, Method = :adaptive_de_rand_1_bin, MaxSteps = 4e3) # Method 32 sec [13.2222, 25.8589, 2.56176]

benchmarks/ParameterEstimation/LotkaVolterraParameterEstimation.jmd

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,37 @@ author: Vaibhav Dixit, Chris Rackauckas
88
```julia
99
using ParameterizedFunctions, OrdinaryDiffEq, DiffEqParamEstim, Optimization, ForwardDiff
1010
using OptimizationBBO, OptimizationNLopt, Plots, RecursiveArrayTools, BenchmarkTools
11+
using ModelingToolkit: t_nounits as t, D_nounits as D
1112
gr(fmt=:png)
1213
```
1314

1415
```julia
1516
loc_bounds = Tuple{Float64, Float64}[(0, 5), (0, 5), (0, 5), (0, 5)]
1617
glo_bounds = Tuple{Float64, Float64}[(0, 10), (0, 10), (0, 10), (0, 10)]
1718
loc_init = [1,0.5,3.5,1.5]
18-
glo_init = [5,5,5,5]
19+
glo_init = [5.0,5.0,5.0,5.0]
1920
```
2021

2122
```julia
22-
f = @ode_def LotkaVolterraTest begin
23-
dx = a*x - b*x*y
24-
dy = -c*y + d*x*y
25-
end a b c d
23+
@mtkmodel LotkaVolterraTest begin
24+
@parameters begin
25+
a = 1.5 # Growth rate of prey
26+
b = 1.0 # Predation rate
27+
c = 3.0 # Death rate of predators
28+
d = 1.0 # Reproduction rate of predators
29+
e = 0.0 # Additional parameter (if needed)
30+
end
31+
@variables begin
32+
x(t) = 1.0 # Population of prey with initial condition
33+
y(t) = 1.0 # Population of predators with initial condition
34+
end
35+
@equations begin
36+
D(x) ~ a * x - b * x * y
37+
D(y) ~ -c * y + d * x * y
38+
end
39+
end
40+
41+
@mtkbuild f = LotkaVolterraTest()
2642
```
2743

2844
```julia
@@ -39,7 +55,7 @@ prob_short = ODEProblem(f, u0, tspan2,p)
3955
dt = 30.0/3000
4056
tf = 30.0
4157
tinterval = 0:dt:tf
42-
t = collect(tinterval)
58+
time_points = collect(tinterval)
4359
```
4460

4561
```julia
@@ -55,7 +71,7 @@ t_short = collect(tinterval_short)
5571
#Generate Data
5672
data_sol_short = solve(prob_short,Tsit5(),saveat=t_short,reltol=1e-9,abstol=1e-9)
5773
data_short = convert(Array, data_sol_short)
58-
data_sol = solve(prob,Tsit5(),saveat=t,reltol=1e-9,abstol=1e-9)
74+
data_sol = solve(prob,Tsit5(),saveat=time_points,reltol=1e-9,abstol=1e-9)
5975
data = convert(Array, data_sol)
6076
```
6177

@@ -181,8 +197,9 @@ opt = Opt(:LD_TNEWTON_PRECOND_RESTART, 4)
181197
Vern9 solver with reltol=1e-9 and abstol=1e-9 is used and the dataset is increased to 3000 observations per variable with the same integration time step of 0.01.
182198

183199
```julia
184-
obj = build_loss_objective(prob,Vern9(),L2Loss(t,data),tstops=t,reltol=1e-9,abstol=1e-9)
185-
optprob = OptimizationProblem(obj, glo_init, lb = first.(glo_bounds), ub = last.(glo_bounds))
200+
t_concrete = collect(0.0:dt:tf)
201+
obj = build_loss_objective(prob,Vern9(),L2Loss(t_concrete,data),tstops=t_concrete,reltol=1e-9,abstol=1e-9)
202+
optprob = OptimizationProblem(obj, glo_init, lb =first.(glo_bounds), ub = last.(glo_bounds))
186203
```
187204

188205
```julia

0 commit comments

Comments
 (0)