# How to hide output from an optimizer?

**URL:** <https://discourse.julialang.org/t/how-to-hide-output-from-an-optimizer/100336>\
**Category:** New to Julia\
**Created:** [June 12, 2023, 12:37am UTC](https://discourse.julialang.org/t/how-to-hide-output-from-an-optimizer/100336 "2023-06-12T00:37:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jorge\_Paredes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jorge_paredes/32/32268_2.png) [@Jorge\_Paredes](https://discourse.julialang.org/u/Jorge_Paredes)\
**Post date:** [June 12, 2023, 12:37am UTC](https://discourse.julialang.org/t/how-to-hide-output-from-an-optimizer/100336/1 "2023-06-12T00:37:22Z")

</div>

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

```julia
* Status: success

 * Candidate solution
    Final objective value: 3.525527e-09

 * Found with
    Algorithm: Nelder-Mead

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

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

```

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [June 12, 2023, 4:43am UTC](https://discourse.julialang.org/t/how-to-hide-output-from-an-optimizer/100336/2 "2023-06-12T04:43:16Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![Jorge\_Paredes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jorge_paredes/32/32268_2.png) [@Jorge\_Paredes](https://discourse.julialang.org/u/Jorge_Paredes)\
**Post date:** [June 13, 2023, 10:19pm UTC](https://discourse.julialang.org/t/how-to-hide-output-from-an-optimizer/100336/3 "2023-06-13T22:19:25Z")

</div>

I am calling it like this

```julia
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:

```julia
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ᵢ-ȳ)²)/n ≤ 1.0e-12

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

```

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [June 14, 2023, 7:47am UTC](https://discourse.julialang.org/t/how-to-hide-output-from-an-optimizer/100336/4 "2023-06-14T07:47:26Z")

</div>

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](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)) 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)

```julia
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
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 🙂

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

---

<div class="post-metadata">

**Author:** ![Jorge\_Paredes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jorge_paredes/32/32268_2.png) [@Jorge\_Paredes](https://discourse.julialang.org/u/Jorge_Paredes)\
**Post date:** [June 15, 2023, 5:36pm UTC](https://discourse.julialang.org/t/how-to-hide-output-from-an-optimizer/100336/5 "2023-06-15T17:36:11Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [June 15, 2023, 6:28pm UTC](https://discourse.julialang.org/t/how-to-hide-output-from-an-optimizer/100336/6 "2023-06-15T18:28:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)\
**Post date:** [July 28, 2023, 7:44pm UTC](https://discourse.julialang.org/t/how-to-hide-output-from-an-optimizer/100336/7 "2023-07-28T19:44:07Z")

</div>

> [@Jorge\_Paredes](#):
>
> Thanks in advance!

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 🙂
