perfect foresight: remove globals
parent
e9ffb16ec6
commit
3faaffacc6
|
@ -142,11 +142,11 @@ mapkeys = unique(cell2mat([keys(unanticipated_p_shocks) keys(unanticipated_t_sho
|
||||||
|
|
||||||
%% Simulation
|
%% Simulation
|
||||||
options_.periods = jm.periods;
|
options_.periods = jm.periods;
|
||||||
perfect_foresight_setup;
|
oo_=perfect_foresight_setup(M_, options_, oo_);
|
||||||
|
|
||||||
% no surprise shocks present
|
% no surprise shocks present
|
||||||
if isempty(mapkeys)
|
if isempty(mapkeys)
|
||||||
perfect_foresight_solver;
|
oo_=perfect_foresight_solver(M_, options_, oo_);
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -161,7 +161,7 @@ end
|
||||||
if mapkeys(1) ~= 1
|
if mapkeys(1) ~= 1
|
||||||
% if first unanticipated shock is not in period 1
|
% if first unanticipated shock is not in period 1
|
||||||
% simulate until first unanticipated shock and save
|
% simulate until first unanticipated shock and save
|
||||||
perfect_foresight_solver;
|
oo_=perfect_foresight_solver(M_, options_, oo_);
|
||||||
yy = [yy oo_.endo_simul(:, 2:mapkeys(1)+1)];
|
yy = [yy oo_.endo_simul(:, 2:mapkeys(1)+1)];
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -203,7 +203,7 @@ for i = 1:length(mapkeys)
|
||||||
last_period = this_period;
|
last_period = this_period;
|
||||||
assert(rows(oo_.exo_simul) == oo_exo_simul_rows, 'error encountered setting oo_.exo_simul');
|
assert(rows(oo_.exo_simul) == oo_exo_simul_rows, 'error encountered setting oo_.exo_simul');
|
||||||
oo_.endo_simul(:, 1) = yy(:, end);
|
oo_.endo_simul(:, 1) = yy(:, end);
|
||||||
perfect_foresight_solver;
|
oo_=perfect_foresight_solver(M_, options_, oo_);
|
||||||
if next_period > 0
|
if next_period > 0
|
||||||
yy = [yy oo_.endo_simul(:, 2:next_period-this_period+1)];
|
yy = [yy oo_.endo_simul(:, 2:next_period-this_period+1)];
|
||||||
else
|
else
|
||||||
|
|
|
@ -679,8 +679,8 @@ else
|
||||||
not_achieved = 1;
|
not_achieved = 1;
|
||||||
alpha = 1;
|
alpha = 1;
|
||||||
while not_achieved
|
while not_achieved
|
||||||
perfect_foresight_setup;
|
oo_=perfect_foresight_setup(M_, options_, oo_);
|
||||||
perfect_foresight_solver;
|
oo_=perfect_foresight_solver(M_, options_, oo_);
|
||||||
result = sum(sum(isfinite(oo_.endo_simul(:,time_index_constraint)))) == ny * length(time_index_constraint);
|
result = sum(sum(isfinite(oo_.endo_simul(:,time_index_constraint)))) == ny * length(time_index_constraint);
|
||||||
if (~oo_.deterministic_simulation.status || ~result) && it > 1
|
if (~oo_.deterministic_simulation.status || ~result) && it > 1
|
||||||
not_achieved = 1;
|
not_achieved = 1;
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
function perfect_foresight_setup()
|
function oo_=perfect_foresight_setup(M_, options_, oo_)
|
||||||
% Prepares a deterministic simulation, by filling oo_.exo_simul and oo_.endo_simul
|
% Prepares a deterministic simulation, by filling oo_.exo_simul and oo_.endo_simul
|
||||||
%
|
%
|
||||||
% INPUTS
|
% INPUTS
|
||||||
% None
|
% M_ [structure] describing the model
|
||||||
|
% options_ [structure] describing the options
|
||||||
|
% oo_ [structure] storing the results
|
||||||
%
|
%
|
||||||
% OUTPUTS
|
% OUTPUTS
|
||||||
% none
|
% oo_ [structure] storing the results
|
||||||
%
|
%
|
||||||
% ALGORITHM
|
% ALGORITHM
|
||||||
%
|
%
|
||||||
|
@ -29,8 +31,6 @@ function perfect_foresight_setup()
|
||||||
% You should have received a copy of the GNU General Public License
|
% You should have received a copy of the GNU General Public License
|
||||||
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
global M_ options_ oo_
|
|
||||||
|
|
||||||
test_for_deep_parameters_calibration(M_);
|
test_for_deep_parameters_calibration(M_);
|
||||||
|
|
||||||
if size(M_.lead_lag_incidence,2)-nnz(M_.lead_lag_incidence(M_.maximum_endo_lag+1,:)) > 0
|
if size(M_.lead_lag_incidence,2)-nnz(M_.lead_lag_incidence(M_.maximum_endo_lag+1,:)) > 0
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
function perfect_foresight_solver(no_error_if_learnt_in_is_present, marginal_linearization_previous_raw_sims)
|
function oo_=perfect_foresight_solver(M_, options_, oo_, no_error_if_learnt_in_is_present, marginal_linearization_previous_raw_sims)
|
||||||
% Computes deterministic simulations
|
% Computes deterministic simulations
|
||||||
%
|
%
|
||||||
% INPUTS
|
% INPUTS
|
||||||
|
% M_ [structure] describing the model
|
||||||
|
% options_ [structure] describing the options
|
||||||
|
% oo_ [structure] storing the results
|
||||||
% no_error_if_learnt_in_is_present [boolean, optional]
|
% no_error_if_learnt_in_is_present [boolean, optional]
|
||||||
% if true, then do not error out if a shocks(learnt_in=…) or endval(learnt_in=…)
|
% if true, then do not error out if a shocks(learnt_in=…) or endval(learnt_in=…)
|
||||||
% block is present
|
% block is present
|
||||||
|
@ -12,7 +15,7 @@ function perfect_foresight_solver(no_error_if_learnt_in_is_present, marginal_lin
|
||||||
% linearization
|
% linearization
|
||||||
%
|
%
|
||||||
% OUTPUTS
|
% OUTPUTS
|
||||||
% none
|
% oo_ [structure] storing the results
|
||||||
%
|
%
|
||||||
% ALGORITHM
|
% ALGORITHM
|
||||||
%
|
%
|
||||||
|
@ -36,14 +39,12 @@ function perfect_foresight_solver(no_error_if_learnt_in_is_present, marginal_lin
|
||||||
% You should have received a copy of the GNU General Public License
|
% You should have received a copy of the GNU General Public License
|
||||||
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
global M_ options_ oo_
|
|
||||||
|
|
||||||
check_input_arguments(options_, M_, oo_);
|
check_input_arguments(options_, M_, oo_);
|
||||||
|
|
||||||
if nargin < 1
|
if nargin < 4
|
||||||
no_error_if_learnt_in_is_present = false;
|
no_error_if_learnt_in_is_present = false;
|
||||||
end
|
end
|
||||||
if nargin < 2
|
if nargin < 5
|
||||||
marginal_linearization_previous_raw_sims = [];
|
marginal_linearization_previous_raw_sims = [];
|
||||||
end
|
end
|
||||||
if (~isempty(M_.learnt_shocks) || ~isempty(M_.learnt_endval)) && ~no_error_if_learnt_in_is_present
|
if (~isempty(M_.learnt_shocks) || ~isempty(M_.learnt_endval)) && ~no_error_if_learnt_in_is_present
|
||||||
|
@ -148,7 +149,7 @@ else
|
||||||
endoorig = marginal_linearization_previous_raw_sims.sim1.endo_simul;
|
endoorig = marginal_linearization_previous_raw_sims.sim1.endo_simul;
|
||||||
exoorig = marginal_linearization_previous_raw_sims.sim1.exo_simul;
|
exoorig = marginal_linearization_previous_raw_sims.sim1.exo_simul;
|
||||||
end
|
end
|
||||||
[completed_share, endo_simul, exo_simul, steady_state, exo_steady_state, iteration, maxerror, solver_iter, per_block_status] = homotopy_loop(options_.simul.homotopy_max_completion_share, shareorig, endoorig, exoorig, endobase, exobase, initperiods, simperiods, lastperiods, recompute_final_steady_state, oo_.steady_state, oo_.exo_steady_state);
|
[completed_share, endo_simul, exo_simul, steady_state, exo_steady_state, iteration, maxerror, solver_iter, per_block_status] = homotopy_loop(M_,options_,oo_,options_.simul.homotopy_max_completion_share, shareorig, endoorig, exoorig, endobase, exobase, initperiods, simperiods, lastperiods, recompute_final_steady_state, oo_.steady_state, oo_.exo_steady_state);
|
||||||
|
|
||||||
% Do linearization if needed and requested, and put results and solver status information in oo_
|
% Do linearization if needed and requested, and put results and solver status information in oo_
|
||||||
if completed_share == 1
|
if completed_share == 1
|
||||||
|
@ -208,7 +209,7 @@ elseif options_.simul.homotopy_marginal_linearization_fallback > 0 && completed_
|
||||||
fprintf('%s\n\n', repmat('*', 1, 80))
|
fprintf('%s\n\n', repmat('*', 1, 80))
|
||||||
end
|
end
|
||||||
extra_simul_time_counter = tic;
|
extra_simul_time_counter = tic;
|
||||||
[extra_success, extra_endo_simul, extra_exo_simul, extra_steady_state, extra_exo_steady_state] = create_scenario(extra_share, shareorig, endoorig, exoorig, endobase, exobase, initperiods, lastperiods, recompute_final_steady_state, endo_simul, steady_state, exo_steady_state);
|
[extra_success, extra_endo_simul, extra_exo_simul, extra_steady_state, extra_exo_steady_state] = create_scenario(M_,options_,oo_,extra_share, shareorig, endoorig, exoorig, endobase, exobase, initperiods, lastperiods, recompute_final_steady_state, endo_simul, steady_state, exo_steady_state);
|
||||||
if extra_success
|
if extra_success
|
||||||
[extra_endo_simul, extra_success] = perfect_foresight_solver_core(extra_endo_simul, extra_exo_simul, extra_steady_state, extra_exo_steady_state, M_, options_);
|
[extra_endo_simul, extra_success] = perfect_foresight_solver_core(extra_endo_simul, extra_exo_simul, extra_steady_state, extra_exo_steady_state, M_, options_);
|
||||||
end
|
end
|
||||||
|
@ -217,7 +218,7 @@ elseif options_.simul.homotopy_marginal_linearization_fallback > 0 && completed_
|
||||||
fprintf('The extra simulation for %.1f%% of the shock did not run when using the first simulation as a guess value. Now trying a full homotopy loop to get that extra simulation working\n\n', extra_share*100)
|
fprintf('The extra simulation for %.1f%% of the shock did not run when using the first simulation as a guess value. Now trying a full homotopy loop to get that extra simulation working\n\n', extra_share*100)
|
||||||
fprintf('%s\n\n', repmat('*', 1, 80))
|
fprintf('%s\n\n', repmat('*', 1, 80))
|
||||||
end
|
end
|
||||||
[extra_completed_share, extra_endo_simul, extra_exo_simul, extra_steady_state, extra_exo_steady_state] = homotopy_loop(extra_share, shareorig, endoorig, exoorig, endobase, exobase, initperiods, simperiods, lastperiods, recompute_final_steady_state, oo_.steady_state, oo_.exo_steady_state);
|
[extra_completed_share, extra_endo_simul, extra_exo_simul, extra_steady_state, extra_exo_steady_state] = homotopy_loop(M_,options_,oo_,extra_share, shareorig, endoorig, exoorig, endobase, exobase, initperiods, simperiods, lastperiods, recompute_final_steady_state, oo_.steady_state, oo_.exo_steady_state);
|
||||||
extra_success = (extra_completed_share == extra_share);
|
extra_success = (extra_completed_share == extra_share);
|
||||||
end
|
end
|
||||||
extra_simul_time_elapsed = toc(extra_simul_time_counter);
|
extra_simul_time_elapsed = toc(extra_simul_time_counter);
|
||||||
|
@ -294,8 +295,10 @@ assignin('base', 'Simulated_time_series', ts);
|
||||||
oo_.gui.ran_perfect_foresight = oo_.deterministic_simulation.status;
|
oo_.gui.ran_perfect_foresight = oo_.deterministic_simulation.status;
|
||||||
|
|
||||||
|
|
||||||
function [completed_share, endo_simul, exo_simul, steady_state, exo_steady_state, iteration, maxerror, solver_iter, per_block_status] = homotopy_loop(max_share, shareorig, endoorig, exoorig, endobase, exobase, initperiods, simperiods, lastperiods, recompute_final_steady_state, steady_state, exo_steady_state)
|
function [completed_share, endo_simul, exo_simul, steady_state, exo_steady_state, iteration, maxerror, solver_iter, per_block_status] = homotopy_loop(M_,options_,oo_,max_share, shareorig, endoorig, exoorig, endobase, exobase, initperiods, simperiods, lastperiods, recompute_final_steady_state, steady_state, exo_steady_state)
|
||||||
% INPUTS
|
% INPUTS
|
||||||
|
% M_ [structure] describing the model
|
||||||
|
% options_ [structure] describing the options
|
||||||
% share [double] the share of the shock that we want to simulate
|
% share [double] the share of the shock that we want to simulate
|
||||||
% simperiods [vector] period indices of simulation periods (between initial and terminal conditions)
|
% simperiods [vector] period indices of simulation periods (between initial and terminal conditions)
|
||||||
% endoorig [matrix] path of endogenous corresponding to 100% of the shock (also possibly used as guess value for first iteration if relevant)
|
% endoorig [matrix] path of endogenous corresponding to 100% of the shock (also possibly used as guess value for first iteration if relevant)
|
||||||
|
@ -312,7 +315,6 @@ function [completed_share, endo_simul, exo_simul, steady_state, exo_steady_state
|
||||||
% solver_iter [integer] corresponds to iter as returned by perfect_foresight_solver_core
|
% solver_iter [integer] corresponds to iter as returned by perfect_foresight_solver_core
|
||||||
% per_block_status [struct] as returned by perfect_foresight_solver_core
|
% per_block_status [struct] as returned by perfect_foresight_solver_core
|
||||||
|
|
||||||
global M_ options_
|
|
||||||
|
|
||||||
completed_share = 0; % Share of shock successfully completed so far
|
completed_share = 0; % Share of shock successfully completed so far
|
||||||
step = min(options_.simul.homotopy_initial_step_size, max_share);
|
step = min(options_.simul.homotopy_initial_step_size, max_share);
|
||||||
|
@ -336,7 +338,7 @@ while step > options_.simul.homotopy_min_step_size
|
||||||
|
|
||||||
iter_time_counter = tic;
|
iter_time_counter = tic;
|
||||||
|
|
||||||
[steady_success, endo_simul, exo_simul, steady_state, exo_steady_state] = create_scenario(new_share, shareorig, endoorig, exoorig, endobase, exobase, initperiods, lastperiods, recompute_final_steady_state, endo_simul, steady_state, exo_steady_state);
|
[steady_success, endo_simul, exo_simul, steady_state, exo_steady_state] = create_scenario(M_,options_,oo_,new_share, shareorig, endoorig, exoorig, endobase, exobase, initperiods, lastperiods, recompute_final_steady_state, endo_simul, steady_state, exo_steady_state);
|
||||||
|
|
||||||
if steady_success
|
if steady_success
|
||||||
% At the first iteration, use the initial guess given by
|
% At the first iteration, use the initial guess given by
|
||||||
|
@ -431,12 +433,15 @@ end
|
||||||
fprintf('\n')
|
fprintf('\n')
|
||||||
|
|
||||||
|
|
||||||
function [steady_success, endo_simul, exo_simul, steady_state, exo_steady_state] = create_scenario(share, shareorig, endoorig, exoorig, endobase, exobase, initperiods, lastperiods, recompute_final_steady_state, endo_simul, steady_state, exo_steady_state)
|
function [steady_success, endo_simul, exo_simul, steady_state, exo_steady_state] = create_scenario(M_,options_,oo_,share, shareorig, endoorig, exoorig, endobase, exobase, initperiods, lastperiods, recompute_final_steady_state, endo_simul, steady_state, exo_steady_state)
|
||||||
% For a given share, comutes the exogenous path and also the initial and
|
% For a given share, comutes the exogenous path and also the initial and
|
||||||
% terminal conditions for the endogenous path (but do not modify the initial
|
% terminal conditions for the endogenous path (but do not modify the initial
|
||||||
% guess for endogenous)
|
% guess for endogenous)
|
||||||
%
|
%
|
||||||
% INPUTS
|
% INPUTS
|
||||||
|
% M_ [structure] describing the model
|
||||||
|
% options_ [structure] describing the options
|
||||||
|
% oo_ [structure] storing the results
|
||||||
% share [double] the share of the shock that we want to simulate
|
% share [double] the share of the shock that we want to simulate
|
||||||
% shareorig [double] the share to which endoorig and exoorig correspond (typically 100%, except for perfect_foresight_with_expectation_errors_solver with homotopy and marginal linearization)
|
% shareorig [double] the share to which endoorig and exoorig correspond (typically 100%, except for perfect_foresight_with_expectation_errors_solver with homotopy and marginal linearization)
|
||||||
% endoorig [matrix] path of endogenous corresponding to shareorig of the shock (only initial and terminal conditions are used)
|
% endoorig [matrix] path of endogenous corresponding to shareorig of the shock (only initial and terminal conditions are used)
|
||||||
|
@ -457,8 +462,6 @@ function [steady_success, endo_simul, exo_simul, steady_state, exo_steady_state]
|
||||||
% steady_state [vector] steady state of endogenous corresponding to the scenario (equal to the input if terminal steady state not recomputed)
|
% steady_state [vector] steady state of endogenous corresponding to the scenario (equal to the input if terminal steady state not recomputed)
|
||||||
% exo_steady_state [vector] steady state of exogenous corresponding to the scenario (equal to the input if terminal steady state not recomputed)
|
% exo_steady_state [vector] steady state of exogenous corresponding to the scenario (equal to the input if terminal steady state not recomputed)
|
||||||
|
|
||||||
global M_ options_ oo_
|
|
||||||
|
|
||||||
% Compute convex combination for the path of exogenous
|
% Compute convex combination for the path of exogenous
|
||||||
exo_simul = exoorig*share/shareorig + exobase*(1-share);
|
exo_simul = exoorig*share/shareorig + exobase*(1-share);
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
function perfect_foresight_with_expectation_errors_setup
|
function oo_=perfect_foresight_with_expectation_errors_setup(M_, options_, oo_)
|
||||||
|
% INPUTS
|
||||||
|
% M_ [structure] describing the model
|
||||||
|
% options_ [structure] describing the options
|
||||||
|
% oo_ [structure] storing the results
|
||||||
|
%
|
||||||
|
% OUTPUTS
|
||||||
|
% oo_ [structure] storing the results
|
||||||
|
|
||||||
% Copyright © 2021-2023 Dynare Team
|
% Copyright © 2021-2023 Dynare Team
|
||||||
%
|
%
|
||||||
|
@ -17,8 +24,6 @@ function perfect_foresight_with_expectation_errors_setup
|
||||||
% You should have received a copy of the GNU General Public License
|
% You should have received a copy of the GNU General Public License
|
||||||
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
global M_ oo_ options_
|
|
||||||
|
|
||||||
if ~isempty(M_.endo_histval)
|
if ~isempty(M_.endo_histval)
|
||||||
error('perfect_foresight_with_expectation_errors_setup: cannot be used in conjunction with histval')
|
error('perfect_foresight_with_expectation_errors_setup: cannot be used in conjunction with histval')
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
function perfect_foresight_with_expectation_errors_solver
|
function oo_=perfect_foresight_with_expectation_errors_solver(M_, options_, oo_)
|
||||||
|
% INPUTS
|
||||||
|
% M_ [structure] describing the model
|
||||||
|
% options_ [structure] describing the options
|
||||||
|
% oo_ [structure] storing the results
|
||||||
|
%
|
||||||
|
% OUTPUTS
|
||||||
|
% oo_ [structure] storing the results
|
||||||
|
|
||||||
% Copyright © 2021-2023 Dynare Team
|
% Copyright © 2021-2023 Dynare Team
|
||||||
%
|
%
|
||||||
|
@ -17,15 +24,9 @@ function perfect_foresight_with_expectation_errors_solver
|
||||||
% You should have received a copy of the GNU General Public License
|
% You should have received a copy of the GNU General Public License
|
||||||
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
global M_ oo_ options_
|
|
||||||
|
|
||||||
% Same for periods (it will be modified before calling perfect_foresight_solver if constants_simulation_length option is false)
|
% Same for periods (it will be modified before calling perfect_foresight_solver if constants_simulation_length option is false)
|
||||||
periods = options_.periods;
|
periods = options_.periods;
|
||||||
|
|
||||||
% Save some options
|
|
||||||
orig_homotopy_max_completion_share = options_.simul.homotopy_max_completion_share;
|
|
||||||
orig_endval_steady = options_.simul.endval_steady;
|
|
||||||
|
|
||||||
% Retrieve initial paths built by pfwee_setup
|
% Retrieve initial paths built by pfwee_setup
|
||||||
% (the versions in oo_ will be truncated before calling perfect_foresight_solver)
|
% (the versions in oo_ will be truncated before calling perfect_foresight_solver)
|
||||||
endo_simul = oo_.endo_simul;
|
endo_simul = oo_.endo_simul;
|
||||||
|
@ -78,7 +79,7 @@ while info_period <= periods
|
||||||
marginal_linearization_previous_raw_sims = [];
|
marginal_linearization_previous_raw_sims = [];
|
||||||
end
|
end
|
||||||
|
|
||||||
perfect_foresight_solver(true, marginal_linearization_previous_raw_sims);
|
oo_= perfect_foresight_solver(M_, options_, oo_, true, marginal_linearization_previous_raw_sims);
|
||||||
|
|
||||||
if ~oo_.deterministic_simulation.status
|
if ~oo_.deterministic_simulation.status
|
||||||
error('perfect_foresight_with_expectation_errors_solver: failed to compute solution for information available at period %d\n', info_period)
|
error('perfect_foresight_with_expectation_errors_solver: failed to compute solution for information available at period %d\n', info_period)
|
||||||
|
@ -106,9 +107,4 @@ end
|
||||||
|
|
||||||
% Set final paths
|
% Set final paths
|
||||||
oo_.endo_simul = endo_simul;
|
oo_.endo_simul = endo_simul;
|
||||||
oo_.exo_simul = exo_simul;
|
oo_.exo_simul = exo_simul;
|
||||||
|
|
||||||
% Restore some options
|
|
||||||
options_.periods = periods;
|
|
||||||
options_.simul.homotopy_max_completion_share = orig_homotopy_max_completion_share;
|
|
||||||
options_.simul.endval_steady = orig_endval_steady;
|
|
Loading…
Reference in New Issue