2012-02-04 18:52:03 +01:00
|
|
|
function [info,tmp] = homotopic_steps(initial_weight,step_length,pfm)
|
2011-12-09 18:03:30 +01:00
|
|
|
global oo_ options_ M_
|
|
|
|
|
2012-02-04 18:52:03 +01:00
|
|
|
%Set bytecode flag
|
|
|
|
bytecode_flag = options_.ep.use_bytecode;
|
|
|
|
|
2012-01-27 18:24:24 +01:00
|
|
|
% Set increase and decrease factors.
|
|
|
|
increase_factor = 5.0;
|
|
|
|
decrease_factor = 0.2;
|
|
|
|
|
2011-12-09 18:03:30 +01:00
|
|
|
% Save current state of oo_.endo_simul and oo_.exo_simul.
|
|
|
|
endo_simul = oo_.endo_simul;
|
|
|
|
exxo_simul = oo_.exo_simul;
|
|
|
|
|
2012-01-27 18:24:24 +01:00
|
|
|
[idx,jdx] = find(abs(exxo_simul)>1e-12);
|
|
|
|
idx = unique(idx);
|
|
|
|
|
|
|
|
if idx(1)-2
|
|
|
|
% The first scalar in idx must be equal to two.
|
|
|
|
error('extended_path::homotopy: Something is wrong in oo_.exo_simul!')
|
|
|
|
end
|
2011-12-09 18:03:30 +01:00
|
|
|
|
2012-01-27 18:24:24 +01:00
|
|
|
stochastic_extended_path_depth = length(idx);
|
|
|
|
|
|
|
|
if stochastic_extended_path_depth>1
|
|
|
|
for i=2:length(idx)
|
|
|
|
if (idx(i)-idx(i-1)-1)
|
|
|
|
error('extended_path::homotopy: Something is wrong in oo_.exo_simul!')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-12-09 18:03:30 +01:00
|
|
|
|
|
|
|
initial_step_length = step_length;
|
|
|
|
max_iter = 1000/step_length;
|
|
|
|
weight = initial_weight;
|
2012-01-27 18:24:24 +01:00
|
|
|
verbose = options_.ep.debug;
|
2011-12-09 18:03:30 +01:00
|
|
|
|
|
|
|
reduce_step_flag = 0;
|
|
|
|
|
|
|
|
if verbose
|
|
|
|
format long
|
|
|
|
end
|
|
|
|
|
2012-01-27 18:24:24 +01:00
|
|
|
for d=1:stochastic_extended_path_depth
|
|
|
|
% (re)Set iter.
|
|
|
|
iter = 0;
|
|
|
|
% (re)Set iter.
|
|
|
|
jter = 0;
|
|
|
|
% (re)Set weight.
|
|
|
|
weight = initial_weight;
|
|
|
|
% (re)Set exo_simul to zero.
|
|
|
|
oo_.exo_simul = zeros(size(oo_.exo_simul));
|
2011-12-09 18:03:30 +01:00
|
|
|
while weight<1
|
|
|
|
iter = iter+1;
|
2012-01-27 18:24:24 +01:00
|
|
|
oo_.exo_simul(idx(d),:) = weight*exxo_simul(idx(d),:);
|
|
|
|
if d>1
|
|
|
|
oo_.exo_simul(1:idx(d-1),:) = exxo_simul(1:idx(d-1),:);
|
|
|
|
end
|
2012-02-04 18:52:03 +01:00
|
|
|
if bytecode_flag
|
|
|
|
[flag,tmp] = bytecode('dynamic');
|
|
|
|
else
|
|
|
|
flag = 1;
|
|
|
|
end
|
|
|
|
if flag
|
|
|
|
[flag,tmp] = solve_perfect_foresight_model(oo_.endo_simul,oo_.exo_simul,pfm);
|
|
|
|
end
|
2012-01-27 18:24:24 +01:00
|
|
|
info.convergence = ~flag;% Equal to one if the perfect foresight solver converged for the current value of weight.
|
2011-12-09 18:03:30 +01:00
|
|
|
if verbose
|
2012-01-27 18:24:24 +01:00
|
|
|
if info.convergence
|
2011-12-09 18:03:30 +01:00
|
|
|
disp(['Iteration n° ' int2str(iter) ', weight is ' num2str(weight,8) ', Ok!' ])
|
2012-01-27 18:24:24 +01:00
|
|
|
else
|
|
|
|
disp(['Iteration n° ' int2str(iter) ', weight is ' num2str(weight,8) ', Convergence problem!' ])
|
2011-12-09 18:03:30 +01:00
|
|
|
end
|
|
|
|
end
|
2012-01-27 18:24:24 +01:00
|
|
|
if info.convergence
|
|
|
|
if d<stochastic_extended_path_depth
|
|
|
|
oo_.endo_simul = tmp;
|
|
|
|
end
|
|
|
|
jter = jter + 1;
|
|
|
|
if jter>3
|
|
|
|
if verbose
|
|
|
|
disp('I am increasing the step length!')
|
|
|
|
end
|
|
|
|
step_length=step_length*increase_factor;
|
|
|
|
jter = 0;
|
|
|
|
end
|
|
|
|
if abs(1-weight)<options_.dynatol.x;
|
|
|
|
break
|
|
|
|
end
|
|
|
|
weight = weight+step_length;
|
|
|
|
else% Perfect foresight solver failed for the current value of weight.
|
|
|
|
if initial_weight>0 && abs(weight-initial_weight)<1e-12% First iteration, the initial weight is too high.
|
2011-12-09 18:03:30 +01:00
|
|
|
if verbose
|
|
|
|
disp('I am reducing the initial weight!')
|
|
|
|
end
|
2012-01-27 18:24:24 +01:00
|
|
|
initial_weight = initial_weight/2;
|
2011-12-09 18:03:30 +01:00
|
|
|
weight = initial_weight;
|
2012-01-23 13:59:25 +01:00
|
|
|
if weight<1e-12
|
2012-01-27 18:24:24 +01:00
|
|
|
oo_.endo_simul = endo_simul;
|
|
|
|
oo_.exo_simul = exxo_simul;
|
|
|
|
info.convergence = 0;
|
|
|
|
info.depth = d;
|
|
|
|
tmp = [];
|
|
|
|
return
|
2011-12-09 18:03:30 +01:00
|
|
|
end
|
|
|
|
continue
|
2012-01-27 18:24:24 +01:00
|
|
|
else% Initial weight is OK, but the perfect foresight solver failed on some subsequent iteration.
|
2011-12-09 18:03:30 +01:00
|
|
|
if verbose
|
|
|
|
disp('I am reducing the step length!')
|
|
|
|
end
|
2012-01-27 18:24:24 +01:00
|
|
|
jter = 0;
|
|
|
|
if weight>0
|
|
|
|
weight = weight-step_length;
|
|
|
|
end
|
|
|
|
step_length=step_length*decrease_factor;
|
2011-12-09 18:03:30 +01:00
|
|
|
weight = weight+step_length;
|
2012-01-27 18:24:24 +01:00
|
|
|
if step_length<options_.dynatol.x
|
2011-12-09 18:03:30 +01:00
|
|
|
break
|
|
|
|
end
|
|
|
|
continue
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if iter>max_iter
|
|
|
|
info = NaN;
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2012-01-27 18:24:24 +01:00
|
|
|
if weight<1
|
2012-01-23 13:57:30 +01:00
|
|
|
oo_.exo_simul = exxo_simul;
|
2012-02-04 18:52:03 +01:00
|
|
|
if bytecode_flag
|
|
|
|
[flag,tmp] = bytecode('dynamic');
|
|
|
|
else
|
|
|
|
flag = 1;
|
|
|
|
end
|
|
|
|
if flag
|
|
|
|
[flag,tmp] = solve_perfect_foresight_model(oo_.endo_simul,oo_.exo_simul,pfm);
|
|
|
|
end
|
2011-12-09 18:03:30 +01:00
|
|
|
info.convergence = ~flag;
|
|
|
|
if info.convergence
|
|
|
|
oo_.endo_simul = tmp;
|
|
|
|
return
|
|
|
|
else
|
2012-01-27 18:24:24 +01:00
|
|
|
if step_length>options_.dynatol.x
|
|
|
|
oo_.endo_simul = endo_simul;
|
|
|
|
oo_.exo_simul = exxo_simul;
|
|
|
|
info.convergence = 0;
|
|
|
|
info.depth = d;
|
|
|
|
tmp = [];
|
|
|
|
return
|
2011-12-09 18:03:30 +01:00
|
|
|
else
|
2012-01-27 18:24:24 +01:00
|
|
|
error('extended_path::homotopy: Oups! I did my best, but I am not able to simulate this model...')
|
2011-12-09 18:03:30 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|