Added the possibility to use linear expressions in VAR_EXPECTATION_MODEL.

See tests/var-expectations/9/example.mod for a self documented example.

Also updated all the integration tests using the option `expression` instead of
`variable` (which is deprecated and will be removed at some point).
time-shift
Stéphane Adjemia (Scylla) 2018-11-30 22:14:12 +01:00
parent 650c78f99d
commit 53ab321de8
Signed by untrusted user who does not match committer: stepan
GPG Key ID: A6D44CB9C64CE77B
14 changed files with 246 additions and 88 deletions

View File

@ -84,8 +84,17 @@ if discountfactor>1
error('The discount cannot be greater than one.')
end
% Set variable_id in VAR model
variable_id_in_var = strcmp(auxmodel.list_of_variables_in_companion_var, varexpectationmodel.variable);
% Set variables_id in VAR model
m = length(varexpectationmodel.expr.vars);
variables_id_in_var = NaN(m,1);
for i = 1:m
j = find(strcmp(auxmodel.list_of_variables_in_companion_var, DynareModel.endo_names{varexpectationmodel.expr.vars(i)}));
if isempty(j)
error('Cannot find variable %s in the companion VAR', DynareModel.endo_names{varexpectationmodel.expr.vars(i)})
else
variables_id_in_var(i) = find(strcmp(auxmodel.list_of_variables_in_companion_var, DynareModel.endo_names{varexpectationmodel.expr.vars(i)}));
end
end
% Get the horizon parameter.
horizon = varexpectationmodel.horizon;
@ -127,8 +136,10 @@ CompanionMatrix = auxcalib.CompanionMatrix;
n = length(CompanionMatrix);
% Set the selection vector
alpha = zeros(1, length(CompanionMatrix));
alpha(variable_id_in_var) = 1;
alpha = zeros(1, n);
alpha(variables_id_in_var) = varexpectationmodel.expr.constants;
params_id_in_var = ~isnan(varexpectationmodel.expr.params);
alpha(variables_id_in_var(params_id_in_var)) = varexpectationmodel.expr.params(params_id_in_var);
if length(horizon)==1
% Compute the reduced form parameters of the (discounted) forecast in period t+horizon(1)

View File

@ -368,6 +368,7 @@ MODFILES = \
var-expectations/7/substitution.mod \
var-expectations/8/example.mod \
var-expectations/8/substitution.mod \
var-expectations/9/example.mod \
trend-component-and-var-models/vm1.mod \
trend-component-and-var-models/vm2.mod \
trend-component-and-var-models/vm3.mod \

View File

@ -36,7 +36,7 @@ var_model(model_name = toto, eqtags = [ 'X' 'Y' 'Z' ]);
** where the sum is over h=0,, and the conditional expectations are computed with VAR model `var_model_name`.
*/
var_expectation_model(model_name = varexp, variable = x, auxiliary_model_name = toto, horizon = 1, discount = beta) ;
var_expectation_model(model_name = varexp, expression = x, auxiliary_model_name = toto, horizon = 1, discount = beta) ;
model;

View File

@ -36,7 +36,7 @@ var_model(model_name = toto, eqtags = [ 'X' 'Y' 'Z' ]);
** where the sum is over h=0,, and the conditional expectations are computed with VAR model `var_model_name`.
*/
var_expectation_model(model_name = varexp, variable = x, auxiliary_model_name = toto, horizon = 2, discount = beta) ;
var_expectation_model(model_name = varexp, expression = x, auxiliary_model_name = toto, horizon = 2, discount = beta) ;
model;

View File

@ -36,7 +36,7 @@ var_model(model_name = toto, eqtags = [ 'X' 'Y' 'Z' ]);
** where the sum is over h=0,, and the conditional expectations are computed with VAR model `var_model_name`.
*/
var_expectation_model(model_name = varexp, variable = x, auxiliary_model_name = toto, horizon = 0:Inf, discount = beta) ;
var_expectation_model(model_name = varexp, expression = x, auxiliary_model_name = toto, horizon = 0:Inf, discount = beta) ;
model;

View File

@ -36,7 +36,7 @@ var_model(model_name = toto, eqtags = [ 'X' 'Y' 'Z' ]);
** where the sum is over h=0,, and the conditional expectations are computed with VAR model `var_model_name`.
*/
var_expectation_model(model_name = varexp, variable = x, auxiliary_model_name = toto, horizon = 0:500, discount = beta) ;
var_expectation_model(model_name = varexp, expression = x, auxiliary_model_name = toto, horizon = 0:500, discount = beta) ;
model;

View File

@ -36,7 +36,7 @@ var_model(model_name = toto, eqtags = [ 'X' 'Y' 'Z' ]);
** where the sum is over h=0,, and the conditional expectations are computed with VAR model `var_model_name`.
*/
var_expectation_model(model_name = varexp, variable = x, auxiliary_model_name = toto, horizon = 15:50, discount = beta) ;
var_expectation_model(model_name = varexp, expression = x, auxiliary_model_name = toto, horizon = 15:50, discount = beta) ;
model;
[ name = 'X' ]

View File

@ -47,7 +47,7 @@ trend_component_model(model_name=toto, eqtags=['eq:x1', 'eq:x2', 'eq:x1bar', 'eq
** where the sum is over h=0,, and the conditional expectations are computed with VAR model `var_model_name`.
*/
var_expectation_model(model_name = varexp, variable = x1, auxiliary_model_name = toto, horizon = 15:50, discount = beta) ;
var_expectation_model(model_name = varexp, expression = x1, auxiliary_model_name = toto, horizon = 15:50, discount = beta) ;
model;
@ -81,14 +81,29 @@ end
var_expectation.print('varexp');
shocks;
var ex1 = .01;
var ex2 = .01;
var ex1bar = .02;
var ex2bar = .02;
end;
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);
ynames = M_.endo_names;
save('example.mat', 'residual', 'g1');
initialconditions =zeros(3,5);
initialconditions(3,1) = .10; % foo(-1)
initialconditions(3,2) = .20; % x1(-1)
initialconditions(2,2) = .22; % x1(-2)
initialconditions(1,2) = .24; % x1(-3)
initialconditions(3,3) = .30; % x2(-1)
initialconditions(2,3) = .32; % x2(-2)
initialconditions(1,3) = .34; % x2(-3)
initialconditions(3,4) = .25; % x1bar(-1)
initialconditions(3,5) = .25; % x2bar(-1)
initialconditions = ...
dseries(initialconditions, dates('2000Q1'), {'foo', 'x1','x2', 'x1bar', 'x2bar'});
set_dynare_seed('default');
ts = simul_backward_model(initialconditions, 100);
foo = ts.foo.data;
save('example.mat', 'foo');
end;

View File

@ -45,23 +45,32 @@ foo = .5*foo(-1) +
;
end;
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);
shocks;
var ex1 = .01;
var ex2 = .01;
var ex1bar = .02;
var ex2bar = .02;
end;
if max(abs(example.residual-residual))>1e-8
error('Residuals do not match!')
end
if max(max(abs(example.g1-g1)))>1e-8
error('Jacobian matrices do not match!')
end
delete('example.mat');
verbatim;
initialconditions =zeros(3,5);
initialconditions(3,1) = .10; % foo(-1)
initialconditions(3,2) = .20; % x1(-1)
initialconditions(2,2) = .22; % x1(-2)
initialconditions(1,2) = .24; % x1(-3)
initialconditions(3,3) = .30; % x2(-1)
initialconditions(2,3) = .32; % x2(-2)
initialconditions(1,3) = .34; % x2(-3)
initialconditions(3,4) = .25; % x1bar(-1)
initialconditions(3,5) = .25; % x2bar(-1)
initialconditions = ...
dseries(initialconditions, dates('2000Q1'), {'foo', 'x1','x2', 'x1bar', 'x2bar'});
set_dynare_seed('default');
ts = simul_backward_model(initialconditions, 100);
ex = load('example.mat');
delete('example.mat')
if max(abs(ex.foo-ts.foo.data))>1e-12
error('Simulations do not match!')
end
end;

View File

@ -17,7 +17,7 @@ beta = 1/(1+.02);
var_model(model_name = toto, eqtags = [ 'X' 'Y' 'Z' ]);
// Define a VAR_EXPECTATION_MODEL
var_expectation_model(model_name = varexp, variable = x, auxiliary_model_name = toto, horizon = 1, discount = beta) ;
var_expectation_model(model_name = varexp, expression = diff(x), auxiliary_model_name = toto, horizon = 1, discount = beta) ;
model;
@ -40,13 +40,26 @@ var_expectation.update('varexp');
// Print expanded VAR_EXPECTATION expression in a file (to be included in substitution.mod).
var_expectation.print('varexp');
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');
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, 100);
foo = ts.foo.data;
save('example.mat', 'foo');
end;

View File

@ -31,23 +31,31 @@ foo = .5*foo(-1) +
;
end;
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];
[residual, g1] = substitution.dynamic(y, x', M_.params, oo_.steady_state, 1);
example = load('example.mat');
shocks;
var e_x = .01;
var e_y = .01;
var e_z = .01;
end;
if max(abs(example.residual-residual))>1e-8
error('Residuals do not match!')
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, 100);
ex = load('example.mat');
end;
delete('example.mat')
if max(abs(ex.foo-ts.foo.data))>1e-12
error('Simulations do not match!')
end
if max(max(abs(example.g1-g1)))>1e-8
error('Jacobian matrices do not match!')
end
delete('example.mat')

View File

@ -17,7 +17,7 @@ beta = 1/(1+.02);
var_model(model_name = toto, eqtags = [ 'X' 'Y' 'Z' ]);
// Define a VAR_EXPECTATION_MODEL
var_expectation_model(model_name = varexp, variable = x, auxiliary_model_name = toto, horizon = 1, discount = beta) ;
var_expectation_model(model_name = varexp, expression = diff(log(x)), auxiliary_model_name = toto, horizon = 1, discount = beta) ;
model;
[ name = 'X' ]
@ -30,22 +30,34 @@ log(y) = d*log(y(-2)) + e*diff(z(-1)) + e_y;
foo = .5*foo(-1) + 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;
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');
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, 100);
foo = ts.foo.data;
save('example.mat', 'foo');
end;

View File

@ -28,23 +28,30 @@ foo = .5*foo(-1) +
;
end;
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];
[residual, g1] = substitution.dynamic(y, x', M_.params, oo_.steady_state, 1);
example = load('example.mat');
shocks;
var e_x = .01;
var e_y = .01;
var e_z = .01;
end;
if max(abs(example.residual-residual))>1e-8
error('Residuals do not match!')
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, 100);
ex = load('example.mat');
end;
if max(max(abs(example.g1-g1)))>1e-8
error('Jacobian matrices do not match!')
end
delete('example.mat')
delete('example.mat')
if max(abs(ex.foo-ts.foo.data))>1e-12
error('Simulations do not match!')
end

View File

@ -0,0 +1,82 @@
// --+ options: stochastic,transform_unary_ops,json=compute +--
var foo x1 x2 x1bar x2bar;
varexo ex1 ex2 ex1bar ex2bar;
parameters a_x1_0 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
beta
alpha ;
a_x1_0 = -.9;
a_x1_0_ = -.8;
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 = 1/(1+.02);
alpha = .5;
// Define a TREND_COMPONENT model from a subset of equations in the model block.
trend_component_model(model_name=toto, eqtags=['eq:x1', 'eq:x2', 'eq:x1bar', 'eq:x2bar'], targets=['eq:x1bar', 'eq:x2bar']);
/* Define a VAR_EXPECTATION_MODEL
** ------------------------------
**
** model_name: the name of the VAR_EXPECTATION_MODEL (mandatory).
** auxiliary_model_name: the name of the VAR model used for the expectations (mandatory).
** variable: the name of the variable to be forecasted (mandatory).
** horizon: the horizon forecast (mandatory).
** discount: the discount factor, which can be a value or a declared parameter (default is 1.0, no discounting).
**
**
** The `horizon` parameter can be an integer in which case the (discounted) `horizon` step ahead forecast
** is computed using the VAR model `var_model_name`. Alternatively, `horizon` can be a range. In this case
** VAR_EXPECTATION_MODEL returns a discounted sum of expected values. If `horizon` is set equal to the range
** 0:Inf, then VAR_EXPECTATION_MODEL computes:
**
** βʰ E[y]
**
** where the sum is over h=0,, and the conditional expectations are computed with VAR model `var_model_name`.
*/
var_expectation_model(model_name = varexp, expression = x1/100-alpha*x2, auxiliary_model_name = toto, horizon = 15:50, discount = beta) ;
model;
[name='eq:x1', data_type='nonstationary']
diff(x1) = a_x1_0*(x1(-1)-x1bar(-1))+a_x1_0_*(x2(-1)-x2bar(-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', data_type='nonstationary']
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', data_type='nonstationary']
x1bar = x1bar(-1) + ex1bar;
[name='eq:x2bar', data_type='nonstationary']
x2bar = x2bar(-1) + ex2bar;
foo = .5*foo(-1) + 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');
weights = M_.params(M_.var_expectation.varexp.param_indices);
if ~all(weights(1:6)) || ~all(weights(9:10)) || weights(7) || weights(8) || weights(11) || weights(12)
error('Wrong reduced form parameter for VAR_EXPECTATION_MODEL')
end