0001 function imcforecast(constrained_paths, constrained_vars, options_cond_fcst)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 global options_ oo_ M_ bayestopt_
0047
0048 if ~isfield(options_cond_fcst,'parameter_set') || isempty(options_cond_fcst.parameter_set)
0049 options_cond_fcst.parameter_set = 'posterior_mode';
0050 end
0051
0052 if ~isfield(options_cond_fcst,'replic') || isempty(options_cond_fcst.replic)
0053 options_cond_fcst.replic = 5000;
0054 end
0055
0056 if ~isfield(options_cond_fcst,'periods') || isempty(options_cond_fcst.periods)
0057 options_cond_fcst.periods = 40;
0058 end
0059
0060 if ~isfield(options_cond_fcst,'conf_sig') || isempty(options_cond_fcst.conf_sig)
0061 options_cond_fcst.conf_sig = .8;
0062 end
0063
0064 if isequal(options_cond_fcst.parameter_set,'calibration')
0065 estimated_model = 0;
0066 else
0067 estimated_model = 1;
0068 end
0069
0070 if estimated_model
0071 if ischar(options_cond_fcst.parameter_set)
0072 switch options_cond_fcst.parameter_set
0073 case 'posterior_mode'
0074 xparam = get_posterior_parameters('mode');
0075 case 'posterior_mean'
0076 xparam = get_posterior_parameters('mean');
0077 case 'posterior_median'
0078 xparam = get_posterior_parameters('median');
0079 case 'prior_mode'
0080 xparam = bayestopt_.p5(:);
0081 case 'prior_mean'
0082 xparam = bayestopt_.p1;
0083 otherwise
0084 disp('imcforecast:: If the input argument is a string, then it has to be equal to:')
0085 disp(' ''calibration'', ')
0086 disp(' ''posterior_mode'', ')
0087 disp(' ''posterior_mean'', ')
0088 disp(' ''posterior_median'', ')
0089 disp(' ''prior_mode'' or')
0090 disp(' ''prior_mean''.')
0091 error('imcforecast:: Wrong argument type!')
0092 end
0093 else
0094 xparam = options_cond_fcst.parameter_set;
0095 if length(xparam)~=length(M_.params)
0096 error('imcforecast:: The dimension of the vector of parameters doesn''t match the number of estimated parameters!')
0097 end
0098 end
0099
0100 set_parameters(xparam);
0101
0102
0103 transformation = [];
0104 if options_.loglinear && ~options_.logdata
0105 transformation = @log;
0106 end
0107 xls.sheet = options_.xls_sheet;
0108 xls.range = options_.xls_range;
0109
0110 if ~isfield(options_,'nobs')
0111 options_.nobs = [];
0112 end
0113
0114 dataset_ = initialize_dataset(options_.datafile,options_.varobs,options_.first_obs,options_.nobs,transformation,options_.prefilter,xls);
0115
0116 data = dataset_.data;
0117 data_index = dataset_.missing.aindex;
0118 gend = options_.nobs;
0119 missing_value = dataset_.missing.state;
0120
0121 [atT,innov,measurement_error,filtered_state_vector,ys,trend_coeff] = DsgeSmoother(xparam,gend,data,data_index,missing_value);
0122
0123 trend = repmat(ys,1,options_cond_fcst.periods+1);
0124 for i=1:M_.endo_nbr
0125 j = strmatch(deblank(M_.endo_names(i,:)),options_.varobs,'exact');
0126 if ~isempty(j)
0127 trend(i,:) = trend(i,:)+trend_coeff(j)*(gend+(0:options_cond_fcst.periods));
0128 end
0129 end
0130 trend = trend(oo_.dr.order_var,:);
0131
0132 InitState(:,1) = atT(:,end);
0133 else
0134 InitState(:,1) = zeros(M_.endo_nbr,1);
0135 trend = repmat(oo_.steady_state(oo_.dr.order_var),1,options_cond_fcst.periods+1);
0136 end
0137
0138 if isempty(options_.qz_criterium)
0139 options_.qz_criterium = 1+1e-6;
0140 end
0141 [T,R,ys,info,M_,options_,oo_] = dynare_resolve(M_,options_,oo_);
0142
0143 sQ = sqrt(M_.Sigma_e);
0144
0145 NumberOfStates = length(InitState);
0146 FORCS1 = zeros(NumberOfStates,options_cond_fcst.periods+1,options_cond_fcst.replic);
0147
0148 FORCS1(:,1,:) = repmat(InitState,1,options_cond_fcst.replic);
0149
0150 EndoSize = M_.endo_nbr;
0151 ExoSize = M_.exo_nbr;
0152
0153 n1 = size(constrained_vars,1);
0154 n2 = size(options_cond_fcst.controlled_varexo,1);
0155
0156 if n1 ~= n2
0157 error(['imcforecast:: The number of constrained variables doesn''t match the number of controlled shocks'])
0158 end
0159
0160 idx = [];
0161 jdx = [];
0162
0163 for i = 1:n1
0164 idx = [idx ; oo_.dr.inv_order_var(constrained_vars(i,:))];
0165 jdx = [jdx ; strmatch(deblank(options_cond_fcst.controlled_varexo(i,:)),M_.exo_names,'exact')];
0166 end
0167 mv = zeros(n1,NumberOfStates);
0168 mu = zeros(ExoSize,n2);
0169 for i=1:n1
0170 mv(i,idx(i)) = 1;
0171 mu(jdx(i),i) = 1;
0172 end
0173
0174 if (size(constrained_paths,2) == 1);
0175 constrained_paths = constrained_paths*ones(1,cL);
0176 else
0177 cL = size(constrained_paths,2);
0178 end
0179
0180 constrained_paths = bsxfun(@minus,constrained_paths,trend(idx,1:cL));
0181
0182
0183
0184 for b=1:options_cond_fcst.replic
0185 shocks = sQ*randn(ExoSize,options_cond_fcst.periods);
0186 shocks(jdx,:) = zeros(length(jdx),options_cond_fcst.periods);
0187 FORCS1(:,:,b) = mcforecast3(cL,options_cond_fcst.periods,constrained_paths,shocks,FORCS1(:,:,b),T,R,mv, mu)+trend;
0188 end
0189
0190 mFORCS1 = mean(FORCS1,3);
0191
0192 tt = (1-options_cond_fcst.conf_sig)/2;
0193 t1 = round(options_cond_fcst.replic*tt);
0194 t2 = round(options_cond_fcst.replic*(1-tt));
0195
0196 forecasts.controled_variables = constrained_vars;
0197 forecasts.instruments = options_cond_fcst.controlled_varexo;
0198
0199 for i = 1:EndoSize
0200 eval(['forecasts.cond.mean.' deblank(M_.endo_names(oo_.dr.order_var(i),:)) ' = mFORCS1(i,:)'';']);
0201 tmp = sort(squeeze(FORCS1(i,:,:))');
0202 eval(['forecasts.cond.ci.' deblank(M_.endo_names(oo_.dr.order_var(i),:)) ...
0203 ' = [tmp(t1,:)'' ,tmp(t2,:)'' ]'';']);
0204 end
0205
0206 clear FORCS1;
0207
0208 FORCS2 = zeros(NumberOfStates,options_cond_fcst.periods+1,options_cond_fcst.replic);
0209 for b=1:options_cond_fcst.replic
0210 FORCS2(:,1,b) = InitState;
0211 end
0212
0213
0214
0215 for b=1:options_cond_fcst.replic
0216 shocks = sQ*randn(ExoSize,options_cond_fcst.periods);
0217 shocks(jdx,:) = zeros(length(jdx),options_cond_fcst.periods);
0218 FORCS2(:,:,b) = mcforecast3(0,options_cond_fcst.periods,constrained_paths,shocks,FORCS2(:,:,b),T,R,mv, mu)+trend;
0219 end
0220
0221 mFORCS2 = mean(FORCS2,3);
0222
0223 for i = 1:EndoSize
0224 eval(['forecasts.uncond.mean.' deblank(M_.endo_names(oo_.dr.order_var(i),:)) ' = mFORCS2(i,:)'';']);
0225 tmp = sort(squeeze(FORCS2(i,:,:))');
0226 eval(['forecasts.uncond.ci.' deblank(M_.endo_names(oo_.dr.order_var(i),:)) ...
0227 ' = [tmp(t1,:)'' ,tmp(t2,:)'' ]'';']);
0228 end
0229
0230 save('conditional_forecasts.mat','forecasts');