Create on the fly a routine for evaluating the (VAR/PAC) expectations.
The routine takes a dseries object as unique argument and return an updated object with the expectation term. If the mod file is named `example.mod` and if the (VAR/PAC) expectation model is named `toto`, then after var_expectation.print('toto'); the expectation term can be evaluated: ts = example.var_expectations.evaluate_varexp(ts); where ts is a dseries object containing all the time series appearign in the auxiliary (var or trend_component).time-shift
parent
53ab321de8
commit
cb4384bb27
|
@ -92,8 +92,9 @@ end
|
|||
|
||||
auxmodel = M_.(expectationmodel.auxiliary_model_type).(expectationmodel.auxiliary_model_name);
|
||||
|
||||
%% First print the list of parameters appearing in the VAR_EXPECTATION/PAC_EXPECTATION term.
|
||||
|
||||
%
|
||||
% First print the list of parameters appearing in the VAR_EXPECTATION/PAC_EXPECTATION term.
|
||||
%
|
||||
if ~exist(sprintf('%s/model/%s', M_.fname, expectationmodelkind), 'dir')
|
||||
mkdir(sprintf('%s/model/%s', M_.fname, expectationmodelkind))
|
||||
end
|
||||
|
@ -154,7 +155,9 @@ end
|
|||
|
||||
fclose(fid);
|
||||
|
||||
%% Second print the expanded VAR_EXPECTATION/PAC_EXPECTATION term.
|
||||
%
|
||||
% Second print the expanded VAR_EXPECTATION/PAC_EXPECTATION term.
|
||||
%
|
||||
filename = sprintf('%s/model/%s/%s-expression.inc', M_.fname, expectationmodelkind, expectationmodelname);
|
||||
fid = fopen(filename, 'w');
|
||||
fprintf(fid, '// This file has been generated by dynare (%s).\n', datestr(now));
|
||||
|
@ -238,4 +241,115 @@ for i=1:maxlag
|
|||
end
|
||||
|
||||
fprintf(fid, '%s', expression);
|
||||
fclose(fid);
|
||||
|
||||
%
|
||||
% Third print a routine for evaluating VAR_EXPECTATION/PAC_EXPECTATION term (updates a dseries object).
|
||||
%
|
||||
kind = strrep(expectationmodelkind, '-', '_');
|
||||
mkdir(sprintf('+%s/+%s', M_.fname, kind));
|
||||
filename = sprintf('+%s/+%s/evaluate_%s.m', M_.fname, kind, expectationmodelname);
|
||||
fid = fopen(filename, 'w');
|
||||
fprintf(fid, 'function dbase = evaluate_%s(dbase)\n\n', expectationmodelname);
|
||||
fprintf(fid, '%% Evaluates %s term (%s), updating a dseries object.\n', kind, expectationmodelname);
|
||||
fprintf(fid, '%%\n');
|
||||
fprintf(fid, '%% INPUTS\n');
|
||||
fprintf(fid, '%% - dbase [dseries] databse containing all the variables appearing in the auxiliary model for the expectation.\n');
|
||||
fprintf(fid, '%%\n');
|
||||
fprintf(fid, '%% OUTPUTS\n');
|
||||
fprintf(fid, '%% - dbase [dseries] same databse augmented with the expectation term .\n');
|
||||
fprintf(fid, '%%\n');
|
||||
fprintf(fid, '%% REMARKS\n');
|
||||
fprintf(fid, '%% The name of the appended variable in dbase is the declared name for the (PAC/VAR) expectation model.\n\n');
|
||||
fprintf(fid, '%% This file has been generated by dynare (%s).\n\n', datestr(now));
|
||||
|
||||
id = 0;
|
||||
|
||||
maxlag = max(auxmodel.max_lag);
|
||||
if isequal(expectationmodel.auxiliary_model_type, 'trend_component')
|
||||
% Need to add a lag since the error correction equations are rewritten in levels.
|
||||
maxlag = maxlag+1;
|
||||
end
|
||||
|
||||
for i=1:maxlag
|
||||
for j=1:length(auxmodel.list_of_variables_in_companion_var)
|
||||
id = id+1;
|
||||
variable = auxmodel.list_of_variables_in_companion_var{j};
|
||||
transformations = {};
|
||||
ida = get_aux_variable_id(variable);
|
||||
while ida
|
||||
if isequal(M_.aux_vars(ida).type, 8)
|
||||
transformations = [transformations, 'diff'];
|
||||
variable = M_.endo_names{M_.aux_vars(ida).orig_index};
|
||||
ida = get_aux_variable_id(variable);
|
||||
elseif isequal(M_.aux_vars(ida).type, 10)
|
||||
transformations = [transformations, 'log'];
|
||||
variable = M_.endo_names{M_.aux_vars(ida).orig_index};
|
||||
ida = get_aux_variable_id(variable);
|
||||
end
|
||||
end
|
||||
switch expectationmodelkind
|
||||
case 'var-expectations'
|
||||
parameter = M_.params(expectationmodel.param_indices(id));
|
||||
case 'pac-expectations'
|
||||
parameter = 0;
|
||||
if isfield(expectationmodel,'h0_param_indices') && ~isempty(expectationmodel.h0_param_indices)
|
||||
parameter = M_.params(expectationmodel.h0_param_indices(id));
|
||||
end
|
||||
if isfield(expectationmodel,'h1_param_indices') && ~isempty(expectationmodel.h1_param_indices)
|
||||
if ~parameter
|
||||
parameter = M_.params(expectationmodel.h1_param_indices(id));
|
||||
else
|
||||
parameter = parameter+M_.params(expectationmodel.h1_param_indices(id));
|
||||
end
|
||||
end
|
||||
otherwise
|
||||
end
|
||||
switch expectationmodelkind
|
||||
case 'var-expectations'
|
||||
if i>1
|
||||
variable = sprintf('dbase.%s(-%d)', variable, i-1);
|
||||
else
|
||||
variable = sprintf('dbase.%s', variable);
|
||||
end
|
||||
case 'pac-expectations'
|
||||
variable = sprintf('dbase.%s(-%d)', variable, i);
|
||||
otherwise
|
||||
end
|
||||
if ~isempty(transformations)
|
||||
for k=length(transformations):-1:1
|
||||
variable = sprintf('%s.%s()', variable, transformations{k});
|
||||
end
|
||||
end
|
||||
if isequal(id, 1)
|
||||
if isequal(expectationmodelkind, 'pac-expectations') && growth_correction
|
||||
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};
|
||||
case 'exogenous'
|
||||
vgrowth = M_.exo_names{expectationmodel.growth_index};
|
||||
otherwise
|
||||
end
|
||||
if parameter>=0
|
||||
expression = sprintf('%s*%s+%s*%s', num2str(pgrowth, '%1.16f'), vgrowth, num2str(parameter, '%1.16f'), variable);
|
||||
else
|
||||
expression = sprintf('%s*%s-%s*%s', num2str(pgrowth, '%1.16f'), vgrowth, num2str(-parameter, '%1.16f'), variable);
|
||||
end
|
||||
else
|
||||
expression = sprintf('%s*%s', num2str(parameter, '%1.16f'), variable);
|
||||
end
|
||||
else
|
||||
if parameter>=0
|
||||
expression = sprintf('%s + %s*%s', expression, num2str(parameter, '%1.16f'), variable);
|
||||
else
|
||||
expression = sprintf('%s - %s*%s', expression, num2str(-parameter, '%1.16f'), variable);
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
fprintf(fid, 'dbase.%s = %s;', expectationmodelname, expression);
|
||||
fclose(fid);
|
|
@ -369,6 +369,7 @@ MODFILES = \
|
|||
var-expectations/8/example.mod \
|
||||
var-expectations/8/substitution.mod \
|
||||
var-expectations/9/example.mod \
|
||||
var-expectations/10/example.mod \
|
||||
trend-component-and-var-models/vm1.mod \
|
||||
trend-component-and-var-models/vm2.mod \
|
||||
trend-component-and-var-models/vm3.mod \
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
// --+ options: stochastic,json=compute +--
|
||||
|
||||
var foo z x y;
|
||||
varexo e_x e_y e_z;
|
||||
parameters a b c d e f beta ;
|
||||
|
||||
a = .9;
|
||||
b = -.2;
|
||||
c = .3;
|
||||
f = .8;
|
||||
d = .5;
|
||||
e = .4;
|
||||
|
||||
beta = 1/(1+.02);
|
||||
|
||||
// Define a VAR model from a subset of equations in the model block.
|
||||
var_model(model_name = toto, eqtags = [ 'X' 'Y' 'Z' ]);
|
||||
|
||||
// Define a VAR_EXPECTATION_MODEL
|
||||
var_expectation_model(model_name = varexp, expression = diff(log(x)), auxiliary_model_name = toto, horizon = 1, discount = beta) ;
|
||||
|
||||
model;
|
||||
[ name = 'X' ]
|
||||
diff(log(x)) = a*diff(log(x(-1))) + b*diff(log(x(-2))) + c*diff(z(-2)) + e_x;
|
||||
[ name = 'Z' ]
|
||||
diff(z) = f*diff(z(-1)) + e_z;
|
||||
[ name = 'Y' ]
|
||||
log(y) = d*log(y(-2)) + e*diff(z(-1)) + e_y;
|
||||
|
||||
foo = var_expectation(varexp);
|
||||
end;
|
||||
|
||||
// Initialize the VAR expectation model, will build the companion matrix of the VAR.
|
||||
var_expectation.initialize('varexp')
|
||||
|
||||
// Update VAR_EXPECTATION reduced form parameters
|
||||
var_expectation.update('varexp');
|
||||
|
||||
// Print expanded VAR_EXPECTATION expression in a file (to be included in substitution.mod).
|
||||
var_expectation.print('varexp');
|
||||
|
||||
shocks;
|
||||
var e_x = .01;
|
||||
var e_y = .01;
|
||||
var e_z = .01;
|
||||
end;
|
||||
|
||||
verbatim;
|
||||
initialconditions =zeros(3,4);
|
||||
initialconditions(3,1) = .1; % foo(-1)
|
||||
initialconditions(:,2) = .2; % y(-1)
|
||||
initialconditions(3,3) = .3; % z(-1)
|
||||
initialconditions(2,3) = .4; % z(-2)
|
||||
initialconditions(3,4) = .5; % x(-1)
|
||||
initialconditions(2,4) = .6; % x(-2)
|
||||
initialconditions(1,4) = .7; % x(-3)
|
||||
initialconditions = ...
|
||||
dseries(initialconditions, dates('2000Q1'), {'foo', 'y','z', 'x'});
|
||||
set_dynare_seed('default');
|
||||
ts = simul_backward_model(initialconditions, 15);
|
||||
foo = ts.foo.data;
|
||||
% Evaluate the (VAR) expectation term
|
||||
ts = example.var_expectations.evaluate_varexp(ts);
|
||||
% Check tthat the evaluation is correct.
|
||||
range = dates('2000Q4'):dates('2004Q2');
|
||||
if max(abs(ts(range).foo.data-ts(range).varexp.data))>1e-5
|
||||
error('Expectation term evaluations do not match!')
|
||||
end
|
||||
end;
|
Loading…
Reference in New Issue