Verified Commit fe63082c authored by Stéphane Adjemian's avatar Stéphane Adjemian
Browse files

Add lagged information set in VAR based expectations.

parent 87b1d5bd
......@@ -84,6 +84,18 @@ if discountfactor>1
error('The discount cannot be greater than one.')
end
% Set timeshift
if isfield(varexpectationmodel, 'time_shift')
timeshift = varexpectationmodel.time_shift;
else
timeshift = 0;
end
% Throw an error if timeshift is positive
if timeshift>0
error('Option time_shift of the var_expectation command cannot be positive.')
end
% Set variables_id in VAR model
m = length(varexpectationmodel.expr.vars);
variables_id_in_var = NaN(m,1);
......@@ -148,11 +160,17 @@ if length(horizon)==1
elseif horizon>1
parameters = alpha*mpower(discountfactor*CompanionMatrix, varexpectationmodel.horizon);
end
if timeshift<0
parameters = parameters*mpower(CompanionMatrix, -timeshift);
end
else
% Compute the reduced form parameters of the discounted sum of forecasts between t+horizon(1) and
% t+horizon(2). Not that horzizon(2) need not be finite.
if horizon(1)==0 && isinf(horizon(2))
parameters = alpha/(eye(n)-discountfactor*CompanionMatrix);
if timeshift<0
parameters = parameters*mpower(CompanionMatrix, -timeshift);
end
elseif horizon(1)>0 && isinf(horizon(2))
% Define the discounted companion matrix
DiscountedCompanionMatrix = discountfactor*CompanionMatrix;
......@@ -162,8 +180,14 @@ else
tmp1 = tmp1 + mpower(DiscountedCompanionMatrix, h);
end
tmp1 = alpha*tmp1;
if timeshift<0
tmp1 = tmp1*mpower(CompanionMatrix, -timeshift);
end
% Second compute the parameters implied by the discounted sum from h=0 to h=Inf
tmp2 = alpha/(eye(n)-DiscountedCompanionMatrix);
if timeshift<0
tmp2 = tmp2*mpower(CompanionMatrix, -timeshift);
end
% Finally
parameters = tmp2-tmp1;
elseif isfinite(horizon(2))
......@@ -174,6 +198,9 @@ else
tmp = tmp + mpower(DiscountedCompanionMatrix, h);
end
parameters = alpha*tmp;
if timeshift<0
parameters = parameters*mpower(CompanionMatrix, -timeshift);
end
end
end
......
function print_equations(variable_name, withexpansion)
function str = print_equations(variable_name, withexpansion)
% Prints equations where the variable appears in.
%
......@@ -84,6 +84,9 @@ for it = 1:length(M_.mapping.(variable_name).eqidx)
withexpansion = false;
end
end
if nargout
str = sprintf('%s = %s;\n', model{M_.mapping.(variable_name).eqidx(it)}.lhs, rhs);
end
fprintf('%s = %s;\n', model{M_.mapping.(variable_name).eqidx(it)}.lhs, rhs);
end
......
......@@ -74,6 +74,10 @@ end
id = 0;
if isequal(expectationmodelkind, 'var')
timeindices = (0:(maxlag-1))+abs(expectationmodel.time_shift);
end
for i=1:maxlag
for j=1:length(auxmodel.list_of_variables_in_companion_var)
id = id+1;
......@@ -114,8 +118,8 @@ for i=1:maxlag
end
switch expectationmodelkind
case 'var'
if i>1
variable = sprintf('%s(-%d)', variable, i-1);
if timeindices(i)
variable = sprintf('%s(-%d)', variable, timeindices(i));
end
case 'pac'
variable = sprintf('%s(-%d)', variable, i);
......
......@@ -440,12 +440,19 @@ MODFILES = \
ECB_MODFILES = \
var-expectations/1/example1.mod \
var-expectations/1-with-time-shift-a/example1.mod \
var-expectations/2/example1.mod \
var-expectations/2-with-time-shift/example1.mod \
var-expectations/3/example1.mod \
var-expectations/3-with-time-shift/example1.mod \
var-expectations/4/example1.mod \
var-expectations/4-with-time-shift/example1.mod \
var-expectations/5/example1.mod \
var-expectations/5-with-time-shift/example1.mod \
var-expectations/6/example1.mod \
var-expectations/6/substitution.mod \
var-expectations/6-with-time-shift/example1.mod \
var-expectations/6-with-time-shift/substitution.mod \
var-expectations/7/example1.mod \
var-expectations/7/substitution.mod \
var-expectations/8/example1.mod \
......@@ -588,7 +595,8 @@ XFAIL_MODFILES = ramst_xfail.mod \
estimation/tune_mh_jscale/fs2000_1_xfail.mod \
estimation/tune_mh_jscale/fs2000_2_xfail.mod \
estimation/no_init_estimation_check_first_obs/fs2000_init_check_XFAIL.mod \
estimation/no_init_estimation_check_first_obs/fs2000_init_check_XFAIL2.mod
estimation/no_init_estimation_check_first_obs/fs2000_init_check_XFAIL2.mod \
var-expectations/1-with-time-shift-b/example1.mod
MFILES = histval_initval_file/ramst_initval_file_data.m
......@@ -821,6 +829,8 @@ kalman/likelihood_from_dynare/fs2000ns_corr_ME_missing.o.trs: kalman/likelihood_
var-expectations/6/substitution.m.trs: var-expectations/6/example1.m.trs
var-expectations/6/substitution.o.trs: var-expectations/6/example1.o.trs
var-expectations/6-with-time-shift/substitution.m.trs: var-expectations/6-with-time-shift/example1.m.trs
var-expectations/6-with-time-shift/substitution.o.trs: var-expectations/6-with-time-shift/example1.o.trs
var-expectations/7/substitution.m.trs: var-expectations/7/example1.m.trs
var-expectations/7/substitution.o.trs: var-expectations/7/example1.o.trs
var-expectations/8/substitution.m.trs: var-expectations/8/example1.m.trs
......@@ -839,6 +849,9 @@ lmmcp/sw_newton.o.trs: lmmcp/sw_lmmcp.o.trs
var-expectations/4/example1.m.trs: var-expectations/3/example1.m.trs
var-expectations/4/example1.o.trs: var-expectations/3/example1.o.trs
var-expectations/4-with-time-shift/example1.m.trs: var-expectations/3-with-time-shift/example1.m.trs
var-expectations/4-with-time-shift/example1.o.trs: var-expectations/3-with-time-shift/example1.o.trs
trend-component-and-var-models/tcm7.m.trs: trend-component-and-var-models/tcm6.m.trs
trend-component-and-var-models/tcm7.o.trs: trend-component-and-var-models/tcm6.o.trs
trend-component-and-var-models/tcm8.m.trs: trend-component-and-var-models/tcm6.m.trs
......
// --+ 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
** ------------------------------
**
** model_name: the name of the VAR_EXPECTATION_MODEL (mandatory).
** var_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).
** time_shift: shifts the information set to the past, must be a non positive scalar. By default, expectations
** about `variable` in period `t+horizon` are formed in period t (time_shift=0)
**
**
** 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 = x, auxiliary_model_name = toto, horizon = 1, time_shift=-1, discount = beta) ;
model;
[ name = 'X' ]
x = a*x(-1) + b*x(-2) + c*z(-2) + e_x;
[ name = 'Z' ]
z = f*z(-1) + e_z;
[ name = 'Y' ]
y = d*y(-2) + e*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');
if ~isfield(M_.var_expectation.varexp, 'time_shift') || ~isequal(M_.var_expectation.varexp.time_shift, -1)
error('Preprocessor does not honour time_shift option as expected.')
end
// --+ 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
** ------------------------------
**
** model_name: the name of the VAR_EXPECTATION_MODEL (mandatory).
** var_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).
** time_shift: shifts the information set to the past, must be a non positive scalar. By default, expectations
** about `variable` in period `t+horizon` are formed in period t (time_shift=0)
**
**
** 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 = x, auxiliary_model_name = toto, horizon = 1, time_shift=1, discount = beta) ;
model;
[ name = 'X' ]
x = a*x(-1) + b*x(-2) + c*z(-2) + e_x;
[ name = 'Z' ]
z = f*z(-1) + e_z;
[ name = 'Y' ]
y = d*y(-2) + e*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
try
var_expectation.update('varexp');
if isfield(M_.var_expectation.varexp, 'time_shift')
error('Preprocessor should not allow positive values for time_shift option.')
end
catch
end
// --+ 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
** ------------------------------
**
** model_name: the name of the VAR_EXPECTATION_MODEL (mandatory).
** var_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).
** time_shift: shifts the information set to the past, must be a non positive scalar. By default, expectations
** about `variable` in period `t+horizon` are formed in period t (time_shift=0)
**
**
** 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 = x, auxiliary_model_name = toto, time_shift=-2, horizon = 2, discount = beta) ;
model;
[ name = 'X' ]
x = a*x(-1) + b*x(-2) + c*z(-2) + e_x;
[ name = 'Z' ]
z = f*z(-1) + e_z;
[ name = 'Y' ]
y = d*y(-2) + e*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 equations where the variable appears in
fprintf('x is in: \n')
print_equations('x')
fprintf('\n')
fprintf('y is in: \n')
str = print_equations('y', true);
fprintf('\n')
if ~isfield(M_.var_expectation.varexp, 'time_shift') || ~isequal(M_.var_expectation.varexp.time_shift, -2)
error('Preprocessor does not honour time_shift option as expected.')
end
str = strrep(str, 'foo = .5*foo(-1)', '');
str = strrep(str, '+ var_expectation_model_varexp_x_0*x(-2)', '');
str = strrep(str, '+ var_expectation_model_varexp_y_0*y(-2)', '');
str = strrep(str, '+ var_expectation_model_varexp_z_0*z(-2)', '');
str = strrep(str, '+ var_expectation_model_varexp_x_1*x(-3)', '');
str = strrep(str, '+ var_expectation_model_varexp_y_1*y(-3)', '');
str = strrep(str, '+ var_expectation_model_varexp_z_1*z(-3)', '');
str = strrep(str, ';', '');
if ~isempty(strtrim(str))
error('Printed equation is wrong.')
end
// --+ 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
** ------------------------------
**
** model_name: the name of the VAR_EXPECTATION_MODEL (mandatory).
** var_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).
** time_shift: shifts the information set to the past, must be a non positive scalar. By default, expectations
** about `variable` in period `t+horizon` are formed in period t (time_shift=0)
**
** 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 = x, auxiliary_model_name = toto, time_shift=-1, horizon = 0:Inf, discount = beta) ;
model;
[ name = 'X' ]
x = a*x(-1) + b*x(-2) + c*z(-2) + e_x;
[ name = 'Z' ]
z = f*z(-1) + e_z;
[ name = 'Y' ]
y = d*y(-2) + e*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');
/*
** REMARK The VAR model is such that x depends on past values of x
** (xₜ₋₁ and xₜ₋₂) and on zₜ₋₂. Consequently the reduced
** form parameters associated to yₜ₋₁, yₜ₋₂ have to be zero.
*/
weights = M_.params(M_.var_expectation.varexp.param_indices);
if weights(2) || ~weights(3) || weights(5) || ~weights(1) || ~weights(4) || ~weights(6)
error('Wrong reduced form parameter for VAR_EXPECTATION_MODEL')
end
save('weights.mat','weights');
......@@ -68,4 +68,4 @@ if weights(2) || ~weights(3) || weights(5) || ~weights(1) || ~weights(4) || ~wei
error('Wrong reduced form parameter for VAR_EXPECTATION_MODEL')
end
save('weights.mat','weights');
\ No newline at end of file
save('weights.mat','weights');
// --+ 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
** ------------------------------
**
** model_name: the name of the VAR_EXPECTATION_MODEL (mandatory).
** var_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).
** time_shift: shifts the information set to the past, must be a non positive scalar. By default, expectations
** about `variable` in period `t+horizon` are formed in period t (time_shift=0)
**
** 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 = x, auxiliary_model_name = toto, time_shift=-1, horizon = 0:500, discount = beta) ;
model;
[ name = 'X' ]
x = a*x(-1) + b*x(-2) + c*z(-2) + e_x;
[ name = 'Z' ]
z = f*z(-1) + e_z;
[ name = 'Y' ]
y = d*y(-2) + e*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');
/*
** REMARK The VAR model is such that x depends on past values of x
** (xₜ₋₁ and xₜ₋₂) and on zₜ₋₂. Consequently the reduced
** form parameters associated to yₜ₋₁, yₜ₋₂ have to be zero.
*/
weights = M_.params(M_.var_expectation.varexp.param_indices);
if weights(2) || ~weights(3) || weights(5) || ~weights(1) || ~weights(4) || ~weights(6)
error('Wrong reduced form parameter for VAR_EXPECTATION_MODEL')
end
WEIGHTS = load('../3-with-time-shift/weights.mat');
if max(abs(weights-WEIGHTS.weights))>1e-12
error('Inconsistent results in var-expectations/3-with-time-shift and var-expectations/4-with-time-shift.')
end
delete('../3-with-time-shift/weights.mat')
// --+ 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
** ------------------------------
**
** model_name: the name of the VAR_EXPECTATION_MODEL (mandatory).
** var_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).
** time_shift: shifts the information set to the past, must be a non positive scalar. By default, expectations
** about `variable` in period `t+horizon` are formed in period t (time_shift=0)
**
** 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 = x, auxiliary_model_name = toto, time_shift=-2, horizon = 15:50, discount = beta) ;
model;
[ name = 'X' ]
x = a*x(-1) + b*x(-2) + c*z(-2) + e_x;
[ name = 'Z' ]
z = f*z(-1) + e_z;
[ name = 'Y' ]
y = d*y(-2) + e*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');
/*
** REMARK The VAR model is such that x depends on past values of x
** (xₜ₋₁ and xₜ₋₂) and on zₜ₋₂. Consequently the reduced
** form parameters associated to yₜ₋₁, yₜ₋₂ have to be zero.
*/
weights = M_.params(M_.var_expectation.varexp.param_indices);
if weights(2) || ~weights(3) || weights(5) || ~weights(1) || ~weights(4) || ~weights(6)
error('Wrong reduced form parameter for VAR_EXPECTATION_MODEL')
end
// --+ 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 ;
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);
// Define a TREND_COMPONENT model from a subset of equations in the model block.