# Optim \`Fminbox()\` -- what is wrong

**URL:** https://discourse.julialang.org/t/optim-fminbox-what-is-wrong/25252
**Category:** Optimization (Mathematical)
**Created:** [June 13, 2019, 2:45pm UTC](https://discourse.julialang.org/t/optim-fminbox-what-is-wrong/25252 "2019-06-13T14:45:43Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [June 13, 2019, 2:45pm UTC](https://discourse.julialang.org/t/optim-fminbox-what-is-wrong/25252/1 "2019-06-13T14:45:43Z")

</div>

I’m trying to use Optim and `Fminbox()` for model fitting, but get an error message – and wonder what I do wrong. Here is what I do:

I have created a loss function `loss(p)` (squared norm of shooting error for ODE). As my initial guess, I have `pvec` given as:

```julia
julia> pvec
1-element Array{Real,1}:
 0.45

```

The loss function at the initial guess is:

```julia
julia> loss(pvec)
2.9500373651496017

```

I can plot the function:

```julia
P = range(pvec[1]*0.1,pvec[1]*50,length=100)
plot(P,loss.(P))

```

leading to:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/7/8788398c3b9babbcb343c94bb74668fb6e1457ce.png)  
I have defined lower and upper bounds:

```julia
p_lo = pvec*0.1;
p_up = pvec*100;

```

`using Optim`, I try to optimize the loss function:

```julia
julia> optimize(loss,p_lo,p_up,pvec,Fminbox())
MethodError: no method matching optimize(::getfield(Main, Symbol("##76#77")), ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Real,1}, ::Fminbox{LBFGS{Nothing,LineSearches.InitialStatic{Float64},LineSearches.HagerZhang{Float64,Base.RefValue{Bool}},getfield(Optim, Symbol("##22#24"))},Float64,getfield(Optim, Symbol("##46#48"))})
Closest candidates are:
  optimize(::Any, ::AbstractArray{T<:AbstractFloat,N} where N, ::AbstractArray{T<:AbstractFloat,N} where N, !Matched::AbstractArray{T<:AbstractFloat,N} where N, ::Fminbox) where T<:AbstractFloat at C:\Users\user_name\.julia\packages\Optim\Agd3B\src\multivariate\solvers\constrained\fminbox.jl:163
  optimize(::Any, ::AbstractArray{T<:AbstractFloat,N} where N, ::AbstractArray{T<:AbstractFloat,N} where N, !Matched::AbstractArray{T<:AbstractFloat,N} where N, ::Fminbox, !Matched::Any; inplace, autodiff) where T<:AbstractFloat at C:\Users\user_name\.julia\packages\Optim\Agd3B\src\multivariate\solvers\constrained\fminbox.jl:163
  optimize(::Any, ::AbstractArray, ::AbstractArray, ::AbstractArray, !Matched::SAMIN) at C:\Users\user_name\.julia\packages\Optim\Agd3B\src\multivariate\solvers\constrained\samin.jl:60
  ...

Stacktrace:
 [1] top-level scope at In[189]:1

```

Question: what is it that I miss?

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [June 13, 2019, 9:18pm UTC](https://discourse.julialang.org/t/optim-fminbox-what-is-wrong/25252/2 "2019-06-13T21:18:01Z")

</div>

AHA!.. I suspect the problem is that `pvec` for some reason is of type `Real` instead of being of type `Float64`.

I constructed `pvec` by stripping out a subset of elements of a named tuple containing a mixture of floats and integers… essentially as follows:

```julia
julia> p = (a=1,b=2.,c=3)
(a = 1, b = 2.0, c = 3)

julia> idx = [1]
1-element Array{Int64,1}:
 1

julia> pvec = collect(p)[idx]
1-element Array{Real,1}:
 1

julia> idx = [2]
1-element Array{Int64,1}:
 2

julia> pvec = collect(p)[idx]
1-element Array{Real,1}:
 2.0

```

Is this intended behavior? I guess the solution around it is to make sure all values of the named tuple are real.

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [June 14, 2019, 7:18am UTC](https://discourse.julialang.org/t/optim-fminbox-what-is-wrong/25252/3 "2019-06-14T07:18:47Z")

</div>

I’m a little puzzled by the result:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/a/faa7ca1963249497e7dce286fbed875ef2892126.png)

The suggested minimizer is approximately p = 15. From the plot, I would have expected a minimizer to be approximately p = 12.

Is the “problem” due to:

- too low tolerances?
- a somewhat unsmooth loss function?
- other things?

I also tested the call `optimize(loss,pvec,LBFGS())`, i.e., without constraints. In that case, the suggested minimizer is p = 21.8.
