Home > matlab > imcforecast.m

imcforecast

PURPOSE ^

Computes conditional forecasts.

SYNOPSIS ^

function imcforecast(constrained_paths, constrained_vars, options_cond_fcst)

DESCRIPTION ^

 Computes conditional forecasts.

 INPUTS
  o constrained_paths    [double]      m*p array, where m is the number of constrained endogenous variables and p is the number of constrained periods. 
  o constrained_vars     [char]        m*x array holding the names of the controlled endogenous variables. 
  o 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   [char]       m*x array, list of controlled exogenous variables.
                                                             + conf_sig            [double]     scalar in [0,1], probability mass covered by the confidence bands. 

 OUTPUTS
  None.
 
 SPECIAL REQUIREMENTS
  This routine has to be called after an estimation statement or an estimated_params block.

 REMARKS
  [1] Results are stored in a structure which is saved in a mat file called conditional_forecasts.mat.
  [2] Use the function plot_icforecast to plot the results.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function imcforecast(constrained_paths, constrained_vars, options_cond_fcst)
0002 % Computes conditional forecasts.
0003 %
0004 % INPUTS
0005 %  o constrained_paths    [double]      m*p array, where m is the number of constrained endogenous variables and p is the number of constrained periods.
0006 %  o constrained_vars     [char]        m*x array holding the names of the controlled endogenous variables.
0007 %  o options_cond_fcst    [structure]   containing the options. The fields are:
0008 %                                                             + replic              [integer]   scalar, number of monte carlo simulations.
0009 %                                                             + parameter_set       [char]      values of the estimated parameters:
0010 %                                                                                               "posterior_mode",
0011 %                                                                                               "posterior_mean",
0012 %                                                                                               "posterior_median",
0013 %                                                                                               "prior_mode" or
0014 %                                                                                               "prior mean".
0015 %                                                                                   [double]     np*1 array, values of the estimated parameters.
0016 %                                                             + controlled_varexo   [char]       m*x array, list of controlled exogenous variables.
0017 %                                                             + conf_sig            [double]     scalar in [0,1], probability mass covered by the confidence bands.
0018 %
0019 % OUTPUTS
0020 %  None.
0021 %
0022 % SPECIAL REQUIREMENTS
0023 %  This routine has to be called after an estimation statement or an estimated_params block.
0024 %
0025 % REMARKS
0026 %  [1] Results are stored in a structure which is saved in a mat file called conditional_forecasts.mat.
0027 %  [2] Use the function plot_icforecast to plot the results.
0028 
0029 % Copyright (C) 2006-2010 Dynare Team
0030 %
0031 % This file is part of Dynare.
0032 %
0033 % Dynare is free software: you can redistribute it and/or modify
0034 % it under the terms of the GNU General Public License as published by
0035 % the Free Software Foundation, either version 3 of the License, or
0036 % (at your option) any later version.
0037 %
0038 % Dynare is distributed in the hope that it will be useful,
0039 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0040 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0041 % GNU General Public License for more details.
0042 %
0043 % You should have received a copy of the GNU General Public License
0044 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
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     % Load and transform data.
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 %randn('state',0);
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 %randn('state',0);
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');

Generated on Mon 21-May-2012 02:42:43 by m2html © 2005