Added routine to evaluate the RHS of an equation.

time-shift
Stéphane Adjemian (Charybdis) 2019-03-19 07:07:47 +01:00
parent 15bd0e3397
commit 567ca19000
Signed by untrusted user who does not match committer: stepan
GPG Key ID: A6D44CB9C64CE77B
6 changed files with 332 additions and 0 deletions

View File

@ -0,0 +1,65 @@
function nds = evaluate(ds, eqtag)
% 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/>.
global M_
% Get equation
[LHS, RHS] = get_lhs_and_rhs(eqtag, M_, true);
% Parse equation and return list of parameters, endogenous and exogenous variables.
[pnames, enames, xnames] = get_variables_and_parameters_in_equation(LHS, RHS, M_);
% Load parameter values.
commands = sprintf('%s = %s;', pnames{1}, num2str(M_.params(strcmp(pnames{1},M_.param_names)), 16));
for i=2:length(pnames)
commands = sprintf('%s %s = %s;', commands, pnames{i}, ...
num2str(M_.params(strcmp(pnames{i},M_.param_names)), 16));
end
eval(commands)
% Substitute endogenous variable x with ds.x
enames = unique(enames);
for i=1:length(enames)
if ismember(enames{i}, ds.name)
RHS = regexprep(RHS, sprintf('\\<(%s)\\>', enames{i}), sprintf('ds.%s', enames{i}));
else
error('Endogenous variable %s is unknown in dseries objet.', enames{i})
end
end
% Substitute exogenous variable x with ds.x, except if
if ~isfield(M_, 'simulation_exo_names')
M_.simulation_exo_names = M_.exo_names;
end
xnames = unique(xnames);
for i=1:length(xnames)
if ismember(xnames{i}, M_.simulation_exo_names)
if ismember(xnames{i}, ds.name)
RHS = regexprep(RHS, sprintf('\\<(%s)\\>', xnames{i}), sprintf('ds.%s', xnames{i}));
else
RHS = regexprep(RHS, sprintf('\\<(%s)\\>', xnames{i}), '0');
warning('Exogenous variable %s is unknown in dseries objet. Assign zero value.', xnames{i})
end
else
RHS = regexprep(RHS, sprintf('(\\ *)(+)(\\ *)%s', xnames{i}), '');
end
end
nds = eval(RHS);

View File

@ -437,6 +437,9 @@ ECB_MODFILES = \
pac/trend-component-24/example.mod \
pac/trend-component-25/example.mod \
pac/trend-component-26/example.mod \
pac/trend-component-27/example.mod \
pac/trend-component-28/example1.mod \
pac/trend-component-28/example2.mod \
write/example1.mod \
ecb/backward-models/irf/solow_1.mod \
ecb/backward-models/irf/solow_2.mod \

View File

@ -0,0 +1 @@
simulation-files/*

View File

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

View File

@ -0,0 +1,123 @@
// --+ 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']
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');
// Set initial conditions to zero for non logged variables, and one for logged variables
init = zeros(10, M_.endo_nbr+M_.exo_nbr);
initialconditions = dseries(init, 2000Q1, vertcat(M_.endo_names,M_.exo_names));
// Simulate the model for 500 periods
TrueData = simul_backward_model(initialconditions, 500);
TrueData.x1bis = equation.evaluate(TrueData, 'eq:x1');
if max(abs(TrueData.x1bis.data(5:end)-diff(TrueData.x1.data(4:end))))>1e-8
error('equation.evaluate() returned wrong values.')
end

View File

@ -0,0 +1,125 @@
// --+ options: json=compute, stochastic +--
var x1 x2 x1bar x2bar z y x u v s;
varexo ex1
ex2 (used='estimationonly')
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']
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');
// Set initial conditions to zero for non logged variables, and one for logged variables
init = zeros(10, M_.endo_nbr+M_.exo_nbr);
initialconditions = dseries(init, 2000Q1, vertcat(M_.endo_names,M_.exo_names));
// Simulate the model for 500 periods
TrueData = simul_backward_model(initialconditions, 500);
TrueData.diff_x2_fit = equation.evaluate(TrueData, 'eq:x2');
dx2 = diff(TrueData.x2);
if max(abs((TrueData.diff_x2_fit.data+TrueData.ex2.data-dx2.data)))>1e-8
error('equation.evaluate() returned wrong values.')
end