* Increase size of homotopic steps if all goes well.

* Break Newton loop if err is NaN or if err is increasing (save useless iterations).



git-svn-id: https://www.dynare.org/svn/dynare/trunk@3152 ac1d8469-bf42-47a9-8791-bf33cf982152
time-shift
stepan 2009-11-17 10:58:19 +00:00
parent 55272c010e
commit 7e463facf6
2 changed files with 41 additions and 8 deletions

View File

@ -60,21 +60,21 @@ function time_series = extended_path(initial_conditions,sample_size,init)
norme = 0;
% Set verbose option
verbose = 1;
verbose = 0;
for t=1:sample_size
shocks = exp(randn(1,number_of_structural_innovations)*covariance_matrix_upper_cholesky-.5*variances(positive_var_indx)');
oo_.exo_simul(tdx,positive_var_indx) = shocks;
info = perfect_foresight_simulation;
time = info.time;
if verbose
t
info
end
if ~info.convergence
norme
info = homotopic_steps(tdx,positive_var_indx,shocks,norme,.5);
info = homotopic_steps(tdx,positive_var_indx,shocks,norme,.2);
if verbose
norme
info
end
else
@ -83,6 +83,7 @@ function time_series = extended_path(initial_conditions,sample_size,init)
if ~info.convergence
error('I am not able to simulate this model!')
end
info.time = info.time+time;
time_series(:,t+1) = oo_.endo_simul(:,tdx);
oo_.endo_simul(:,1:end-1) = oo_.endo_simul(:,2:end);
oo_.endo_simul(:,end) = oo_.steady_state;
@ -92,8 +93,9 @@ function time_series = extended_path(initial_conditions,sample_size,init)
function info = homotopic_steps(tdx,positive_var_indx,shocks,init_weight,step)
global oo_
weight = init_weight;
max_iter = 100;
verbose = 0;
iter = 0;
time = 0;
reduce_step = 0;
while iter<=100 && weight<=1
iter = iter+1;
@ -101,15 +103,34 @@ function info = homotopic_steps(tdx,positive_var_indx,shocks,init_weight,step)
weight = weight+step;
oo_.exo_simul(tdx,positive_var_indx) = weight*shocks+(1-weight);
info = perfect_foresight_simulation;
time = time+info.time;
if verbose
[iter,step]
[info.iterations.time,info.iterations.error]
end
if ~info.convergence
if verbose
disp('Reduce step size!')
end
reduce_step = 1;
break
else
if length(info.iterations.error)<5
if verbose
disp('Increase step size!')
end
step = step*1.5;
end
end
end
if reduce_step
step=step/1.5;
info = homotopic_steps(tdx,positive_var_indx,shocks,old_weight,step);
time = time+info.time;
elseif weight<1 && iter<100
oo_.exo_simul(tdx,positive_var_indx) = shocks;
info = perfect_foresight_simulation;
info.time = info.time+time;
else
info.time = time;
end

View File

@ -77,6 +77,9 @@ function info = perfect_foresight_simulation(init)
info.iterations.time = zeros(options_.maxit_,1);
info.iterations.error = info.iterations.time;
last_line = options_.maxit_;
error_growth = 0;
h1 = clock;
for iter = 1:options_.maxit_
h2 = clock;
@ -114,9 +117,16 @@ function info = perfect_foresight_simulation(init)
c = reshape(c,ny,periods);
endo_simul(:,it_init+(0:periods-1)) = endo_simul(:,it_init+(0:periods-1))+options_.slowc*c;
end
err = max(max(abs(c./options_.scalv')));
err = max(max(abs(c)));
info.iterations.time(iter) = etime(clock,h2);
info.iterations.error(iter) = err;
info.iterations.error(iter) = err;
if iter>1
error_growth = error_growth + (info.iterations.error(iter)>info.iterations.error(iter-1));
end
if isnan(err) || error_growth>3
last_line = iter;
break
end
if err < options_.dynatol
stop = 1;
info.time = etime(clock,h1);
@ -128,8 +138,10 @@ function info = perfect_foresight_simulation(init)
end
end
if ~stop
if ~stop
info.time = etime(clock,h1);
info.error = err;
info.convergence = 0;
info.iterations.time = info.iterations.time(1:last_line);
info.iterations.error = info.iterations.error(1:last_line);
end