# Why is this scenario of function parameter estimation acting strangely? Using LsqFit

**URL:** <https://discourse.julialang.org/t/why-is-this-scenario-of-function-parameter-estimation-acting-strangely-using-lsqfit/44778>\
**Category:** General Usage\
**Tags:** package\
**Created:** [August 12, 2020, 4:05am UTC](https://discourse.julialang.org/t/why-is-this-scenario-of-function-parameter-estimation-acting-strangely-using-lsqfit/44778 "2020-08-12T04:05:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [August 12, 2020, 4:05am UTC](https://discourse.julialang.org/t/why-is-this-scenario-of-function-parameter-estimation-acting-strangely-using-lsqfit/44778/1 "2020-08-12T04:05:53Z")

</div>

FYI for the other definitions of model (commented out), I’ve tested them with the same pair of parameters.

```julia
using Plots
using LsqFit

# Define expected curve behaviour
# @. model(t, p) = p[2]*exp(p[1]*t) # works
# @. model(t, p) = exp(p[1]*t)*sin(p[2]*t) # works
@. model(t, p) = p[1]*t*sin(p[2]*t) # doesn't work

# Mock data
t = 0:0.1:10
p₀ = [-1.2, 3.5]
data = model(t, p₀)

# Find parameters
lb = [-2., 2.5]
ub = [-0.1, 4.]
p0_bounds = [-1., 3.]
fit = curve_fit(model, t, data, p0)

# Plot result
scatter(t, data)
plot!(t, model(t, coef(fit)))

@show stderror(fit)

```

 ![CurveFittingAttempt](https://global.discourse-cdn.com/julialang/original/3X/9/c/9cae4a0aa74e6a89e6ceef6d8a552247a44b2ed0.png)

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [August 12, 2020, 9:15am UTC](https://discourse.julialang.org/t/why-is-this-scenario-of-function-parameter-estimation-acting-strangely-using-lsqfit/44778/2 "2020-08-12T09:15:30Z")

</div>

well seems to be related to [this thread](https://discourse.julialang.org/t/julia-vs-gnu-octave-for-plot-fitting-finding-peaks/42237/2). Note that Levenberg Marquardt is not the robustest Method for such kind of problems (multiple local minima).

Optim.jl provides a simple SA-Optimizer which tend to be more robust for this task

```julia
using Optim

loss(u) = sum((data .- model(t,u)).^2)
res = optimize(loss, p0_bounds, SimulatedAnnealing())

# Plot result
scatter(t, data)
plot!(t, model(t, Optim.minimizer(res)))

```

Or you can try the awesome [BlackBoxoptim.jl](https://github.com/robertfeldt/BlackBoxOptim.jl)

EDIT: To elaborate a bit on this:  
By simply plotting the value of `loss(u)` around the `p₀` you can visualize how challenging your problem actually is:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/f/bf304f94094e214cdcf5c733522b6a9f415a6a9f.png)

you can see multiple local minima next to the global one

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [August 13, 2020, 1:24pm UTC](https://discourse.julialang.org/t/why-is-this-scenario-of-function-parameter-estimation-acting-strangely-using-lsqfit/44778/3 "2020-08-13T13:24:24Z")

</div>

Thanks! I gave it a shot and it worked! I’ll know how to investigate tractability in future.

> [@MatFi](#):
>
> Levenberg Marquardt is not the robustest Method for such kind of problems

Thank you for teaching me the name of such a method, didn’t know it had one.

> [@MatFi](#):
>
> By simply plotting the value of `loss(u)` around the `p₀` you can visualize how challenging your problem actually is:

Oh wow, that’s really insightful.
