Skip to content

Commit

Permalink
parser improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
RJDennis committed Apr 17, 2022
1 parent 8b16277 commit 3934f03
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SolveDSGE"
uuid = "00bf1f32-23ad-54cc-bf6e-3216db8a43a2"
authors = ["Richard Dennis <[email protected]>"]
version = "0.4.21"
version = "0.4.22"

[deps]
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand Down
50 changes: 33 additions & 17 deletions src/analysis_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2943,6 +2943,10 @@ end

function euler_errors(model::REModel,soln::R,domain::Union{Array{T,2},Array{T,1}},npoints::S,seed::S = 123456) where {S<:Integer,T<:AbstractFloat,R<:PerturbationSolutionDet}

if model.solvers == "Perturbation"
error("The model hase been restricted to perturbation solvers only")
end

nx = length(soln.hbar)
ny = length(soln.gbar)
f = zeros(nx+ny)
Expand All @@ -2966,6 +2970,10 @@ end

function euler_errors(model::REModel,soln::R,domain::Union{Array{T,2},Array{T,1}},npoints::S,seed::S = 123456) where {S<:Integer,T<:AbstractFloat,R<:PerturbationSolutionStoch}

if model.solvers == "Perturbation"
error("The model hase been restricted to perturbation solvers only")
end

nx = length(soln.hbar)
ny = length(soln.gbar)
ns = size(soln.k,2)
Expand All @@ -2992,6 +3000,10 @@ end

function euler_errors(model::REModel,soln::R,npoints::S,seed::S = 123456) where {S<:Integer,R<:ProjectionSolutionDet}

if model.solvers == "Perturbation"
error("The model hase been restricted to perturbation solvers only")
end

nx = size(soln.domain,2)
nv = length(soln.variables)
ny = nv - nx
Expand All @@ -3017,6 +3029,10 @@ end

function euler_errors(model::REModel,soln::R,npoints::S,seed::S = 123456) where {S<:Integer,R<:ProjectionSolutionStoch}

if model.solvers == "Perturbation"
error("The model hase been restricted to perturbation solvers only")
end

nx = size(soln.domain,2)
nv = length(soln.variables)
ny = nv - nx
Expand Down Expand Up @@ -3122,21 +3138,21 @@ function check_taylor_convergence(model::REModel,ss::Array{T,1},z::Array{T,1},de
ddddddd(x) = ForwardDiff.jacobian(dddddd,x,ForwardDiff.JacobianConfig(dddddd,x,ForwardDiff.Chunk{2}()))[:,1:2*nv]

for j = 1:degree
kprod = kron(kprod,(ss - z))
kprod = kron(kprod,(z - ss))
if j == 1
taylor_terms[i,j+1] = d(point)'kprod
taylor_terms[i,j+1] = abs.(d(point))'kprod
elseif j == 2
taylor_terms[i,j+1] = vec(dd(point))'kprod/factorial(j)
taylor_terms[i,j+1] = abs.(vec(dd(point)))'kprod/factorial(j)
elseif j == 3
taylor_terms[i,j+1] = vec(ddd(point))'kprod/factorial(j)
taylor_terms[i,j+1] = abs.(vec(ddd(point)))'kprod/factorial(j)
elseif j == 4
taylor_terms[i,j+1] = vec(dddd(point))'kprod/factorial(j)
taylor_terms[i,j+1] = abs.(vec(dddd(point)))'kprod/factorial(j)
elseif j == 5
taylor_terms[i,j+1] = vec(ddddd(point))'kprod/factorial(j)
taylor_terms[i,j+1] = abs.(vec(ddddd(point)))'kprod/factorial(j)
elseif j ==6
taylor_terms[i,j+1] = vec(dddddd(point))'kprod/factorial(j)
taylor_terms[i,j+1] = abs.(vec(dddddd(point)))'kprod/factorial(j)
else
taylor_terms[i,j+1] = vec(ddddddd(point))'kprod/factorial(j)
taylor_terms[i,j+1] = abs.(vec(ddddddd(point)))'kprod/factorial(j)
end
end
end
Expand Down Expand Up @@ -3183,24 +3199,24 @@ function check_taylor_convergence(model::REModel,ss::Array{T,1},z::Array{T,1},de
ddddddd(x) = ForwardDiff.jacobian(dddddd,x,ForwardDiff.JacobianConfig(dddddd,x,ForwardDiff.Chunk{2}()))[:,1:2*nv]

for j = 1:degree
kprod = kron(kprod,(ss - z))
kprod = kron(kprod,(z - ss))
if j == 1
taylor_terms[i,j+1] = d(point)'kprod
taylor_terms[i,j+1] = abs.(d(point))'kprod
elseif j == 2
taylor_terms[i,j+1] = vec(dd(point))'kprod/factorial(j)
taylor_terms[i,j+1] = abs.(vec(dd(point)))'kprod/factorial(j)
elseif j == 3
taylor_terms[i,j+1] = vec(ddd(point))'kprod/factorial(j)
taylor_terms[i,j+1] = abs.(vec(ddd(point)))'kprod/factorial(j)
elseif j == 4
taylor_terms[i,j+1] = vec(dddd(point))'kprod/factorial(j)
taylor_terms[i,j+1] = abs.(vec(dddd(point)))'kprod/factorial(j)
elseif j == 5
taylor_terms[i,j+1] = vec(ddddd(point))'kprod/factorial(j)
taylor_terms[i,j+1] = abs.(vec(ddddd(point)))'kprod/factorial(j)
elseif j ==6
taylor_terms[i,j+1] = vec(dddddd(point))'kprod/factorial(j)
taylor_terms[i,j+1] = abs.(vec(dddddd(point)))'kprod/factorial(j)
else
taylor_terms[i,j+1] = vec(ddddddd(point))'kprod/factorial(j)
taylor_terms[i,j+1] = abs.(vec(ddddddd(point)))'kprod/factorial(j)
end
end
end
return taylor_terms
end
end
end
21 changes: 21 additions & 0 deletions src/parser_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,17 @@ function create_steady_state_equations(model::ModelPrimatives)

sorted_combined_names = combined_names[sortperm(length.(combined_names),rev = true)]

#= First we go through every equation and replace exp with : and log with ;.
This is to guard them during variables and parameter substitution. =#

for i = 1:length(steady_state_equations)
if occursin("exp",equations[i]) == true
steady_state_equations[i] = replace(steady_state_equations[i],"exp" => ":")
elseif occursin("log",steady_state_equations[i]) == true
steady_state_equations[i] = replace(steady_state_equations[i],"log" => ";")
end
end

# Now we go through every equation and replace future variables, variables, and
# shocks with a numbered element of a vector, "x". We also replace parameter
# names with parameter values
Expand Down Expand Up @@ -690,6 +701,16 @@ function create_steady_state_equations(model::ModelPrimatives)
end
end

#= Finally, go back through every equation and restore exp and log where necessary =#

for i = 1:length(steady_state_equations)
if occursin(":",steady_state_equations[i]) == true
steady_state_equations[i] = replace(steady_state_equations[i],":" => "exp")
elseif occursin(";",steady_state_equations[i]) == true
steady_state_equations[i] = replace(steady_state_equations[i],";" => "log")
end
end

return steady_state_equations

end
Expand Down
4 changes: 2 additions & 2 deletions src/structures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ abstract type ModelPrimatives end

struct REModelPrimatives{Q<:AbstractString} <: ModelPrimatives

# This structure contains the critial model information extracted from a
# This structure contains the crucial model information extracted from a
# model file.

states::Array{Q,1}
Expand Down Expand Up @@ -497,4 +497,4 @@ struct DenHaanMarcetStatistic{T<:Real,S<:Integer}
ten_percent::T
degrees_of_freedom::S

end
end

0 comments on commit 3934f03

Please sign in to comment.