Streamlined code in print_expectations and allow diff and unary op in growth option of pac_model.

time-shift
Stéphane Adjemian (Charybdis) 2019-03-02 22:36:13 +01:00
parent d00b57541e
commit cdadab34b4
Signed by untrusted user who does not match committer: stepan
GPG Key ID: A6D44CB9C64CE77B
6 changed files with 192 additions and 125 deletions

View File

@ -265,26 +265,7 @@ if isequal(expectationmodelkind, 'pac-expectations') && growth_correction
filename = sprintf('%s/model/%s/%s-%s-growth-neutrality-correction.inc', M_.fname, expectationmodelkind, eqtag, expectationmodelname);
fid = fopen(filename, 'w');
fprintf(fid, '// This file has been generated by dynare (%s).\n', datestr(now));
pgrowth = M_.param_names{expectationmodel.growth_neutrality_param_index};
switch expectationmodel.growth_type
case 'parameter'
vgrowth = M_.param_names{expectationmodel.growth_index};
case 'endogenous'
vgrowth = M_.endo_names{expectationmodel.growth_index};
lgrowth = expectationmodel.growth_lag;
case 'exogenous'
if expectationmodel.growth_index<=M_.exo_nbr
vgrowth = M_.exo_names{expectationmodel.growth_index};
else
vgrowth = M_.endo_names{expectationmodel.growth_index};
end
lgrowth = expectationmodel.growth_lag;
otherwise
end
if ismember(expectationmodel.growth_type, {'endogenous','exogenous'})
vgrowth = rewritegrowthvariable(vgrowth, lgrowth, M_);
end
fprintf(fid, '%s*%s', pgrowth, vgrowth);
fprintf(fid, '%s*%s', M_.param_names{expectationmodel.growth_neutrality_param_index}, expectationmodel.growth_str);
fclose(fid);
fprintf('Growth neutrality correction is saved in %s.\n', filename);
end
@ -387,28 +368,27 @@ for i=1:maxlag
if isequal(id, 1)
if isequal(expectationmodelkind, 'pac-expectations') && growth_correction
pgrowth = M_.params(expectationmodel.growth_neutrality_param_index);
vgrowth = expectationmodel.growth_str;
switch expectationmodel.growth_type
case 'parameter'
vgrowth = M_.param_names{expectationmodel.growth_index};
case 'endogenous'
vgrowth = M_.endo_names{expectationmodel.growth_index};
lgrowth = expectationmodel.growth_lag;
case 'exogenous'
if expectationmodel.growth_index<=M_.exo_nbr
vgrowth = M_.exo_names{expectationmodel.growth_index};
else
vgrowth = M_.endo_names{expectationmodel.growth_index};
end
lgrowth = expectationmodel.growth_lag;
vgrowth = M_.params(strcmp(vgrowth, M_.param_names));
otherwise
end
if ismember(expectationmodel.growth_type, {'endogenous','exogenous'})
vgrowth = rewritegrowthvariable(vgrowth, lgrowth, M_);
vgrowth = regexprep(vgrowth, '\<(?!diff\>)\<(?!log\>)\<(?!\d\>)\w+', 'dbase.$0');
end
if parameter>=0
expression = sprintf('%s*%s+%s*%s', num2str(pgrowth, '%1.16f'), vgrowth, num2str(parameter, '%1.16f'), variable);
switch expectationmodel.growth_type
case 'parameter'
expression = sprintf('%s*%s+%s*%s', num2str(pgrowth, '%1.16f'), num2str(vgrowth, '%1.16f'), num2str(parameter, '%1.16f'), variable);
otherwise
expression = sprintf('%s*%s+%s*%s', num2str(pgrowth, '%1.16f'), vgrowth, num2str(parameter, '%1.16f'), variable);
end
else
expression = sprintf('%s*%s-%s*%s', num2str(pgrowth, '%1.16f'), vgrowth, num2str(-parameter, '%1.16f'), variable);
switch expectationmodel.growth_type
case 'parameter'
expression = sprintf('%s*%s-%s*%s', num2str(pgrowth, '%1.16f'), num2str(vgrowth, '%1.16f'), num2str(-parameter, '%1.16f'), variable);
otherwise
expression = sprintf('%s*%s-%s*%s', num2str(pgrowth, '%1.16f'), vgrowth, num2str(-parameter, '%1.16f'), variable);
end
end
else
expression = sprintf('%s*%s', num2str(parameter, '%1.16f'), variable);
@ -428,91 +408,4 @@ fclose(fid);
fprintf('Expectation dseries expression is saved in %s.\n', filename);
skipline();
function vgrowth = rewritegrowthvariable(vgrowth, lgrowth, M_)
if isauxiliary(vgrowth)
% We need to rewrite vgrowth in terms of the original (exogenous or endogenous) variable.
auxinfo = M_.aux_vars(get_aux_variable_id(vgrowth));
switch auxinfo.type
case 1
tmp = get_aux_variable_id(auxinfo.orig_index);
% Lagged endogenous.
if ~isauxiliary(auxinfo.orig_index)
v = M_.endo_names{auxinfo.orig_index};
s = auxinfo.orig_lead_lag;
d = 0;
t = [];
elseif tmp.type==10
t = tmp.unary_op;
s = auxinfo.orig_lead_lag;
d = 0;
v = M_.endo_names{tmp.orig_index};
else
error('Auxiliary variable has wrong type (1).')
end
case 3
% Lagged exogenous.
v = M_.exo_names{auxinfo.orig_index};
s = auxinfo.orig_lead_lag;
d = 0;
t = [];
case 8
tmp = get_aux_variable_id(auxinfo.orig_index);
% First difference.
if ~isauxiliary(auxinfo.orig_index)
v = M_.endo_names{auxinfo.orig_index};
s = 0;
d = 1;
t = [];
elseif tmp.type==10
t = tmp.unary_op;
s = 0;
d = 1;
v = M_.endo_names{tmp.orig_index};
else
error('Auxiliary variable has wrong type (8).')
end
case 9
% Lagged first difference
s = 0;
while auxinfo.type==9
s = s+1;
auxinfo = M_.aux_vars(get_aux_variable_id(auxinfo.orig_index));
end
if auxinfo.type==8
if isauxiliary(auxinfo.orig_index)
% First difference of an auxiliary variable.
tmp = get_aux_variable_id(auxinfo.orig_index);
if tmp.type==10
t = tmp.unary_op;
d = 1;
v = M_.endo_names{tmp.orig_index};
else
error('Auxiliary variable has wrong type (9).')
end
else
% First difference of a declared endogenous variable.
t = [];
d = 1;
v = M_.endo_names{auxinfo.orig_index};
end
else
error('Auxiliary variable has wrong type (9).')
end
otherwise
error('Auxiliary variable has wrong type.')
end
s = s+lgrowth;
if s
vgrowth = sprintf('%s(-%u)', v, abs(s));
else
vgrowth = v;
end
if ~isempty(t)
vgrowth = sprintf('%s(%s)', t, vgrowth);
end
if d
vgrowth = sprintf('%s(%s)', 'diff', vgrowth);
end
end
skipline();

@ -1 +1 @@
Subproject commit 5f013756f23fe89ec946fcff2cfdad7627693e12
Subproject commit 34f02a37e375ab464905daae36c8d01a764a83c0

View File

@ -396,6 +396,8 @@ MODFILES = \
pac/var-4/example.mod \
pac/var-5/example.mod \
pac/var-5/substitution.mod \
pac/var-6/example.mod \
pac/var-6/substitution.mod \
pac/trend-component-1/example.mod \
pac/trend-component-2/example.mod \
pac/trend-component-3/example.mod \

10
tests/pac/var-6/clean Executable file
View File

@ -0,0 +1,10 @@
#!/bin/sh
rm -rf example
rm -rf +example
rm -f example*.mat
rm -f example.log
rm -rf substitution
rm -rf +substitution
rm -f substitution*.mat
rm -f substitution.log

View File

@ -0,0 +1,77 @@
// --+ options: json=compute, stochastic +--
var y x z u;
varexo ex ey ez eu;
parameters a_y_1 a_y_2 b_y_1 b_y_2 b_x_1 b_x_2 ; // VAR parameters
parameters g beta e_c_m c_z_1 c_z_2; // PAC equation parameters
a_y_1 = .2;
a_y_2 = .3;
b_y_1 = .1;
b_y_2 = .4;
b_x_1 = -.1;
b_x_2 = -.2;
beta = .9;
e_c_m = .1;
c_z_1 = .7;
c_z_2 = -.3;
g = .1;
var_model(model_name=toto, eqtags=['eq:x', 'eq:y']);
pac_model(auxiliary_model_name=toto, discount=beta, model_name=pacman, growth=diff(u(-1)));
model;
[name='eq:u']
diff(u) = g + eu;
[name='eq:y']
y = a_y_1*y(-1) + a_y_2*diff(x(-1)) + b_y_1*y(-2) + b_y_2*diff(x(-2)) + ey ;
[name='eq:x']
diff(x) = b_x_1*y(-2) + b_x_2*diff(x(-1)) + g*(1-b_x_2) + ex ;
[name='eq:pac']
diff(z) = e_c_m*(x(-1)-z(-1)) + c_z_1*diff(z(-1)) + c_z_2*diff(z(-2)) + pac_expectation(pacman) + ez;
end;
shocks;
var ex = 1.0;
var ey = 1.0;
var ez = 1.0;
var eu = 0.1;
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');
// Set initial conditions to zero. Please use more sensible values if any...
initialconditions = dseries(zeros(10, M_.endo_nbr+M_.exo_nbr), 2000Q1, vertcat(M_.endo_names,M_.exo_names));
// Simulate the model for 20 periods
set_dynare_seed('default');
TrueData = simul_backward_model(initialconditions, 20);
// Print expanded PAC_EXPECTATION term.
pac.print('pacman', 'eq:pac');
verbatim;
set_dynare_seed('default');
y = zeros(M_.endo_nbr,1);
y(1:M_.orig_endo_nbr) = rand(M_.orig_endo_nbr, 1);
x = randn(M_.exo_nbr,1);
y = example.set_auxiliary_variables(y, x, M_.params);
y = [y(find(M_.lead_lag_incidence(1,:))); y];
[residual, g1] = example.dynamic(y, x', M_.params, oo_.steady_state, 1);
save('example.mat', 'residual', 'g1', 'TrueData');
end;

View File

@ -0,0 +1,85 @@
// --+ options: transform_unary_ops, json=compute, stochastic +--
var y x z u;
varexo ex ey ez eu;
parameters a_y_1 a_y_2 b_y_1 b_y_2 b_x_1 b_x_2 g; // VAR parameters
parameters e_c_m c_z_1 c_z_2; // PAC equation parameters
a_y_1 = .2;
a_y_2 = .3;
b_y_1 = .1;
b_y_2 = .4;
b_x_1 = -.1;
b_x_2 = -.2;
g = .1;
e_c_m = .1;
c_z_1 = .7;
c_z_2 = -.3;
@#include "example/model/pac-expectations/eq0-pacman-parameters.inc"
model;
[name='eq:u']
diff(u) = g + eu;
[name='eq:y']
y = a_y_1*y(-1) + a_y_2*diff(x(-1)) + b_y_1*y(-2) + b_y_2*diff(x(-2)) + ey ;
[name='eq:x']
diff(x) = b_x_1*y(-2) + b_x_2*diff(x(-1)) + g*(1-b_x_2) + ex ;
[name='eq:pac']
diff(z) = e_c_m*(x(-1)-z(-1)) + c_z_1*diff(z(-1)) + c_z_2*diff(z(-2)) +
@#include "example/model/pac-expectations/eq0-pacman-growth-neutrality-correction.inc"
+
@#include "example/model/pac-expectations/eq0-pacman-expression.inc"
+ ez;
end;
shocks;
var ex = 1.0;
var ey = 1.0;
var ez = 1.0;
var eu = 0.1;
end;
// Set initial conditions to zero. Please use more sensible values if any...
initialconditions = dseries(zeros(10, M_.endo_nbr+M_.exo_nbr), 2000Q1, vertcat(M_.endo_names,M_.exo_names));
// Simulate the model for 20 periods
set_dynare_seed('default');
TrueData = simul_backward_model(initialconditions, 20);
verbatim;
set_dynare_seed('default');
y = zeros(M_.endo_nbr,1);
y(1:M_.orig_endo_nbr) = rand(M_.orig_endo_nbr, 1);
x = randn(M_.exo_nbr,1);
y = substitution.set_auxiliary_variables(y, x, M_.params);
y = [y(find(M_.lead_lag_incidence(1,:))); y];
example = load('example.mat');
[residual, g1] = substitution.dynamic(y, x', M_.params, oo_.steady_state, 1);
if max(abs(example.TrueData.data(:)-TrueData.data(:)))>1e-9
error('Simulations do not match.')
end
if ~isequal(length(residual), length(example.residual)) || max(abs(example.residual-residual))>1e-8
warning('Residuals do not match!')
end
if ~isequal(length(g1(:)), length(example.g1(:))) || max(abs(example.g1(:)-g1(:)))>1e-8
warning('Jacobian matrices do not match!')
end
delete('example.mat');
end;