2014-01-03 12:09:39 +01:00
function imcforecast ( constrained_paths, constrained_vars, options_cond_fcst)
2018-09-26 22:47:32 +02:00
2009-10-22 15:03:30 +02:00
% Computes conditional forecasts.
%
% INPUTS
2020-06-24 21:36:17 +02:00
% - constrained_paths [double] m*p array, where m is the number of constrained endogenous variables and p is the number of constrained periods.
2018-09-26 22:47:32 +02:00
% - constrained_vars [integer] m*1 array, indices in M_.endo_names of the constrained variables.
% - options_cond_fcst [structure] containing the options. The fields are:
%
% + replic [integer] scalar, number of monte carlo simulations.
% + parameter_set [char] values of the estimated parameters:
% 'posterior_mode',
% 'posterior_mean',
% 'posterior_median',
% 'prior_mode' or
% 'prior mean'.
% [double] np*1 array, values of the estimated parameters.
% + controlled_varexo [cell] m*1 cell of row char array, list of controlled exogenous variables.
% + conf_sig [double] scalar in [0,1], probability mass covered by the confidence bands.
2009-10-22 15:03:30 +02:00
%
% OUTPUTS
2018-09-26 22:47:32 +02:00
% None.
2017-05-16 15:10:20 +02:00
%
2009-10-22 15:03:30 +02:00
% SPECIAL REQUIREMENTS
2018-09-26 22:47:32 +02:00
% This routine has to be called after an estimation statement or an estimated_params block.
2009-10-22 15:03:30 +02:00
%
% REMARKS
2019-11-29 11:13:21 +01:00
% [1] Results are stored in oo_.conditional_forecast.
2018-09-26 22:47:32 +02:00
% [2] Use the function plot_icforecast to plot the results.
2008-08-01 20:53:30 +02:00
2020-02-13 18:20:07 +01:00
% Copyright (C) 2006-2020 Dynare Team
2008-08-01 20:53:30 +02:00
%
% 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
2021-06-09 17:33:48 +02:00
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
2008-08-01 20:53:30 +02:00
2016-12-15 10:36:16 +01:00
global options_ oo_ M_ bayestopt_ estim_params_
2006-12-15 12:37:24 +01:00
2011-07-30 20:19:15 +02:00
if ~ isfield ( options_cond_fcst , ' parameter_set' ) || isempty ( options_cond_fcst . parameter_set )
2016-06-07 10:11:49 +02:00
if isfield ( oo_ , ' posterior_mode' )
options_cond_fcst . parameter_set = ' posterior_mode' ;
elseif isfield ( oo_ , ' mle_mode' )
options_cond_fcst . parameter_set = ' mle_mode' ;
else
error ( ' No valid parameter set found' )
end
2006-12-15 12:37:24 +01:00
end
2009-12-16 18:17:34 +01:00
2009-10-22 17:56:32 +02:00
if ~ isfield ( options_cond_fcst , ' replic' ) || isempty ( options_cond_fcst . replic )
2009-10-22 15:03:30 +02:00
options_cond_fcst . replic = 5000 ;
end
2009-10-22 17:56:32 +02:00
if ~ isfield ( options_cond_fcst , ' periods' ) || isempty ( options_cond_fcst . periods )
options_cond_fcst . periods = 40 ;
2009-10-22 15:03:30 +02:00
end
2016-07-21 12:17:30 +02:00
if ~ isfield ( options_cond_fcst , ' conditional_forecast' ) || ~ isfield ( options_cond_fcst . conditional_forecast , ' conf_sig' ) || isempty ( options_cond_fcst . conditional_forecast . conf_sig )
options_cond_fcst . conditional_forecast . conf_sig = . 8 ;
2006-12-15 12:37:24 +01:00
end
2011-07-30 20:19:15 +02:00
if isequal ( options_cond_fcst . parameter_set , ' calibration' )
estimated_model = 0 ;
else
estimated_model = 1 ;
2009-10-22 15:03:30 +02:00
end
2011-07-30 20:19:15 +02:00
if estimated_model
2016-04-01 11:19:58 +02:00
if options_ . prefilter
error ( ' imcforecast:: Conditional forecasting does not support the prefiltering option' )
end
2011-07-30 20:19:15 +02:00
if ischar ( options_cond_fcst . parameter_set )
switch options_cond_fcst . parameter_set
case ' posterior_mode'
2017-10-03 15:32:17 +02:00
xparam = get_posterior_parameters ( ' mode' , M_ , estim_params_ , oo_ , options_ ) ;
2013-05-19 23:41:59 +02:00
graph_title = ' Posterior Mode' ;
2011-07-30 20:19:15 +02:00
case ' posterior_mean'
2017-10-03 15:32:17 +02:00
xparam = get_posterior_parameters ( ' mean' , M_ , estim_params_ , oo_ , options_ ) ;
2013-05-19 23:41:59 +02:00
graph_title = ' Posterior Mean' ;
2011-07-30 20:19:15 +02:00
case ' posterior_median'
2017-10-03 15:32:17 +02:00
xparam = get_posterior_parameters ( ' median' , M_ , estim_params_ , oo_ , options_ ) ;
2013-05-19 23:41:59 +02:00
graph_title = ' Posterior Median' ;
2016-06-07 10:11:49 +02:00
case ' mle_mode'
2017-10-03 15:32:17 +02:00
xparam = get_posterior_parameters ( ' mode' , M_ , estim_params_ , oo_ , options_ , ' mle_' ) ;
2016-06-07 10:11:49 +02:00
graph_title = ' ML Mode' ;
2011-07-30 20:19:15 +02:00
case ' prior_mode'
xparam = bayestopt_ . p5 ( : ) ;
2013-05-19 23:41:59 +02:00
graph_title = ' Prior Mode' ;
2011-07-30 20:19:15 +02:00
case ' prior_mean'
xparam = bayestopt_ . p1 ;
2013-05-19 23:41:59 +02:00
graph_title = ' Prior Mean' ;
2011-07-30 20:19:15 +02:00
otherwise
disp ( ' imcforecast:: If the input argument is a string, then it has to be equal to:' )
disp ( ' ' ' calibration' ' , ' )
disp ( ' ' ' posterior_mode' ' , ' )
disp ( ' ' ' posterior_mean' ' , ' )
disp ( ' ' ' posterior_median' ' , ' )
disp ( ' ' ' prior_mode' ' or' )
disp ( ' ' ' prior_mean' ' .' )
error ( ' imcforecast:: Wrong argument type!' )
2009-10-22 15:03:30 +02:00
end
else
2011-07-30 20:19:15 +02:00
xparam = options_cond_fcst . parameter_set ;
if length ( xparam ) ~= length ( M_ . params )
error ( ' imcforecast:: The dimension of the vector of parameters doesn' ' t match the number of estimated parameters!' )
end
2009-10-22 15:03:30 +02:00
end
2011-07-30 20:19:15 +02:00
set_parameters ( xparam ) ;
2014-06-26 10:55:54 +02:00
[ dataset_ , dataset_info ] = makedataset ( options_ ) ;
data = transpose ( dataset_ . data ) ;
data_index = dataset_info . missing . aindex ;
gend = dataset_ . nobs ;
missing_value = dataset_info . missing . state ;
2017-05-16 15:10:20 +02:00
2016-10-02 09:26:30 +02:00
%store qz_criterium
qz_criterium_old = options_ . qz_criterium ;
options_ = select_qz_criterium_value ( options_ ) ;
2016-11-14 11:08:02 +01:00
options_smoothed_state_uncertainty_old = options_ . smoothed_state_uncertainty ;
2021-06-22 14:28:46 +02:00
[ atT , ~ , ~ , ~ , ys , ~ , ~ , ~ , ~ , ~ , ~ , ~ , ~ , ~ , M_ , oo_ , bayestopt_ ] = ...
2018-09-26 22:49:41 +02:00
DsgeSmoother ( xparam , gend , data , data_index , missing_value , M_ , oo_ , options_ , bayestopt_ , estim_params_ ) ;
2016-11-14 11:08:02 +01:00
options_ . smoothed_state_uncertainty = options_smoothed_state_uncertainty_old ;
2016-04-01 11:19:58 +02:00
%get constant part
if options_ . noconstant
constant = zeros ( size ( ys , 1 ) , options_cond_fcst . periods + 1 ) ;
else
if options_ . loglinear
constant = repmat ( log ( ys ) , 1 , options_cond_fcst . periods + 1 ) ;
else
constant = repmat ( ys , 1 , options_cond_fcst . periods + 1 ) ;
2011-07-30 20:19:15 +02:00
end
2009-10-06 16:41:56 +02:00
end
2016-04-01 11:19:58 +02:00
%get trend part (which also takes care of prefiltering); needs to
%include the last period
if bayestopt_ . with_trend == 1
[ trend_addition ] = compute_trend_coefficients ( M_ , options_ , size ( bayestopt_ . smoother_mf , 1 ) , gend + options_cond_fcst . periods ) ;
trend_addition = trend_addition ( : , gend : end ) ;
else
trend_addition = zeros ( size ( bayestopt_ . smoother_mf , 1 ) , 1 + options_cond_fcst . periods ) ;
end
% add trend to constant
for obs_iter = 1 : length ( options_ . varobs )
2018-09-26 22:49:41 +02:00
j = strcmp ( options_ . varobs { obs_iter } , M_ . endo_names ) ;
2017-10-10 10:05:59 +02:00
constant ( j , : ) = constant ( j , : ) + trend_addition ( obs_iter , : ) ;
2016-04-01 11:19:58 +02:00
end
trend = constant ( oo_ . dr . order_var , : ) ;
2011-07-30 20:19:15 +02:00
InitState ( : , 1 ) = atT ( : , end ) ;
else
2016-10-02 09:26:30 +02:00
qz_criterium_old = options_ . qz_criterium ;
if isempty ( options_ . qz_criterium )
options_ . qz_criterium = 1 + 1e-6 ;
end
2013-05-26 08:30:47 +02:00
graph_title = ' Calibration' ;
2016-04-01 11:19:58 +02:00
if ~ isfield ( oo_ . dr , ' kstate' )
error ( ' You need to call stoch_simul before conditional_forecast' )
end
2009-10-06 16:41:56 +02:00
end
2017-02-14 12:54:22 +01:00
if options_ . logged_steady_state %if steady state was previously logged, undo this
oo_ . dr . ys = exp ( oo_ . dr . ys ) ;
oo_ . steady_state = exp ( oo_ . steady_state ) ;
options_ . logged_steady_state = 0 ;
end
2021-06-22 14:28:46 +02:00
[ T , R , ys , ~ , M_ , oo_ ] = dynare_resolve ( M_ , options_ , oo_ ) ;
2006-12-15 12:37:24 +01:00
2017-02-14 12:54:22 +01:00
if options_ . loglinear && isfield ( oo_ . dr , ' ys' ) && options_ . logged_steady_state == 0 %log steady state
oo_ . dr . ys = log_variable ( 1 : M_ . endo_nbr , oo_ . dr . ys , M_ ) ;
ys = oo_ . dr . ys ;
oo_ . steady_state = log_variable ( 1 : M_ . endo_nbr , oo_ . steady_state , M_ ) ;
options_ . logged_steady_state = 1 ; %set option for use in stoch_simul
end
2019-03-26 16:49:39 +01:00
if ~ isdiag ( M_ . Sigma_e )
2014-07-18 18:36:02 +02:00
warning ( sprintf ( ' The innovations are correlated (the covariance matrix has non zero off diagonal elements), the results of the conditional forecasts will\ndepend on the ordering of the innovations (as declared after varexo) because a Cholesky decomposition is used to factorize the covariance matrix.\n\n=> It is preferable to declare the correlations in the model block (explicitly imposing the identification restrictions), unless you are satisfied\nwith the implicit identification restrictions implied by the Cholesky decomposition.' ) )
2016-09-02 16:18:02 +02:00
sQ = chol ( M_ . Sigma_e , ' lower' ) ;
else
sQ = sqrt ( M_ . Sigma_e ) ;
2014-07-18 18:36:02 +02:00
end
2006-12-15 12:37:24 +01:00
2014-07-17 10:40:33 +02:00
if ~ estimated_model
2014-12-06 21:40:02 +01:00
if isempty ( M_ . endo_histval )
y0 = ys ;
else
2017-02-14 12:54:22 +01:00
if options_ . loglinear
%make sure that only states are updated (controls have value of 0 in vector)
y0 = zeros ( size ( ys ) ) ;
y0_logged = log_variable ( 1 : M_ . endo_nbr , M_ . endo_histval , M_ ) ;
y0 ( M_ . endo_histval ~= 0 ) = y0_logged ( M_ . endo_histval ~= 0 ) ;
else
y0 = M_ . endo_histval ;
end
2014-12-06 21:40:02 +01:00
end
InitState ( : , 1 ) = y0 ( oo_ . dr . order_var ) - ys ( oo_ . dr . order_var , : ) ; %initial state in deviations from steady state
2014-07-17 10:40:33 +02:00
trend = repmat ( ys ( oo_ . dr . order_var , : ) , 1 , options_cond_fcst . periods + 1 ) ; %trend needs to contain correct steady state
end
2006-12-15 12:37:24 +01:00
NumberOfStates = length ( InitState ) ;
2009-10-22 15:03:30 +02:00
FORCS1 = zeros ( NumberOfStates , options_cond_fcst . periods + 1 , options_cond_fcst . replic ) ;
2006-12-15 12:37:24 +01:00
2014-07-17 10:40:33 +02:00
FORCS1 ( : , 1 , : ) = repmat ( InitState , 1 , options_cond_fcst . replic ) ; %set initial steady state to deviations from steady state in first period
2006-12-15 12:37:24 +01:00
2009-10-02 13:47:56 +02:00
EndoSize = M_ . endo_nbr ;
ExoSize = M_ . exo_nbr ;
2006-12-15 12:37:24 +01:00
2009-10-22 15:03:30 +02:00
n1 = size ( constrained_vars , 1 ) ;
n2 = size ( options_cond_fcst . controlled_varexo , 1 ) ;
2018-09-26 22:49:41 +02:00
constrained_vars = oo_ . dr . inv_order_var ( constrained_vars ) ; % must be in decision rule order
2006-12-15 12:37:24 +01:00
if n1 ~= n2
2018-09-26 22:49:41 +02:00
error ( ' imcforecast:: The number of constrained variables doesn' ' t match the number of controlled shocks' )
2006-12-15 12:37:24 +01:00
end
2018-09-26 22:49:41 +02:00
% Get indices of controlled varexo.
[ ~ , controlled_varexo ] = ismember ( options_cond_fcst . controlled_varexo , M_ . exo_names ) ;
2006-12-15 12:37:24 +01:00
2018-09-26 22:49:41 +02:00
mv = zeros ( n1 , NumberOfStates ) ;
mu = zeros ( ExoSize , n2 ) ;
2006-12-15 12:37:24 +01:00
for i = 1 : n1
2018-09-26 22:49:41 +02:00
mv ( i , constrained_vars ( i ) ) = 1 ;
mu ( controlled_varexo ( i ) , i ) = 1 ;
2006-12-15 12:37:24 +01:00
end
2012-12-10 21:14:00 +01:00
% number of periods with constrained values
cL = size ( constrained_paths , 2 ) ;
2006-12-15 12:37:24 +01:00
2016-04-01 11:19:58 +02:00
%transform constrained periods into deviations from steady state; note that
%trend includes last actual data point and therefore we need to start in
%period 2
2018-09-26 22:49:41 +02:00
constrained_paths = bsxfun ( @ minus , constrained_paths , trend ( constrained_vars , 2 : 1 + cL ) ) ;
2010-04-01 15:27:42 +02:00
2014-07-17 10:40:33 +02:00
FORCS1_shocks = zeros ( n1 , cL , options_cond_fcst . replic ) ;
2011-07-30 20:19:15 +02:00
%randn('state',0);
2006-12-15 12:37:24 +01:00
2014-07-17 10:40:33 +02:00
for b = 1 : options_cond_fcst . replic %conditional forecast using cL set to constrained values
2009-10-22 15:03:30 +02:00
shocks = sQ * randn ( ExoSize , options_cond_fcst . periods ) ;
2018-09-26 22:49:41 +02:00
shocks ( controlled_varexo , : ) = zeros ( n1 , options_cond_fcst . periods ) ;
2014-07-17 10:40:33 +02:00
[ FORCS1 ( : , : , b ) , FORCS1_shocks ( : , : , b ) ] = mcforecast3 ( cL , options_cond_fcst . periods , constrained_paths , shocks , FORCS1 ( : , : , b ) , T , R , mv , mu ) ;
FORCS1 ( : , : , b ) = FORCS1 ( : , : , b ) + trend ; %add trend
2006-12-15 12:37:24 +01:00
end
2020-03-21 12:06:24 +01:00
if max ( max ( max ( abs ( bsxfun ( @ minus , FORCS1 ( constrained_vars , 1 + 1 : 1 + cL , : ) , trend ( constrained_vars , 1 : cL ) + constrained_paths ) ) ) ) ) > 1e-4
2020-02-12 11:04:46 +01:00
fprintf ( ' \nconditional_forecasts: controlling of variables was not successful.\n' )
fprintf ( ' This can be due to numerical imprecision (e.g. explosive simulations)\n' )
fprintf ( ' or because the instrument(s) do not allow controlling the variable(s).\n' )
end
2006-12-15 12:37:24 +01:00
mFORCS1 = mean ( FORCS1 , 3 ) ;
2014-07-17 10:40:33 +02:00
mFORCS1_shocks = mean ( FORCS1_shocks , 3 ) ;
2006-12-15 12:37:24 +01:00
2016-07-21 12:17:30 +02:00
tt = ( 1 - options_cond_fcst . conditional_forecast . conf_sig ) / 2 ;
2020-02-05 21:18:25 +01:00
t1 = max ( 1 , round ( options_cond_fcst . replic * tt ) ) ;
t2 = min ( options_cond_fcst . replic , round ( options_cond_fcst . replic * ( 1 - tt ) ) ) ;
2006-12-15 12:37:24 +01:00
2013-10-15 13:31:11 +02:00
forecasts . controlled_variables = constrained_vars ;
2009-10-22 15:03:30 +02:00
forecasts . instruments = options_cond_fcst . controlled_varexo ;
2006-12-15 12:37:24 +01:00
for i = 1 : EndoSize
2017-10-10 10:05:59 +02:00
forecasts . cond . Mean . ( M_ . endo_names { oo_ . dr . order_var ( i ) } ) = mFORCS1 ( i , : ) ' ;
2019-09-02 18:47:46 +02:00
if size ( FORCS1 , 2 ) > 1
tmp = sort ( squeeze ( FORCS1 ( i , : , : ) ) ' ) ;
else
2019-12-20 16:28:06 +01:00
tmp = sort ( squeeze ( FORCS1 ( i , : , : ) ) ) ;
2019-09-02 18:47:46 +02:00
end
2017-10-10 10:05:59 +02:00
forecasts . cond . ci . ( M_ . endo_names { oo_ . dr . order_var ( i ) } ) = [ tmp ( t1 , : ) ' , tmp ( t2 , : ) ' ] ' ;
2006-12-15 12:37:24 +01:00
end
2014-07-17 10:40:33 +02:00
for i = 1 : n1
2017-10-10 10:05:59 +02:00
forecasts . controlled_exo_variables . Mean . ( options_cond_fcst . controlled_varexo { i } ) = mFORCS1_shocks ( i , : ) ' ;
2019-09-02 18:47:46 +02:00
if size ( FORCS1_shocks , 2 ) > 1
tmp = sort ( squeeze ( FORCS1_shocks ( i , : , : ) ) ' ) ;
else
2019-12-20 16:28:06 +01:00
tmp = sort ( squeeze ( FORCS1_shocks ( i , : , : ) ) ) ;
2019-09-02 18:47:46 +02:00
end
2017-10-10 10:05:59 +02:00
forecasts . controlled_exo_variables . ci . ( options_cond_fcst . controlled_varexo { i } ) = [ tmp ( t1 , : ) ' , tmp ( t2 , : ) ' ] ' ;
2014-07-17 10:40:33 +02:00
end
clear FORCS1 mFORCS1_shocks ;
2006-12-15 12:37:24 +01:00
2009-10-22 15:03:30 +02:00
FORCS2 = zeros ( NumberOfStates , options_cond_fcst . periods + 1 , options_cond_fcst . replic ) ;
2014-07-17 10:40:33 +02:00
FORCS2 ( : , 1 , : ) = repmat ( InitState , 1 , options_cond_fcst . replic ) ; %set initial steady state to deviations from steady state in first period
2006-12-15 12:37:24 +01:00
2014-07-17 10:40:33 +02:00
for b = 1 : options_cond_fcst . replic %conditional forecast using cL set to 0
2009-10-22 15:03:30 +02:00
shocks = sQ * randn ( ExoSize , options_cond_fcst . periods ) ;
2018-09-26 22:49:41 +02:00
shocks ( controlled_varexo , : ) = zeros ( n1 , options_cond_fcst . periods ) ;
2009-10-22 15:03:30 +02:00
FORCS2 ( : , : , b ) = mcforecast3 ( 0 , options_cond_fcst . periods , constrained_paths , shocks , FORCS2 ( : , : , b ) , T , R , mv , mu ) + trend ;
2006-12-15 12:37:24 +01:00
end
mFORCS2 = mean ( FORCS2 , 3 ) ;
for i = 1 : EndoSize
2017-10-10 10:05:59 +02:00
forecasts . uncond . Mean . ( M_ . endo_names { oo_ . dr . order_var ( i ) } ) = mFORCS2 ( i , : ) ' ;
2019-09-02 18:47:46 +02:00
if size ( FORCS2 , 2 ) > 1
tmp = sort ( squeeze ( FORCS2 ( i , : , : ) ) ' ) ;
else
tmp = sort ( squeeze ( FORCS2 ( i , : , : ) ) ) ;
end
2017-10-10 10:05:59 +02:00
forecasts . uncond . ci . ( M_ . endo_names { oo_ . dr . order_var ( i ) } ) = [ tmp ( t1 , : ) ' , tmp ( t2 , : ) ' ] ' ;
2006-12-15 12:37:24 +01:00
end
2018-09-26 22:49:41 +02:00
forecasts . graph . title = graph_title ;
forecasts . graph . fname = M_ . fname ;
2006-12-15 12:37:24 +01:00
2016-10-02 09:26:30 +02:00
%reset qz_criterium
options_ . qz_criterium = qz_criterium_old ;
2019-11-29 11:13:21 +01:00
oo_ . conditional_forecast = forecasts ;