0001 function [dataset_,xparam1, M_, options_, oo_, estim_params_,bayestopt_, fake] = dynare_estimation_init(var_list_, dname, gsa_flag, M_, options_, oo_, estim_params_, bayestopt_)
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 if isempty(gsa_flag)
0039 gsa_flag = 0;
0040 else
0041 if ~isempty(strmatch('dsge_prior_weight',M_.param_names))
0042 options_.dsge_var = 1;
0043 end
0044 var_list_ = check_list_of_variables(options_, M_, var_list_);
0045 options_.varlist = var_list_;
0046 end
0047
0048
0049 options_.lgyidx2varobs = zeros(size(M_.endo_names,1),1);
0050 for i = 1:size(M_.endo_names,1)
0051 tmp = strmatch(deblank(M_.endo_names(i,:)),options_.varobs,'exact');
0052 if ~isempty(tmp)
0053 if length(tmp)>1
0054 disp(' ')
0055 error(['Multiple declarations of ' deblank(M_.endo_names(i,:)) ' as an observed variable is not allowed!'])
0056 end
0057 options_.lgyidx2varobs(i) = tmp;
0058 end
0059 end
0060
0061 if options_.order>2
0062 error(['I cannot estimate a model with a ' int2str(options_.order) ' order approximation of the model!'])
0063 end
0064
0065
0066
0067 if (options_.diffuse_filter==1) || (options_.kalman_algo > 2)
0068 if options_.lik_init == 2
0069 error(['options diffuse_filter, lik_init and/or kalman_algo have ' ...
0070 'contradictory settings'])
0071 else
0072 options_.lik_init = 3;
0073 end
0074 end
0075
0076
0077
0078
0079
0080 if options_.lik_init == 1
0081 if isempty(options_.qz_criterium)
0082 options_.qz_criterium = 1-1e-6;
0083 elseif options_.qz_criterium > 1-eps
0084 error(['estimation: option qz_criterium is too large for estimating ' ...
0085 'a stationary model. If your model contains unit roots, use ' ...
0086 'option diffuse_filter'])
0087 end
0088 elseif isempty(options_.qz_criterium)
0089 options_.qz_criterium = 1+1e-6;
0090 end
0091
0092
0093
0094 if options_.prefilter == 1
0095 options_.noconstant = 1;
0096 end
0097
0098
0099 if ~isequal(options_.filtered_vars,0) && isempty(options_.filter_step_ahead)
0100 options_.filter_step_ahead = 1;
0101 end
0102 if ~isequal(options_.filtered_vars,0) && isequal(options_.filter_step_ahead,0)
0103 options_.filter_step_ahead = 1;
0104 end
0105 if ~isequal(options_.filter_step_ahead,0)
0106 options_.nk = max(options_.filter_step_ahead);
0107 end
0108
0109
0110 if isempty(dname)
0111 M_.dname = M_.fname;
0112 else
0113 M_.dname = dname;
0114 end
0115
0116
0117 n_varobs = size(options_.varobs,1);
0118
0119
0120 if ~isempty(estim_params_)
0121 [xparam1,estim_params_,bayestopt_,lb,ub,M_] = set_prior(estim_params_,M_,options_);
0122 if any(bayestopt_.pshape > 0)
0123
0124 if ~options_.nograph && options_.plot_priors
0125 plot_priors(bayestopt_,M_,estim_params_,options_)
0126 end
0127
0128 bounds = prior_bounds(bayestopt_,options_);
0129 bounds(:,1)=max(bounds(:,1),lb);
0130 bounds(:,2)=min(bounds(:,2),ub);
0131 else
0132
0133
0134 options_.mh_replic = 0;
0135 bounds(:,1) = lb;
0136 bounds(:,2) = ub;
0137 end
0138
0139
0140 if any(xparam1 < bounds(:,1)) || any(xparam1 > bounds(:,2))
0141 find(xparam1 < bounds(:,1))
0142 find(xparam1 > bounds(:,2))
0143 error('Initial parameter values are outside parameter bounds')
0144 end
0145 lb = bounds(:,1);
0146 ub = bounds(:,2);
0147 bayestopt_.lb = lb;
0148 bayestopt_.ub = ub;
0149 else
0150 xparam1 = [];
0151 bayestopt_.lb = [];
0152 bayestopt_.ub = [];
0153 bayestopt_.jscale = [];
0154 bayestopt_.pshape = [];
0155 bayestopt_.p1 = [];
0156 bayestopt_.p2 = [];
0157 bayestopt_.p3 = [];
0158 bayestopt_.p4 = [];
0159 bayestopt_.p5 = [];
0160 bayestopt_.p6 = [];
0161 bayestopt_.p7 = [];
0162 estim_params_.nvx = 0;
0163 estim_params_.nvn = 0;
0164 estim_params_.ncx = 0;
0165 estim_params_.ncn = 0;
0166 estim_params_.np = 0;
0167 end
0168
0169
0170 oo_.prior.mean = bayestopt_.p1;
0171 oo_.prior.variance = diag(bayestopt_.p2.^2);
0172
0173
0174 if ~isfield(options_,'trend_coeffs')
0175 bayestopt_.with_trend = 0;
0176 else
0177 bayestopt_.with_trend = 1;
0178 bayestopt_.trend_coeff = {};
0179 trend_coeffs = options_.trend_coeffs;
0180 nt = length(trend_coeffs);
0181 for i=1:n_varobs
0182 if i > length(trend_coeffs)
0183 bayestopt_.trend_coeff{i} = '0';
0184 else
0185 bayestopt_.trend_coeff{i} = trend_coeffs{i};
0186 end
0187 end
0188 end
0189
0190
0191 bayestopt_.penalty = 1e8;
0192
0193
0194 dr = set_state_space(oo_.dr,M_);
0195 oo_.dr = dr;
0196 nstatic = dr.nstatic;
0197 npred = dr.npred;
0198 nspred = dr.nspred;
0199
0200
0201 if isempty(options_.varobs)
0202 error('VAROBS is missing')
0203 end
0204
0205
0206 var_obs_index = [];
0207 k1 = [];
0208 for i=1:n_varobs
0209 var_obs_index = [var_obs_index; strmatch(deblank(options_.varobs(i,:)),M_.endo_names(dr.order_var,:),'exact')];
0210 k1 = [k1; strmatch(deblank(options_.varobs(i,:)),M_.endo_names, 'exact')];
0211 end
0212
0213
0214 k2 = union(var_obs_index,[dr.nstatic+1:dr.nstatic+dr.npred]', 'rows');
0215
0216 oo_.dr.restrict_var_list = k2;
0217 bayestopt_.restrict_var_list = k2;
0218
0219 [junk,bayestopt_.mf0] = ismember([dr.nstatic+1:dr.nstatic+dr.npred]',k2);
0220
0221 [junk,bayestopt_.mf1] = ismember(var_obs_index,k2);
0222
0223 bayestopt_.mf2 = var_obs_index;
0224 bayestopt_.mfys = k1;
0225
0226 [junk,ic] = intersect(k2,nstatic+(1:npred)');
0227 oo_.dr.restrict_columns = [ic; length(k2)+(1:nspred-npred)'];
0228
0229 k3 = [];
0230 k3p = [];
0231 if options_.selected_variables_only
0232 for i=1:size(var_list_,1)
0233 k3 = [k3; strmatch(var_list_(i,:),M_.endo_names(dr.order_var,:), ...
0234 'exact')];
0235 k3p = [k3; strmatch(var_list_(i,:),M_.endo_names, ...
0236 'exact')];
0237 end
0238 else
0239 k3 = (1:M_.endo_nbr)';
0240 k3p = (1:M_.endo_nbr)';
0241 end
0242
0243
0244 if options_.block == 1
0245 k1 = k1';
0246 [k2, i_posA, i_posB] = union(k1', M_.state_var', 'rows');
0247
0248 oo_.dr.restrict_var_list = [k1(i_posA) M_.state_var(sort(i_posB))];
0249
0250 [junk,bayestopt_.mf0] = ismember(M_.state_var',oo_.dr.restrict_var_list);
0251
0252 [junk,bayestopt_.mf1] = ismember(k1,oo_.dr.restrict_var_list);
0253
0254 bayestopt_.mf2 = var_obs_index;
0255 bayestopt_.mfys = k1;
0256 oo_.dr.restrict_columns = [size(i_posA,1)+(1:size(M_.state_var,2))];
0257
0258 [k2, i_posA, i_posB] = union(k3p, M_.state_var', 'rows');
0259 bayestopt_.smoother_var_list = [k3p(i_posA); M_.state_var(sort(i_posB))'];
0260 [junk,junk,bayestopt_.smoother_saved_var_list] = intersect(k3p,bayestopt_.smoother_var_list(:));
0261 [junk,ic] = intersect(bayestopt_.smoother_var_list,M_.state_var);
0262 bayestopt_.smoother_restrict_columns = ic;
0263 [junk,bayestopt_.smoother_mf] = ismember(k1, ...
0264 bayestopt_.smoother_var_list);
0265 else
0266 k2 = union(var_obs_index,[dr.nstatic+1:dr.nstatic+dr.npred]', 'rows');
0267
0268 oo_.dr.restrict_var_list = k2;
0269
0270 [junk,bayestopt_.mf0] = ismember([dr.nstatic+1:dr.nstatic+dr.npred]',k2);
0271
0272 [junk,bayestopt_.mf1] = ismember(var_obs_index,k2);
0273
0274 bayestopt_.mf2 = var_obs_index;
0275 bayestopt_.mfys = k1;
0276 [junk,ic] = intersect(k2,nstatic+(1:npred)');
0277 oo_.dr.restrict_columns = [ic; length(k2)+(1:nspred-npred)'];
0278
0279 bayestopt_.smoother_var_list = union(k2,k3);
0280 [junk,junk,bayestopt_.smoother_saved_var_list] = intersect(k3,bayestopt_.smoother_var_list(:));
0281 [junk,ic] = intersect(bayestopt_.smoother_var_list,nstatic+(1:npred)');
0282 bayestopt_.smoother_restrict_columns = ic;
0283 [junk,bayestopt_.smoother_mf] = ismember(var_obs_index, ...
0284 bayestopt_.smoother_var_list);
0285 end;
0286
0287 if options_.analytic_derivation,
0288 if ~(exist('sylvester3','file')==2),
0289 dynareroot = strrep(which('dynare'),'dynare.m','');
0290 addpath([dynareroot 'gensylv'])
0291 end
0292 end
0293
0294
0295 if isempty(options_.datafile)
0296 if gsa_flag
0297 dataset_ = [];
0298
0299
0300 return
0301 else
0302 error('datafile option is missing')
0303 end
0304 end
0305
0306
0307 k = find(isnan(bayestopt_.jscale));
0308 bayestopt_.jscale(k) = options_.mh_jscale;
0309
0310
0311 transformation = [];
0312 if options_.loglinear && ~options_.logdata
0313 transformation = @log;
0314 end
0315 xls.sheet = options_.xls_sheet;
0316 xls.range = options_.xls_range;
0317
0318 if ~isfield(options_,'nobs')
0319 options_.nobs = [];
0320 end
0321
0322 dataset_ = initialize_dataset(options_.datafile,options_.varobs,options_.first_obs,options_.nobs,transformation,options_.prefilter,xls);
0323
0324 options_.nobs = dataset_.info.ntobs;
0325
0326
0327 if options_.diffuse_filter
0328 steadystate_check_flag = 0;
0329 else
0330 steadystate_check_flag = 1;
0331 end
0332
0333 M = M_;
0334 nvx = estim_params_.nvx;
0335 ncx = estim_params_.ncx;
0336 nvn = estim_params_.nvn;
0337 ncn = estim_params_.ncn;
0338 if isfield(estim_params_,'param_vals')
0339 M.params(estim_params_.param_vals(:,1)) = xparam1(nvx+ncx+nvn+ncn+1:end);
0340 end;
0341 oo_.steady_state = evaluate_steady_state(oo_.steady_state,M,options_,oo_,steadystate_check_flag);
0342 if all(abs(oo_.steady_state(bayestopt_.mfys))<1e-9)
0343 options_.noconstant = 1;
0344 else
0345 options_.noconstant = 0;
0346 end
0347
0348