# Parameter Estimation

Parameter estimation for ODE models is provided by the DiffEq suite. The current functionality includes `build_loss_objective`

and `lm_fit`

. Note these require that the problem is defined using a ParameterizedFunction.

## build_loss_objective

`build_loss_objective`

builds an objective function to be used with Optim.jl and MathProgBase-associated solvers like NLopt.

```
function build_loss_objective(prob::DEProblem,alg,loss_func;
mpg_autodiff = false,
verbose_opt = false,
verbose_steps = 100,
prob_generator = problem_new_parameters,
kwargs...)
```

The first argument is the DEProblem to solve, and next is the `alg`

to use. One can also choose `verbose_opt`

and `verbose_steps`

, which, in the optimization routines, will print the steps and the values at the steps every `verbose_steps`

steps. `mpg_autodiff`

uses autodifferentiation to define the derivative for the MathProgBase solver. The extra keyword arguments are passed to the differential equation solver.

### The Loss Function

`loss_func(sol)`

is a function which reduces the problem's solution. While this is very flexible, a two convenience routines is included for fitting to data:

```
L2DistLoss(t,data)
CostVData(t,data;loss_func = L2DistLoss)
```

where `t`

is the set of timepoints which the data is found at, and `data`

which are the values that are known. `L2DistLoss`

is an optimized version of the L2-distance. In `CostVData`

, one can choose any loss function from LossFunctions.jl or use the default of an L2 loss.

### The Problem Generator

The argument `prob_generator`

allows one to specify a the function for generating new problems from a given parameter set. By default, this just builds a new version of `f`

that inserts all of the parameters. For example, for ODEs this is given by the dispatch on `DiffEqBase.problem_new_parameters`

that does the following:

```
function problem_new_parameters(prob::ODEProblem,p)
f = (t,u,du) -> prob.f(t,u,p,du)
uEltype = eltype(p)
u0 = [uEltype(prob.u0[i]) for i in 1:length(prob.u0)]
tspan = (uEltype(prob.tspan[1]),uEltype(prob.tspan[2]))
ODEProblem(f,u0,tspan)
end
```

`f = (t,u,du) -> prob.f(t,u,p,du)`

creates a new version of `f`

that encloses the new parameters. The element types for `u0`

and `tspan`

are set to match the parameters. This is required to make autodifferentiation work. Then the new problem with these new values is returned.

One can use this to change the meaning of the parameters using this function. For example, if one instead wanted to optimize the initial conditions for a function without parameters, you could change this to:

```
my_problem_new_parameters = function (prob::ODEProblem,p)
uEltype = eltype(p)
tspan = (uEltype(prob.tspan[1]),uEltype(prob.tspan[2]))
ODEProblem(prob.f,p,tspan)
end
```

which simply matches the type for time to `p`

(once again, for autodifferentiation) and uses `p`

as the initial condition in the initial value problem.

## build_lsoptim_objective

`build_lsoptim_objective`

builds an objective function to be used with LeastSquaresOptim.jl.

`build_lsoptim_objective(prob,tspan,t,data;prob_generator = problem_new_parameters,kwargs...)`

The arguments are the same as `build_loss_objective`

.

## lm_fit

`lm_fit`

is a function for fitting the parameters of an ODE using the Levenberg-Marquardt algorithm. This algorithm is really bad and thus not recommended since, for example, the Optim.jl algorithms on an L2 loss are more performant and robust. However, this is provided for completeness as most other differential equation libraries use an LM-based algorithm, so this allows one to test the increased effectiveness of not using LM.

`lm_fit(prob::DEProblem,tspan,t,data,p0;prob_generator = problem_new_parameters,kwargs...)`

The arguments are similar to before, but with `p0`

being the initial conditions for the parameters and the `kwargs`

as the args passed to the LsqFit `curve_fit`

function (which is used for the LM solver). This returns the fitted parameters.

## Local Optimization Examples

We choose to optimize the parameters on the Lotka-Volterra equation. We do so by defining the function as a ParmaeterizedFunction:

```
f = @ode_def_nohes LotkaVolterraTest begin
dx = a*x - b*x*y
dy = -c*y + d*x*y
end a=>1.5 b=1.0 c=3.0 d=1.0
u0 = [1.0;1.0]
tspan = (0.0,10.0)
prob = ODEProblem(f,u0,tspan)
```

Notice that since we only used `=>`

for `a`

, it's the only free parameter. We create data using the numerical result with `a=1.5`

:

```
sol = solve(prob,Tsit5())
t = collect(linspace(0,10,200))
randomized = [(sol(t[i]) + .01randn(2)) for i in 1:length(t)]
using RecursiveArrayTools
data = vecarr_to_arr(randomized)
```

Here we used `vecarr_to_arr`

from RecursiveArrayTools.jl to turn the result of an ODE into a matrix.

If we plot the solution with the parameter at `a=1.42`

, we get the following:

Notice that after one period this solution begins to drift very far off: this problem is sensitive to the choice of `a`

.

To build the objective function for Optim.jl, we simply call the `build_loss_objective`

funtion:

`cost_function = build_loss_objective(prob,Tsit5(),L2DistLoss(t,data),maxiters=10000)`

Note that we set `maxiters`

so that way the differential equation solvers would error more quickly when in bad regions of the parameter space, speeding up the process. Now this cost function can be used with Optim.jl in order to get the parameters. For example, we can use Brent's algorithm to search for the best solution on the interval `[0,10]`

by:

```
using Optim
result = optimize(cost_function, 0.0, 10.0)
```

This returns `result.minimizer[1]==1.5`

as the best parameter to match the data. When we plot the fitted equation on the data, we receive the following:

Thus we see that after fitting, the lines match up with the generated data and receive the right parameter value.

We can also use the multivariate optimization functions. For example, we can use the `BFGS`

algorithm to optimize the parameter starting at `a=1.42`

using:

`result = optimize(cost_function, [1.42], BFGS())`

Note that some of the algorithms may be sensitive to the initial condition. For more details on using Optim.jl, see the documentation for Optim.jl.

Lastly, we can use the same tools to estimate multiple parameters simultaneously. Let's use the Lotka-Volterra equation with all parameters free:

```
f2 = @ode_def_nohes LotkaVolterraAll begin
dx = a*x - b*x*y
dy = -c*y + d*x*y
end a=>1.5 b=>1.0 c=>3.0 d=>1.0
u0 = [1.0;1.0]
tspan = (0.0,10.0)
prob = ODEProblem(f2,u0,tspan)
```

To solve it using LeastSquaresOptim.jl, we use the `build_lsoptim_objective`

function:

`cost_function = build_lsoptim_objective(prob,Tsit5(),L2DistLoss(t,data))`

The result is a cost function which can be used with LeastSquaresOptim. For more details, consult the documentation for LeastSquaresOptim.jl:

```
x = [1.3,0.8,2.8,1.2]
res = optimize!(LeastSquaresProblem(x = x, f! = cost_function,
output_length = length(t)*length(prob.u0)),
LeastSquaresOptim.Dogleg(),LeastSquaresOptim.LSMR(),
ftol=1e-14,xtol=1e-15,iterations=100,grtol=1e-14)
```

We can see the results are:

```
println(res.minimizer)
Results of Optimization Algorithm
* Algorithm: Dogleg
* Minimizer: [1.4995074428834114,0.9996531871795851,3.001556360700904,1.0006272074128821]
* Sum of squares at Minimum: 0.035730
* Iterations: 63
* Convergence: true
* |x - x'| < 1.0e-15: true
* |f(x) - f(x')| / |f(x)| < 1.0e-14: false
* |g(x)| < 1.0e-14: false
* Function Calls: 64
* Gradient Calls: 9
* Multiplication Calls: 135
```

and thus this algorithm was able to correctly identify all four parameters.

## More Algorithms (Global Optimization) via MathProgBase Solvers

The `build_loss_objective`

function builds an objective function which is able to be used with MathProgBase-associated solvers. This includes packages like IPOPT, NLopt, MOSEK, etc. Building off of the previous example, we can build a cost function for the single parameter optimization problem like:

```
f = @ode_def_nohes LotkaVolterraTest begin
dx = a*x - b*x*y
dy = -c*y + d*x*y
end a=>1.5 b=1.0 c=3.0 d=1.0
u0 = [1.0;1.0]
tspan = (0.0,10.0)
prob = ODEProblem(f,u0,tspan)
sol = solve(prob,Tsit5())
t = collect(linspace(0,10,200))
randomized = [(sol(t[i]) + .01randn(2)) for i in 1:length(t)]
data = vecarr_to_arr(randomized)
obj = build_loss_objective(prob,Tsit5(),L2DistLoss(t,data),maxiters=10000)
```

We can now use this `obj`

as the objective function with MathProgBase solvers. For our example, we will use NLopt. To use the local derivative-free Constrained Optimization BY Linear Approximations algorithm, we can simply do:

```
using NLopt
opt = Opt(:LN_COBYLA, 1)
min_objective!(opt, obj)
(minf,minx,ret) = NLopt.optimize(opt,[1.3])
```

This finds a minimum at `[1.49997]`

. For a modified evolutionary algorithm, we can use:

```
opt = Opt(:GN_ESCH, 1)
min_objective!(opt, obj.cost_function2)
lower_bounds!(opt,[0.0])
upper_bounds!(opt,[5.0])
xtol_rel!(opt,1e-3)
maxeval!(opt, 100000)
(minf,minx,ret) = NLopt.optimize(opt,[1.3])
```

We can even use things like the Improved Stochastic Ranking Evolution Strategy (and add constraints if needed). This is done via:

```
opt = Opt(:GN_ISRES, 1)
min_objective!(opt, obj.cost_function2)
lower_bounds!(opt,[-1.0])
upper_bounds!(opt,[5.0])
xtol_rel!(opt,1e-3)
maxeval!(opt, 100000)
(minf,minx,ret) = NLopt.optimize(opt,[0.2])
```

which is very robust to the initial condition. The fastest result comes from the following:

```
using NLopt
opt = Opt(:LN_BOBYQA, 1)
min_objective!(opt, obj)
(minf,minx,ret) = NLopt.optimize(opt,[1.3])
```

For more information, see the NLopt documentation for more details. And give IPOPT or MOSEK a try!