From 71e3e0d49e8f759135e3b054f997f8902bcee4fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Wed, 14 Oct 2020 17:24:29 +0200 Subject: [PATCH] Fix extended_path with bytecode By the way, document and enforce the fact that stochastic extended_path (i.e. order > 0) is not compatible with either bytecode or block. Closes: #1742 --- doc/manual/source/the-model-file.rst | 4 +- matlab/ep/extended_path_core.m | 65 +++++++++++------------ tests/Makefile.am | 1 + tests/ep/rbc_bytecode.mod | 77 ++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+), 37 deletions(-) create mode 100644 tests/ep/rbc_bytecode.mod diff --git a/doc/manual/source/the-model-file.rst b/doc/manual/source/the-model-file.rst index 4ab202d60..8dc850262 100644 --- a/doc/manual/source/the-model-file.rst +++ b/doc/manual/source/the-model-file.rst @@ -4240,7 +4240,9 @@ which is described below. the endogenous variables are generated by assuming that the agents believe that there will no more shocks after period :math:`t+S`. This is an experimental feature and can be quite - slow. Default: ``0``. + slow. A non-zero value is not compatible with either the + ``bytecode`` or the ``block`` option of the ``model`` block. + Default: ``0``. .. option:: hybrid diff --git a/matlab/ep/extended_path_core.m b/matlab/ep/extended_path_core.m index 995757342..ab42d3b6f 100644 --- a/matlab/ep/extended_path_core.m +++ b/matlab/ep/extended_path_core.m @@ -40,46 +40,39 @@ if debug save ep_test_1.mat endo_simul exo_simul end -if bytecode_flag && ~ep.stochastic.order - try - tmp = bytecode('dynamic', endo_simul, exo_simul, M.params, endo_simul, periods); - flag = false; - catch ME - disp(ME.message); - flag = true; - end -else - flag = true; +if bytecode_flag && order > 0 + error('Option order > 0 of extended_path command is not compatible with bytecode option.') +end +if options.block && order > 0 + error('Option order > 0 of extended_path command is not compatible with block option.') end -if flag - if order == 0 - options.periods = periods; - options.block = pfm.block; - oo.endo_simul = endo_simul; - oo.exo_simul = exo_simul; - oo.steady_state = steady_state; - options.bytecode = bytecode_flag; - options.lmmcp = olmmcp; - options.solve_algo = solve_algo; - options.stack_solve_algo = stack_solve_algo; - tmp = perfect_foresight_solver_core(M, options, oo); - if ~tmp.deterministic_simulation.status - info_convergence = false; - else - info_convergence = true; - end +if order == 0 + options.periods = periods; + options.block = pfm.block; + oo.endo_simul = endo_simul; + oo.exo_simul = exo_simul; + oo.steady_state = steady_state; + options.bytecode = bytecode_flag; + options.lmmcp = olmmcp; + options.solve_algo = solve_algo; + options.stack_solve_algo = stack_solve_algo; + tmp = perfect_foresight_solver_core(M, options, oo); + if ~tmp.deterministic_simulation.status + info_convergence = false; else - switch(algo) - case 0 - [flag, tmp.endo_simul] = ... - solve_stochastic_perfect_foresight_model(endo_simul, exo_simul, pfm, ep.stochastic.quadrature.nodes, ep.stochastic.order); - case 1 - [flag, tmp.endo_simul] = ... - solve_stochastic_perfect_foresight_model_1(endo_simul, exo_simul, options, pfm, ep.stochastic.order); - end - info_convergence = ~flag; + info_convergence = true; end +else + switch(algo) + case 0 + [flag, tmp.endo_simul] = ... + solve_stochastic_perfect_foresight_model(endo_simul, exo_simul, pfm, ep.stochastic.quadrature.nodes, ep.stochastic.order); + case 1 + [flag, tmp.endo_simul] = ... + solve_stochastic_perfect_foresight_model_1(endo_simul, exo_simul, options, pfm, ep.stochastic.order); + end + info_convergence = ~flag; end if ~info_convergence && ~options.no_homotopy diff --git a/tests/Makefile.am b/tests/Makefile.am index 64a9fa6d7..89a6731c6 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -289,6 +289,7 @@ MODFILES = \ ep/rbcii.mod \ ep/linearmodel0.mod \ ep/linearmodel1.mod \ + ep/rbc_bytecode.mod \ stochastic_simulations/example1_noprint.mod \ stochastic-backward-models/solow_cd.mod \ stochastic-backward-models/solow_ces.mod \ diff --git a/tests/ep/rbc_bytecode.mod b/tests/ep/rbc_bytecode.mod new file mode 100644 index 000000000..a45607db8 --- /dev/null +++ b/tests/ep/rbc_bytecode.mod @@ -0,0 +1,77 @@ +// Test extended_path + bytecode (regression check for #1742) + +var Capital, Output, Labour, Consumption, Efficiency, efficiency, ExpectedTerm; + +varexo EfficiencyInnovation; + +parameters beta, theta, tau, alpha, psi, delta, rho, effstar, sigma; + +/* +** Calibration +*/ + + +beta = 0.990; +theta = 0.357; +tau = 30.000; +alpha = 0.450; +psi = -5.000; +delta = 0.020; +rho = 0.950; +effstar = 1.000; +sigma = 0.010; + +model(bytecode); + + // Eq. n°1: + efficiency = rho*efficiency(-1) + sigma*EfficiencyInnovation; + + // Eq. n°2: + Efficiency = effstar*exp(efficiency-.5*sigma*sigma/(1-rho*rho)); + + // Eq. n°3: + Output = Efficiency*(alpha*(Capital(-1)^psi)+(1-alpha)*(Labour^psi))^(1/psi); + + // Eq. n°4: + Consumption + Capital - Output - (1-delta)*Capital(-1); + + // Eq. n°5: + ((1-theta)/theta)*(Consumption/(1-Labour)) - (1-alpha)*(Output/Labour)^(1-psi); + + // Eq. n°6: + (((Consumption^theta)*((1-Labour)^(1-theta)))^(1-tau))/Consumption - ExpectedTerm(1); + + // Eq. n°7: + ExpectedTerm = beta*((((Consumption^theta)*((1-Labour)^(1-theta)))^(1-tau))/Consumption)*(alpha*((Output/Capital(-1))^(1-psi))+1-delta); + +end; + +steady_state_model; +efficiency = 0; +Efficiency = effstar; +// Compute steady state ratios. +Output_per_unit_of_Capital=((1/beta-1+delta)/alpha)^(1/(1-psi)); +Consumption_per_unit_of_Capital=Output_per_unit_of_Capital-delta; +Labour_per_unit_of_Capital=(((Output_per_unit_of_Capital/Efficiency)^psi-alpha)/(1-alpha))^(1/psi); +Output_per_unit_of_Labour=Output_per_unit_of_Capital/Labour_per_unit_of_Capital; +Consumption_per_unit_of_Labour=Consumption_per_unit_of_Capital/Labour_per_unit_of_Capital; + +// Compute steady state share of capital. +ShareOfCapital=alpha/(alpha+(1-alpha)*Labour_per_unit_of_Capital^psi); + +/// Compute steady state of the endogenous variables. +Labour=1/(1+Consumption_per_unit_of_Labour/((1-alpha)*theta/(1-theta)*Output_per_unit_of_Labour^(1-psi))); +Consumption = Consumption_per_unit_of_Labour*Labour; +Capital = Labour/Labour_per_unit_of_Capital; +Output = Output_per_unit_of_Capital*Capital; +ExpectedTerm = beta*((((Consumption^theta)*((1-Labour)^(1-theta)))^(1-tau))/Consumption)*(alpha*((Output/Capital)^(1-psi))+1-delta); +end; + + +shocks; +var EfficiencyInnovation = 1; +end; + +steady(nocheck); + +extended_path(periods=10, order=0);