# Optimize not working with function that has find\_zero

**URL:** https://discourse.julialang.org/t/optimize-not-working-with-function-that-has-find-zero/24043
**Category:** Optimization (Mathematical)
**Tags:** question
**Created:** [May 9, 2019, 6:00pm UTC](https://discourse.julialang.org/t/optimize-not-working-with-function-that-has-find-zero/24043 "2019-05-09T18:00:52Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![jints07](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jints07/32/20586_2.png) [@jints07](https://discourse.julialang.org/u/jints07)
#### Post date: [May 9, 2019, 6:00pm UTC](https://discourse.julialang.org/t/optimize-not-working-with-function-that-has-find-zero/24043/1 "2019-05-09T18:00:52Z")

</div>

I have been using Julia on and off for over a year now. I know the basics but am not an expert by any means. I have had this problem many times in the past - some problem with forwardDiff not working because of floating point numbers. In this case I am using ‘optimize’ to minimize a function. Before I have had the same issue with NLsolve or other actions that use forwardDiff to calculate derivatives. I am pasting my code below so someone can help me on how to fix it

using Parameters,NLsolve,Optim,Roots;  
@with\_kw struct Params  
β = 0.96; #discount factor  
α = 0.33; #capital share  
ψ = 0.05; #default penalty  
δ = 0.1; #depreciation rate  
ϕ = 0.5; #inverse frisch elasticity  
χ = 1.7; #coefficient for disutility of labor  
λ = 0.5; #fraction of firms’ capital seized on default  
λstar = 0.4; #fraction of firm capital recovered by creditors  
ηmin = 0.9; #min idiosyncratic shock  
ηmax = 1.1; #max idiosyncratic shock  
zGdef = 0.97; #productivity cost of default for govt  
hdef = 0.97; #decrease in firm prod after default  
G = 0.08; #constant govt expenditure  
dbar = 0.02; #long run dividend payout target  
ζ = 1; #friction in paying out dividend  
end  
@unpack β,α,ψ,δ,ϕ,χ,λ,λstar,ηmin,ηmax,zGdef,hdef,G,dbar,ζ = Params();

function ηbar(Params,lev,z,K,L::Real)  
@unpack α,λ,δ,hdef = Params();  
f(ηbar,L,K) = ((lev-ηbar_λ_(1-δ))/(ηbar_α_z\*(1-(1-λ)_hdef^(1/α))))^(1/(1-α))_  
((ηmax-ηbar)/(ηmax-ηmin)_(ηbar+ηmax)/2 + (1-λ)_(ηbar-ηmin)/(ηmax-ηmin)_(ηbar+ηmin)/2_hdef^(1/α)) - L/K;  
g(ηbar) = f(ηbar,L,K);  
return find\_zero(g,.95);  
end

function wage(z,K,lev,L,L\_high,L\_low,Params)  
# z controls for govt default decision  
@unpack α,hdef = Params();  
if L\>=L\_high  
w = (1-α)_z_(K/L)^α;  
elseif L\>L\_low  
η = ηbar(Params,lev,z,K,L);  
w = (1-α)_z_((lev-η_λ_(1-δ))/(η_α_z\*(1-(1-λ)_hdef^(1/α))))^(α/(α-1));  
else  
w = (1-α)zhdef_((1-λ)\*K/L)^α;  
end  
return w;  
end

z,K,lev,L\_high,L\_low = 0.852,1.5,.4336,.1435,0.0;  
negtaxrevenue1(L) = (-wage(z,K,lev,L,L\_high,L\_low,Params) + χ\*L^ϕ)\*L; #negative of tax revenue  
result = optimize(x-\>negtaxrevenue1(first(x)), [0.35],Newton(); autodiff = :forward);

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [May 9, 2019, 6:08pm UTC](https://discourse.julialang.org/t/optimize-not-working-with-function-that-has-find-zero/24043/2 "2019-05-09T18:08:23Z")

</div>

Please read [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757), particularly about formatting code so that it is readable.

In addition, say what the error you encountered was, and the behavior that you expected.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [May 9, 2019, 6:09pm UTC](https://discourse.julialang.org/t/optimize-not-working-with-function-that-has-find-zero/24043/3 "2019-05-09T18:09:17Z")

</div>

Could you please edit your post to make it a bit easier to read (and therefore easier to help you)? If you [format your code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530) then it will render properly, and if you can add the actual error message or incorrect result you’re getting, then we’ll be able to more easily figure out what’s wrong and how to fix it.

Edit: sorry for the double-post. I’m just echoing what @odow said.

---

<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: [May 9, 2019, 6:38pm UTC](https://discourse.julialang.org/t/optimize-not-working-with-function-that-has-find-zero/24043/4 "2019-05-09T18:38:45Z")

</div>

Sorry to say it, but there’s a significant chance that you’re going to have to work around quite a few things. You’re going to run into some annoying things with nested forward differentiation that will be hard to work around.

What’s the exact error you get?

---

<div class="post-metadata">

### Author: ![jints07](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jints07/32/20586_2.png) [@jints07](https://discourse.julialang.org/u/jints07)
#### Post date: [May 9, 2019, 6:42pm UTC](https://discourse.julialang.org/t/optimize-not-working-with-function-that-has-find-zero/24043/5 "2019-05-09T18:42:57Z")

</div>

The whole code works fine except for the last line. When I try to minimize the function ‘negtaxrevenue1’, I get the error I just posted in my last post. Can someone tell me how to work around this issue?

---

<div class="post-metadata">

### Author: ![jints07](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jints07/32/20586_2.png) [@jints07](https://discourse.julialang.org/u/jints07)
#### Post date: [May 9, 2019, 6:43pm UTC](https://discourse.julialang.org/t/optimize-not-working-with-function-that-has-find-zero/24043/6 "2019-05-09T18:43:03Z")

</div>

My apologies. I didn’t know how to get the code to look like in Juno. Here it is

```julia
using Parameters,NLsolve,Optim,Roots;
@with_kw struct Params
    β = 0.96; #discount factor
    α = 0.33; #capital share
    ψ = 0.05; #default penalty
    δ = 0.1; #depreciation rate
    ϕ = 0.5; #inverse frisch elasticity
    χ = 1.7; #coefficient for disutility of labor
    λ = 0.5; #fraction of firms' capital seized on default
    λstar = 0.4; #fraction of firm capital recovered by creditors
    ηmin = 0.9; #min idiosyncratic shock
    ηmax = 1.1; #max idiosyncratic shock
    zGdef = 0.97; #productivity cost of default for govt
    hdef = 0.97; #decrease in firm prod after default
    G = 0.08; #constant govt expenditure
    dbar = 0.02; #long run dividend payout target
    ζ = 1; #friction in paying out dividend
end
@unpack β,α,ψ,δ,ϕ,χ,λ,λstar,ηmin,ηmax,zGdef,hdef,G,dbar,ζ = Params();

function ηbar(Params,lev,z,K,L::Real)
    @unpack α,λ,δ,hdef = Params();
    f(ηbar,L,K) = ((lev-ηbar*λ*(1-δ))/(ηbar*α*z*(1-(1-λ)*hdef^(1/α))))^(1/(1-α))*
    ((ηmax-ηbar)/(ηmax-ηmin)*(ηbar+ηmax)/2 + (1-λ)*(ηbar-ηmin)/(ηmax-ηmin)*(ηbar+ηmin)/2*hdef^(1/α)) - L/K;
    g(ηbar) = f(ηbar,L,K);
    return find_zero(g,.95);
end

function wage(z,K,lev,L,L_high,L_low,Params)
    # z controls for govt default decision
    @unpack α,hdef = Params();
    if L>=L_high
        w = (1-α)*z*(K/L)^α;
    elseif L>L_low
        η = ηbar(Params,lev,z,K,L);
        w = (1-α)*z*((lev-η*λ*(1-δ))/(η*α*z*(1-(1-λ)*hdef^(1/α))))^(α/(α-1));
    else
        w = (1-α)*z*hdef*((1-λ)*K/L)^α;
    end
    return w;
end

z,K,lev,L_high,L_low = 0.852,1.5,.4336,.1435,0.0;
negtaxrevenue1(L) = (-wage(z,K,lev,L,L_high,L_low,Params) + χ*L^ϕ)*L; #negative of tax revenue
result = optimize(x->negtaxrevenue1(first(x)), [0.35],Newton(); autodiff = :forward);

```

The error message I am getting is the following

```julia
MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{getfield(Main, Symbol("##34#35")),Float64},Float64,1})
Closest candidates are:
  Float64(::Real, !Matched::RoundingMode) where T<:AbstractFloat at rounding.jl:194
  Float64(::T<:Number) where T<:Number at boot.jl:741
  Float64(!Matched::Int8) at float.jl:60
  ...
in top-level scope at base\none
in at base\none
in at base\none
in #optimize#87 at Optim\Agd3B\src\multivariate\optimize\optimize.jl:33 
in optimize at Optim\Agd3B\src\multivariate\optimize\optimize.jl:57
in update_state! at Optim\Agd3B\src\multivariate\solvers\second_order\newton.jl:79
in perform_linesearch! at Optim\Agd3B\src\utilities\perform_linesearch.jl:40
in HagerZhang at LineSearches\WrsMD\src\hagerzhang.jl:101 
in at LineSearches\WrsMD\src\hagerzhang.jl:140
in at NLSolversBase\KG9Ie\src\interface.jl:69
in value_gradient!! at NLSolversBase\KG9Ie\src\interface.jl:82
in at NLSolversBase\KG9Ie\src\objective_types\twicedifferentiable.jl:131
in gradient! at ForwardDiff\N0wMF\src\gradient.jl:33 
in gradient! at ForwardDiff\N0wMF\src\gradient.jl:35 
in vector_mode_gradient! at ForwardDiff\N0wMF\src\gradient.jl:103
in vector_mode_dual_eval at ForwardDiff\N0wMF\src\apiutils.jl:37 
in #34 at test.jl:45 
in negtaxrevenue1 at untitled:44
in wage at untitled:35
in ηbar at untitled:26
in find_zero at Roots\VcYzu\src\find_zero.jl:556 
in #find_zero#5 at Roots\VcYzu\src\find_zero.jl:556 
in find_zero at Roots\VcYzu\src\derivative_free.jl:120 
in #find_zero#34 at Roots\VcYzu\src\derivative_free.jl:123
in at base\none
in #find_zero#4 at Roots\VcYzu\src\find_zero.jl:541
in find_zero at Roots\VcYzu\src\find_zero.jl:682
in update_state at Roots\VcYzu\src\derivative_free.jl:163
in setproperty! at base\sysimg.jl:19
in convert at base\number.jl:7

```

---

<div class="post-metadata">

### Author: ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)
#### Post date: [May 9, 2019, 6:48pm UTC](https://discourse.julialang.org/t/optimize-not-working-with-function-that-has-find-zero/24043/7 "2019-05-09T18:48:13Z")

</div>

did you test the optimization without using a derivative method?

---

<div class="post-metadata">

### Author: ![jints07](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jints07/32/20586_2.png) [@jints07](https://discourse.julialang.org/u/jints07)
#### Post date: [May 9, 2019, 6:53pm UTC](https://discourse.julialang.org/t/optimize-not-working-with-function-that-has-find-zero/24043/8 "2019-05-09T18:53:33Z")

</div>

No I did not. I am checking how to do it now. I will post the result once I run optimize without derivatives

---

<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: [May 9, 2019, 8:02pm UTC](https://discourse.julialang.org/t/optimize-not-working-with-function-that-has-find-zero/24043/9 "2019-05-09T20:02:25Z")

</div>

Try with `autodiff=:finite` instead of `autodiff=:forward`. It’s not going to be “real” AD though, but it will probably work.

---

<div class="post-metadata">

### Author: ![jints07](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jints07/32/20586_2.png) [@jints07](https://discourse.julialang.org/u/jints07)
#### Post date: [May 9, 2019, 8:14pm UTC](https://discourse.julialang.org/t/optimize-not-working-with-function-that-has-find-zero/24043/10 "2019-05-09T20:14:15Z")

</div>

It worked! Thank you very much. Any idea about which is the fastest way to implement what I have done? I have to run this code \>10^5 times and knowing the fastest way to do it would be very helpful

---

<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: [May 9, 2019, 8:40pm UTC](https://discourse.julialang.org/t/optimize-not-working-with-function-that-has-find-zero/24043/11 "2019-05-09T20:40:02Z")

</div>

I don’t have time right now, but I will try to have a look tomorrow or in the weekend.

It won’t help you right now, but I have a WIP version of Optim that allows for scalars/StaticArray input and that would probably help performance. However, I have other commitments at the moment, so it’s progressing quite slowly.

---

<div class="post-metadata">

### Author: ![jints07](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jints07/32/20586_2.png) [@jints07](https://discourse.julialang.org/u/jints07)
#### Post date: [May 9, 2019, 8:58pm UTC](https://discourse.julialang.org/t/optimize-not-working-with-function-that-has-find-zero/24043/12 "2019-05-09T20:58:36Z")

</div>

Thanks a lot for your help now. If I need help with performance, I’ll ask again here later
