# Parameter estimation of an ODE in Julia is slower than in R

**URL:** <https://discourse.julialang.org/t/parameter-estimation-of-an-ode-in-julia-is-slower-than-in-r/85114>\
**Category:** Performance\
**Tags:** optim, r, differentialequation, parameters\
**Created:** [August 1, 2022, 7:08pm UTC](https://discourse.julialang.org/t/parameter-estimation-of-an-ode-in-julia-is-slower-than-in-r/85114 "2022-08-01T19:08:40Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [August 2, 2022, 10:56pm UTC](https://discourse.julialang.org/t/parameter-estimation-of-an-ode-in-julia-is-slower-than-in-r/85114/21 "2022-08-02T22:56:25Z")

</div>

> [@ChrisRackauckas](#):
>
> ```julia
> reinit!(integ, u0; t0=TSPAN[1], tf=TSPAN[2], erase_sol=false)
> integ.saveiter = 1
> copyto!(integ.sol.u[1], u0)
> 
> ```

~~Could you explain what is happening here? Why is the last line necessary? What does the `save_start=true` option do?~~

> - `save_start`: Denotes whether the initial condition should be included in the solution type as the first timepoint. Defaults to `true`.

The rest I can glean from the source:

> <https://github.com/SciML/OrdinaryDiffEq.jl/blob/master/src/integrators/integrator_interface.jl>

Regarding my earlier solution, I think the minimum change involves:

```julia
function DEsol_l(parms)
    u0 = @view(parms[1:n])
    integrator.p[1] = parms[n+1]
    integrator.p[2] = parms[n+2]
    reinit!(integrator, u0)
    sol = solve!(integrator)
    sol_v = vec(transpose(@view(sol[:,:])))   
    return sol_v
end

```

That seems to still return the correct answer.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 3, 2022, 11:29am UTC](https://discourse.julialang.org/t/parameter-estimation-of-an-ode-in-julia-is-slower-than-in-r/85114/22 "2022-08-03T11:29:46Z")

</div>

Your solution is fine. Mine is just doing some extra tricks to keep the `sol.u` and `sol.t` caches and decrease the memory usage a bit more. It seems that memory pressure isn’t the core left in the compute though, so it doesn’t matter much.

---

<div class="post-metadata">

**Author:** ![laurar1891](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurar1891/32/38443_2.png) [@laurar1891](https://discourse.julialang.org/u/laurar1891)\
**Post date:** [August 5, 2022, 6:37pm UTC](https://discourse.julialang.org/t/parameter-estimation-of-an-ode-in-julia-is-slower-than-in-r/85114/23 "2022-08-05T18:37:06Z")

</div>

Something additional has come up with this example. I’m trying to solve it with other methods (BFGS and Newton) just to check if I get the same values. It is not working with the BFGS method.

If I do this:

```julia
result_BFGS = optimize(b -> loglik1(AGE, HDOM, b), inits1, BFGS(), Optim.Options(iterations=1000000))

```

It gets solved but the minimum value is 1024.27 instead of 35.08.

If I use AD, I get an error related to the dimensions.

```julia
func_once = OnceDifferentiable(b -> loglik1(AGE, HDOM, b), inits1; autodiff=:forward);
result_once = optimize(func_once, inits1, BFGS())

```

I get this warning:

_Warning: First function call produced NaNs. Exiting. Double check that none of the initial conditions, parameters, or timespan values are NaN._

And then:  
_DimensionMismatch(“arrays could not be broadcast to a common size; got a dimension with lengths 50 and 450”)_

Remember 50 was the size of the vector of initial values, and 450 is what we get if we multiply the ages for which this was solved (9) by 50.

I don’t have this problem with Newton.I get the same value as with the Nelder-Mead method:

```julia
func_twice = TwiceDifferentiable(b -> loglik1(AGE, HDOM, b), inits1; autodiff=:forward);
@time result_twice = optimize(func_twice, inits1, Newton())

```

Why can’t I get it to work with BFGS?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 5, 2022, 7:41pm UTC](https://discourse.julialang.org/t/parameter-estimation-of-an-ode-in-julia-is-slower-than-in-r/85114/24 "2022-08-05T19:41:53Z")

</div>

> [@laurar1891](#):
>
> _Warning: First function call produced NaNs. Exiting. Double check that none of the initial conditions, parameters, or timespan values are NaN._

Did you try ForwardDiff NaNSafe mode? I mention in the debugging talk this exact case:

[![](https://global.discourse-cdn.com/julialang/original/3X/5/d/5d8be9be2d861eecc910a9bdda15b5b546dd839f.jpeg "How to debug Julia simulation codes (ODEs, optimization, etc.!)") ](https://www.youtube.com/watch?v=g-iOOhh2U6o&t=1436)

---

<div class="post-metadata">

**Author:** ![laurar1891](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurar1891/32/38443_2.png) [@laurar1891](https://discourse.julialang.org/u/laurar1891)\
**Post date:** [August 5, 2022, 8:06pm UTC](https://discourse.julialang.org/t/parameter-estimation-of-an-ode-in-julia-is-slower-than-in-r/85114/25 "2022-08-05T20:06:57Z")

</div>

Yes. I tried that before posting. I tried with this but it didn’t solve it.

```julia
set_preferences!(ForwardDiff, "nansafe_mode" => true)

```

I also tried different ODE solvers to see if the problem was consistent, and it was.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 7, 2022, 11:15pm UTC](https://discourse.julialang.org/t/parameter-estimation-of-an-ode-in-julia-is-slower-than-in-r/85114/26 "2022-08-07T23:15:04Z")

</div>

Can you isolate it? What parameters cause the NaN? And Why? Most likely it’s parameters that are just resulting in an unstable model and doing something like reducing the initial stepnorm on BFGS will stabilize it.

[Previous page](https://discourse.julialang.org/t/parameter-estimation-of-an-ode-in-julia-is-slower-than-in-r/85114.md?page=1)
