Commit 7ce3974b authored by MichelJuillard's avatar MichelJuillard
Browse files

refactor parser add ramsey model

parent 6ad9c9ed
...@@ -8,8 +8,8 @@ include("dynare_containers.jl") ...@@ -8,8 +8,8 @@ include("dynare_containers.jl")
include("model.jl") include("model.jl")
export get_abc, get_de export get_abc, get_de
include("symboltable.jl") include("symboltable.jl")
include("initialization.jl")
include("deterministic_trends.jl") include("deterministic_trends.jl")
include("first_order/simul_first_order.jl")
include("DynareParser.jl") include("DynareParser.jl")
export parser, get_jacobian_at_steadystate! export parser, get_jacobian_at_steadystate!
include("DynarePreprocessor.jl") include("DynarePreprocessor.jl")
...@@ -20,6 +20,11 @@ include("Utils.jl") ...@@ -20,6 +20,11 @@ include("Utils.jl")
export get_power_deriv export get_power_deriv
include("dynare_table.jl") include("dynare_table.jl")
include("reporting/report.jl") include("reporting/report.jl")
include("graphics.jl")
include("data.jl")
include("filters/kalman/kalman.jl")
include("optimal_policy.jl")
include("perturbations.jl")
export @dynare export @dynare
......
This diff is collapsed.
function deterministic_trends!(context, field)
model = context.models[1]
work = context.work
trend = context.results.model_results[1].trends.endogenous_linear_trend
symboltable = context.symboltable
work.model_has_trend = true
fill!(trend, 0.0)
for (key, value) in field["trends"]
trend[symboltable[key].orderintype] = work.params[symboltable[value].orderintype]
end
end
function remove_linear_trend!(data_out, data_in, steady_state, linear_trend_coeffs; row = 1) function remove_linear_trend!(data_out, data_in, steady_state, linear_trend_coeffs; row = 1)
n = size(data_in, 2) n = size(data_in, 2)
linear_trend = collect(row - 1 .+ (1:n)) linear_trend = collect(row - 1 .+ (1:n))
......
...@@ -257,6 +257,7 @@ mutable struct Work ...@@ -257,6 +257,7 @@ mutable struct Work
temporary_values::Vector{Float64} temporary_values::Vector{Float64}
dynamic_variables::Vector{Float64} dynamic_variables::Vector{Float64}
exogenous_variables::Matrix{Float64} exogenous_variables::Matrix{Float64}
observed_variables::Vector{String}
jacobian::Matrix{Float64} jacobian::Matrix{Float64}
qr_jacobian::Matrix{Float64} qr_jacobian::Matrix{Float64}
model_has_trend::Bool model_has_trend::Bool
......
function calib_smoother!(context, field)
symboltable = context.symboltable
varobs = context.work.observed_variables
varobs_ids = [symboltable[v].orderintype for v in varobs if is_endogenous(v, symboltable)]
model = context.models[1]
options = context.options
results = context.results.model_results[1]
options["calib_smoother"] = Dict()
copy!(options["calib_smoother"], field["options"])
file = get(options["calib_smoother"], "datafile", "")
if (filename = get(options["calib_smoother"], "datafile", "")) != ""
varnames = [v for v in varobs if is_endogenous(v, symboltable)]
Yorig = get_data(filename, varnames, options["calib_smoother"])
else
error("calib_smoother needs a data file or a TimeDataFrame!")
end
Y = Matrix{Union{Float64, Missing}}(undef, size(Yorig))
remove_linear_trend!(Y, Yorig, results.trends.endogenous_steady_state[varobs_ids],
results.trends.endogenous_linear_trend[varobs_ids])
statevar_ids = model.i_bkwrd_b
kalman_statevar_ids = collect(1:model.endogenous_nbr)
ns = length(kalman_statevar_ids)
np = model.exogenous_nbr
ny, nobs = size(Y)
c = zeros(ny)
k1 = findall(in(varobs_ids), kalman_statevar_ids)
k2 = findall(in(statevar_ids), kalman_statevar_ids)
Z = zeros(ny, ns)
for i in 1:ny
Z[i, varobs_ids[i]] = 1.0
end
H = zeros(ny, ny)
d = zeros(ns)
T = zeros(ns, ns)
vg1 = view(context.results.model_results[1].linearrationalexpectations.g1_1, kalman_statevar_ids, :)
T[:, k2] .= vg1
R = zeros(ns, np)
vg2 = view(context.results.model_results[1].linearrationalexpectations.g1_2, kalman_statevar_ids, :)
R .= vg2
Q = model.Sigma_e
a0 = zeros(ns, nobs + 1)
alphah = zeros(ns, nobs)
att = zeros(ns, nobs)
epsilonh = zeros(ny, nobs)
etah = zeros(np, nobs)
P = zeros(ns, ns, nobs + 1)
Ptt = zeros(ns, ns, nobs + 1)
vv = view(context.results.model_results[1].endogenous_variance, kalman_statevar_ids, kalman_statevar_ids)
P[:, :, 1] .= vv
Valpha = zeros(ns, ns, nobs)
Vepsilon = zeros(ny, ny, nobs)
Veta = zeros(np, np, nobs)
presample = 0
data_pattern = Vector{Vector{Int64}}(undef, 0)
for i = 1:nobs
push!(data_pattern, findall(.!ismissing.(Y[:, i])))
end
if count(results.stationary_variables) == model.endogenous_nbr
kws = KalmanSmootherWs{Float64, Int64}(ny, ns, model.exogenous_nbr, nobs)
kalman_smoother!(Y, c, Z, H, d, T, R, Q, a0, att, P, Ptt, alphah, epsilonh, etah,
Valpha, Vepsilon, Veta, start, last, presample,
kws, data_pattern)
else
dgees_ws = DgeesWs(ns)
dgees!(dgees_ws, T, >, 1-1e-6)
td = transpose(dgees_ws.vs)*d
tR = transpose(dgees_ws.vs)*R
tZ = Z*dgees_ws.vs
P = zeros(ns, ns, nobs + 1)
k = count(abs.(dgees_ws.eigen_values) .> 1-1e-6)
vT = view(T, (k + 1):ns, (k + 1):ns)
vP = view(P, (k + 1):ns, (k + 1):ns, 1)
vtR = view(tR, (k + 1):ns, :)
k1 = ns - k
lyapd_ws = LyapdWs(k1)
LinearRationalExpectations.extended_lyapd_core!(vP, vT, vtR*Q*transpose(vtR), lyapd_ws)
Pinf = zeros(ns, ns, nobs + 1)
for i = 1:k
Pinf[i, i, 1] = 1.0
end
Pinftt = zeros(ns, ns, nobs + 1)
kws = DiffuseKalmanSmootherWs{Float64, Int64}(ny, ns, model.exogenous_nbr, nobs)
start = 1
last = nobs
diffuse_kalman_smoother!(Y, c, tZ, H, td, T, tR, Q, a0, att,
Pinf, Pinftt, P, Ptt, alphah,
epsilonh, etah, Valpha, Vepsilon,
Veta, start, last, presample, 1e-8,
kws, data_pattern)
alphah = dgees_ws.vs*alphah
end
results.smoother["alphah"] = Matrix{Float64}(undef, ns, nobs)
add_linear_trend!(results.smoother["alphah"], alphah,
results.trends.endogenous_steady_state,
results.trends.endogenous_linear_trend)
end
function make_state_space!(A, B, Σy, g1_1, g1_2, context, ws) function make_state_space!(A, B, Σy, g1_1, g1_2, context, ws)
vA = view(A, :, context.models[1].i_bckwrd_b] vA = view(A, :, context.models[1].i_bckwrd_b)
vA .= g1_1 vA .= g1_1
B = g1_2 B = g1_2
extended_lyapd!(Σy, A, B, ws.lyapd_ws) extended_lyapd!(Σy, A, B, ws.lyapd_ws)
......
using LinearAlgebra
function simul_first_order!(results, initial_values, x, c, A, B, periods)
r_1 = view(results, 1, :)
r_1 .= initial_values .- c
for t = 2:periods + 1
r = view(results, t, :)
e = view(x, t, :)
mul!(r, B, e)
mul!(r, A, r_1, 1.0, 1.0)
r_1 .+= c
r_1 = r
end
r_1 .+= c
end
...@@ -13,4 +13,10 @@ for typ in instances(SymbolType) ...@@ -13,4 +13,10 @@ for typ in instances(SymbolType)
end end
end end
end end
s = Symbol("is_$(lowercase(string(typ)))")
@eval begin
function $s(name::String, symboltable::SymbolTable)
return (symboltable[name].type == $typ)
end
end
end end
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