Add new “log” option to “var” statement

Ref. #349
fix-nonlinear-solvers
Sébastien Villemot 2022-03-30 18:01:22 +02:00
parent 7d3ab36743
commit 3b4ef0df49
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
8 changed files with 302 additions and 7 deletions

View File

@ -124,7 +124,9 @@ not recommended as it already denotes the inverse operator. Commands
for declaring variables and parameters are described below.
.. command:: var VAR_NAME [$TEX_NAME$] [(long_name=QUOTED_STRING|NAME=QUOTED_STRING)]...;
var(log) VAR_NAME [$TEX_NAME$] [(long_name=QUOTED_STRING|NAME=QUOTED_STRING)]...;
var(deflator=MODEL_EXPR) VAR_NAME (... same options apply)
var(log, deflator=MODEL_EXPR) VAR_NAME (... same options apply)
var(log_deflator=MODEL_EXPR) VAR_NAME (... same options apply)
|br| This required command declares the endogenous variables in
@ -138,14 +140,28 @@ for declaring variables and parameters are described below.
in the order of declaration, in a column cell array
``M_.endo_names``.
*Options*
If the model is nonstationary and is to be written as such in the
``model`` block, Dynare will need the trend deflator for the
appropriate endogenous variables in order to stationarize the
model. The trend deflator must be provided alongside the variables
that follow this trend.
*Options*
.. option:: log
In addition to the endogenous variable(s) thus declared, this option
also triggers the creation of auxiliary variable(s) equal to the log of
the corresponding endogenous variable(s). For example, given a
``var(log) y`` statement, two endogenous will be created (``y`` and
``LOG_y``), and an auxiliary equation linking the two will also be
added (equal to ``LOG_y = log(y)``). Moreover, every occurence of ``y``
in the model will be replaced by ``exp(LOG_y)``. This option is for
example useful when one wants to perform a loglinear approximation of
some variable(s) in the context of a first-order stochastic
approximation; or when one wants to ensure the variable(s) stay(s) in
the definition domain of the function defining the steady state or the
dynamic residuals when the nonlinear solver is used.
.. option:: deflator = MODEL_EXPR
@ -155,6 +171,8 @@ for declaring variables and parameters are described below.
the ``trend_var, log_trend_var, var`` and ``parameters``
commands. The deflator is assumed to be multiplicative; for an
additive deflator, use ``log_deflator``.
This option can be used together with the ``log`` option (the latter
must come first).
.. option:: log_deflator = MODEL_EXPR
@ -162,6 +180,10 @@ for declaring variables and parameters are described below.
be additive instead of multiplicative (or, to put it
otherwise, the declared variable is equal to the log of a
variable with a multiplicative trend).
This option cannot be used together with the ``log`` option, because it
would not make much sense from an economic point of view (the
corresponding auxiliary variable would correspond to the log taken
two times on a variable with a multiplicative trend).
.. _long-name:

View File

@ -11,7 +11,7 @@ function str = subst_auxvar(var_index, aux_lead_lag, M_)
% OUTPUTS
% - str [string] name of auxiliary
% Copyright (C) 2001-2021 Dynare Team
% Copyright (C) 2001-2022 Dynare Team
%
% This file is part of Dynare.
%
@ -61,8 +61,9 @@ if ~isempty(aux_index)
end
return
case 7
% currently unused
error('This type of auxiliary variable should not occur, please contact the developers.')
% Log-transformation of a variable
str = sprintf('log(%s(%d))', M_.endo_names{M_.aux_vars(aux_index).orig_index}, aux_lead_lag);
return;
case 8
% Diff operator
str = sprintf('diff(%s)', M_.endo_names{M_.aux_vars(aux_index).orig_index});

@ -1 +1 @@
Subproject commit 46cc47266bac728eeb0758d3b7cff18f841437bb
Subproject commit 71edfd05e4cc28cb3e36e217aaa0e0cbdc030fd2

View File

@ -461,7 +461,11 @@ MODFILES = \
bgp/solow-1/solow.mod \
bgp/nk-1/nk.mod \
bgp/ramsey-1/ramsey.mod \
dynare-command-options/ramst.mod
dynare-command-options/ramst.mod \
log_transform/example1.mod \
log_transform/ramst.mod \
log_transform/fs2000_nonstationary.mod \
log_transform/nk_ramsey.mod
ECB_MODFILES = \
var-expectations/1/example1.mod \
@ -938,6 +942,10 @@ pruning/AS_pruned_state_space_red_shock.o.trs: pruning/AnSchorfheide_pruned_stat
ramst_model_edit.m.trs: ramst.m.trs
ramst_model_edit.o.trs: ramst.o.trs
log_transform/ramst.m.trs: ramst.m.trs
log_transform/ramst.o.trs: ramst.o.trs
observation_trends_and_prefiltering/MCMC: m/observation_trends_and_prefiltering/MCMC o/observation_trends_and_prefiltering/MCMC
m/observation_trends_and_prefiltering/MCMC: $(patsubst %.mod, %.m.trs, $(filter observation_trends_and_prefiltering/MCMC/%.mod, $(MODFILES)))
o/observation_trends_and_prefiltering/MCMC: $(patsubst %.mod, %.o.trs, $(filter observation_trends_and_prefiltering/MCMC/%.mod, $(MODFILES)))

View File

@ -0,0 +1,50 @@
// Test for var(log) in a stochastic context
var(log) c, y, k;
var a, h, b;
varexo e, u;
parameters beta, rho, alpha, delta, theta, psi, tau;
alpha = 0.36;
rho = 0.95;
tau = 0.025;
beta = 0.99;
delta = 0.025;
psi = 0;
theta = 2.95;
phi = 0.1;
model;
c*theta*h^(1+psi)=(1-alpha)*y;
k = beta*(((exp(b)*c)/(exp(b(+1))*c(+1)))
*(exp(b(+1))*alpha*y(+1)+(1-delta)*k));
y = exp(a)*(k(-1)^alpha)*(h^(1-alpha));
k = exp(b)*(y-c)+(1-delta)*k(-1);
a = rho*a(-1)+tau*b(-1) + e;
b = tau*a(-1)+rho*b(-1) + u;
end;
initval;
y = 1.08068253095672;
c = 0.80359242014163;
h = 0.29175631001732;
k = 11.08360443260358;
a = 0;
b = 0;
e = 0;
u = 0;
end;
shocks;
var e; stderr 0.009;
var u; stderr 0.009;
var e, u = phi*0.009*0.009;
end;
stoch_simul;
if isempty(strmatch('LOG_y', M_.endo_names))
error('Log-transformed variable not created')
end

View File

@ -0,0 +1,78 @@
// Test for var(log, deflator=)
/*
* Copyright (C) 2004-2022 Dynare Team
*
* This file is part of Dynare.
*
* Dynare is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Dynare is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dynare. If not, see <https://www.gnu.org/licenses/>.
*/
var gM gA;
trend_var(growth_factor=gA) A;
trend_var(growth_factor=gM) M;
var(log, deflator=A) k c y;
var(deflator=M(-1)/A) P;
var(deflator=M(-1)) W l d;
var R n;
varexo e_a e_m;
parameters alp bet gam mst rho psi del;
alp = 0.33;
bet = 0.99;
gam = 0.003;
mst = 1.011;
rho = 0.7;
psi = 0.787;
del = 0.02;
model;
gA = exp(gam+e_a);
log(gM) = (1-rho)*log(mst) + rho*log(gM(-1))+e_m;
c+k = k(-1)^alp*(A*n)^(1-alp)+(1-del)*k(-1);
P*c = M;
P/(c(+1)*P(+1))=bet*P(+1)*(alp*k^(alp-1)*(A(+1)*n(+1))^(1-alp)+(1-del))/(c(+2)*P(+2));
(psi/(1-psi))*(c*P/(1-n))=W;
R = P*(1-alp)*k(-1)^alp*A^(1-alp)*n^(-alp)/W;
W = l/n;
M-M(-1)+d = l;
1/(c*P)=bet*R/(c(+1)*P(+1));
y = k(-1)^alp*(A*n)^(1-alp);
end;
initval;
k = 6;
gM = mst;
P = 2.25;
c = 0.45;
W = 4;
R = 1.02;
d = 0.85;
n = 0.19;
l = 0.86;
y = 0.6;
gA = exp(gam);
end;
shocks;
var e_a; stderr 0.014;
var e_m; stderr 0.005;
end;
steady;
check;
stoch_simul;

View File

@ -0,0 +1,85 @@
// Tests var(log) in combination with ramsey_model
//------------------------------------------------------------------------------------------------------------------------
//1. Variable declaration
//------------------------------------------------------------------------------------------------------------------------
var pai, n, r, a;
var(log) c;
//4 variables + 1 shock
varexo u;
//------------------------------------------------------------------------------------------------------------------------
// 2. Parameter declaration and calibration
//-------------------------------------------------------------------------------------------------------------------------
parameters beta, rho, epsilon, omega, phi, gamma;
beta=0.99;
gamma=3; //Frish elasticity
omega=17; //price stickyness
epsilon=8; //elasticity for each variety of consumption
phi=1; //coefficient associated to labor effort disutility
rho=0.95; //coefficient associated to productivity shock
//-----------------------------------------------------------------------------------------------------------------------
// 3. The model
//-----------------------------------------------------------------------------------------------------------------------
model;
a=rho*(a(-1))+u;
1/c=beta*(1/(c(+1)))*(r/(pai(+1))); //euler
omega*pai*(pai-1)=beta*omega*(c/(c(+1)))*(pai(+1))*(pai(+1)-1)+epsilon*exp(a)*n*(c/exp(a)*phi*n^gamma-(epsilon-1)/epsilon); //NK pc
//pai*(pai-1)/c = beta*pai(+1)*(pai(+1)-1)/c(+1)+epsilon*phi*n^(gamma+1)/omega-exp(a)*n*(epsilon-1)/(omega*c); //NK pc
(exp(a))*n=c+(omega/2)*((pai-1)^2);
end;
//--------------------------------------------------------------------------------------------------------------------------
// 4. Steady state
//---------------------------------------------------------------------------------------------------------------------------
initval;
pai=1;
r=1/beta;
c=0.9671684882;
n=0.9671684882;
a=0;
end;
//---------------------------------------------------------------------------------------------------------------------------
// 5. shocks
//---------------------------------------------------------------------------------------------------------------------------
shocks;
var u; stderr 0.008;
end;
//--------------------------------------------------------------------------------------------------------------------------
// 6. Ramsey problem
//--------------------------------------------------------------------------------------------------------------------------
planner_objective(ln(c)-phi*((n^(1+gamma))/(1+gamma)));
ramsey_model(planner_discount=0.99, planner_discount_latex_name = $\delta$);
stoch_simul(order=1,irf=20);
evaluate_planner_objective;

View File

@ -0,0 +1,51 @@
// Test for var(log) in a deterministic context
var(log) c k;
varexo x;
parameters alph gam delt bet aa;
alph=0.5;
gam=0.5;
delt=0.02;
bet=0.05;
aa=0.5;
model;
c + k - aa*x*k(-1)^alph - (1-delt)*k(-1);
c^(-gam) - (1+bet)^(-1)*(aa*alph*x(+1)*k^(alph-1) + 1 - delt)*c(+1)^(-gam);
end;
initval;
x = 1;
k = ((delt+bet)/(1.0*aa*alph))^(1/(alph-1));
c = aa*k^alph-delt*k;
end;
steady;
check;
shocks;
var x;
periods 1;
values 1.2;
end;
perfect_foresight_setup(periods=200);
perfect_foresight_solver;
idx_c = strmatch('c', M_.endo_names);
idx_log_c = strmatch('LOG_c', M_.endo_names);
if isempty(idx_log_c)
error('Log-transformed variable not created')
end
if max(abs(oo_.endo_simul(idx_log_c, :) - log(oo_.endo_simul(idx_c, :)))) > 1e-7
error('Transformation not correctly performed')
end
S = load('../ramst/Output/ramst_results.mat');
if max(abs(oo_.endo_simul(idx_c, :) - S.oo_.endo_simul(idx_c, :))) > 1e-7
error('Result differs from non-transformed model')
end