Commit 2b14b8ca authored by MichelJuillard's avatar MichelJuillard
Browse files

adding files

parent 6219d424
......@@ -2,6 +2,7 @@ module Dynare
include("model.jl")
export get_abc, get_de
include("symboltable.jl")
include("parser/DynareParser.jl")
export parser, get_jacobian_at_steadystate!
include("parser/DynarePreprocessor.jl")
......@@ -10,7 +11,8 @@ include("steady_state/SteadyState.jl")
export steady_state!
include("Utils.jl")
export get_power_deriv
include("dynare_table.jl")
export @dynare
macro dynare(modfilename, args...)
......
nearbyint(x::Float64) = (abs((x)-floor(x)) < abs((x)-ceil(x)) ? floor(x) : ceil(x))
function get_power_deriv(x::Float64, p::Float64, k::Int64)
if ( abs(x) < 1e-12 && p > 0 && k > p && abs(p-nearbyint(p)) < 1e-12 )
return 0.0
else
dxp = x^(p-k)
for i = 1:k
dxp *= p
p -= 1
end
return dxp
end
end
using PrettyTables
function dynare_table(data, title, column_header, row_header, note;
fmt="%10.4f")
if length(title) > 0
pretty_table([title]; noheader = true, tf = borderless,
highlighters = hl_row(1, crayon"bold"))
end
pretty_table(data,
noheader = true;
tf = borderless,
formatter = ft_printf(fmt, 1:size(data,1)+1),
cell_alignment = Dict( (1, i) => :c
for i = 1 :size(data,1)+1),
highlighters = (hl_row(1, crayon"bold"),
hl_col(1, crayon"bold")),
hlines = [0, 1, length(row_header)],
hlines_format = Tuple('─' for _ = 1:4),
)
if length(note) > 0
pretty_table([note];
noheader = true,
tf = borderless)
end
end
......@@ -3,14 +3,6 @@ using FastLapackInterface.LinSolveAlgo
using JSON
using LinearRationalExpectations
using Perturbation
@enum SymbolType Endogenous Exogenous ExogenousDeterministic Parameter DynareFunction
struct Symbol
longname::String
texname::String
type::SymbolType
orderintype::Integer
end
struct ModelResults
endogenous_steady_state::Vector{Float64}
......@@ -33,7 +25,7 @@ mutable struct Work
end
mutable struct Context
symboltable::Dict{String, Symbol}
symboltable::Dict{String, DynareSymbol}
models::Vector{Model}
options::Dict
results::Results
......@@ -55,7 +47,7 @@ function parser(modfilename)
modelstring = open(f -> read(f, String), modfilename*"/model/json/modfile.json")
modeljson = JSON.parse(modelstring)
symboltable = Dict{String, Dynare.Symbol}()
symboltable = SymbolTable()
endo_nbr = set_symbol_table!(symboltable, modeljson["endogenous"], Endogenous)
exo_nbr = set_symbol_table!(symboltable, modeljson["exogenous"], Exogenous)
exo_det_nbr = set_symbol_table!(symboltable, modeljson["exogenous_deterministic"], ExogenousDeterministic)
......@@ -98,7 +90,6 @@ function parser(modfilename)
elseif field["statementName"] == "check"
check(field)
elseif field["statementName"] == "stoch_simul"
println(field)
stoch_simul!(context, field)
elseif field["statementName"] == "perfect_foresight_setup"
perfect_foresight_setup!(options, field)
......@@ -108,15 +99,16 @@ function parser(modfilename)
error("Unrecognized statement $(field["statementName"])")
end
end
return context
end
function set_symbol_table!(table::Dict{String, Dynare.Symbol},
function set_symbol_table!(table::Dict{String, DynareSymbol},
modelfile,
type::SymbolType)
count = 0
for entry in modelfile
count += 1
symbol = Dynare.Symbol(entry["longName"],
symbol = DynareSymbol(entry["longName"],
entry["texName"],
type,
count)
......@@ -142,7 +134,7 @@ function initialize_parameter!(params, field, symboltable)
end
function native_statement(field)
println("NATIVE: $field")
# println("NATIVE: $field")
end
function initval(field)
......@@ -187,10 +179,30 @@ function set_correlation!(Sigma, correlation, symboltable)
end
end
function display_stoch_simul(x, title, context)
endogenous_names = get_endogenous_longname(context.symboltable)
emptyrow = ["" for _= 1:size(x,1)]
column_header = []
map(x -> push!(column_header, string(x, "\U0209C")), endogenous_names)
row_header = [""]
map(x -> push!(row_header, "ϕ($x)"), endogenous_names[context.models[1].i_bkwrd_b])
map(x -> push!(row_header, "$x\U0209C"), get_exogenous_longname(context.symboltable))
data = hcat(row_header,
vcat(reshape(column_header, 1, length(column_header)),
x))
# Note: ϕ(x) = x_{t-1} - \bar x
note = string("Note: ϕ(x) = x\U0209C\U0208B\U02081 - ", "\U00305", "x")
println("\n")
dynare_table(data, title, column_header, row_header, note)
end
function stoch_simul!(context, field)
context.options["stoch_simul"] = Dict()
copy!(context.options["stoch_simul"], field["options"])
compute_stoch_simul(context)
x = context.results.model_results[1].perturbation.g[1]
vx = view(x, :, 1:size(x, 2) - 1)
display_stoch_simul(vx', "Coefficients of approximate solution function", context)
end
function perfect_foresight_setup(context, field)
......@@ -207,7 +219,7 @@ end
function verbatim(field)
println("VERBATIM: $field")
# println("VERBATIM: $field")
end
function check(field)
......@@ -217,7 +229,6 @@ function compute_stoch_simul(context)
m = context.models[1]
results = context.results.model_results[1]
options = context.options["stoch_simul"]
println(options)
options["cyclic_reduction"] = Dict()
options["generalized_schur"] = Dict()
work = context.work
......@@ -251,7 +262,6 @@ function compute_stoch_simul(context)
options["cyclic_reduction"]["tol"] = 1e-8
end
LinearRationalExpectations.first_order_solver!(results.perturbation, algo, work.jacobian, options, ws)
println(results)
end
function compute_prefect_foresight_setup(context); end
......@@ -272,7 +282,6 @@ end
function get_jacobian_at_steadystate!(work::Work, steadystate, exogenous, m::Model, period::Int64)
lli = m.lead_lag_incidence
get_dynamic_variables!(work.dynamic_variables, steadystate, lli)
println(work.dynamic_variables)
Base.invokelatest(m.dynamic!.dynamic!,
work.temporary_values,
work.residuals,
......
function steady_state!(context)
model = context.models[1]
work = context.work
steadystatemodule = model.steady_state!
if isnothing(steadystatemodule)
else
results = context.results.model_results[1]
fill!(results.exogenous_steady_state, 0.0)
steadystatemodule.steady_state!(results.endogenous_steady_state,
results.exogenous_steady_state,
work.params)
end
end
@enum SymbolType Endogenous Exogenous ExogenousDeterministic Parameter DynareFunction
struct DynareSymbol
longname::String
texname::String
type::SymbolType
orderintype::Integer
end
SymbolTable = Dict{String, DynareSymbol}
for typ in instances(SymbolType)
for f in fieldnames(DynareSymbol)
s = Symbol("get_$(lowercase(string(typ)))_$(f)")
@eval begin
function $s(symboltable::SymbolTable)
symbols = collect(values(symboltable))
subset = filter(s -> s.type == $typ, symbols)
sorted_index = sortperm(subset, by = v -> v.orderintype)
names = [s.$f for s in subset[sorted_index]]
return names
end
end
end
end
# This file is machine-generated - editing it directly is not advised
[[Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
[[Distributed]]
deps = ["Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"
[[InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
[[Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
[[Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
[[Random]]
deps = ["Serialization"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
[[Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
[[Sockets]]
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"
[[Test]]
deps = ["Distributed", "InteractiveUtils", "Logging", "Random"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
[deps]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment