%plot_policy_dp_xi83.m 
%Produces Figure 12 `Debt Policy Functions with Fundamental Uncertainty, \xi=0.83' in 
%``Multiple Equilibria in Open Economies with  Collateral Constraints,''  
% by Stephanie Schmitt-Grohé and Martín Uribe. 
%©  Stephanie Schmitt-Grohé and Martín Uribe, January 2018.

clear all
clf
step = 1; 
rows = 2;
cols = 2;

fname = {'constrained_152018ab_xi83',  'ramsey_1122018_xi83'};
%produced by running simu('constrained_152018ab_xi83'),  simu(ramsey_1122018_xi83')

nf = numel(fname); 

load simu_constrained_152018ab_xi83 ny nd YT YN yTgridlevel yNgridlevel yT yN a rstar kapa xi dgrid
%produced by running simu('constrained_152018ab_xi83'), 

%Find the grid point for the  mean of yT and yN
x = 0*std(YT);
y = mean(YT); 
[zT, zTix] = min(abs(yT(:,1)-(y-x)));
yT_m = yT(zTix); 
x = 0*std(YN);
y = mean(YN); 
[zN, zNix] = min(abs(yN(:,1)-( y-x)));
yN_m = yN(zNix); 
s1 = find(yT(:,1)==yT_m & yN(:,1)==yN_m);

ddpp = zeros(ny,nd,3); 
ccrisis = ddpp; 
sslope = ddpp;
for i = 1:nf
eval(['load simu_' fname{i} '.mat  lad dgrid d dp  crisis '])
ddpp(:,:,i) = dp; 
end

dpA=squeeze(ddpp(s1,:,1));
dpR=squeeze(ddpp(s1,:,2));

crit = {'Competitive Equilibrium','Ramsey'};
k=1;
%plot dpA
subplot(rows, cols, k)
%Now plot dpA in two parts
plot(dgrid, dpA, 'g')
hold on 
plot(dgrid,dgrid, ':')
xlim([min(dpA) dgrid(end)])
ylim([min(dpA) dgrid(end)])

title(crit(1))
xlabel('$d_t$', 'Interpreter', 'LaTeX')
ylabel('$d_{t+1}$', 'Interpreter', 'LaTeX')


%plot dpR
%for the Ramsey case find  the largest D such that yT and yN are at the  mean. 
load simu_ramsey_1122018_xi83  YT YN D Dp yT yN dgrid
find(YT==yT(zTix)& YN==yN(zNix));
d_high = max(D(ans));
d_low = min(D(ans));

k=k+step;
subplot(rows, cols, k)

%Now plot dpR in  parts
plot(dgrid, dpR)
hold on 
plot(dgrid,dgrid, ':')
xlim([min(dpR) dgrid(end)])
ylim([min(dpR) dgrid(end)])

title(crit(2))
xlabel('$d_t$', 'Interpreter', 'LaTeX')
ylabel('$d_{t+1}$', 'Interpreter', 'LaTeX')
shg