Skip to content

Commit

Permalink
Fix bugs in handling of keywords in solve.
Browse files Browse the repository at this point in the history
  • Loading branch information
chenwilliam77 committed Oct 23, 2020
1 parent eb9484a commit 1520599
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
25 changes: 13 additions & 12 deletions src/numerical_algorithms/solve.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""
```
solve!(m; algorithm = :relaxation, autodiff = :central, use_anderson = false, verbose = :high, kwargs...)
solve!(m, z0, y0; algorithm = :relaxation, autodiff = :central, use_anderson = false, verbose = :high, kwargs...)
solve!(m, z0, y0, Ψ0; algorithm = :relaxation, autodiff = :central, use_anderson = false, verbose = :high, kwargs...)
solve!(m; algorithm = :relaxation, autodiff = :central, verbose = :high, kwargs...)
solve!(m, z0, y0; kwargs...)
solve!(m, z0, y0, Ψ0; kwargs...)
```
computes the risk-adjusted linearization of the dynamic economic model
Expand Down Expand Up @@ -31,9 +31,10 @@ The three available `solve!` algorithms are slight variations on each other.
- `S1 <: Real`
### Keywords
- `algorithm::Symbol`: speciifies which numerical algorithm to use. Can be one of `[:relaxation, :homotopy, :deterministic]`.
- `autodiff::Symbol`: specifies whether to use autodiff. This is the keyword is the same as in `nlsolve`.
- `use_anderson::Bool`: specifies whether to use Anderson acceleration if the relaxation algorithm is applied.
- `algorithm::Symbol`: which numerical algorithm to use? Can be one of `[:relaxation, :homotopy, :deterministic]`
- `autodiff::Symbol`: use autodiff or not? This keyword is the same as in `nlsolve`
- `use_anderson::Bool`: use Anderson acceleration if the relaxation algorithm is applied. Defaults to `false`
- `step::Float64`: size of step from 0 to 1 if the homotopy algorithm is applied. Defaults to 0.1
The solution algorithms all use `nlsolve` to calculate the solution to systems of nonlinear
equations. The user can pass in any of the keyword arguments for `nlsolve` to adjust
Expand All @@ -48,18 +49,18 @@ Note these methods are not exported.
"""
function solve!(m::RiskAdjustedLinearization; algorithm::Symbol = :relaxation,
autodiff::Symbol = :central, use_anderson::Bool = false,
verbose::Symbol = :high, kwargs...)
step::Float64 = .1, verbose::Symbol = :high, kwargs...)
if algorithm == :deterministic
solve!(m, m.z, m.y; algorithm = algorithm, autodiff = autodiff, verbose = verbose, kwargs...)
else
solve!(m, m.z, m.y, m.Ψ; algorithm = algorithm, autodiff = autodiff,
use_anderson = use_anderson, verbose = verbose, kwargs...)
use_anderson = use_anderson, step = step, verbose = verbose, kwargs...)
end
end

function solve!(m::RiskAdjustedLinearization, z0::AbstractVector{S1}, y0::AbstractVector{S1};
algorithm::Symbol = :relaxation, autodiff::Symbol = :central,
use_anderson::Bool = false,
use_anderson::Bool = false, step::Float64 = .1,
verbose::Symbol = :high, kwargs...) where {S1 <: Real}

@assert algorithm in [:deterministic, :relaxation, :homotopy]
Expand All @@ -86,7 +87,7 @@ function solve!(m::RiskAdjustedLinearization, z0::AbstractVector{S1}, y0::Abstra
blanchard_kahn(m; deterministic = true, verbose = verbose)
else
solve!(m, m.z, m.y, m.Ψ; algorithm = algorithm,
use_anderson = use_anderson,
use_anderson = use_anderson, step = step,
verbose = verbose, kwargs...)
end

Expand All @@ -95,7 +96,7 @@ end

function solve!(m::RiskAdjustedLinearization, z0::AbstractVector{S1}, y0::AbstractVector{S1}, Ψ0::AbstractMatrix{S1};
algorithm::Symbol = :relaxation, autodiff::Symbol = :central,
use_anderson::Bool = false, verbose::Symbol = :high, kwargs...) where {S1 <: Number}
use_anderson::Bool = false, step::Float64 = .1, verbose::Symbol = :high, kwargs...) where {S1 <: Number}

@assert algorithm in [:relaxation, :homotopy] "The algorithm must be :relaxation or :homotopy because this function calculates the stochastic steady state"

Expand All @@ -105,7 +106,7 @@ function solve!(m::RiskAdjustedLinearization, z0::AbstractVector{S1}, y0::Abstra
relaxation!(m, vcat(z0, y0), Ψ0; autodiff = autodiff,
use_anderson = use_anderson, verbose = verbose, kwargs...)
elseif algorithm == :homotopy
homotopy!(m, vcat(z0, y0, vec(Ψ0)); autodiff = autodiff, verbose = verbose, kwargs...)
homotopy!(m, vcat(z0, y0, vec(Ψ0)); autodiff = autodiff, step = step, verbose = verbose, kwargs...)
end

# Check Blanchard-Kahn
Expand Down
4 changes: 2 additions & 2 deletions test/numerical_algorithms/homotopy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ update!(ral, 1.01 .* z, 1.01 .* y, 1.01 .* Ψ)
# Solve!
@info "The following series of print statements are expected."
RiskAdjustedLinearizations.homotopy!(ral, vcat(ral.z, ral.y, vec(ral.Ψ)); verbose = :low, autodiff = :central,
step = .1, ftol = 1e-8) # first with finite diff NLsolve Jacobian
step = .12, ftol = 1e-8) # first with finite diff NLsolve Jacobian
@test ral.z sssout["z"] atol=1e-6
@test ral.y sssout["y"] atol=1e-4
@test ral.Ψ sssout["Psi"] atol=5e-3

update!(ral, 1.01 .* z, 1.01 .* y, 1.01 .* Ψ) # now autodiff Jacobian
@test_broken RiskAdjustedLinearizations.homotopy!(ral, vcat(ral.z, ral.y, vec(ral.Ψ)); verbose = :low, autodiff = :forward,
step = .1, ftol = 1e-8) # currently can't autodiff b/c problem with chunk size selection
step = .12, ftol = 1e-8) # currently can't autodiff b/c problem with chunk size selection
#=@test ral.z ≈ sssout["z"] atol=1e-6
@test ral.y ≈ sssout["y"] atol=1e-4
@test ral.Ψ ≈ sssout["Psi"] atol=5e-3=#
10 changes: 6 additions & 4 deletions test/numerical_algorithms/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,22 @@ solve!(ral, 1.01 .* z, 1.01 .* y, 1.01 .* Ψ;
@test ral.Ψ sssout["Psi"]

# homotopy w/finite diff Jacobian
solve!(ral, zguess, yguess;
verbose = :high, algorithm = :homotopy, autodiff = :central, ftol = 1e-8) # first w/ calculating the deterministic steady state
solve!(ral, zguess, yguess; algorithm = :homotopy, step = .12,
verbose = :high, autodiff = :central, ftol = 1e-8) # first w/ calculating the deterministic steady state
@test ral.z sssout["z"] # and then proceeding to stochastic steady state
@test ral.y sssout["y"] atol=1e-6
@test ral.Ψ sssout["Psi"]

update!(ral, 1.01 .* z, 1.01 .* y, 1.01 .* Ψ)
solve!(ral; verbose = :none, algorithm = :homotopy, autodiff = :central, ftol = 1e-8) # Now just go straight to solving stochastic steady state
solve!(ral; verbose = :none, algorithm = :homotopy, step = .12,
autodiff = :central, ftol = 1e-8) # Now just go straight to solving stochastic steady state
@test ral.z sssout["z"]
@test ral.y sssout["y"] atol=1e-6
@test ral.Ψ sssout["Psi"]

solve!(ral, 1.01 .* z, 1.01 .* y, 1.01 .* Ψ;
verbose = :none, algorithm = :homotopy, autodiff = :central, ftol = 1e-8) # Now just go straight to solving stochastic steady state
verbose = :none, algorithm = :homotopy, step = .12,
autodiff = :central, ftol = 1e-8) # Now just go straight to solving stochastic steady state
@test ral.z sssout["z"]
@test ral.y sssout["y"] atol=1e-6
@test ral.Ψ sssout["Psi"]
Expand Down

2 comments on commit 1520599

@chenwilliam77
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/23496

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.1 -m "<description of version>" 1520599dac907ab208ba28b73a5df799f7dad9bb
git push origin v0.3.1

Please sign in to comment.