Home > matlab > ep > homotopic_steps.m

homotopic_steps

PURPOSE ^

SYNOPSIS ^

function [info,tmp] = homotopic_steps(endo_simul0,exo_simul0,initial_weight,step_length,pfm)

DESCRIPTION ^

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [info,tmp] = homotopic_steps(endo_simul0,exo_simul0,initial_weight,step_length,pfm)
0002 global options_ oo_
0003 
0004 %Set bytecode flag
0005 bytecode_flag = options_.ep.use_bytecode;
0006 
0007 % Set increase and decrease factors.
0008 increase_factor = 5.0;
0009 decrease_factor = 0.2;
0010 
0011 % Save current state of oo_.endo_simul and oo_.exo_simul.
0012 endo_simul = endo_simul0;
0013 exxo_simul = exo_simul0;
0014 
0015 initial_step_length = step_length;
0016 max_iter = 1000/step_length;
0017 weight   = initial_weight;
0018 verbose  = options_.ep.debug;
0019 
0020 reduce_step_flag = 0;
0021 
0022 if verbose
0023     format long
0024 end
0025 
0026 % (re)Set iter.
0027 iter = 0;
0028 % (re)Set iter.
0029 jter = 0;
0030 % (re)Set weight.
0031 weight = initial_weight;
0032 % (re)Set exo_simul to zero.
0033 exo_simul0 = zeros(size(exo_simul0));
0034 while weight<1
0035     iter = iter+1;
0036     exo_simul0(2,:) = weight*exxo_simul(2,:);
0037     if bytecode_flag
0038         oo_.endo_simul = endo_simul_1;
0039         oo_.exo_simul = exo_simul_1;
0040         [flag,tmp] = bytecode('dynamic');
0041     else
0042         flag = 1;
0043     end
0044     if flag
0045         [flag,tmp] = solve_perfect_foresight_model(endo_simul0,exo_simul0,pfm);
0046     end
0047     info.convergence = ~flag;% Equal to one if the perfect foresight solver converged for the current value of weight.
0048     if verbose
0049         if info.convergence
0050             disp(['Iteration n° ' int2str(iter) ', weight is ' num2str(weight,8) ', Ok!' ])
0051         else
0052             disp(['Iteration n° ' int2str(iter) ', weight is ' num2str(weight,8) ', Convergence problem!' ])
0053         end
0054     end
0055     if info.convergence
0056         %if d<stochastic_extended_path_depth
0057             endo_simul0 = tmp;
0058             %end
0059         jter = jter + 1;
0060         if jter>3
0061             if verbose
0062                 disp('I am increasing the step length!')
0063             end
0064             step_length=step_length*increase_factor;
0065             jter = 0;
0066         end
0067         if abs(1-weight)<options_.dynatol.x;
0068             break
0069         end
0070         weight = weight+step_length;
0071     else% Perfect foresight solver failed for the current value of weight.
0072         if initial_weight>0 && abs(weight-initial_weight)<1e-12% First iteration, the initial weight is too high.
0073             if verbose
0074                 disp('I am reducing the initial weight!')
0075             end
0076             initial_weight = initial_weight/2;
0077             weight = initial_weight;
0078             if weight<1e-12
0079                 endo_simul0 = endo_simul;
0080                 exo_simul0 = exxo_simul;
0081                 info.convergence = 0;
0082                 info.depth = d;
0083                 tmp = [];
0084                 return
0085             end
0086             continue
0087         else% Initial weight is OK, but the perfect foresight solver failed on some subsequent iteration.
0088             if verbose
0089                 disp('I am reducing the step length!')
0090             end
0091             jter = 0;
0092             if weight>0
0093                 weight = weight-step_length;
0094             end
0095             step_length=step_length*decrease_factor;
0096             weight = weight+step_length;
0097             if step_length<options_.dynatol.x
0098                 break
0099             end
0100             continue
0101         end
0102     end
0103     if iter>max_iter
0104         info = NaN;
0105         return
0106     end
0107 end
0108 if weight<1
0109     exo_simul0 = exxo_simul;
0110     if bytecode_flag
0111         oo_.endo_simul = endo_simul_1;
0112         oo_.exo_simul = exo_simul_1;
0113         [flag,tmp] = bytecode('dynamic');
0114     else
0115         flag = 1;
0116     end
0117     if flag
0118         [flag,tmp] = solve_perfect_foresight_model(endo_simul0,exo_simul0,pfm);
0119     end
0120     info.convergence = ~flag;
0121     if info.convergence
0122         endo_simul0 = tmp;
0123         return
0124     else
0125         if step_length>options_.dynatol.x
0126             endo_simul0 = endo_simul;
0127             exo_simul0 = exxo_simul;
0128             info.convergence = 0;
0129             info.depth = d;
0130             tmp = [];
0131             return
0132         else
0133             error('extended_path::homotopy: Oups! I did my best, but I am not able to simulate this model...')
0134         end
0135     end
0136 end

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