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)/2hdef^(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);