From 4d508d8e12dc73632d742f7439e756efd03f5f77 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Mon, 3 Feb 2020 18:02:55 +0100
Subject: [PATCH] Discretionary policy: preparatory work for estimation
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This removes global variables from discretionary_policy_1.m, and also adapts
the behaviour and interface of the function so that it is similar to
resol.m (in particular, it no longer returns an empty “dr” in case of failure,
and it sets “oo_.dr”).

Ref. #1173

(cherry picked from commit 59e11615a083c2e95dba6257f6dfaa9ce0f5731a)
---
 matlab/discretionary_policy_1.m | 19 +++++++++----------
 matlab/stoch_simul.m            |  4 ++--
 2 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/matlab/discretionary_policy_1.m b/matlab/discretionary_policy_1.m
index 4f18ec737f..16143e44ec 100644
--- a/matlab/discretionary_policy_1.m
+++ b/matlab/discretionary_policy_1.m
@@ -1,6 +1,8 @@
-function [dr,ys,info]=discretionary_policy_1(oo_,Instruments)
+function [dr, info, M_, options_, oo_]=discretionary_policy_1(Instruments, M_, options_, oo_)
+% Higher-level function for solving discretionary optimal policy
+% Has the same interface as resol.m
 
-% Copyright (C) 2007-2018 Dynare Team
+% Copyright (C) 2007-2020 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -17,11 +19,8 @@ function [dr,ys,info]=discretionary_policy_1(oo_,Instruments)
 % You should have received a copy of the GNU General Public License
 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 
-global M_ options_
 persistent Hold
 
-dr = [];
-ys = [];
 info = 0;
 
 if isempty(options_.qz_criterium)
@@ -150,15 +149,15 @@ else
     [H,G,info]=discretionary_policy_engine(Alag,A0,Alead,B,W,instr_id,beta,solve_maxit,discretion_tol,qz_criterium);
 end
 
+% set the state
+dr=oo_.dr;
+
 if info
-    dr=[];
     return
 else
     Hold=H; %save previous solution
             % Hold=[]; use this line if persistent command is not used.
 end
-% set the state
-dr=oo_.dr;
 dr.ys =zeros(endo_nbr,1);
 dr=set_state_space(dr,M_,options_);
 order_var=dr.order_var;
@@ -168,8 +167,8 @@ dr.ghu=G(order_var,:);
 Selection=lead_lag_incidence(1,order_var)>0;%select state variables
 dr.ghx=T(:,Selection);
 
-ys=NondistortionarySteadyState(M_);
-dr.ys=ys; % <--- dr.ys =zeros(NewEndo_nbr,1);
+dr.ys=NondistortionarySteadyState(M_);
+oo_.dr = dr;
 
 function ys=NondistortionarySteadyState(M_)
 if exist([M_.fname,'_steadystate.m'],'file')
diff --git a/matlab/stoch_simul.m b/matlab/stoch_simul.m
index 9d6d798b52..4740e70323 100644
--- a/matlab/stoch_simul.m
+++ b/matlab/stoch_simul.m
@@ -1,6 +1,6 @@
 function [info, oo_, options_] = stoch_simul(M_, options_, oo_, var_list)
 
-% Copyright (C) 2001-2019 Dynare Team
+% Copyright (C) 2001-2020 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -82,7 +82,7 @@ elseif options_.discretionary_policy
     if ~options_.linear
         error('discretionary_policy: only linear-quadratic problems can be solved');
     end
-    [oo_.dr, ~, info] = discretionary_policy_1(oo_,options_.instruments);
+    [~,info,M_,options_,oo_] = discretionary_policy_1(options_.instruments,M_,options_,oo_);
 else
     if options_.logged_steady_state %if steady state was previously logged, undo this
         oo_.dr.ys=exp(oo_.dr.ys);
-- 
GitLab