Skip to content

Commit

Permalink
write_to_dynare fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
thorek1 committed Sep 30, 2023
1 parent f48df9e commit 5b17d5b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/src/unfinished_docs/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
- [ ] Find any SS by optimising over both SS guesses and parameter inputs
- [ ] weed out SS solver and saved objects

- [x] write parameter equations to dynare (take ordering on board)
- [x] pruning of 3rd order takes pruned 2nd order input
- [x] implement moment matching for pruned models
- [x] test pruning and add literature
Expand Down
4 changes: 2 additions & 2 deletions src/MacroModelling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ Base.show(io::IO, 𝓂::ℳ) = println(io,


function translate_symbol_to_ascii(x::Symbol)
ss = Unicode.normalize(string(x), :NFD)
ss = Unicode.normalize(replace(string(x), "" => "__", "" => "__"), :NFD)

outstr = ""

Expand All @@ -131,7 +131,7 @@ function translate_symbol_to_ascii(x::Symbol)
if out == ""
outstr *= string(i)
else
outstr *= replace(out,
outstr *= replace(out,
r"\^" => s"_",
r"\_\^" => s"_",
r"\+" => s"plus",
Expand Down
32 changes: 24 additions & 8 deletions src/dynare.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ The recommended workflow is to use this function to write a .mod-file, and then
function write_mod_file(m::ℳ)
NSSS = get_SS(m, derivatives = false)

index_in_name = NSSS.keys isa Base.RefValue{Vector{String}}

open(m.model_name * ".mod", "w") do io
println(io, "var ")
[print(io, translate_symbol_to_ascii(v) * " ") for v in setdiff(m.vars_in_ss_equations, m.➕_vars)]
Expand All @@ -149,9 +151,20 @@ function write_mod_file(m::ℳ)
for (i, p) in enumerate(m.parameters)
println(io, "\t" * translate_symbol_to_ascii(p) * "\t=\t" * string(m.parameter_values[i]) * ";")
end

for p in m.calibration_equations_parameters
println(io, "\t" * translate_symbol_to_ascii(p) * "\t=\t" * string(NSSS(index_in_name ? replace(string(p), "" => "{", "" => "}") : p)) * ";")
end

[
println(io, "\t" * translate_symbol_to_ascii(p) * "\t=\t" * string(NSSS(p)) * ";") for
p in m.calibration_equations_parameters
println(io, "\t" * replace(
string(translate_expression_to_ascii(e)),
r"norminv(?=\()" => s"norminvcdf",
r"qnorm(?=\()" => s"norminvcdf",
r"pnorm(?=\()" => s"normcdf",
r"dnorm(?=\()" => s"normpdf",
) * ";") for
e in m.calibration_equations_no_var
]

println(io, "\nmodel;")
Expand All @@ -167,6 +180,10 @@ function write_mod_file(m::ℳ)
r"(\w+)\[(x|ex|exo|exogenous){1}\]" => s"\1",
r"(\w+)\[(x|ex|exo|exogenous){1}(\s*(\-|\+)\s*(\d{1}))\]" =>
s"\1(\4\5)",
r"norminv(?=\()" => s"norminvcdf",
r"qnorm(?=\()" => s"norminvcdf",
r"pnorm(?=\()" => s"normcdf",
r"dnorm(?=\()" => s"normpdf",
) *
";\n",
) for e in m.original_equations
Expand All @@ -176,17 +193,16 @@ function write_mod_file(m::ℳ)
[println(io, "var\t" * translate_symbol_to_ascii(e) * "\t=\t1;") for e in m.exo]

println(io, "end;\n\ninitval;")
[
print(io, "\t" * translate_symbol_to_ascii(v) * "\t=\t" * string(NSSS(v)) * ";\n") for
v in setdiff(m.vars_in_ss_equations, m.➕_vars)
]
for v in setdiff(m.vars_in_ss_equations, m.➕_vars)
print(io, "\t" * translate_symbol_to_ascii(v) * "\t=\t" * string(NSSS(index_in_name ? replace(string(v), "" => "{", "" => "}") : v)) * ";\n")
end

println(io, "end;\n\nstoch_simul(irf=40);")
println(io, "end;\n\nstoch_simul(order = 1, irf = 40);")
end

@info "Created " * m.model_name * ".mod"

@warn "This is an experimental function. Manual adjustments are most likely necessary. Please check before running the model."
# @warn "This is an experimental function. Manual adjustments are most likely necessary. Please check before running the model."
end

"""
Expand Down

0 comments on commit 5b17d5b

Please sign in to comment.