Added the possibility to rename an endogenous variable in equation.

time-shift
Stéphane Adjemia (Scylla) 2019-04-02 18:06:47 +02:00
parent 599ebc2614
commit aa499671ce
Signed by untrusted user who does not match committer: stepan
GPG Key ID: A6D44CB9C64CE77B
6 changed files with 304 additions and 3 deletions

View File

@ -56,6 +56,10 @@ if ~exist(sprintf('%s/model/json', infile), 'dir')
error('Cannot find %s/model/json folder. Did you run %s.mod with the json option?', infile, infile);
end
% Check if some variables have to be renamed.
rename = M_.equations_tags(strcmp('rename',M_.equations_tags(:,2)),[1,3]);
isrename = ~isempty(rename);
% Load json file (original mod file)
orig = loadjson(sprintf('%s/model/json/modfile-original.json', M_.dname));
@ -66,6 +70,9 @@ plist = {};
elist = {};
xlist = {};
eappend = {};
xappend = {};
for i=1:length(eqtags)
rhs = [];
lhs = [];
@ -75,6 +82,24 @@ for i=1:length(eqtags)
[LHS, RHS] = get_lhs_and_rhs(eqtags{i}, M_, true);
% Get the parameters, endogenous and exogenous variables in the current equation.
[pnames, enames, xnames] = get_variables_and_parameters_in_equation(LHS, RHS, M_);
if isrename
[variable_has_to_be_renamed, id] = ismember(eqnum, [rename{:,1}]);
if variable_has_to_be_renamed
tmp = strsplit(rename{id,2}, '->');
LHS = exactstrrep(LHS, tmp{1}, tmp{2});
RHS = exactstrrep(RHS, tmp{1}, tmp{2});
rep = strcmp(tmp{1}, enames);
if any(rep)
enames(rep) = tmp(2);
eappend = union(eappend, tmp{2});
end
rep = strcmp(tmp{1}, xnames);
if any(rep)
xnames(rep) = tmp(2);
xappend = union(xappend, tmp{2});
end
end
end
% Remove residual from equation if required.
if noresids
exogenous_variables_to_be_removed = ~ismember(xnames, M_.simulation_exo_names);
@ -161,12 +186,12 @@ fclose(fid);
% Export endogegnous variables
fid = fopen(sprintf('%s/endogenous.inc', outfold), 'w');
printlistofvariables(fid, 'endo', elist, M_);
printlistofvariables(fid, 'endo', elist, M_, eappend);
fclose(fid);
% Export exogenous variables
fid = fopen(sprintf('%s/exogenous.inc', outfold), 'w');
printlistofvariables(fid, 'exo', xlist, M_);
printlistofvariables(fid, 'exo', xlist, M_, xappend);
fclose(fid);
% Export parameter values
@ -179,7 +204,7 @@ for i=1:length(plist)
end
fclose(fid);
function printlistofvariables(fid, kind, list, DynareModel)
function printlistofvariables(fid, kind, list, DynareModel, vappend)
if isfield(DynareModel, sprintf('%s_partitions', kind))
% Some endogenous variables are tagged.
switch kind
@ -208,6 +233,8 @@ function printlistofvariables(fid, kind, list, DynareModel)
if ~isempty(tags)
tags = sprintf('(%s)', tags(3:end));
end
elseif ~isempty(strmatch(list{i}, vappend, 'exact'))
% Nothing to do, this variable was renamed bvy cherrypick
else
if isequal(kind, 'endo') && (isequal(list{i}(1:16), 'pac_expectation_') || isequal(list{i}(1:16), 'var_expectation_'))
tags = sprintf('(expectation=''%s'')', list{i}(1:3));

22
matlab/exactstrrep.m Normal file
View File

@ -0,0 +1,22 @@
function str = exactstrrep(str, old, new)
% Same as strrep but with exact word matching.
% Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
str = regexprep(str, ['\<', old,'\>'], new);

View File

@ -0,0 +1,2 @@
simulation-files1/*
simulation-files2/*

View File

@ -0,0 +1,17 @@
#!/bin/sh
rm -rf example1
rm -rf +example1
rm -f example1.log
rm -rf simulation-files1
rm -rf example2
rm -rf +example2
rm -f example2.log
rm -rf simulation-files2
rm -f *.mat
rm -f *.m
rm -f *.dat

View File

@ -0,0 +1,118 @@
// --+ options: json=compute, stochastic +--
var x1 x2 x1bar x2bar z y x u v s azertyuiopiop;
varexo ex1
ex2
ex1bar (used='estimationonly')
ex2bar (used='estimationonly')
ez
ey
ex
eu
ev
es;
parameters
rho_1 rho_2 rho_3 rho_4
a_x1_0 a_x1_1 a_x1_2 a_x1_x2_1 a_x1_x2_2
a_x2_0 a_x2_1 a_x2_2 a_x2_x1_1 a_x2_x1_2
e_c_m c_z_1 c_z_2 c_z_dx2 c_z_u c_z_dv c_z_s cx cy beta
lambda;
rho_1 = .9;
rho_2 = -.2;
rho_3 = .4;
rho_4 = -.3;
a_x1_0 = -.9;
a_x1_1 = .4;
a_x1_2 = .3;
a_x1_x2_1 = .1;
a_x1_x2_2 = .2;
a_x2_0 = -.9;
a_x2_1 = .2;
a_x2_2 = -.1;
a_x2_x1_1 = -.1;
a_x2_x1_2 = .2;
beta = .2;
e_c_m = .5;
c_z_1 = .2;
c_z_2 = -.1;
c_z_dx2 = .3;
c_z_u = .3;
c_z_dv = .4;
c_z_s = -.2;
cx = 1.0;
cy = 1.0;
lambda = 0.5; // Share of optimizing agents.
trend_component_model(model_name=toto, eqtags=['eq:x1', 'eq:x2', 'eq:x1bar', 'eq:x2bar'], targets=['eq:x1bar', 'eq:x2bar']);
pac_model(auxiliary_model_name=toto, discount=beta, model_name=pacman);
model;
[name='eq:u']
s = .3*s(-1) - .1*s(-2) + es;
[name='eq:diff(v)']
diff(v) = .5*diff(v(-1)) + ev;
[name='eq:u']
u = .5*u(-1) - .2*u(-2) + eu;
[name='eq:y']
y = rho_1*y(-1) + rho_2*y(-2) + ey;
[name='eq:x']
x = rho_3*x(-1) + rho_4*x(-2) + ex;
[name='eq:azertyuiopiop', rename='azertyuiopiop->qsdfghjklm']
azertyuiopiop = x + y;
[name='eq:x1']
diff(x1) = a_x1_0*(x1(-1)-x1bar(-1)) + a_x1_1*diff(x1(-1)) + a_x1_2*diff(x1(-2)) + a_x1_x2_1*diff(x2(-1)) + a_x1_x2_2*diff(x2(-2)) + ex1;
[name='eq:x2']
diff(x2) = a_x2_0*(x2(-1)-x2bar(-1)) + a_x2_1*diff(x1(-1)) + a_x2_2*diff(x1(-2)) + a_x2_x1_1*diff(x2(-1)) + a_x2_x1_2*diff(x2(-2)) + ex2;
[name='eq:x1bar']
x1bar = x1bar(-1) + ex1bar;
[name='eq:x2bar']
x2bar = x2bar(-1) + ex2bar;
[name='zpac']
diff(z) = lambda*(e_c_m*(x1(-1)-z(-1)) + c_z_1*diff(z(-1)) + c_z_2*diff(z(-2)) + pac_expectation(pacman) + c_z_s*s + c_z_dv*diff(v) ) + (1-lambda)*( cy*y + cx*x) + c_z_u*u + c_z_dx2*diff(x2) + ez;
end;
shocks;
var ex1 = 1.0;
var ex2 = 1.0;
var ex1bar = 1.0;
var ex2bar = 1.0;
var ez = 1.0;
var ey = 0.1;
var ex = 0.1;
var eu = 0.05;
var ev = 0.05;
var es = 0.07;
end;
// Initialize the PAC model (build the Companion VAR representation for the auxiliary model).
pac.initialize('pacman');
// Update the parameters of the PAC expectation model (h0 and h1 vectors).
pac.update.expectation('pacman');
// Select a subset of the equations and print the equations, the list of parameters, endogenous
// variables and exogenous variables in .inc files under ./simulation-files folder. Note that
// innovations ex1bar and ex2bar will not appear in the equations.
cherrypick('example1', 'simulation-files1', {'zpac', 'eq:x1', 'eq:x2', 'eq:x1bar', 'eq:x2bar', 'eq:azertyuiopiop'}, true);

View File

@ -0,0 +1,115 @@
// --+ options: json=compute, stochastic +--
var x1 x2 x1bar x2bar z y x u v s;
varexo ex1
ex2
ex1bar (used='estimationonly')
ex2bar (used='estimationonly')
ez
ey
ex
eu
ev
es;
parameters
rho_1 rho_2 rho_3 rho_4
a_x1_0 a_x1_1 a_x1_2 a_x1_x2_1 a_x1_x2_2
a_x2_0 a_x2_1 a_x2_2 a_x2_x1_1 a_x2_x1_2
e_c_m c_z_1 c_z_2 c_z_dx2 c_z_u c_z_dv c_z_s cx cy beta
lambda;
rho_1 = .9;
rho_2 = -.2;
rho_3 = .4;
rho_4 = -.3;
a_x1_0 = -.9;
a_x1_1 = .4;
a_x1_2 = .3;
a_x1_x2_1 = .1;
a_x1_x2_2 = .2;
a_x2_0 = -.9;
a_x2_1 = .2;
a_x2_2 = -.1;
a_x2_x1_1 = -.1;
a_x2_x1_2 = .2;
beta = .2;
e_c_m = .5;
c_z_1 = .2;
c_z_2 = -.1;
c_z_dx2 = .3;
c_z_u = .3;
c_z_dv = .4;
c_z_s = -.2;
cx = 1.0;
cy = 1.0;
lambda = 0.5; // Share of optimizing agents.
trend_component_model(model_name=toto, eqtags=['eq:x1', 'eq:x2', 'eq:x1bar', 'eq:x2bar'], targets=['eq:x1bar', 'eq:x2bar']);
pac_model(auxiliary_model_name=toto, discount=beta, model_name=pacman);
model;
[name='eq:u']
s = .3*s(-1) - .1*s(-2) + es;
[name='eq:diff(v)']
diff(v) = .5*diff(v(-1)) + ev;
[name='eq:u']
u = .5*u(-1) - .2*u(-2) + eu;
[name='eq:y']
y = rho_1*y(-1) + rho_2*y(-2) + ey;
[name='eq:x']
x = rho_3*x(-1) + rho_4*x(-2) + ex;
[name='eq:x1', rename='x1->x3']
diff(x1) = a_x1_0*(x1(-1)-x1bar(-1)) + a_x1_1*diff(x1(-1)) + a_x1_2*diff(x1(-2)) + a_x1_x2_1*diff(x2(-1)) + a_x1_x2_2*diff(x2(-2)) + ex1;
[name='eq:x2']
diff(x2) = a_x2_0*(x2(-1)-x2bar(-1)) + a_x2_1*diff(x1(-1)) + a_x2_2*diff(x1(-2)) + a_x2_x1_1*diff(x2(-1)) + a_x2_x1_2*diff(x2(-2)) + ex2;
[name='eq:x1bar']
x1bar = x1bar(-1) + ex1bar;
[name='eq:x2bar']
x2bar = x2bar(-1) + ex2bar;
[name='zpac']
diff(z) = lambda*(e_c_m*(x1(-1)-z(-1)) + c_z_1*diff(z(-1)) + c_z_2*diff(z(-2)) + pac_expectation(pacman) + c_z_s*s + c_z_dv*diff(v) ) + (1-lambda)*( cy*y + cx*x) + c_z_u*u + c_z_dx2*diff(x2) + ez;
end;
shocks;
var ex1 = 1.0;
var ex2 = 1.0;
var ex1bar = 1.0;
var ex2bar = 1.0;
var ez = 1.0;
var ey = 0.1;
var ex = 0.1;
var eu = 0.05;
var ev = 0.05;
var es = 0.07;
end;
// Initialize the PAC model (build the Companion VAR representation for the auxiliary model).
pac.initialize('pacman');
// Update the parameters of the PAC expectation model (h0 and h1 vectors).
pac.update.expectation('pacman');
// Select a subset of the equations and print the equations, the list of parameters, endogenous
// variables and exogenous variables in .inc files under ./simulation-files folder. Note that
// innovations ex1bar and ex2bar will not appear in the equations.
cherrypick('example2', 'simulation-files2', {'zpac', 'eq:x1', 'eq:x2', 'eq:x1bar', 'eq:x2bar'}, true);