How to hide output from an optimizer?

Just a question, how do you disappear this part: (I don’t wan’t to show this part in the console)

* Status: success

 * Candidate solution
    Final objective value:     3.525527e-09

 * Found with
    Algorithm:     Nelder-Mead

 * Convergence measures
    √(Σ(yᵢ-ȳ)²)/n ≤ 1.0e-08

 * Work counters
    Seconds run:   0  (vs limit Inf)
    Iterations:    60
    f(x) calls:    117

You can just put a ; behind the solver call, see

julia> using Optim

julia> f(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
f (generic function with 1 method)

julia> x0 = [0.0, 0.0]
2-element Vector{Float64}:
 0.0
 0.0

julia> optimize(f, x0) # prints the summary
 * Status: success

 * Candidate solution
    Final objective value:     3.525527e-09

 * Found with
    Algorithm:     Nelder-Mead

 * Convergence measures
    √(Σ(yᵢ-ȳ)²)/n ≤ 1.0e-08

 * Work counters
    Seconds run:   0  (vs limit Inf)
    Iterations:    60
    f(x) calls:    117

julia> optimize(f, x0); #does not print a summary

I am calling it like this

elapsed_time = @elapsed begin
        res = optimize(SMM, Init, NelderMead(), Optim.Options(iterations = 100_000, g_tol = 1e-12, f_tol = 1e-12, x_tol = 1e-12));
    end
    println("Simulated Method of Moments Estimation", "\n" , "-----------------------------------------")
    println("Distance: " , Optim.minimum(res))
    println("Parameters = [γ_h, γ_c, α_bar, σ_ε]")
    println("True Parameters: ", θ_true)
    println("Initial Parameters: ", Init)
    println("Estimated Parameters: ", Optim.minimizer(res))
    println("Total Execution Time: ", elapsed_time, " seconds")
    return res;

This is the output in the console:

Simulated Method of Moments Estimation
-----------------------------------------
Distance: 0.00018408632036199504
Parameters = [γ_h, γ_c, α_bar, σ_ε]
True Parameters: [16.0, 6.0, 0.4, 0.01]
Initial Parameters: [18.0, 2.0, 0.5, 0.03]
Estimated Parameters: [16.478046426337254, 5.883339808852042, 0.3904303035310908, 0.021272031658422667]
Total Execution Time: 27.2408378 seconds
 * Status: success

 * Candidate solution
    Final objective value:     1.840863e-04

 * Found with
    Algorithm:     Nelder-Mead

 * Convergence measures
    √(Σ(yᵢ-ȳ)²)/n ≤ 1.0e-12

 * Work counters
    Seconds run:   27  (vs limit Inf)
    Iterations:    339
    f(x) calls:    639

Hi,
without your full code (with the return it seems to be part of a function?) I have to guess a bit (see Please read: make it easier to help you) but I think I see what you did.

If you would execute your first line on REPL you would indeed just get the time and not the printed statistics, since that is what @elapsed returns from the code it runs. So (from my code)

using Optim
f(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
elapsed_time = @elapsed begin
	res = optimize(f, [0.0, 0.0]) 
end

just prints the time. res however is the whole result, i.e.

julia> typeof(res)
Optim.MultivariateOptimizationResults{NelderMead{Optim.AffineSimplexer, Optim.AdaptiveParameters}, Vector{Float64}, Float64, Float64, Vector{OptimizationState{Float64, NelderMead{Optim.AffineSimplexer, Optim.AdaptiveParameters}}}, Bool, NamedTuple{(:f_limit_reached, :g_limit_reached, :h_limit_reached, :time_limit, :callback, :f_increased), NTuple{6, Bool}}}

(and not just a number, but a very complicated type). If you print it (or get it as a return value from any function on REPL) there is – however – a nicer display, namely the full statistics (because this type really does not look nice) we already saw above.

If your posted code is within a function (which I just assume due to the return in the last line), then your whole function is run but returns res (the complicated type above) and that on REPL prints the statistics again.

So at the place you call the function (whose inner part I think you posted) put a ; behind that function call
Or change what you return here, do you really need to return all of res? Or is maybe Optim.minimizer(res) enough? That of course depends on your function :slight_smile:

PS: And I just noticed these are your first steps here – so: Welcome to Julia Discourse :wave:

3 Likes

Thank you for all your help! Yes, this is my first step here. I just commented on the return res line. It worked smoothly because I printed all the information I needed.

I don’t know if it’s worth sharing the SMM function here (because it’s very long and very model specific) but it computes the distance between two vectors. (Simulated Method of Moments), and return that distance.

However, that distance (Optim.minimum(res)) is 1.840863e-04, and as you notice in my code I set all the tolerance parameters (I could find) to 1e-12. Is there any way to get a lower distance? It’d be nice if I get it at least to 1e-6 order (I just feel confident with that magic number).

Thanks in advance!

How many moments are you matching? And how many parameters are being minimized over?

1 Like

There are not parameters you can set to guarantee that the objective is of a certain size, but you could try to lower g_tol if you didn’t already. Otherwise, you may want to use another algorithm that uses gradients. However, in your original post the values seems to be 1e-9’ish :slight_smile: