clear; clc; close all;
cd('/Users/julioortiz/Dropbox/ME/replication/code/Appendix_D');
addpath '/Users/julioortiz/Dropbox/ME/replication/processed_data';

data_bgms    = -0.2029366;
data_bgms_se = 0.0865532;
data_cg      = 0.6279067;
data_cg_se   = 0.2729455;

load('simulation_results.mat');


sim_bgms    = [data_bgms    BGMS(1,:)];
sim_bgms_sd = [data_bgms_se  BGMS(2,:)];

sim_cg    = [data_cg CG(1,:)];
sim_cg_sd = [data_cg_se CG(2,:)];

bgmslb = sim_bgms - 1.96*sim_bgms_sd;
bgmsub = sim_bgms + 1.96*sim_bgms_sd;
cglb   = sim_cg - 1.96*sim_cg_sd;
cgub   = sim_cg + 1.96*sim_cg_sd;

Y   = [sim_bgms;sim_cg];
Ylb = [bgmslb;cglb];
Yub = [bgmsub;cgub];
err = 1.96*[sim_bgms_sd;sim_cg_sd];

%% Plot
X = categorical({'Data' 'Rational','Overconfidence', 'Diagnostic' 'Misspecified'});
X = reordercats(X,{'Data' 'Rational','Overconfidence', 'Diagnostic' 'Misspecified'});

x0=10;
y0=10;
width=750;
height=400;

%BGMS
f0=figure;
hold on;
ylim([-0.65 0.65]);
yticks([-0.50 -0.25 0 0.25 0.5]);
b1=bar(X,Y(1,:),'FaceColor','w','EdgeColor','none','FaceAlpha',0);
b1err=errorbar(X,Y(1,:),err(1,:),'LineStyle','none','LineWidth',2,'Color','k');
h1=yline(0,'-k','LineWidth',1);
% h1=yline(0,'-k','LineStyle','--');
p1=plot(X,sim_bgms, 'o','MarkerSize',6,'MarkerFaceColor','k');
set(gca,'FontSize',16,'TickLabelInterpreter', 'latex','ycolor','k','YGrid','on','XGrid','off');
hold off;
set(gcf,'position',[x0,y0,width,height])
saveas(gcf,'/Users/julioortiz/Dropbox/ME/replication/results/FigureD1','epsc');


%CG
X = categorical({'Data' 'Rational','Overconfidence', 'Diagnostic' 'Misspecified'});
X = reordercats(X,{'Data' 'Rational','Overconfidence', 'Diagnostic' 'Misspecified'});

x0=10;
y0=10;
width=750;
height=400;

f0=figure;
hold on;
ylim([-0.50 1.50]);
yticks([-0.50 -0.25 0.0 0.25 0.5 0.75 1.0 1.25 1.5]);
b1=bar(X,Y(2,:),'FaceColor','w','EdgeColor','none','FaceAlpha',0);
b1err=errorbar(X,Y(2,:),err(2,:),'LineStyle','none','LineWidth',2,'Color','k');
h1=yline(0,'-k','LineWidth',1);
p1=plot(X,sim_cg, 'o','MarkerSize',6,'MarkerFaceColor','k');
set(gca,'FontSize',16,'TickLabelInterpreter', 'latex','ycolor','k','YGrid','on','XGrid','off');
hold off;
set(gcf,'position',[x0,y0,width,height])
saveas(gcf,'/Users/julioortiz/Dropbox/ME/replication/results/FigureD2','epsc');


%Disagree
data_disagree= [1.537929 1.106231 0.9187855 0.8547304];

disagree=[data_disagree;
         disagree(1:4);
         disagree(5:8);
         disagree(9:12);
         disagree(13:16)];
     
disagree = disagree./disagree(:,1);

X = categorical({'CQ' '1Q','2Q', '3Q'});
X = reordercats(X,{'CQ' '1Q','2Q', '3Q'});

x0=10;
y0=10;
width=750;
height = 11;
aspect = 16/9;

green = [0.0 0.6 0.0];
blue  = [0.0 0.0 0.6];
red   = [0.6 0.0 0.0];
myred= [0.6350 0.0780 0.1840];
myblue = [0 0.4470 0.7410];

figure('units','centimeter','Position',[10 10 1.75*height*aspect 1.75*height],'color','w')
hold on;
h1=plot([0 1 2 3],disagree(1,:),'-+','Color','k','MarkerSize',10,'LineWidth',3,'MarkerFaceColor','k');
% h2=plot([0 1 2 3],disagree(2,:),'-*','Color','green','MarkerSize',10,'LineWidth',3,'MarkerFaceColor','green');
% h3=plot([0 1 2 3],disagree(3,:),'-x','Color','m','MarkerSize',10,'LineWidth',3,'MarkerFaceColor','m');
h4=plot([0 1 2 3],disagree(4,:),'-^','Color',myred,'MarkerSize',10,'LineWidth',3,'MarkerFaceColor',myred);
h5=plot([0 1 2 3],disagree(5,:),'-o','Color',myblue,'MarkerSize',10,'LineWidth',3,'MarkerFaceColor',myblue);
hold off;
xlim([0 3])
xticks([0 1 2 3])
set(gca,'FontSize',16,'TickLabelInterpreter', 'latex','XTickLabels',{'CQ','1Q','2Q','3Q'})
ylabel('Disagreement (normalized to CQ=1)','interpreter','latex')
legend({'Data','Other models','Misspecified expectations'},'Location','northeast','Interpreter','Latex','FontSize',14,'Location','Southwest')
legend box off
saveas(gcf,'/Users/julioortiz/Dropbox/ME/replication/results/FigureD5','epsc')
