diff --git a/matlab/+var_expectation/+update/parameters.m b/matlab/+var_expectation/+update/parameters.m index e3dae46e3..bb62649e9 100644 --- a/matlab/+var_expectation/+update/parameters.m +++ b/matlab/+var_expectation/+update/parameters.m @@ -44,23 +44,23 @@ end varexpectationmodel = DynareModel.var_expectation.(varexpectationmodelname); % Get the name of the associated VAR model and test its existence. -if ~isfield(DynareModel.var, varexpectationmodel.auxiliary_model_name) +if ~isfield(DynareModel.(varexpectationmodel.auxiliary_model_type), varexpectationmodel.auxiliary_model_name) error('Unknown VAR (%s) in VAR_EXPECTATION_MODEL (%s)!', varexpectationmodel.auxiliary_model_name, varexpectationmodelname) end -varmodel = DynareModel.var.(varexpectationmodel.auxiliary_model_name); +auxmodel = DynareModel.(varexpectationmodel.auxiliary_model_type).(varexpectationmodel.auxiliary_model_name); % Check that we have the values of the VAR matrices. -if ~isfield(DynareOutput.var, varexpectationmodel.auxiliary_model_name) - error('VAR model %s has to be estimated or calibrated first!', varexpectationmodel.auxiliary_model_name) +if ~isfield(DynareOutput.(varexpectationmodel.auxiliary_model_type), varexpectationmodel.auxiliary_model_name) + error('Auxiliary model %s has to be estimated or calibrated first!', varexpectationmodel.auxiliary_model_name) end -varcalib = DynareOutput.var.(varexpectationmodel.auxiliary_model_name); +auxcalib = DynareOutput.(varexpectationmodel.auxiliary_model_type).(varexpectationmodel.auxiliary_model_name); -if ~isfield(varcalib, 'CompanionMatrix') || any(isnan(varcalib.CompanionMatrix(:))) - message = sprintf('VAR model %s has to be estimated first.', varexpectationmodel.auxiliary_model_name); - message = sprintf('s\nPlease use get_companion_matrix command first.', message); - error(message) +if ~isfield(auxcalib, 'CompanionMatrix') || any(isnan(auxcalib.CompanionMatrix(:))) + message = sprintf('Auxiliary model %s has to be estimated first.', varexpectationmodel.auxiliary_model_name); + message = sprintf('%s\nPlease use get_companion_matrix command first.', message); + error(message); end % Set discount factor @@ -85,7 +85,7 @@ if discountfactor>1 end % Set variable_id in VAR model -variable_id_in_var = find(varexpectationmodel.variable_id==varmodel.lhs); +variable_id_in_var = find(varexpectationmodel.variable_id==auxmodel.lhs); % Get the horizon parameter. horizon = varexpectationmodel.horizon; @@ -121,7 +121,7 @@ if wrong_horizon_parameter end % Get the companion matrix -CompanionMatrix = varcalib.CompanionMatrix; +CompanionMatrix = auxcalib.CompanionMatrix; % Get the dimension of the problem. n = length(CompanionMatrix); diff --git a/matlab/+var_expectation/initialize.m b/matlab/+var_expectation/initialize.m new file mode 100644 index 000000000..06f40eab3 --- /dev/null +++ b/matlab/+var_expectation/initialize.m @@ -0,0 +1,45 @@ +function initialize(varexpectationmodel) + +% Initialize a VAR_EXPECTATION_MODEL. +% +% INPUTS +% - varepxpectationmodel [string] Name of the VAR expectation model. +% +% OUTPUTS +% None +% +% SPECIAL REQUIREMENTS +% none + +% Copyright (C) 2018 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 . + +global M_ + +auxiliary_model_name = M_.var_expectation.(varexpectationmodel).auxiliary_model_name; + +if isfield(M_, 'var') && isfield(M_.var, auxiliary_model_name) + auxiliary_model_type = 'var'; +elseif isfield(M_, 'trend_component') && isfield(M_.trend_component, auxiliary_model_name) + auxiliary_model_type = 'trend_component'; +else + error('Unknown type of auxiliary model.') +end + +M_.var_expectation.(varexpectationmodel).auxiliary_model_type = auxiliary_model_type; + +get_companion_matrix(auxiliary_model_name, auxiliary_model_type); \ No newline at end of file diff --git a/tests/var-expectations/1/example.mod b/tests/var-expectations/1/example.mod index b534c4265..6b78d9acf 100644 --- a/tests/var-expectations/1/example.mod +++ b/tests/var-expectations/1/example.mod @@ -51,9 +51,8 @@ foo = .5*foo(-1) + var_expectation(varexp); end; -// Build the companion matrix of the VAR model (toto). -get_companion_matrix('toto'); - +// 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'); diff --git a/tests/var-expectations/2/example.mod b/tests/var-expectations/2/example.mod index 8b75989b1..236a55085 100644 --- a/tests/var-expectations/2/example.mod +++ b/tests/var-expectations/2/example.mod @@ -51,9 +51,8 @@ foo = .5*foo(-1) + var_expectation(varexp); end; -// Build the companion matrix of the VAR model (toto). -get_companion_matrix('toto'); - +// 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'); diff --git a/tests/var-expectations/3/example.mod b/tests/var-expectations/3/example.mod index ffe8a3d42..1b65b6df6 100644 --- a/tests/var-expectations/3/example.mod +++ b/tests/var-expectations/3/example.mod @@ -50,10 +50,8 @@ y = d*y(-2) + e*z(-1) + e_y; foo = .5*foo(-1) + var_expectation(varexp); end; - -// Build the companion matrix of the VAR model (toto). -get_companion_matrix('toto'); - +// 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'); diff --git a/tests/var-expectations/4/example.mod b/tests/var-expectations/4/example.mod index f356696ad..b05049f9f 100644 --- a/tests/var-expectations/4/example.mod +++ b/tests/var-expectations/4/example.mod @@ -50,10 +50,8 @@ y = d*y(-2) + e*z(-1) + e_y; foo = .5*foo(-1) + var_expectation(varexp); end; - -// Build the companion matrix of the VAR model (toto). -get_companion_matrix('toto'); - +// 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'); diff --git a/tests/var-expectations/5/example.mod b/tests/var-expectations/5/example.mod index 1a5a4e515..010eceb92 100644 --- a/tests/var-expectations/5/example.mod +++ b/tests/var-expectations/5/example.mod @@ -38,7 +38,6 @@ var_model(model_name = toto, eqtags = [ 'X' 'Y' 'Z' ]); var_expectation_model(model_name = varexp, variable = x, auxiliary_model_name = toto, horizon = 15:50, discount = beta) ; - model; [ name = 'X' ] x = a*x(-1) + b*x(-2) + c*z(-2) + e_x; @@ -50,10 +49,8 @@ y = d*y(-2) + e*z(-1) + e_y; foo = .5*foo(-1) + var_expectation(varexp); end; - -// Build the companion matrix of the VAR model (toto). -get_companion_matrix('toto'); - +// 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'); diff --git a/tests/var-expectations/6/example.mod b/tests/var-expectations/6/example.mod new file mode 100644 index 000000000..b36299178 --- /dev/null +++ b/tests/var-expectations/6/example.mod @@ -0,0 +1,86 @@ +// --+ options: stochastic,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. +trend_component_model(model_name=toto, eqtags=['eq:x1', 'eq:x2', 'eq:x1bar', 'eq:x2bar'], trends=['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, variable = x1bar, 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'); + +/* +** 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 \ No newline at end of file