Skip to content

Commit

Permalink
Revert changes
Browse files Browse the repository at this point in the history
  • Loading branch information
RJDennis committed Jun 25, 2022
1 parent 659a30c commit 94e7c7d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 63 deletions.
105 changes: 44 additions & 61 deletions src/parser_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function get_variables(model_array::Array{Q,1},term::Q) where {Q<:AbstractString

# Remove any trailing variable separators: "," or ";".

for i in eachindex(term_block)
for i = 1:length(term_block)
if endswith(term_block[i],union(",",";"))
term_block[i] = term_block[i][1:end-1]
end
Expand Down Expand Up @@ -155,7 +155,7 @@ function get_parameters_and_values(model_array::Array{Q,1},term::Q) where {Q<:Ab

# Remove any trailing separators: "," or ";".

for i in eachindex(parameterblock)
for i = 1:length(parameterblock)
if endswith(parameterblock[i],union(",",";"))
parameterblock[i] = parameterblock[i][1:end-1]
end
Expand All @@ -173,7 +173,7 @@ function get_parameters_and_values(model_array::Array{Q,1},term::Q) where {Q<:Ab

parameters = Array{Q}(undef,length(revised_parameterblock))
values = Array{Q}(undef,length(revised_parameterblock))
for i in eachindex(revised_parameterblock)
for i = 1:length(revised_parameterblock)
if occursin("=",revised_parameterblock[i]) == false
parameters[i] = revised_parameterblock[i]
values[i] = "p[$unassigned_parameter_index]" # p is a reserved name
Expand Down Expand Up @@ -210,7 +210,7 @@ function get_equations(model_array::Array{Q,1},term::Q) where {Q<:AbstractString

# Remove any trailing separators: "," or ";".

for i in eachindex(equation_block)
for i = 1:length(equation_block)
if endswith(equation_block[i],union(",",";"))
equation_block[i] = equation_block[i][1:end-1]
end
Expand All @@ -225,7 +225,7 @@ function get_equations(model_array::Array{Q,1},term::Q) where {Q<:AbstractString

# For every model equation...

for i in eachindex(equation_block)
for i = 1:length(equation_block)
if occursin("[",equations[i]) == true # Replace open square bracket with open round parenthesis
equations[i] = replace(equations[i],"[" => "(")
elseif occursin("]",equations[i]) == true # Replace close square bracket with close round parenthesis
Expand Down Expand Up @@ -298,7 +298,7 @@ function reorder_equations_deterministic(equations::Array{Q,1},states::Array{Q,1
states_number = zeros(Int64,length(equations))
jumps_number = zeros(Int64,length(equations))

for i in eachindex(junk_equations)
for i = 1:length(junk_equations)
for j in sorted_combined_names
if occursin(j,junk_equations[i]) == true
if j in states
Expand Down Expand Up @@ -326,10 +326,10 @@ function reorder_equations_deterministic(equations::Array{Q,1},states::Array{Q,1
# Now sort out the order of the states in the system

states_that_have_been_ordered = Int64[]
for k in eachindex(states)
for k = 1:length(states)
for j = 1:number_eqns_with_no_jumps
if states_number[j] == k
for i in eachindex(states)
for i = 1:length(states)
if occursin(reordered_states[i],states_left_in_eqns[j]) == true && j != i && (i in states_that_have_been_ordered) == false
reordered_states[i], reordered_states[j] = reordered_states[j], reordered_states[i]
push!(states_that_have_been_ordered,j)
Expand Down Expand Up @@ -364,7 +364,7 @@ function reorder_equations_stochastic(equations::Array{Q,1},shocks::Array{Q,1},s
states_number = zeros(Int64,length(equations))
jumps_number = zeros(Int64,length(equations))

for i in eachindex(junk_equations)
for i = 1:length(junk_equations)
for j in sorted_combined_names
if occursin(j,junk_equations[i]) == true
if j in states
Expand Down Expand Up @@ -405,10 +405,10 @@ function reorder_equations_stochastic(equations::Array{Q,1},shocks::Array{Q,1},s
# Order the shock processes to try to make shock's variance-covariance matrix have non-zero diagonals.

shocks_that_have_been_ordered = Int64[]
for k in eachindex(shocks)
for k = 1:length(shocks)
for j = 1:number_eqns_with_shocks
if shocks_number[j] == k
for i in eachindex(shocks)
for i = 1:length(shocks)
if occursin(reordered_shocks[i],reordered_equations[j]) == true && j == i && (i in shocks_that_have_been_ordered) == false
push!(shocks_that_have_been_ordered,i)
break
Expand All @@ -426,10 +426,10 @@ function reorder_equations_stochastic(equations::Array{Q,1},shocks::Array{Q,1},s
# Now sort out the order of the states in the system

states_that_have_been_ordered = Int64[]
for k in eachindex(states)
for k = 1:length(states)
for j = 1:number_eqns_with_shocks
if states_number[j] == k
for i in eachindex(states)
for i = 1:length(states)
if occursin(reordered_states[i],states_left_in_eqns[j]) == true && j != i && (i in states_that_have_been_ordered) == false
reordered_states[i], reordered_states[j] = reordered_states[j], reordered_states[i]
push!(states_that_have_been_ordered,j)
Expand Down Expand Up @@ -457,7 +457,7 @@ function deal_with_lags(equations::Array{Q,1},states::Array{Q,1},jumps::Array{Q,
# First we determine if any equation contains a lagged variable.

model_has_lags = false
for i in eachindex(equations)
for i = 1:length(equations)
for j in lag_variables
if occursin(j,equations[i]) == true
model_has_lags = true
Expand All @@ -471,9 +471,9 @@ function deal_with_lags(equations::Array{Q,1},states::Array{Q,1},jumps::Array{Q,
equations. =#

if model_has_lags == true
for j in eachindex(lag_variables)
for j = 1:length(lag_variables)
if sum(occursin.(lag_variables[j],equations)) != 0
for i in eachindex(equations)
for i = 1:length(equations)
reorganized_equations[i] = replace(reorganized_equations[i],lag_variables[j] => string(variables[j],"lag"))
end
reorganized_states = [reorganized_states; string(variables[j],"lag")]
Expand Down Expand Up @@ -519,7 +519,7 @@ function get_re_model_primatives(model_array::Array{Q,1}) where {Q<:AbstractStri
error("Some parameters, variables, or shocks have the same name.")
end

reserved_names = ("deriv", "exp", "log", "x", "p", ":", ";")
reserved_names = ("exp", "log", "x", "p", ":", ";")
for name in reserved_names
if name in combined_names
error("$name cannot be the name for a variable, a shock, or a parameter.")
Expand All @@ -535,7 +535,7 @@ function get_re_model_primatives(model_array::Array{Q,1}) where {Q<:AbstractStri

end

function repackage_equations(model::DSGEModelPrimatives)
function repackage_equations(model::ModelPrimatives)

#= This function is critical for repackaging the model's equations, replacing
parameter names with values, and numbering variables. =#
Expand All @@ -561,7 +561,7 @@ function repackage_equations(model::DSGEModelPrimatives)
#= 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 in eachindex(repackaged_equations)
for i = 1:length(repackaged_equations)
if occursin("exp",equations[i]) == true
repackaged_equations[i] = replace(repackaged_equations[i],"exp" => ":")
elseif occursin("log",equations[i]) == true
Expand All @@ -572,7 +572,7 @@ function repackage_equations(model::DSGEModelPrimatives)
#= Next we go through every parameter expression and replace exp with : and log with ;.
This is to guard them during variables and parameter substitution. =#

for i in eachindex(parametervalues)
for i = 1:length(parametervalues)
if occursin("exp",parametervalues[i]) == true
repackaged_parametervalues[i] = replace(repackaged_parametervalues[i],"exp" => ":")
elseif occursin("log",parametervalues[i]) == true
Expand All @@ -588,7 +588,7 @@ function repackage_equations(model::DSGEModelPrimatives)
count = 0 # counts whether parameter values are still being assigned
for j in sorted_parameters
parameter_index = findfirst(isequal(j),parameters)
for i in eachindex(repackaged_parametervalues)
for i = 1:length(repackaged_parametervalues)
if occursin(j,repackaged_parametervalues[i]) == true
repackaged_parametervalues[i] = replace(repackaged_parametervalues[i],j => string("(",repackaged_parametervalues[parameter_index],")"))
count += 1
Expand All @@ -611,27 +611,26 @@ function repackage_equations(model::DSGEModelPrimatives)
for j in sorted_combined_names
if j in variables
variable_index = findfirst(isequal(j),variables)
for i in eachindex(repackaged_equations)
for i = 1:length(repackaged_equations)
repackaged_equations[i] = replace(repackaged_equations[i],"$j(+1)" => "x[$(length(variables) + variable_index)]")
repackaged_equations[i] = replace(repackaged_equations[i],j => "x[$(variable_index)]")
end
elseif j in parameters
parameter_index = findfirst(isequal(j),parameters)
for i in eachindex(repackaged_equations)
repackaged_equations[i] = replace(repackaged_equations[i],j => string("(",j,")"))
for i = 1:length(repackaged_equations)
repackaged_equations[i] = replace(repackaged_equations[i],j => repackaged_parametervalues[parameter_index])
end
elseif j in shocks # Okay even if there are no shocks
shock_index = findfirst(isequal(j),shocks)
for i in eachindex(repackaged_equations)
for i = 1:length(repackaged_equations)
repackaged_equations[i] = replace(repackaged_equations[i],j => "x[$(2*length(variables) + shock_index)]")
end
end
end

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

for i in eachindex(repackaged_equations)
for i = 1:length(repackaged_equations)
if occursin(":",repackaged_equations[i]) == true
repackaged_equations[i] = replace(repackaged_equations[i],":" => "exp")
end
Expand All @@ -644,7 +643,7 @@ function repackage_equations(model::DSGEModelPrimatives)

end

function create_steady_state_equations(model::DSGEModelPrimatives)
function create_steady_state_equations(model::ModelPrimatives)

# Make the model static by replacing leads and lags with current variables
# and setting shocks equal to zero. Also, replace parameter names with
Expand All @@ -671,7 +670,7 @@ function create_steady_state_equations(model::DSGEModelPrimatives)
#= 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 in eachindex(steady_state_equations)
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
Expand All @@ -682,7 +681,7 @@ function create_steady_state_equations(model::DSGEModelPrimatives)
#= Next we go through every parameter expression and replace exp with : and log with ;.
This is to guard them during variables and parameter substitution. =#

for i in eachindex(parametervalues)
for i = 1:length(parametervalues)
if occursin("exp",parametervalues[i]) == true
steady_state_parametervalues[i] = replace(steady_state_parametervalues[i],"exp" => ":")
elseif occursin("log",parametervalues[i]) == true
Expand All @@ -698,9 +697,10 @@ function create_steady_state_equations(model::DSGEModelPrimatives)
count = 0 # counts whether parameter values are still being assigned
for j in sorted_parameters
parameter_index = findfirst(isequal(j),parameters)
for i in eachindex(steady_state_parametervalues)
for i = 1:length(steady_state_parametervalues)
if occursin(j,steady_state_parametervalues[i]) == true
steady_state_parametervalues[i] = replace(steady_state_parametervalues[i],j => string("(",steady_state_parametervalues[parameter_index],")"))
#steady_state_equations[i] = replace(steady_state_equations[i],j => steady_state_parametervalues[parameter_index])
count += 1
end
end
Expand All @@ -721,28 +721,27 @@ function create_steady_state_equations(model::DSGEModelPrimatives)
for j in sorted_combined_names
if j in variables
variable_index = findfirst(isequal(j),variables)
for i in eachindex(equations)
for i = 1:length(equations)
steady_state_equations[i] = replace(steady_state_equations[i],"$j(-1)" => "x[$(variable_index)]")
steady_state_equations[i] = replace(steady_state_equations[i],"$j(+1)" => "x[$(variable_index)]")
steady_state_equations[i] = replace(steady_state_equations[i],j => "x[$(variable_index)]")
end
elseif j in parameters
parameter_index = findfirst(isequal(j),parameters)
for i in eachindex(equations)
steady_state_equations[i] = replace(steady_state_equations[i],j => string("(",j,")"))
for i = 1:length(equations)
steady_state_equations[i] = replace(steady_state_equations[i],j => steady_state_parametervalues[parameter_index])
end
elseif j in shocks
shock_index = findfirst(isequal(j),shocks)
for i in eachindex(equations)
for i = 1:length(equations)
steady_state_equations[i] = replace(steady_state_equations[i],j => 0.0)
end
end
end

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

for i in eachindex(steady_state_equations)
for i = 1:length(steady_state_equations)
if occursin(":",steady_state_equations[i]) == true
steady_state_equations[i] = replace(steady_state_equations[i],":" => "exp")
end
Expand All @@ -761,7 +760,7 @@ function make_equations_equal_zero(equations::Array{Q,1}) where {Q<:AbstractStri

zeroed_equations = similar(equations)

for i in eachindex(equations)
for i = 1:length(equations)
pair = strip.(split(equations[i],"="))
zeroed_equations[i] = string(pair[1]," - (",pair[2],")")
end
Expand All @@ -770,7 +769,7 @@ function make_equations_equal_zero(equations::Array{Q,1}) where {Q<:AbstractStri

end

function create_projection_equations(equations::Array{Q,1},model::DSGEModelPrimatives) where {Q<:AbstractString}
function create_projection_equations(equations::Array{Q,1},model::ModelPrimatives) where {Q<:AbstractString}

projection_equations = copy(equations)

Expand Down Expand Up @@ -822,7 +821,7 @@ function create_projection_equations(equations::Array{Q,1},model::DSGEModelPrima

end

function create_processed_model_file(model::DSGEModelPrimatives, path::Q) where {Q<:AbstractString}
function create_processed_model_file(model::ModelPrimatives, path::Q) where {Q<:AbstractString}

# Takes the model's primatives and turns these into a processed-model file.
# This file is saved as a text file in the same folder as the model file.
Expand All @@ -844,9 +843,7 @@ function create_processed_model_file(model::DSGEModelPrimatives, path::Q) where
number_variables = length(model.variables)
number_equations = length(model.equations)

#variables = model.variables
#variables = sort(Dict(model.variables[i] => i for i = 1:number_variables), byvalue = :true)
variables = OrderedDict(model.variables[i] => i for i = 1:number_variables)
variables = model.variables

# Build up the string containing the processed model information that gets saved

Expand All @@ -872,7 +869,7 @@ function create_processed_model_file(model::DSGEModelPrimatives, path::Q) where
static_string = "function static_equations(x::Array{T,1}) where {T<:Number} \n \n"
end
static_string = string(static_string, " f = Array{T,1}(undef,length(x)) \n \n")
for i in eachindex(static_equations)
for i = 1:length(static_equations)
nlsolve_static_string = string(nlsolve_static_string, " f[$i] = ", static_equations[i], "\n")
static_string = string(static_string, " f[$i] = ", static_equations[i], "\n")
end
Expand Down Expand Up @@ -935,7 +932,7 @@ function create_processed_model_file(model::DSGEModelPrimatives, path::Q) where
weight_number += 1
end
closure_cheb_string = string(closure_cheb_string, "\n", " #f = Array{T,1}(undef,$number_equations) \n \n")
for i in eachindex(projection_equations)
for i = 1:length(projection_equations)
closure_cheb_string = string(closure_cheb_string, " f[$i] = ", projection_equations[i], "\n")
end
closure_cheb_string = string(closure_cheb_string, "\n #return f \n \n end \n \n return chebyshev_equations \n \n", "end \n")
Expand All @@ -956,7 +953,7 @@ function create_processed_model_file(model::DSGEModelPrimatives, path::Q) where
weight_number += 1
end
closure_smol_string = string(closure_smol_string, "\n", " #f = Array{T,1}(undef,$number_equations) \n \n")
for i in eachindex(projection_equations)
for i = 1:length(projection_equations)
closure_smol_string = string(closure_smol_string, " f[$i] = ", projection_equations[i], "\n")
end
closure_smol_string = string(closure_smol_string, "\n #return f \n \n end \n \n return smolyak_equations \n \n", "end \n")
Expand All @@ -977,7 +974,7 @@ function create_processed_model_file(model::DSGEModelPrimatives, path::Q) where
weight_number += 1
end
closure_hcross_string = string(closure_hcross_string, "\n", " #f = Array{T,1}(undef,$number_equations) \n \n")
for i in eachindex(projection_equations)
for i = 1:length(projection_equations)
closure_hcross_string = string(closure_hcross_string, " f[$i] = ", projection_equations[i], "\n")
end
closure_hcross_string = string(closure_hcross_string, "\n #return f \n \n end \n \n return hcross_equations \n \n", "end \n")
Expand Down Expand Up @@ -1008,7 +1005,7 @@ function create_processed_model_file(model::DSGEModelPrimatives, path::Q) where
end

closure_pl_string = string(closure_pl_string, "\n", " #f = Array{T,1}(undef,$number_equations) \n \n")
for i in eachindex(projection_equations)
for i = 1:length(projection_equations)
closure_pl_string = string(closure_pl_string, " f[$i] = ", projection_equations[i], "\n")
end

Expand Down Expand Up @@ -1075,7 +1072,7 @@ function retrieve_processed_model(path::Q) where {Q<:AbstractString}

end

function assign_parameters(model::REModelPartial,param::Array{T,1}) where {T<:Number}
function assign_parameters(model,param::Array{T,1}) where {T<:Number}

nx = model.number_states
ny = model.number_jumps
Expand Down Expand Up @@ -1112,17 +1109,3 @@ function assign_parameters(model::REModelPartial,param::Array{T,1}) where {T<:Nu
end

end

function assign_parameters(model::REModelPartial,paramdict::Dict{Q,T}) where {T<:Number,Q<:AbstractString}

param = zeros(length(paramdict))

for i in eachindex(model.unassigned_parameters)
param[i] = paramdict[model.unassigned_parameters[i]]
end

newmod = assign_parameters(model,param)

return newmod

end
4 changes: 2 additions & 2 deletions src/structures.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
################ Introduce the model structures ###############

abstract type DSGEModel end
abstract type DSGEModelPrimatives end
abstract type ModelPrimatives end

struct REModelPrimatives{Q<:AbstractString} <: DSGEModelPrimatives
struct REModelPrimatives{Q<:AbstractString} <: ModelPrimatives

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

0 comments on commit 94e7c7d

Please sign in to comment.