0001 function time_series = extended_path(initial_conditions,sample_size)
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 global M_ options_ oo_
0034
0035 options_.verbosity = options_.ep.verbosity;
0036 verbosity = options_.ep.verbosity+options_.ep.debug;
0037
0038
0039 pfm.lead_lag_incidence = M_.lead_lag_incidence;
0040 pfm.ny = M_.endo_nbr;
0041 pfm.max_lag = M_.maximum_endo_lag;
0042 pfm.nyp = nnz(pfm.lead_lag_incidence(1,:));
0043 pfm.iyp = find(pfm.lead_lag_incidence(1,:)>0);
0044 pfm.ny0 = nnz(pfm.lead_lag_incidence(2,:));
0045 pfm.iy0 = find(pfm.lead_lag_incidence(2,:)>0);
0046 pfm.nyf = nnz(pfm.lead_lag_incidence(3,:));
0047 pfm.iyf = find(pfm.lead_lag_incidence(3,:)>0);
0048 pfm.nd = pfm.nyp+pfm.ny0+pfm.nyf;
0049 pfm.nrc = pfm.nyf+1;
0050 pfm.isp = [1:pfm.nyp];
0051 pfm.is = [pfm.nyp+1:pfm.ny+pfm.nyp];
0052 pfm.isf = pfm.iyf+pfm.nyp;
0053 pfm.isf1 = [pfm.nyp+pfm.ny+1:pfm.nyf+pfm.nyp+pfm.ny+1];
0054 pfm.iz = [1:pfm.ny+pfm.nyp+pfm.nyf];
0055 pfm.periods = options_.ep.periods;
0056 pfm.steady_state = oo_.steady_state;
0057 pfm.params = M_.params;
0058 pfm.i_cols_1 = nonzeros(pfm.lead_lag_incidence(2:3,:)');
0059 pfm.i_cols_A1 = find(pfm.lead_lag_incidence(2:3,:)');
0060 pfm.i_cols_T = nonzeros(pfm.lead_lag_incidence(1:2,:)');
0061 pfm.i_cols_j = 1:pfm.nd;
0062 pfm.i_upd = pfm.ny+(1:pfm.periods*pfm.ny);
0063 pfm.dynamic_model = str2func([M_.fname,'_dynamic']);
0064 pfm.verbose = options_.ep.verbosity;
0065 pfm.maxit_ = options_.maxit_;
0066 pfm.tolerance = options_.dynatol.f;
0067
0068 exo_nbr = M_.exo_nbr;
0069 periods = options_.periods;
0070 ep = options_.ep;
0071 steady_state = oo_.steady_state;
0072 dynatol = options_.dynatol;
0073
0074
0075 if isempty(initial_conditions)
0076 initial_conditions = oo_.steady_state;
0077 end
0078
0079
0080 options_.maxit_ = options_.ep.maxit;
0081
0082
0083 periods = options_.ep.periods;
0084 pfm.periods = options_.ep.periods;
0085 pfm.i_upd = pfm.ny+(1:pfm.periods*pfm.ny);
0086
0087
0088 options_.stack_solve_algo = options_.ep.stack_solve_algo;
0089
0090
0091 do_not_check_stability_flag = ~options_.ep.check_stability;
0092
0093
0094
0095
0096
0097
0098
0099 dr = struct();
0100 if options_.ep.init
0101 options_.order = 1;
0102 [dr,Info,M_,options_,oo_] = resol(1,M_,options_,oo_);
0103 end
0104
0105
0106 options_.minimal_solving_period = 100;
0107
0108
0109 make_ex_;
0110
0111
0112 make_y_;
0113
0114
0115 time_series = zeros(M_.endo_nbr,sample_size);
0116
0117
0118 variances = diag(M_.Sigma_e);
0119 positive_var_indx = find(variances>0);
0120 effective_number_of_shocks = length(positive_var_indx);
0121 stdd = sqrt(variances(positive_var_indx));
0122 covariance_matrix = M_.Sigma_e(positive_var_indx,positive_var_indx);
0123 covariance_matrix_upper_cholesky = chol(covariance_matrix);
0124
0125
0126 if options_.ep.set_dynare_seed_to_default
0127 set_dynare_seed('default');
0128 end
0129
0130
0131 bytecode_flag = options_.ep.use_bytecode;
0132
0133
0134 switch options_.ep.innovation_distribution
0135 case 'gaussian'
0136 oo_.ep.shocks = randn(sample_size,effective_number_of_shocks)*covariance_matrix_upper_cholesky;
0137 otherwise
0138 error(['extended_path:: ' options_.ep.innovation_distribution ' distribution for the structural innovations is not (yet) implemented!'])
0139 end
0140
0141
0142 if options_.ep.stochastic.status
0143 switch options_.ep.stochastic.method
0144 case 'tensor'
0145 switch options_.ep.stochastic.ortpol
0146 case 'hermite'
0147 [r,w] = gauss_hermite_weights_and_nodes(options_.ep.stochastic.nodes);
0148 otherwise
0149 error('extended_path:: Unknown orthogonal polynomial option!')
0150 end
0151 if options_.ep.stochastic.order*M_.exo_nbr>1
0152 for i=1:options_.ep.stochastic.order*M_.exo_nbr
0153 rr(i) = {r};
0154 ww(i) = {w};
0155 end
0156 rrr = cartesian_product_of_sets(rr{:});
0157 www = cartesian_product_of_sets(ww{:});
0158 else
0159 rrr = r;
0160 www = w;
0161 end
0162 www = prod(www,2);
0163 number_of_nodes = length(www);
0164 relative_weights = www/max(www);
0165 switch options_.ep.stochastic.pruned.status
0166 case 1
0167 jdx = find(relative_weights>options_.ep.stochastic.pruned.relative);
0168 www = www(jdx);
0169 www = www/sum(www);
0170 rrr = rrr(jdx,:);
0171 case 2
0172 jdx = find(weights>options_.ep.stochastic.pruned.level);
0173 www = www(jdx);
0174 www = www/sum(www);
0175 rrr = rrr(jdx,:);
0176 otherwise
0177
0178 end
0179 nnn = length(www);
0180 otherwise
0181 error('extended_path:: Unknown stochastic_method option!')
0182 end
0183 else
0184 rrr = zeros(1,effective_number_of_shocks);
0185 www = 1;
0186 nnn = 1;
0187 end
0188
0189
0190 t = 0;
0191
0192
0193 hh = dyn_waitbar(0,'Please wait. Extended Path simulations...');
0194 set(hh,'Name','EP simulations.');
0195
0196 if options_.ep.memory
0197 mArray1 = zeros(M_.endo_nbr,100,nnn,sample_size);
0198 mArray2 = zeros(M_.exo_nbr,100,nnn,sample_size);
0199 end
0200
0201
0202 while (t<sample_size)
0203 if ~mod(t,10)
0204 dyn_waitbar(t/sample_size,hh,'Please wait. Extended Path simulations...');
0205 end
0206
0207 t = t+1;
0208 shocks = oo_.ep.shocks(t,:);
0209
0210 oo_.exo_simul(2,positive_var_indx) = shocks;
0211 parfor s = 1:nnn
0212 periods1 = periods;
0213 exo_simul_1 = zeros(periods1+2,exo_nbr);
0214 pfm1 = pfm;
0215 info_convergence = 0;
0216 switch ep.stochastic.ortpol
0217 case 'hermite'
0218 for u=1:ep.stochastic.order
0219 exo_simul_1(2+u,positive_var_indx) = rrr(s,(((u-1)*effective_number_of_shocks)+1):(u*effective_number_of_shocks))*covariance_matrix_upper_cholesky;
0220 end
0221 otherwise
0222 error('extended_path:: Unknown orthogonal polynomial option!')
0223 end
0224 if ep.stochastic.order && ep.stochastic.scramble
0225 exo_simul_1(2+ep.stochastic.order+1:2+ep.stochastic.order+ep.stochastic.scramble,positive_var_indx) = ...
0226 randn(ep.stochastic.scramble,effective_number_of_shocks)*covariance_matrix_upper_cholesky;
0227 end
0228 if ep.init
0229 ex = zeros(size(endo_simul_1,2),size(exo_simul_1,2));
0230 ex(1:size(exo_simul_1,1),:) = exo_simul_1;
0231 exo_simul_1 = ex;
0232 initial_path = simult_(initial_conditions,dr,exo_simul_1(2:end,:),1);
0233 endo_simul_1(:,1:end-1) = initial_path(:,1:end-1)*ep.init+endo_simul_1(:,1:end-1)*(1-ep.init);
0234 else
0235 endo_simul_1 = repmat(steady_state,1,periods1+2);
0236 end
0237
0238 increase_periods = 0;
0239 endo_simul = endo_simul_1;
0240 while 1
0241 if ~increase_periods
0242 if bytecode_flag
0243 [flag,tmp] = bytecode('dynamic');
0244 else
0245 flag = 1;
0246 end
0247 if flag
0248 [flag,tmp] = solve_perfect_foresight_model(endo_simul_1,exo_simul_1,pfm1);
0249 end
0250 info_convergence = ~flag;
0251 end
0252 if verbosity
0253 if info_convergence
0254 if t<10
0255 disp(['Time: ' int2str(t) '. Convergence of the perfect foresight model solver!'])
0256 elseif t<100
0257 disp(['Time: ' int2str(t) '. Convergence of the perfect foresight model solver!'])
0258 elseif t<1000
0259 disp(['Time: ' int2str(t) '. Convergence of the perfect foresight model solver!'])
0260 else
0261 disp(['Time: ' int2str(t) '. Convergence of the perfect foresight model solver!'])
0262 end
0263 else
0264 if t<10
0265 disp(['Time: ' int2str(t) '. No convergence of the perfect foresight model solver!'])
0266 elseif t<100
0267 disp(['Time: ' int2str(t) '. No convergence of the perfect foresight model solver!'])
0268 elseif t<1000
0269 disp(['Time: ' int2str(t) '. No convergence of the perfect foresight model solver!'])
0270 else
0271 disp(['Time: ' int2str(t) '. No convergence of the perfect foresight model solver!'])
0272 end
0273 end
0274 end
0275 if do_not_check_stability_flag
0276
0277 endo_simul_1 = tmp;
0278 break
0279 else
0280
0281
0282 periods1 = periods1 + ep.step;
0283 pfm1.periods = periods1;
0284 pfm1.i_upd = pfm1.ny+(1:pfm1.periods*pfm1.ny);
0285
0286 increase_periods = increase_periods + 1;
0287 if verbosity
0288 if t<10
0289 disp(['Time: ' int2str(t) '. I increase the number of periods to ' int2str(periods1) '.'])
0290 elseif t<100
0291 disp(['Time: ' int2str(t) '. I increase the number of periods to ' int2str(periods1) '.'])
0292 elseif t<1000
0293 disp(['Time: ' int2str(t) '. I increase the number of periods to ' int2str(periods1) '.'])
0294 else
0295 disp(['Time: ' int2str(t) '. I increase the number of periods to ' int2str(periods1) '.'])
0296 end
0297 end
0298 if info_convergence
0299
0300
0301
0302 endo_simul_1 = [ tmp , repmat(steady_state,1,ep.step) ];
0303 exo_simul_1 = [ exo_simul_1 ; zeros(ep.step,size(shocks,2)) ];
0304 tmp_old = tmp;
0305 else
0306
0307
0308
0309
0310
0311 endo_simul_1 = [ endo_simul_1 , repmat(steady_state,1,ep.step) ];
0312 exo_simul_1 = [ exo_simul_1 ; zeros(ep.step,size(shocks,2)) ];
0313 end
0314
0315 if bytecode_flag
0316 [flag,tmp] = bytecode('dynamic');
0317 else
0318 flag = 1;
0319 end
0320 if flag
0321 [flag,tmp] = solve_perfect_foresight_model(endo_simul_1,exo_simul_1,pfm1);
0322 end
0323 info_convergence = ~flag;
0324 if info_convergence
0325
0326
0327
0328
0329 delta = max(max(abs(tmp(:,2:ep.fp)-tmp_old(:,2:ep.fp))));
0330 if delta < dynatol.x
0331
0332
0333 periods1 = ep.periods;
0334 pfm1.periods = periods1;
0335 pfm1.i_upd = pfm1.ny+(1:pfm1.periods*pfm1.ny);
0336
0337
0338 exo_simul_1 = exo_simul_1(1:(periods1+2),:);
0339 endo_simul_1 = endo_simul_1(:,1:(periods1+2));
0340 break
0341 end
0342 else
0343
0344
0345
0346 if increase_periods==10;
0347 if verbosity
0348 if t<10
0349 disp(['Time: ' int2str(t) '. Even with ' int2str(periods1) ', I am not able to solve the perfect foresight model. Use homotopy instead...'])
0350 elseif t<100
0351 disp(['Time: ' int2str(t) '. Even with ' int2str(periods1) ', I am not able to solve the perfect foresight model. Use homotopy instead...'])
0352 elseif t<1000
0353 disp(['Time: ' int2str(t) '. Even with ' int2str(periods1) ', I am not able to solve the perfect foresight model. Use homotopy instead...'])
0354 else
0355 disp(['Time: ' int2str(t) '. Even with ' int2str(periods1) ', I am not able to solve the perfect foresight model. Use homotopy instead...'])
0356 end
0357 end
0358
0359 break
0360 end
0361 end
0362 end
0363 end
0364 if ~info_convergence
0365 [INFO,tmp] = homotopic_steps(.5,.01,pfm1);
0366 if (~isstruct(INFO) && isnan(INFO)) || ~info_convergence
0367 [INFO,tmp] = homotopic_steps(0,.01,pfm1);
0368 if ~info_convergence
0369 disp('Homotopy:: No convergence of the perfect foresight model solver!')
0370 error('I am not able to simulate this model!');
0371 else
0372 info_convergence = 1;
0373 endo_simul_1 = tmp;
0374 if verbosity && info_convergence
0375 disp('Homotopy:: Convergence of the perfect foresight model solver!')
0376 end
0377 end
0378 else
0379 info_convergence = 1;
0380 endo_simul_1 = tmp;
0381 if verbosity && info_convergence
0382 disp('Homotopy:: Convergence of the perfect foresight model solver!')
0383 end
0384 end
0385 end
0386
0387 if ep.memory
0388 mArray1(:,:,s,t) = endo_simul_1(:,1:100);
0389 mArrat2(:,:,s,t) = transpose(exo_simul_1(1:100,:));
0390 end
0391 results(:,s) = www(s)*endo_simul_1(:,2);
0392 end
0393 time_series(:,t) = sum(results,2);
0394 oo_.endo_simul(:,1:end-1) = oo_.endo_simul(:,2:end);
0395 oo_.endo_simul(:,1) = time_series(:,t);
0396 oo_.endo_simul(:,end) = oo_.steady_state;
0397 end
0398
0399 dyn_waitbar_close(hh);
0400
0401 oo_.endo_simul = oo_.steady_state;
0402
0403 if ep.memory
0404 save([M_.fname '_memory'],'mArray1','mArray2','www');
0405 end