Optim constrained optimization issues


Apologies, Julia newcomer. I’m trying to use Optim.jl to fit the parameters of a particular custom noise model by minimizing negative log-likelihood, but I’m running into some issues where methods that exist in previous examples don’t seem to exist anymore.

In particular, I’m trying to use the following code:

function fit_model(data_x, data_y; x0 = [0.3, 0.1, 0.05])
    lower = [0, 0, 0]
    upper = [1, 1, 1]
    result = optimize(x -> session_neg_log_likelihood(x, data_x, data_y), lower, upper, x0, Fminbox(LBFGS()))

However, it’s producing an error:

MethodError: no method matching optimize(::getfield(Main, Symbol("##52#53")){Array{Int64,3},Array{Int64,1}}, ::Array{Int64,1}, ::Array{Int64,1}, ::Array{Float64,1}, ::Fminbox{LBFGS{Nothing,LineSearches.InitialStatic{Float64},LineSearches.HagerZhang{Float64,Base.RefValue{Bool}},getfield(Optim, Symbol("##19#21"))},Float64,getfield(Optim, Symbol("##43#45"))})

I’m not really sure how to go about debugging this, since I (think) I’ve seen examples that look like the above around.

Any help would be appreciated.


Please provide a self-contained example and the version of Optim that you are using.

There is a fair chance that changing to

lower = [0, 0, 0.0]
upper = [1, 1, 1.0]

will solve the problem. (Yes, the bounds should be floats.)

This did it. Thank you so much.

This might be a dumb question but I’ve been having a little difficulty finding documentation regarding things like this (e.g. bounds arguments to optimize should be floats, types of function outputs, etc). In the spirit of learning to fish, where could I have found this information?

I am not sure this is documented.

And not dealing with all kinds of AbstractArrays (as long as they contain numbers, which may or may not be reflected by their element type) is probably something that could be improved in Optim’s interface, so perhaps open an issue.

This is a known issue (arguably an API design issue common in Julia) that I’ve ranted about: Common error: providing an Integer when a Float is expected - #3 by miles.lubin.


I agree with you about preferring an informative error message to a non-informative one, but I think that in general just accepting generic valid inputs (and potentially converting them if necessary) is the best solution.

Agreed. Either providing informative error messages or performing the appropriate conversions are good solutions.

I see, thanks for discussion everyone!

In principle, I would have no issues as a complete Julia-novice if this point was properly documented somewhere. Given that Julia has these strong typing constraints, I find it a little odd that type information is not freely disseminated.

Related question: Is there anywhere I could look to find function signatures? One of the other issues I was having was not being aware how to call the optimize function correctly (e.g., should it go optimize(f, lower, upper, x0, method) or optimize(f, x0, lower, upper, method), etc. I solved this by looking through tutorials, so it’s not a total barrier, but is there anywhere such information is listed directly?