I want to find the maximum of a function with the Optim.jl package, but I keep getting a DomainError. (I think Optim is trying to evaluate the log of a negative number)
using Optim, Interpolations α = 0.4; β = 0.96; k = range(1e-3, 90.0, length = 1001) # grid on k initial_v = 5 * log.(k.^α); #initial guess of value function evaluated at k v_func = CubicSplineInterpolation(k, initial_v, extrapolation_bc = Line()) k_eval = k #Function to maximize (RHS of Bellman equation) RHS(kprime) = -log(k_eval^α - kprime) - β*v_func(kprime) Optim.optimize(RHS, [k_eval]) #Getting a domain error #Adding constraints to this univariate function Optim.optimize(RHS, eps(1.0), 10000.0) #Domain error persists
For some reason, when I try to solve the exact same problem (for the economists out there, it’s the optimal growth model) but change what the maximization is done w.r.t., it’s able to find a solution.
#Trying alternative specification RHS(c) = -log(c) - β*v_func(k_eval^α - c) Optim.optimize(RHS, [k_eval]) #solution successfully found
My two questions are:
- Is there any reason why the first approach ran into the DomainError while the second didn’t?
- Is there a way to avoid the DomainErrors?