Skip to content

Commit

Permalink
ENH: compatability with NLsolve.jl 0.14.0+ (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
sglyon authored Jan 27, 2018
1 parent 72b195d commit ec4a855
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ MacroTools
YAML 0.2.1
DataStructures
AxisArrays
NLsolve
NLsolve 0.14.0
Optim
Requests
QuantEcon 0.10.1
Expand Down
15 changes: 12 additions & 3 deletions src/algos/perfect_foresight.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function perfect_foresight(model, exo::AbstractMatrix{Float64}; T=200, verbose=t

u0 = [s0; x0]

sol_init = NLsolve.nlsolve(u->res_ss(u,m0), u0, inplace=false)
sol_init = NLsolve.nlsolve(u->res_ss(u,m0), u0, inplace=false, autodiff=:central)
if ~NLsolve.converged(sol_init)
error("Couldn't find initial guess.")
end
Expand Down Expand Up @@ -91,7 +91,14 @@ function perfect_foresight(model, exo::AbstractMatrix{Float64}; T=200, verbose=t

sh = size(initial_guess)

fun = u->residuals(model,s0,driving_process,reshape(u, sh...))[:]
function fun(out, in)
copy!(out, residuals(model,s0,driving_process, reshape(in, sh...)))
end
function fun(in)
out = similar(in)
fun(out, in)
out
end

vv0 = initial_guess[:]

Expand All @@ -104,7 +111,9 @@ function perfect_foresight(model, exo::AbstractMatrix{Float64}; T=200, verbose=t
ub = [ss0*0+Inf Dolo.controls_ub(model, mm0, ss0, p0)]

R0 = fun(vv0)
sol = NLsolve.mcpsolve(not_in_place(fun), lb[:], ub[:], vv0, show_trace=verbose)
sol = NLsolve.mcpsolve(
fun, lb[:], ub[:], vv0, show_trace=verbose,
)
end

if ~NLsolve.converged(sol)
Expand Down
17 changes: 7 additions & 10 deletions src/algos/steady_state.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,19 @@ function find_deterministic_equilibrium(model::AModel, calibration::ModelCalibra
ns = length(s0)
nx = length(x0)

function obj!(out, sx)
s = view(sx, 1:ns)
x = view(sx, ns+1:ns+nx)
s_out = view(out, 1:ns)
x_out = view(out, ns+1:ns+nx)
function obj!(sx)
s = sx[1:ns]
x = sx[ns+1:ns+nx]

# update state part of residual
transition!(model, s_out, m, s, x, m, p)
broadcast!(-, s_out, s_out, s)
s_new = transition(model, m, s, x, m, p)

# now update control part
arbitrage!(model, x_out, m, s, x, m, s, x, p)
out
resids = arbitrage(model, m, s, x, m, s, x, p)
vcat(s_new - s, resids)
end

sol = nlsolve(obj!, vcat(s0, x0))
sol = nlsolve(obj!, vcat(s0, x0), autodiff=:central, inplace=false)
NLsolve.converged(sol) || error("Nonlinear solver failed to find steady state")

# otherwise set controls
Expand Down

0 comments on commit ec4a855

Please sign in to comment.