evaluate_steady_state: accept exogenous steady state as argument instead of whole oo_ structure

This is a move towards a more functional programming style.
remove-submodule
Sébastien Villemot 2023-06-07 17:18:18 +02:00
parent 5931e3aa46
commit 37870e4a40
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
17 changed files with 46 additions and 55 deletions

View File

@ -62,8 +62,8 @@ function [oo_, options_mom_, M_] = run(bayestopt_, options_, oo_, estim_params_,
% o set_state_space.m
% o set_all_parameters.m
% o test_for_deep_parameters_calibration.m
% =========================================================================
% Copyright © 2020-2022 Dynare Team
% Copyright © 2020-2023 Dynare Team
%
% This file is part of Dynare.
%
@ -688,7 +688,7 @@ end
old_steady_params=M_.params; %save initial parameters for check if steady state changes param values
% Check steady state at initial model parameter values
[oo_.steady_state, new_steady_params, info] = evaluate_steady_state(oo_.steady_state,M_,options_mom_,oo_,steadystate_check_flag);
[oo_.steady_state, new_steady_params, info] = evaluate_steady_state(oo_.steady_state,[oo_.exo_steady_state; oo_.exo_det_steady_state],M_,options_mom_,steadystate_check_flag);
if info(1)
fprintf('\nmethod_of_moments: The steady state at the initial parameters cannot be computed.\n')
print_info(info, 0, options_mom_);
@ -699,7 +699,7 @@ if isfield(estim_params_,'param_vals') && ~isempty(estim_params_.param_vals)
Model_par_varied=M_; %store M_ structure
Model_par_varied.params(estim_params_.param_vals(:,1))=Model_par_varied.params(estim_params_.param_vals(:,1))*1.01; %vary parameters
[~, new_steady_params_2] = evaluate_steady_state(oo_.steady_state,Model_par_varied,options_mom_,oo_,1);
[~, new_steady_params_2] = evaluate_steady_state(oo_.steady_state,[oo_.exo_steady_state; oo_.exo_det_steady_state],Model_par_varied,options_mom_,true);
changed_par_indices=find((old_steady_params(estim_params_.param_vals(:,1))-new_steady_params(estim_params_.param_vals(:,1))) ...
| (Model_par_varied.params(estim_params_.param_vals(:,1))-new_steady_params_2(estim_params_.param_vals(:,1))));

View File

@ -13,7 +13,7 @@ function y0 = get_mean(varargin)
% SPECIAL REQUIREMENTS
% none
% Copyright © 2019 Dynare Team
% Copyright © 2019-2023 Dynare Team
%
% This file is part of Dynare.
%
@ -39,7 +39,7 @@ else
end
if order==1
ys_ = oo_.steady_state;
ys_ = evaluate_steady_state(ys_,M_,options_,oo_,1);
ys_ = evaluate_steady_state(ys_,[oo_.exo_steady_state; oo_.exo_det_steady_state],M_,options_,true);
elseif order==2
ys_ = oo_.dr.ys;
ys_(oo_.dr.order_var)=ys_(oo_.dr.order_var)+oo_.dr.ghs2./2;

View File

@ -1,4 +1,4 @@
function [steady_state, params, check] = dyn_ramsey_static(ys_init, M, options_, oo)
function [steady_state, params, check] = dyn_ramsey_static(ys_init, exo_ss, M, options_)
% Computes the steady state for optimal policy
%
@ -12,9 +12,9 @@ function [steady_state, params, check] = dyn_ramsey_static(ys_init, M, options_,
% INPUTS
% ys_init: vector of endogenous variables or instruments
% exo_ss vector of exogenous steady state (incl. deterministic exogenous)
% M: Dynare model structure
% options: Dynare options structure
% oo: Dynare results structure
%
% OUTPUTS
% steady_state: steady state value
@ -46,10 +46,9 @@ function [steady_state, params, check] = dyn_ramsey_static(ys_init, M, options_,
params = M.params;
check = 0;
options_.steadystate.nocheck = 1; %locally disable checking because Lagrange multipliers are not accounted for in evaluate_steady_state_file
nl_func = @(x) dyn_ramsey_static_1(x,M,options_,oo);
exo_ss = [oo.exo_steady_state oo.exo_det_steady_state];
nl_func = @(x) dyn_ramsey_static_1(x,exo_ss,ys_init,M,options_);
if ~options_.steadystate_flag && check_static_model(ys_init,M,options_,oo)
if ~options_.steadystate_flag && check_static_model(ys_init,exo_ss,M,options_)
steady_state = ys_init;
return
elseif options_.steadystate_flag
@ -82,7 +81,7 @@ elseif options_.steadystate_flag
[xx,params] = evaluate_steady_state_file(ys_init,exo_ss,M,options_,~options_.steadystate.nocheck); %run steady state file again to update parameters
[~,~,steady_state] = nl_func(inst_val); %compute and return steady state
else
xx = oo.steady_state(1:M.orig_endo_nbr);
xx = ys_init(1:M.orig_endo_nbr);
o_jacobian_flag = options_.jacobian_flag;
options_.jacobian_flag = false;
[xx, errorflag] = dynare_solve(nl_func, xx, options_.ramsey.maxit, options_.solve_tolf, options_.solve_tolx, options_);
@ -94,21 +93,18 @@ else
end
function [resids,rJ,steady_state] = dyn_ramsey_static_1(x,M,options_,oo)
function [resids,rJ,steady_state] = dyn_ramsey_static_1(x,exo_ss,ys_init,M,options_)
resids = [];
rJ = [];
mult = [];
inst_nbr = M.ramsey_orig_endo_nbr - M.ramsey_orig_eq_nbr;
exo_ss = [oo.exo_steady_state oo.exo_det_steady_state];
if options_.steadystate_flag
k_inst = [];
for i = 1:size(options_.instruments,1)
k_inst = [k_inst; strmatch(options_.instruments{i}, M.endo_names, 'exact')];
end
ys_init=zeros(size(oo.steady_state)); %create starting vector for steady state computation as only instrument value is handed over
ys_init(k_inst) = x; %set instrument, the only value required for steady state computation, to current value
[x,M.params,check] = evaluate_steady_state_file(ys_init,... %returned x now has size endo_nbr as opposed to input size of n_instruments
exo_ss, ...
@ -168,9 +164,8 @@ else
steady_state = [xx(1:M.ramsey_orig_endo_nbr); mult];
end
function result = check_static_model(ys,M,options_,oo)
function result = check_static_model(ys,exo_ss,M,options_)
result = false;
exo_ss = [oo.exo_steady_state oo.exo_det_steady_state];
if (options_.bytecode)
[res, ~] = bytecode('static', ys, exo_ss, M.params, 'evaluate');
else

View File

@ -488,7 +488,7 @@ if options_.analytic_derivation
else
steadystate_check_flag = 1;
end
[tmp1, params] = evaluate_steady_state(oo_.steady_state,M,options_,oo_,steadystate_check_flag);
[tmp1, params] = evaluate_steady_state(oo_.steady_state,[oo_.exo_steady_state; oo_.exo_det_steady_state],M,options_,steadystate_check_flag);
change_flag=any(find(params-M.params));
if change_flag
skipline()
@ -561,7 +561,7 @@ ncn = estim_params_.ncn;
if estim_params_.np
M.params(estim_params_.param_vals(:,1)) = xparam1(nvx+ncx+nvn+ncn+1:end);
end
[oo_.steady_state, params,info] = evaluate_steady_state(oo_.steady_state,M,options_,oo_,steadystate_check_flag);
[oo_.steady_state, params,info] = evaluate_steady_state(oo_.steady_state,[oo_.exo_steady_state; oo_.exo_det_steady_state],M,options_,steadystate_check_flag);
if info(1)
fprintf('\ndynare_estimation_init:: The steady state at the initial parameters cannot be computed.\n')
@ -696,4 +696,4 @@ if options_.occbin.smoother.status || options_.occbin.likelihood.status
options_.occbin.simul.exo_pos=1:M_.exo_nbr;
M_.surprise_shocks=[];
end
end
end

View File

@ -1,13 +1,13 @@
function [ys,params,info] = evaluate_steady_state(ys_init,M,options,oo,steadystate_check_flag)
% function [ys,params,info] = evaluate_steady_state(ys_init,M,options,oo,steadystate_check_flag)
function [ys,params,info] = evaluate_steady_state(ys_init,exo_ss,M,options,steadystate_check_flag)
% function [ys,params,info] = evaluate_steady_state(ys_init,exo_ss,M,options,steadystate_check_flag)
% Computes the steady state
%
% INPUTS
% ys_init vector initial values used to compute the steady
% state
% exo_ss vector exogenous steady state (incl. deterministic exogenous)
% M struct model structure
% options struct options
% oo struct output results
% steadystate_check_flag boolean if true, check that the
% steadystate verifies the
% static model
@ -61,7 +61,6 @@ check = 0;
steadystate_flag = options.steadystate_flag;
params = M.params;
exo_ss = [oo.exo_steady_state; oo.exo_det_steady_state];
if length(M.aux_vars) > 0 && ~steadystate_flag && M.set_auxiliary_variables
h_set_auxiliary_variables = str2func([M.fname '.set_auxiliary_variables']);
@ -192,7 +191,7 @@ if options.ramsey_policy
end
%either if no steady state file or steady state file without problems
[ys,params,info] = dyn_ramsey_static(ys_init,M,options,oo);
[ys,params,info] = dyn_ramsey_static(ys_init,exo_ss,M,options);
if info
return
end

View File

@ -5,7 +5,7 @@ function [ys,params,info] = evaluate_steady_state_file(ys_init,exo_ss,M,options,
% INPUTS
% ys_init vector initial values used to compute the steady
% state
% exo_ss vector exogenous steady state
% exo_ss vector exogenous steady state (incl. deterministic exogenous)
% M struct model parameters
% options struct options
% steady_state_checkflag boolean indicator whether to check steady state returned

View File

@ -14,7 +14,7 @@ function [rmse_MC, ixx] = filt_mc_(OutDir,options_gsa_,dataset_,dataset_info)
% marco.ratto@ec.europa.eu
% Copyright © 2012-2016 European Commission
% Copyright © 2012-2022 Dynare Team
% Copyright © 2012-2023 Dynare Team
%
% This file is part of Dynare.
%
@ -126,11 +126,11 @@ end
if ~loadSA
if exist('xparam1','var')
M_ = set_all_parameters(xparam1,estim_params_,M_);
ys_mode=evaluate_steady_state(oo_.steady_state,M_,options_,oo_,~options_.steadystate.nocheck);
ys_mode=evaluate_steady_state(oo_.steady_state,[oo_.exo_steady_state; oo_.exo_det_steady_state],M_,options_,~options_.steadystate.nocheck);
end
if exist('xparam1_mean','var')
M_ = set_all_parameters(xparam1_mean,estim_params_,M_);
ys_mean=evaluate_steady_state(oo_.steady_state,M_,options_,oo_,~options_.steadystate.nocheck);
ys_mean=evaluate_steady_state(oo_.steady_state,[oo_.exo_steady_state; oo_.exo_det_steady_state],M_,options_,~options_.steadystate.nocheck);
end
Y = transpose(dataset_.data);
gend = dataset_.nobs;

View File

@ -164,13 +164,13 @@ end
old_steady_params=Model.params; %save initial parameters for check if steady state changes param values
% % check if steady state solves static model (except if diffuse_filter == 1)
[DynareResults.steady_state, new_steady_params] = evaluate_steady_state(DynareResults.steady_state,Model,DynareOptions,DynareResults,DynareOptions.diffuse_filter==0);
[DynareResults.steady_state, new_steady_params] = evaluate_steady_state(DynareResults.steady_state,[DynareResults.exo_steady_state; DynareResults.exo_det_steady_state],Model,DynareOptions,DynareOptions.diffuse_filter==0);
if isfield(EstimatedParameters,'param_vals') && ~isempty(EstimatedParameters.param_vals)
%check whether steady state file changes estimated parameters
Model_par_varied=Model; %store Model structure
Model_par_varied.params(EstimatedParameters.param_vals(:,1))=Model_par_varied.params(EstimatedParameters.param_vals(:,1))*1.01; %vary parameters
[~, new_steady_params_2] = evaluate_steady_state(DynareResults.steady_state,Model_par_varied,DynareOptions,DynareResults,DynareOptions.diffuse_filter==0);
[~, new_steady_params_2] = evaluate_steady_state(DynareResults.steady_state,[DynareResults.exo_steady_state; DynareResults.exo_det_steady_state],Model_par_varied,DynareOptions,DynareOptions.diffuse_filter==0);
changed_par_indices=find((old_steady_params(EstimatedParameters.param_vals(:,1))-new_steady_params(EstimatedParameters.param_vals(:,1))) ...
| (Model_par_varied.params(EstimatedParameters.param_vals(:,1))-new_steady_params_2(EstimatedParameters.param_vals(:,1))));

View File

@ -111,7 +111,7 @@ if options.logged_steady_state %if steady state was previously logged, undo this
oo.steady_state=exp(oo.steady_state);
options.logged_steady_state=0;
end
[dr.ys,M.params,check1]=evaluate_steady_state(oo.steady_state,M,options,oo,options.steadystate.nocheck);
[dr.ys,M.params,check1]=evaluate_steady_state(oo.steady_state,[oo.exo_steady_state; oo.exo_det_steady_state],M,options,options.steadystate.nocheck);
% testing for problem
if check1(1)

View File

@ -127,7 +127,6 @@ end
%% Compute the terminal steady state for all informational periods
oo_.pfwee.terminal_steady_state = NaN(M_.endo_nbr, periods);
orig_exo_steady_state = oo_.exo_steady_state;
for p = 1:periods
if p > 1 && all(oo_.pfwee.terminal_info(:, p) == oo_.pfwee.terminal_info(:, p-1))
oo_.pfwee.terminal_steady_state(:, p) = oo_.pfwee.terminal_steady_state(:, p-1);
@ -137,11 +136,9 @@ for p = 1:periods
else
init = oo_.pfwee.terminal_steady_state(:, p-1);
end
oo_.exo_steady_state = oo_.pfwee.terminal_info(:, p);
oo_.pfwee.terminal_steady_state(:, p) = evaluate_steady_state(init, M_, options_, oo_, true);
oo_.pfwee.terminal_steady_state(:, p) = evaluate_steady_state(init, oo_.pfwee.terminal_info(:, p), M_, options_, true);
end
end
oo_.exo_steady_state = orig_exo_steady_state;
% Build initial paths for endos and exos (only initial conditions are set, the rest is NaN)
if isempty(ys0_)

View File

@ -62,7 +62,7 @@ end
if options_.steadystate_flag
[oo_.steady_state,M_.params,info] = ...
evaluate_steady_state(oo_.steady_state,M_,options_,oo_,0);
evaluate_steady_state(oo_.steady_state,[oo_.exo_steady_state; oo_.exo_det_steady_state],M_,options_,false);
end
% Compute the residuals

View File

@ -74,7 +74,7 @@ if M.exo_nbr == 0
oo.exo_steady_state = [] ;
end
[dr.ys,M.params,info] = evaluate_steady_state(oo.steady_state,M,options,oo,~options.steadystate.nocheck);
[dr.ys,M.params,info] = evaluate_steady_state(oo.steady_state,[oo.exo_steady_state; oo.exo_det_steady_state],M,options,~options.steadystate.nocheck);
if info(1)
oo.dr = dr;

View File

@ -77,7 +77,7 @@ if info(1)
end
end
[oo_.steady_state,M_.params,info] = evaluate_steady_state(oo_.steady_state,M_,options_,oo_,~options_.steadystate.nocheck);
[oo_.steady_state,M_.params,info] = evaluate_steady_state(oo_.steady_state,[oo_.exo_steady_state; oo_.exo_det_steady_state],M_,options_,~options_.steadystate.nocheck);
if info(1) == 0
if ~options_.noprint
@ -170,7 +170,7 @@ for i=1:step_nbr+1
oo.exo_steady_state(values(ix,2)) = points(ix,i);
oo.exo_det_steady_state(values(ixd,2)) = points(ixd,i);
[steady_state,M.params,info] = evaluate_steady_state(oo.steady_state,M,options,oo,~options.steadystate.nocheck);
[steady_state,M.params,info] = evaluate_steady_state(oo.steady_state,[oo.exo_steady_state; oo.exo_det_steady_state],M,options,~options.steadystate.nocheck);
if info(1) == 0
% if homotopy step is not successful, current values of steady
% state are not modified
@ -261,7 +261,7 @@ for i = 1:nv
disp([ 'HOMOTOPY mode 2: lauching solver with ' varname ' = ' num2str(v) ' ...'])
oo_.steady_state = evaluate_steady_state(oo_.steady_state,M_,options_,oo_,~options_.steadystate.nocheck);
oo_.steady_state = evaluate_steady_state(oo_.steady_state,[oo_.exo_steady_state; oo_.exo_det_steady_state],M_,options_,~options_.steadystate.nocheck);
end
end
@ -344,7 +344,7 @@ while iter <= step_nbr
old_ss = oo.steady_state;
[steady_state,params,info] = evaluate_steady_state(old_ss,M,options,oo,~options.steadystate.nocheck);
[steady_state,params,info] = evaluate_steady_state(old_ss,[oo.exo_steady_state; oo.exo_det_steady_state],M,options,~options.steadystate.nocheck);
if info(1) == 0
oo.steady_state = steady_state;
M.params = params;

View File

@ -49,7 +49,7 @@ perfect_foresight_solver;
oo_.exo_simul(3,1) = 1.3;
oo_.exo_steady_state = 1.1;
oo_.exo_simul(end, 1) = oo_.exo_steady_state;
oo_.steady_state = evaluate_steady_state(oo_.steady_state, M_, options_, oo_, true);
oo_.steady_state = evaluate_steady_state(oo_.steady_state, oo_.exo_steady_state, M_, options_, true);
oo_.endo_simul(:, end) = oo_.steady_state;
options_.periods = 6;
saved_endo = oo_.endo_simul(:, 1);
@ -64,7 +64,7 @@ oo_.exo_simul = [ saved_exo; oo_.exo_simul ];
oo_.exo_simul(4,1) = 1.4;
oo_.exo_steady_state = 1.2;
oo_.exo_simul(end, 1) = oo_.exo_steady_state;
oo_.steady_state = evaluate_steady_state(oo_.steady_state, M_, options_, oo_, true);
oo_.steady_state = evaluate_steady_state(oo_.steady_state, oo_.exo_steady_state, M_, options_, true);
oo_.endo_simul(:, end) = oo_.steady_state;
options_.periods = 5;
saved_endo = oo_.endo_simul(:, 1:2);
@ -80,7 +80,7 @@ oo_.exo_simul(7,1) = 1.1;
oo_.exo_simul(8,1) = 1.1;
oo_.exo_steady_state = 1.1;
oo_.exo_simul(end, 1) = oo_.exo_steady_state;
oo_.steady_state = evaluate_steady_state(oo_.steady_state, M_, options_, oo_, true);
oo_.steady_state = evaluate_steady_state(oo_.steady_state, oo_.exo_steady_state, M_, options_, true);
oo_.endo_simul(:, end) = oo_.steady_state;
options_.periods = 2;
saved_endo = oo_.endo_simul(:, 1:5);

View File

@ -50,7 +50,7 @@ perfect_foresight_solver;
oo_.exo_simul(3,1) = 1.3;
oo_.exo_steady_state = 1.1;
oo_.exo_simul(9:10, 1) = repmat(oo_.exo_steady_state', 2, 1);
oo_.steady_state = evaluate_steady_state(oo_.steady_state, M_, options_, oo_, true);
oo_.steady_state = evaluate_steady_state(oo_.steady_state, oo_.exo_steady_state, M_, options_, true);
oo_.endo_simul(:, 10) = oo_.steady_state;
saved_endo = oo_.endo_simul(:, 1);
saved_exo = oo_.exo_simul(1, :);
@ -64,7 +64,7 @@ oo_.exo_simul = [ saved_exo; oo_.exo_simul ];
oo_.exo_simul(4,1) = 1.4;
oo_.exo_steady_state = 1.2;
oo_.exo_simul(9:11, 1) = repmat(oo_.exo_steady_state', 3, 1);
oo_.steady_state = evaluate_steady_state(oo_.steady_state, M_, options_, oo_, true);
oo_.steady_state = evaluate_steady_state(oo_.steady_state, oo_.exo_steady_state, M_, options_, true);
oo_.endo_simul(:, 11) = oo_.steady_state;
saved_endo = oo_.endo_simul(:, 1:2);
saved_exo = oo_.exo_simul(1:2, :);
@ -79,7 +79,7 @@ oo_.exo_simul(7,1) = 1.1;
oo_.exo_simul(8,1) = 1.1;
oo_.exo_steady_state = 1.1;
oo_.exo_simul(9:14, 1) = repmat(oo_.exo_steady_state', 6, 1);
oo_.steady_state = evaluate_steady_state(oo_.steady_state, M_, options_, oo_, true);
oo_.steady_state = evaluate_steady_state(oo_.steady_state, oo_.exo_steady_state, M_, options_, true);
oo_.endo_simul(:, 12:13) = repmat(initial_steady_state, 1, 2);
oo_.endo_simul(:, 14) = oo_.steady_state;
saved_endo = oo_.endo_simul(:, 1:5);

View File

@ -92,7 +92,7 @@ perfect_foresight_solver;
oo_.exo_simul(3,1) = 1.3;
oo_.exo_steady_state = 1.1;
oo_.exo_simul(end, 1) = oo_.exo_steady_state;
oo_.steady_state = evaluate_steady_state(oo_.steady_state, M_, options_, oo_, true);
oo_.steady_state = evaluate_steady_state(oo_.steady_state, oo_.exo_steady_state, M_, options_, true);
oo_.endo_simul(:, end) = oo_.steady_state;
options_.periods = 6;
saved_endo = oo_.endo_simul(:, 1);
@ -108,7 +108,7 @@ oo_.exo_simul(4,1) = 1.4;
oo_.exo_simul(8,1) = 1.5;
oo_.exo_steady_state = 1.1+0.1;
oo_.exo_simul(end, 1) = oo_.exo_steady_state;
oo_.steady_state = evaluate_steady_state(oo_.steady_state, M_, options_, oo_, true);
oo_.steady_state = evaluate_steady_state(oo_.steady_state, oo_.exo_steady_state, M_, options_, true);
oo_.endo_simul(:, end) = oo_.steady_state;
options_.periods = 5;
saved_endo = oo_.endo_simul(:, 1:2);
@ -124,7 +124,7 @@ oo_.exo_simul(7,1) = 1*0.8;
oo_.exo_simul(8,1) = 1.5*0.8;
oo_.exo_steady_state = (1.1+0.1)*0.75;
oo_.exo_simul(end, 1) = oo_.exo_steady_state;
oo_.steady_state = evaluate_steady_state(oo_.steady_state, M_, options_, oo_, true);
oo_.steady_state = evaluate_steady_state(oo_.steady_state, oo_.exo_steady_state, M_, options_, true);
oo_.endo_simul(:, end) = oo_.steady_state;
options_.periods = 2;
saved_endo = oo_.endo_simul(:, 1:5);

View File

@ -24,7 +24,7 @@ function output_cell =posterior_function_demo(xparam1,M_,options_,oo_,estim_para
% output_cell [1 by n cell] 1 by n Matlab cell allowing to store any
% desired computation or result (strings, matrices, structures, etc.)
% Copyright © 2015 Dynare Team
% Copyright © 2015-2023 Dynare Team
%
% This file is part of Dynare.
%
@ -49,8 +49,8 @@ output_cell{1,1}=mean(xparam1);
% set the parameters draws to the model structure
M_ = set_all_parameters(xparam1,estim_params_,M_);
% compute the steady state for the parameter draw written to M_
[ys,params,info] = evaluate_steady_state(oo_.steady_state,M_,options_,oo_,0);
[ys,params,info] = evaluate_steady_state(oo_.steady_state,[oo_.exo_steady_state; oo_.exo_det_steady_state],M_,options_,false);
%set second part of output cell
output_cell{1,2}=ys';
end
end