From 6bca322809421870d3e31cf14d22fa945d7566b5 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Wed, 23 Mar 2016 21:20:43 +0100 Subject: [PATCH] Add check whether real part of complex solution solves the model Fixes #896 --- .../perfect_foresight_solver.m | 26 ++++++++++++++++++- matlab/perfect-foresight-models/sim1.m | 8 ++---- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/matlab/perfect-foresight-models/perfect_foresight_solver.m b/matlab/perfect-foresight-models/perfect_foresight_solver.m index e171c749d..41cb60849 100644 --- a/matlab/perfect-foresight-models/perfect_foresight_solver.m +++ b/matlab/perfect-foresight-models/perfect_foresight_solver.m @@ -166,7 +166,31 @@ if ~oo_.deterministic_simulation.status && ~options_.no_homotopy warning(warning_old_state); end -if oo_.deterministic_simulation.status == 1 + +if ~isreal(oo_.endo_simul(:)) %can only happen without bytecode + y0 = real(oo_.endo_simul(:,1)); + yT = real(oo_.endo_simul(:,options_.periods+2)); + yy = real(oo_.endo_simul(:,2:options_.periods+1)); + illi = M_.lead_lag_incidence'; + [i_cols,junk,i_cols_j] = find(illi(:)); + illi = illi(:,2:3); + [i_cols_J1,junk,i_cols_1] = find(illi(:)); + i_cols_T = nonzeros(M_.lead_lag_incidence(1:2,:)'); + residuals = perfect_foresight_problem(yy(:),str2func([M_.fname '_dynamic']), y0, yT, ... + oo_.exo_simul,M_.params,oo_.steady_state, ... + M_.maximum_lag,options_.periods,M_.endo_nbr,i_cols, ... + i_cols_J1, i_cols_1, i_cols_T, i_cols_j, ... + M_.NNZDerivatives(1)); + if max(abs(residuals))< options_.dynatol.f + oo_.deterministic_simulation.status = 1; + oo_.endo_simul=real(oo_.endo_simul); + else + oo_.deterministic_simulation.status = 0; + disp('Simulation terminated with imaginary parts in the residuals or endogenous variables.') + end +end + +if oo_.deterministic_simulation.status == 1 disp('Perfect foresight solution found.') skipline() else diff --git a/matlab/perfect-foresight-models/sim1.m b/matlab/perfect-foresight-models/sim1.m index 13e404f22..91113e893 100644 --- a/matlab/perfect-foresight-models/sim1.m +++ b/matlab/perfect-foresight-models/sim1.m @@ -178,7 +178,7 @@ end if stop - if any(isnan(res)) || any(isinf(res)) || any(isnan(Y)) || any(isinf(Y)) || ~isreal(res) || ~isreal(Y) + if any(isnan(res)) || any(isinf(res)) || any(isnan(Y)) || any(isinf(Y)) oo.deterministic_simulation.status = false;% NaN or Inf occurred oo.deterministic_simulation.error = err; oo.deterministic_simulation.iterations = iter; @@ -187,11 +187,7 @@ if stop if verbose skipline() disp(sprintf('Total time of simulation: %s.', num2str(etime(clock,h1)))) - if ~isreal(res) || ~isreal(Y) - disp('Simulation terminated with imaginary parts in the residuals or endogenous variables.') - else - disp('Simulation terminated with NaN or Inf in the residuals or endogenous variables.') - end + disp('Simulation terminated with NaN or Inf in the residuals or endogenous variables.') disp('There is most likely something wrong with your model. Try model_diagnostics or another simulation method.') printline(105) end