| read_dynare | R Documentation |
Reads a Dynare model file and translates it into a nonlinear dsge model, together with the calibration, shock standard deviations, measurement errors, priors, optimal-policy problem and occasionally binding constraints it declares, so that the model can be solved, simulated or estimated in R without Dynare, MATLAB or Octave.
read_dynare(file, text = NULL, observed = NULL, defines = NULL)
file |
Path to a |
text |
Alternatively, the model code as a character vector (one
element per line, or a single string). Used instead of |
observed |
Optional character vector of observed variables. Overrides
the file's |
defines |
Optional named list of macro variables, the equivalent of
Dynare's |
Declarations and model. var, varexo, varexo_det, parameters,
predetermined_variables, varobs, parameter assignments (and
constants assigned to undeclared names), the model block (including
model(linear), equation tags and # model-local variables), leads and
lags of any length on variables and shocks, and STEADY_STATE(x),
which always equals the steady state of x at the current parameter
values.
MATLAB code. Dynare passes MATLAB statements in a .mod file
through to MATLAB; read_dynare() runs them with a built-in MATLAB
interpreter (matrices, cell arrays, structures, indexing, loops,
functions, eval, fsolve, fzero, csolve, ...), so calibrations
computed in MATLAB (e.g. sigma = sqrt(V(1, 1)), verbatim blocks or
set_param_value()) are reproduced. Parameters take the values they
have when the file first solves, simulates or estimates the model
(the first stoch_simul, estimation, ...); later assignments and
MATLAB code that post-processes results (plots, tables) are skipped and
listed in notes.
Macro processor. @#define, @#if/@#elseif/@#else/@#endif,
@#ifdef, @#ifndef, @#for (over arrays, ranges and tuples, with
optional when filters), @#include, @#includepath, @#echo,
@#error, simple macro functions and @{...} substitution are
expanded before the model is translated.
Timing. No manual re-timing is needed. Every Dynare variable
becomes a control; lags become auxiliary state variables named
x_lag1, x_lag2, ...; leads beyond one period become auxiliary
controls x_lead1, ...; and each shock becomes an exogenous state that
holds the current innovation. Impulse responses therefore have the
same timing as in Dynare. The auxiliary variables also appear in
solution and IRF output.
Steady state and shocks. steady_state_model becomes the model's
steady-state function (variables it does not set keep their initval
value, 0 by default, as in Dynare) and initval supplies starting
values for the numerical solver. A MATLAB steady-state file
<model>_steadystate.m next to the .mod file is run with the MATLAB
interpreter instead, including helper functions in other .m files of
the same folder. Parameters that steady_state_model or the
steady-state file set (calibrated to targets) are recomputed from the
other parameters whenever the model is solved, as in Dynare. Models declared model(linear) are
linearised with an exact Jacobian. In the shocks block, standard deviations,
variances, covariances and correlations are supported; correlated shocks
are orthogonalised by Cholesky factorisation in varexo order, which
reproduces Dynare's impulse responses. Deterministic paths
(periods/values) are returned in shock_paths.
Observed variables and measurement errors. A model may have fewer
observed variables than shocks. A stderr on an observed endogenous
variable (in shocks or estimated_params) is a measurement error: the
variable y is observed as y_obs = y + y_me, where y_me is an
i.i.d. shock. estimate() and bayes_dsge() rename a data column y
to y_obs automatically. With more observed variables than shocks and
measurement errors, the likelihood would be singular, so the extra
variables are dropped with a note.
Priors. Dynare's mean/standard-deviation prior parameterisation is
converted to dsge's, exactly: normal_pdf, beta_pdf, gamma_pdf,
uniform_pdf, inv_gamma2_pdf, and inv_gamma_pdf / inv_gamma1_pdf
(a prior on a standard deviation, translated to the "inv_gamma1"
distribution of prior() with Dynare's own parameterisation). Shape
names are case-insensitive. Shifted or generalised priors and
weibull_pdf are not translated and are listed in notes.
Estimation options. presample, first_obs and nobs from the
file's estimation command are stored in estimation and used by
estimate() and bayes_dsge(): the data are restricted to the
estimation sample and the first presample observations only
initialise the Kalman filter. The filter starts from the stationary
distribution (Dynare's lik_init = 1, the default) or, with
lik_init = 2, from Dynare's covariance of 10 times the identity on its
state vector (the observed and predetermined variables); other
lik_init values are reported in notes. Set x$model$kalman_init
to NULL to use the stationary initialisation instead.
Optimal policy. With planner_objective and ramsey_model or
ramsey_policy, the planner's first-order conditions are derived
symbolically and added to the model together with Lagrange multipliers
MULT_1, ..., as in Dynare, so solve_dsge() returns the Ramsey
equilibrium; the steady state is found with the multipliers concentrated
out. With discretionary_policy (linear models), the time-consistent
rule is computed with the Dennis (2007) algorithm at the calibrated
parameters and the model is closed with the planner's time-consistent
targeting rule. With osr_params,
osr_params_bounds and optim_weights, osr() can be called directly
on the imported model.
OccBin. Equations tagged bind = 'c' / relax = 'c' and an
occbin_constraints block define occasionally binding constraints;
simulate_occbin() on the imported model solves them with the
piecewise-linear algorithm of Guerrieri and Iacoviello (2015), as
Dynare's occbin_solver does, using the file's shocks(surprise)
block by default.
Perfect foresight. initval, endval, histval, steady, the
deterministic shocks (periods/values), mcp equation tags and
perfect_foresight_setup(periods = ) / simul are stored in
perfect_foresight; simulate_perfect_foresight() solves the
deterministic path as Dynare's perfect_foresight_solver does.
MATLAB data and toolbox functions. The MATLAB interpreter reads data
with load (Octave text files; MATLAB .mat files up to version 7 with
the R.matlab package), xlsread / readmatrix (spreadsheets, with
readxl), csvread and dlmread, and provides fmincon,
fminunc, lsqnonlin, hpfilter, ksdensity, interp1, polyfit
and other common functions. When a file cannot be run (e.g. a data file
is missing), the error names the MATLAB statement that failed.
Not supported: external_function, trend_var, EXPECTATION(),
diff(), adl(), PAC and VAR expectation operators; these raise an
error. Other blocks and commands are recorded but not run, as is MATLAB
code that calls Dynare's internal functions (e.g.
perfect_foresight_solver_core).
An object of class "dsge_dynare", a list with components:
The translated dsgenl_model. Parameters listed in
estimated_params are free (with starting values); all other
parameters are fixed at their calibrated values.
Named numeric vector of calibrated parameter values.
Named numeric vector of standard deviations of the model's shocks, including measurement errors (0 for shocks with no declared variance, as in Dynare).
Named list of prior() objects translated from
estimated_params, ready for bayes_dsge(), or NULL.
Data frame describing each estimated_params
entry and how it was translated.
Observed variables (Dynare names).
Observed variables with a
measurement error, and the model variable (y_obs) each is mapped
to.
Names declared in the file.
Period-by-shock matrix of deterministic shock values
from shocks blocks with periods/values, or NULL.
Data frame of auxiliary variables created for leads, lags and measurement errors.
Optimal-policy problem (ramsey, discretion or osr),
or NULL.
Occasionally binding constraints and regime models, or
NULL.
Options of the file's estimation command used by
estimate() and bayes_dsge() (presample, first_obs, nobs).
List of Dynare commands found in the file (such as
stoch_simul or estimation), recorded but not executed.
The file's perfect-foresight setup, used by
simulate_perfect_foresight().
Character vector of translation notes, including anything that was ignored or approximated.
Dennis, R. (2007). Optimal policy in rational expectations models: new solution algorithms. Macroeconomic Dynamics, 11(1), 31-55.
Guerrieri, L. and Iacoviello, M. (2015). OccBin: A toolkit for solving dynamic models with occasionally binding constraints easily. Journal of Monetary Economics, 70, 22-38.
dsgenl_model(), solve_dsge(), bayes_dsge(), osr(),
simulate_occbin(), simulate_perfect_foresight()
rbc <- read_dynare(system.file("examples", "rbc.mod", package = "dsge"))
rbc
sol <- solve_dsge(rbc)
irf(sol, periods = 20, se = FALSE)
# Model code can also be passed as text, including macro directives
ar <- read_dynare(text = "
@#define lags = 2
var y;
varexo e;
parameters rho;
rho = 0.5;
model;
y = e
@#for k in 1:lags
+ rho^@{k} * y(-@{k})
@#endfor
;
end;
shocks;
var e; stderr 0.01;
end;
")
solve_dsge(ar)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.