DynareParser.jl 7.92 KB
Newer Older
1
2
using CSV
using DataFrames
MichelJuillard's avatar
MichelJuillard committed
3
#using .DynareContainers
MichelJuillard's avatar
MichelJuillard committed
4
5
using FastLapackInterface
using FastLapackInterface.SchurAlgo
Michel Juillard's avatar
Michel Juillard committed
6
using JSON
7
using KalmanFilterTools
8
using LinearRationalExpectations
9
10
11
12
13
14
15
16
17
18
19
20
using Periods
using TimeDataFrames

context = Context(Dict{String, DynareSymbol}(),
                  Vector{Model}(undef, 0),
                  Dict(),
                  Results(Vector{ModelResults}(undef, 0)),
                  Work(Vector{Float64}(undef, 0),
                       Vector{Float64}(undef, 0),
                       Vector{Float64}(undef, 0),
                       Vector{Float64}(undef, 0),
                       Matrix{Float64}(undef, 0, 0),
21
                       Vector{Int64}(undef, 0),
22
                       Matrix{Float64}(undef, 0, 0),
MichelJuillard's avatar
MichelJuillard committed
23
                       Matrix{Float64}(undef, 0, 0),
24
25
26
27
28
                       false,
                       Matrix{Float64}(undef, 0, 0)
                       )
                  )
                         
29
function parser(modfilename)
Michel Juillard's avatar
Michel Juillard committed
30
31
32
    modelstring = open(f -> read(f, String), modfilename*"/model/json/modfile.json")
    modeljson = JSON.parse(modelstring)

MichelJuillard's avatar
MichelJuillard committed
33
    symboltable = SymbolTable()
34
35
    endo_nbr = set_symbol_table!(symboltable, modeljson["endogenous"], Endogenous)
    exo_nbr = set_symbol_table!(symboltable, modeljson["exogenous"], Exogenous)
MichelJuillard's avatar
MichelJuillard committed
36
37
38
    exo_det_nbr = set_symbol_table!(symboltable,
                                    modeljson["exogenous_deterministic"],
                                    ExogenousDeterministic)
39
    param_nbr = set_symbol_table!(symboltable, modeljson["parameters"], Parameter)
Michel Juillard's avatar
Michel Juillard committed
40
    model_info = get_model_info(modeljson["model_info"])
41
42
43
44
45
46
    model = Model(modfilename,
                  endo_nbr,
                  model_info.lead_lag_incidence,
                  exo_nbr,
                  0,
                  exo_det_nbr,
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
                  param_nbr,
                  model_info.maximum_endo_lag,
                  model_info.maximum_endo_lead,
                  model_info.maximum_exo_lag,
                  model_info.maximum_exo_lead,
                  model_info.maximum_exo_det_lag,
                  model_info.maximum_exo_det_lead,
                  model_info.maximum_lag,
                  model_info.maximum_lead,
                  model_info.orig_maximum_endo_lag,
                  model_info.orig_maximum_endo_lead,
                  model_info.orig_maximum_exo_lag,
                  model_info.orig_maximum_exo_lead,
                  model_info.orig_maximum_exo_det_lag,
                  model_info.orig_maximum_exo_det_lead,
                  model_info.orig_maximum_lag,
                  model_info.orig_maximum_lead
                  )
65
66

    varobs = Vector{String}()
67
    if "varobs" in keys(modeljson)
68
        varobs = vcat(varobs, modeljson["varobs"])
69
70
    end
    if "varexobs" in keys(modeljson)
71
72
73
74
        varobs = vcat(varobs,modeljson["varexobs"])
    end
    if "varexdetobs" in keys(modeljson)
        varobs = vcat(varobs,modeljson["varexdetobs"])
75
76
    end

77
78
    order = 1
    modelresults = ModelResults(Vector{Float64}(undef, endo_nbr),
79
                                Trends(endo_nbr, exo_nbr, exo_det_nbr),
80
                                Matrix{Float64}(undef, endo_nbr, endo_nbr),
81
                                Vector{Bool}(undef, endo_nbr),
82
83
                                Vector{Float64}(undef, exo_nbr),
                                Vector{Float64}(undef, exo_det_nbr),
MichelJuillard's avatar
MichelJuillard committed
84
85
86
                                LinearRationalExpectationsResults(order,
                                                                  endo_nbr,
                                                                  exo_nbr,
87
88
89
                                                                  model.n_states),
                                Vector{Simulation}(undef, 0),
                                Dict{String, Any}())
90
91
    ncol = model.n_bkwrd + model.n_current + model.n_fwrd + 2*model.n_both
    ncol1 = ncol + model.exogenous_nbr
MichelJuillard's avatar
MichelJuillard committed
92
    nrow = model.maximum_exo_lag + model.maximum_exo_lead + 1
93
94
95
96
    work = Work(Vector{Float64}(undef, model.parameter_nbr),
                Vector{Float64}(undef, model.endogenous_nbr),
                Vector{Float64}(undef, sum(model.dynamic!.tmp_nbr[1:2])),
                Vector{Float64}(undef, ncol),
MichelJuillard's avatar
MichelJuillard committed
97
                Matrix{Float64}(undef, nrow, model.exogenous_nbr),
98
                varobs, 
99
                Matrix{Float64}(undef, model.endogenous_nbr, ncol1),
100
101
102
                Matrix{Float64}(undef, model.endogenous_nbr, ncol1),
                false,
                Matrix{Float64}(undef, 0, 0))
103
    results = Results([modelresults])
104
    global context = Context(symboltable, [model], Dict(), results, work)
105
106
107
108
109
110
    parse_statements!(modeljson["statements"])
    return context
end

function parse_statements!(statements)
    for field in statements
111
        if field["statementName"] == "calib_smoother"
112
            calib_smoother!(context, field)
MichelJuillard's avatar
MichelJuillard committed
113
114
        elseif field["statementName"] == "check"
            check!(context, field)
115
116
117
        elseif field["statementName"] == "deterministic_trends"
            deterministic_trends!(context, field)
        elseif field["statementName"] == "histval"
118
            histval!(context, field)
119
        elseif field["statementName"] == "initval"
120
            initval!(context, field)
121
122
123
124
        elseif field["statementName"] == "native"
            expr = Meta.parse(field["string"])
            eval(expr)
        elseif field["statementName"] == "param_init"
125
            param_init!(context, field)
Michel Juillard's avatar
Michel Juillard committed
126
        elseif field["statementName"] == "perfect_foresight_setup"
127
            perfect_foresight_setup!(context, field)
Michel Juillard's avatar
Michel Juillard committed
128
129
        elseif field["statementName"] == "perfect_foresight_solver"
            perfect_foresight_solver!(context, field)
130
131
132
133
        elseif field["statementName"] == "planner_objective"
            planner_objective!(context, field)
        elseif field["statementName"] == "ramsey_model"
            Nothing
134
        elseif field["statementName"] == "shocks"
135
            shocks!(context, field)
136
137
138
        elseif field["statementName"] == "stoch_simul"
            stoch_simul!(context, field)
        elseif field["statementName"] == "verbatim"
MichelJuillard's avatar
MichelJuillard committed
139
            Nothing
Michel Juillard's avatar
Michel Juillard committed
140
        else
141
            error("""Unrecognized statement $(field["statementName"])""")
Michel Juillard's avatar
Michel Juillard committed
142
143
144
145
        end
    end
end

MichelJuillard's avatar
MichelJuillard committed
146
function set_symbol_table!(table::Dict{String, DynareSymbol},
147
148
                           modelfile,
                           type::SymbolType)
Michel Juillard's avatar
Michel Juillard committed
149
150
151
    count = 0
    for entry in modelfile
        count += 1
MichelJuillard's avatar
MichelJuillard committed
152
        symbol = DynareSymbol(entry["longName"],
153
154
155
                              entry["texName"],
                              type,
                              count)
Michel Juillard's avatar
Michel Juillard committed
156
157
158
159
160
161
        table[entry["name"]] = symbol

    end
    return count
end

162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
get_model_info(field) =
    ModelInfo(hcat(field["lead_lag_incidence"]...),
              field["nstatic"],
              field["nfwrd"],
              field["npred"],
              field["nboth"],
              field["nsfwrd"],
              field["nspred"],
              field["ndynamic"],
              field["maximum_endo_lag"],
              field["maximum_endo_lead"],
              field["maximum_exo_lag"],
              field["maximum_exo_lead"],
              field["maximum_exo_det_lag"],
              field["maximum_exo_det_lead"],
              field["maximum_lag"],
              field["maximum_lead"],
              field["orig_maximum_endo_lag"],
              field["orig_maximum_endo_lead"],
              field["orig_maximum_exo_lag"],
              field["orig_maximum_exo_lead"],
              field["orig_maximum_exo_det_lag"],
              field["orig_maximum_exo_det_lead"],
              max(field["orig_maximum_lag"],
                  field["orig_maximum_lag_with_diffs_expanded"]),
              field["orig_maximum_lead"]
              )
                                  
Michel Juillard's avatar
Michel Juillard committed
190
function verbatim(field)
MichelJuillard's avatar
MichelJuillard committed
191
#    println("VERBATIM: $field")
Michel Juillard's avatar
Michel Juillard committed
192
193
end

MichelJuillard's avatar
MichelJuillard committed
194
195
196
197
198
199
function get_smoothed_values(variable_name::String;
                             context=context)
    k = context.symboltable[variable_name].orderintype
    return context.results.model_results[1].smoother["alphah"][k,:]
end