%% BBCDL_figure3.m produces Figure 3 of the paper "Macroeconomic Policy Games" 
% by Martin Bodenstein, Luca Guerrieri, and Joe LaBriola.
%%
% The figure produces two sets of results:
% 1) A second-order check for the cooperative solution (ramsey)
%    and, in this case, the relevant model shadow_ramsey.mod
% 2) A second-order check for the Nash game 
%    and, in this case, the relevant model is shadow_nash.mod
%
% Both models were generated by our toolbox, starting from
% BBCDLmodelcomp.mod, which is the original decentralized model.
%
% The relevant steps to generate these files are detailed below.
%
% For the check on the cooperative solution, the first step is to use the toolbox to 
% to genrate that solution, which is achieved by invoking:
% convertmodfiles('BBCDLmodelcomp','ramsey','c1pid','c2pid',1)
%
% Notice that setting the last input argument of convertmodelfiles to 1
% has the effect of including in the output file a new variable, 
% Welf_correction, which captures the modification to the objective 
% function implied by the timeless perspective. See Section 2 of our paper.
% This variable is essential to avoid spurious welfare results when switching from
% the cooperative policies to an alternative policy.
%
% 
% To perform the second-order check we need to create another .mod file, by
% invoking:
% add_shadow_economy('BBCDLmodelcomp','ramsey','c1pid','c2pid');
% This routine stacks together the original decentralized model and the 
% model with the cooperative policy. The output .mod file 
% is called shadow_BBCDLmodelcomp_ramsey_c1pid_c2pid.mod
% To keep track of the endogenous variables from both model, the endogenous
% variables that come from the original decentralized model now have the 
% prefix "shadow" at the beginning of the original variable names.
%
% At this point we are almost good to go, but we need to add the 
% Welfcorrection variable to the "shadow" decentralized model.
% To achieve this feat of numerical engineering, search the file
% shadow_BBCDLmodelcomp_ramsey_c1pid_c2pid.mod for the variable
% Welfcorrection. You will find a line that is commented out that 
% contains the definition we want to use -- Welfcorrection (NB: there's another
% line containing "Welfcorrection" that needs to stay as it is).
% Uncomment that line and attach the prefix "shadow_" to the variable
% "Welfcorrection" on the left-hand-side of the equation.
% Also, add "shadow_Welfcorrection" to the list of endogenous variables --
% it should be the very first variable listed after "var" in the .mod file.
%
% The next step is to modify the steady state file.
% Edit shadow_BBCDLmodelcomp_ramsey_c1pid_c2pid_steadystate.m
% We need to add an element to the vector ys, defined starting on line 13.
% We should add "0" as the first entry (all the other entries slide down).
% 0 is the steady-state value of shadow_Welfcorrection, the endogeous
% variable we just added.
%
% At this point we are almost done. The only rub is that the name of the
% .mod file we just created is too long for Dynare. So we need to shorten
% it. For this purpose, we need to take 3 steps:
% 1. change the name of the .mod file with the command
% !mv shadow_BBCDLmodelcomp_ramsey_c1pid_c2pid.mod shadow_ramsey.mod
% 2. change the name of the steady state file with the command
% !mv shadow_BBCDLmodelcomp_ramsey_c1pid_c2pid_steadystate.m shadow_ramsey_steadystate.m
% 3. change the name of the Dynare static function invoked in the steady state file to
% match the new name. For this purpose edit shadow_ramsey_steadystate.m
% and in the last line replace
% "shadow_BBCDLmodelcomp_ramsey_c1pid_c2pid_static"
% with "shadow_ramsey_static".
%
% At this point we need to replace the garden-variety Taylor-type rule 
% in the original decentralized economy with a rule that can nest both the
% optimal cooperative policy and convex combination of that policy we
% another (suboptimal) desired policy.
% In the paper we used a rule that responds to inflation only in the home
% country  (c1 in the notation of the .mod file) country.
% To this purpose replace the policy rule for country 1 with 
% shadow_c1pid = shadow_c1rn_weight + (1-shadow_c1rn_weight)*c1pid;
% and the policy rule for country 2 with
% shadow_c2pid = c2pid;
% The latter choice has the effect of keeping the optimal policy in the "shadow"
% economy for the foreign country.
% 
% 
% The convention used here is that the innovations to the shocks are 
% set as Standard Normal in the shock block (i.e. std 1), but the processes for the
% shocks are defined as to incorporate the desired standard deviation
% (which multiplies the innovation). Notice that if the shock block is
% missing, Dynare assumes the innovations have mean 0 when deriving the
% model solution.
%
% The code below shows how to use the Welfcorrection and
% shadow_Welfcorrection terms for any comparison between models that
% involves policymakers breaking previous commitments.
%
% The steps necessary to create the the file shadow_nash.mod in this
% directory are analogous to those outlined above for the file
% shadow_ramsey.mod. 
% The exception is that the .mod file for the open-loop Nash policies will
% offer two welfare correction terms, one per regulator.
%%
setpathdynare6_5

loadswitch = 0;
if loadswitch == 1
    load shadow_nash_results_constinfl Welf_diff grid_weight
    load shadow_ramsey_results_constinfl Welf_diff grid_weight
else
    call_conditional_welfare_shadow_loop_ramsey
    call_conditional_welfare_shadow_loop_nash
end

figure
subplot(2,1,1)
plot(grid_weight,Welf_diff,'b-','LineWidth',1.5)
title('Welfare Difference Between the Optimal Cooperative Policy  and \newline              a Suboptimal Policy with Weight on an Instrument Rule','FontWeight','Normal')
ylabel('Welfare Difference')
xlabel('Weight on Instrument Rule \upsilon^s')

subplot(2,1,2)
plot(grid_weight,Welf_diff,'r--','LineWidth',1.5)
title('Home-Country Welfare Difference Between the Optimal Open-Loop Nash Policy and \newline              a Suboptimal Policy with Weight on an Instrument Rule','FontWeight','Normal') 
xlabel('Weight on Optimal Rule')
ylabel('Welfare Difference')
xlabel('Weight on Instrument Rule \upsilon^s')

printpref
