Roots::find_zero fails depending on small differences in initial condition

Hi there,

I’m having trouble with the Roots package. I have an MVE below (base of post) which is setting up a simple economics model.

The last two lines showcase the issue. The first line works and returns the correct value of about 0.9. When I change the initial condition slightly, it fails with the following error message:

ERROR: MethodError: no method matching Roots.UnivariateZeroState(::Float64, ::Float64, ::Int64, ::Float64)
Closest candidates are:
  Roots.UnivariateZeroState(::T, ::T, ::S, ::S) where {T, S} at C:\Users\nvonturk\.julia\packages\Roots\Ez3Om\src\state.jl:3

I’m making the transition over to Julia from Matlab and “fzero” in Matlab doesn’t exhibit this issue. I’m repeating this calculation for different parameter values so simply changing the initial guess would not be a robust approach.

Also, any feedback on how to ask for help is appreciated. First time posting.



# Load libraries

using Parameters, QuantEcon, Roots

# Define utility function

u(c;p) = (1/(1-p.σ)) * c^(1-p.σ) - p.ψ * (n^(1+p.ε)/(1+p.ε))

# Define production function

f(k,n,z;p) = exp(z)*k^(p.α)*n^(1-p.α)

# Define optimal labor choice as a function of k, k prime, z, and a choice for consumption

n_star(c, k, kp, z;p) = ((1-p.α)/p.ψ * exp(z) * k^p.α * c^(-p.σ))^(1/(p.ε + p.α))

# Taking the optimal choice of labor as given, return how much the resource constraint is violated for particular value of c

resource_constraint(c, k, kp, z;p) = c>0 ? (f(k,n_star(c, k, kp, z;p),z;p) - c - kp + (1-p.δ)*k) : -100

# Define model parameters

RBCModel = @with_kw (α = 0.333, δ = 0.025, β = 0.984, σ = 1, ψ = 0.8356, ε = 0.25, z0 = 1, ρ = 0.9, σ_z = 0.05, μ = 0, n_std = 1.2, u = u, f = f, Kn = 500, mc = tauchen(10, ρ, σ_z, μ, n_std), tol = 1e-7)

p = RBCModel()

# Compute steady state capital and consumption

kss = ((1/p.β − 1 + p.δ)/p.α)^(1/(p.α−1))

css = kss^(p.α)-p.δ*kss

# Compute range for potential capital levels

k_grid = range(0.5*kss, 2*kss, p.Kn-1)

kss_id = findlast(k_grid .< kss)

k_grid = [k_grid[1:kss_id]; kss; k_grid[kss_id+1:end]]

# Benchmarking exercise

resource_constraint_anon(c) = resource_constraint(c, k_grid[1], k_grid[37],[1]; p)

# This will work

cs = find_zero(resource_constraint_anon, css/2)

# This will fail

cs = find_zero(resource_constraint_anon, css)

Also, I tried to simply attach the .jl file with the code but as a new poster I’m not allowed to. Any advice on the best way to share code examples is appreciated.

From the error message, it looks like a missing promotion. I’ll have a look.

Very appreciated - thanks for the quick response. Let me know if the code example I sent is not sufficient and I’ll update

The quick fix is to make -100 equal to 100.0 above, as then the function will s always return Floats. This should be automated in the next tagged release.


Fantastic - thank you for the help. I’m still getting used to the way in which Julia handles types. This was very helpful.

In markdown,

You can insert inline code with backticks like this

You can insert inline code with `backticks` like this

For code blocks,

you can use triple-backticks
you can use triple-backticks