addpath('function')

% Define the horizon
hor = 40;
shock_var = 1;
titles = 'EPU';
oo=1;
save_name = '';

% This file computes IRF for constant sigmas
irf_median_rolling = zeros(nvar_help,nvar_help,hor,ii);
irf_ub_rolling = zeros(nvar_help,nvar_help,hor,ii);
irf_lb_rolling = zeros(nvar_help,nvar_help,hor,ii);
irf_all_win = zeros(nvar_help,nvar_help,hor,nsave,ii);

% These values ensure that the shock size is the same in all countries
% for country_euro_oo
% italy = 0.979;
% germany = 1;
france = 1.027;
% euro= 1.025;

% for country_euro_oo_sr
% italy= 0.977;
% germany= 1;
% france= 1.035;
% euro= 1.029;


% Fix variance covariance matrix to ist posterior median
SIGMA_draws=repmat(squeeze(median(squeeze(median(SIGMA_draws_win(:,:,:,:),3)),3))*france,1,1,1000);

time_of_response = char('01-Sep-2006', '01-Sep-2011', '01-Sep-2016');

tic
for k= 1:ii
% get coefficient for certain point in time
ALPHA_draws=squeeze(ALPHA_draws_win(:,:,:,k));
%SIGMA_draws=squeeze(SIGMA_draws_win(:,:,:,k));

% calculate IRF
[irf_mat_all, irf_median, irf_ub, irf_lb,fevd_median, fevd_ub, fevd_lb, total_spillover_median, total_spillover_ub, total_spillover_lb, spillover_from_median, spillover_from_ub, spillover_from_lb, spillover_to_median, spillover_to_lb, spillover_to_ub] = IRF1(SIGMA_draws, ALPHA_draws, hor,  0.84, 0.16, 0, 0, 0);

% safe IRF matrices
irf_all_win(:,:,:,:,k) = irf_mat_all;
irf_median_rolling(:,:,:,k) = irf_median;
% irf_ub_rolling(:,:,:,k) = irf_ub;
% irf_lb_rolling(:,:,:,k) = irf_lb;
disp(k)
toc
end

StartDate = datetime(2005,03,01);
EndDate = datetime(2018,01,01);
dates = datenum(StartDate:calmonths(1):EndDate);
[XXX,YYY] = meshgrid(dates,1:hor);

% Set time point for confidence interval
[time1]=strmatch(time_of_response(1,:), datestr(dates));
[time2]=strmatch(time_of_response(2,:), datestr(dates));
[time3]=strmatch(time_of_response(3,:), datestr(dates));

time=[time1 time2 time3];

% On EPU
subplot(1,4,1)
handles = meshc(XXX,YYY,squeeze(irf_median_rolling(1,shock_var,:,:)));
datetick('x','yy')
xlim([datenum(StartDate) datenum(EndDate)])
if oo==0
title('\fontsize{14}EPU Europe on HICP')
zlim([-0.2 0.2])
else
zlim([-0.2 0.30])
title('\fontsize{14}EPU on EPU')    
end
hContour = handles(2);
hContour.ContourZLevel = -0.2;

% EPU
    for i=1:3
    subplot(1,4,i+1)
        a = median(squeeze(irf_all_win(1,shock_var,:,:,time(i))),2);
        b1 = quantile(squeeze(irf_all_win(1,shock_var,:,:,time(i))),0.05,2);
        b2 = quantile(squeeze(irf_all_win(1,shock_var,:,:,time(i))),0.16,2);
        b3 = quantile(squeeze(irf_all_win(1,shock_var,:,:,time(i))),0.84,2);
        b4 = quantile(squeeze(irf_all_win(1,shock_var,:,:,time(i))),0.95,2);
        %figure
        plot(1:hor,a,'Color', 'black')
        xlim([1,hor])
        hold on 
        plot(1:hor,b1,'Color','none')
        plot(1:hor,b2,'Color','none')
        plot(1:hor,b3,'Color','none')
        plot(1:hor,b4,'Color','none')
        plot(1:hor, zeros(1,size(1:hor,2)), 'Color', 'black')
        x_axis2 = [1:hor,  fliplr(1:hor)];
        inBetween = [a', fliplr(b2')];
        fill(x_axis2, inBetween,[0.75    0.75    0.75],'LineStyle','none');
        inBetween = [b2', fliplr(b1')];
        fill(x_axis2, inBetween,[0.9   0.9   0.9],'LineStyle','none','FaceAlpha',.8);
        inBetween = [a', fliplr(b3')];
        fill(x_axis2, inBetween,[0.75    0.75    0.75],'LineStyle','none');
        inBetween = [b3', fliplr(b4')];
        fill(x_axis2, inBetween,[0.9    0.9   0.9],'LineStyle','none','FaceAlpha',.8);
        plot(1:hor,a,'Color', 'black')
        if oo==0
        ylim([-0.2 0.2])
        title(['\fontsize{14}' titles ' on HCPI ' datestr(time_of_response(i,:), 'mmm yyyy')])
        else
        title(['\fontsize{14}' titles ' on EPU ' datestr(time_of_response(i,:), 'mmm yyyy')])
        ylim([-0.2 0.30])    
        end
        hold off
    end
    
temp=['pics\new\', dataset, save_name, 'EPU_3D_2D.png'];        
% saveas(gcf,temp);
export_fig(temp, '-transparent')  

% on HICP
subplot(1,4,1)
handles = meshc(XXX,YYY,squeeze(irf_median_rolling(2,shock_var,:,:)));
datetick('x','yy')
xlim([datenum(StartDate) datenum(EndDate)])
if oo==0
zlim([-1.5 0.5])
title('\fontsize{14}EPU Europe on IPI')
else
title('\fontsize{14}EPU on HICP')
zlim([-0.2 0.2])    
end
hContour = handles(2);
hContour.ContourZLevel = -0.2;

   % HICP
    for i=1:3
    subplot(1,4,i+1)
        a = median(squeeze(irf_all_win(2,shock_var,:,:,time(i))),2);
        b1 = quantile(squeeze(irf_all_win(2,shock_var,:,:,time(i))),0.05,2);
        b2 = quantile(squeeze(irf_all_win(2,shock_var,:,:,time(i))),0.16,2);
        b3 = quantile(squeeze(irf_all_win(2,shock_var,:,:,time(i))),0.84,2);
        b4 = quantile(squeeze(irf_all_win(2,shock_var,:,:,time(i))),0.95,2);
        %figure
        plot(1:hor,a,'Color', 'black')
        xlim([1,hor])
        hold on 
        plot(1:hor,b1,'Color','none')
        plot(1:hor,b2,'Color','none')
        plot(1:hor,b3,'Color','none')
        plot(1:hor,b4,'Color','none')
        plot(1:hor, zeros(1,size(1:hor,2)), 'Color', 'black')
        x_axis2 = [1:hor,  fliplr(1:hor)];
        inBetween = [a', fliplr(b2')];
        fill(x_axis2, inBetween,[0.75    0.75    0.75],'LineStyle','none');
        inBetween = [b2', fliplr(b1')];
        fill(x_axis2, inBetween,[0.9   0.9   0.9],'LineStyle','none','FaceAlpha',.8);
        inBetween = [a', fliplr(b3')];
        fill(x_axis2, inBetween,[0.75    0.75    0.75],'LineStyle','none');
        inBetween = [b3', fliplr(b4')];
        fill(x_axis2, inBetween,[0.9    0.9   0.9],'LineStyle','none','FaceAlpha',.8);
        plot(1:hor,a,'Color', 'black')
        if oo==0
        ylim([-1.5 0.5])
        title(['\fontsize{14}' titles ' on IPI ' datestr(time_of_response(i,:), 'mmm yyyy')])
        else 
        ylim([-0.2 0.2])
        title(['\fontsize{14}' titles ' on HCPI ' datestr(time_of_response(i,:), 'mmm yyyy')])            
        end
        hold off
    end
    
temp=['pics\new\', dataset, save_name, 'HICP_3D_2D.png'];        
% saveas(gcf,temp);
export_fig(temp, '-transparent')  

% IPI
subplot(1,4,1)
handles = meshc(XXX,YYY,squeeze(irf_median_rolling(3,shock_var,:,:)));
datetick('x','yy')
xlim([datenum(StartDate) datenum(EndDate)])
if oo==0
zlim([-0.2 0.05])
title('\fontsize{14}EPU Europe on EONIA')
else
zlim([-1.5 1])
title('\fontsize{14}EPU on IPI')   
end
hContour = handles(2);
hContour.ContourZLevel = -1.5;

    % IPI
    for i=1:3
    subplot(1,4,i+1)
        a = median(squeeze(irf_all_win(3,shock_var,:,:,time(i))),2);
        b1 = quantile(squeeze(irf_all_win(3,shock_var,:,:,time(i))),0.05,2);
        b2 = quantile(squeeze(irf_all_win(3,shock_var,:,:,time(i))),0.16,2);
        b3 = quantile(squeeze(irf_all_win(3,shock_var,:,:,time(i))),0.84,2);
        b4 = quantile(squeeze(irf_all_win(3,shock_var,:,:,time(i))),0.95,2);
        %figure
        plot(1:hor,a,'Color', 'black')
        xlim([1,hor])
        hold on 
        plot(1:hor,b1,'Color','none')
        plot(1:hor,b2,'Color','none')
        plot(1:hor,b3,'Color','none')
        plot(1:hor,b4,'Color','none')
        plot(1:hor, zeros(1,size(1:hor,2)), 'Color', 'black')
        x_axis2 = [1:hor,  fliplr(1:hor)];
        inBetween = [a', fliplr(b2')];
        fill(x_axis2, inBetween,[0.75    0.75    0.75],'LineStyle','none');
        inBetween = [b2', fliplr(b1')];
        fill(x_axis2, inBetween,[0.9   0.9   0.9],'LineStyle','none','FaceAlpha',.8);
        inBetween = [a', fliplr(b3')];
        fill(x_axis2, inBetween,[0.75    0.75    0.75],'LineStyle','none');
        inBetween = [b3', fliplr(b4')];
        fill(x_axis2, inBetween,[0.9    0.9   0.9],'LineStyle','none','FaceAlpha',.8);
        plot(1:hor,a,'Color', 'black')
        if oo==0
        title(['\fontsize{14}' titles ' on EONIA ' datestr(time_of_response(i,:), 'mmm yyyy')])
        ylim([-0.2 0.05])
        else
        ylim([-1.5 1])
        title(['\fontsize{14}' titles ' on IPI ' datestr(time_of_response(i,:), 'mmm yyyy')])            
        end
        hold off
    end
    
temp=['pics\new\', dataset, save_name, 'IPI_3D_2D.png'];        
% saveas(gcf,temp);
export_fig(temp, '-transparent')  

% IR
subplot(1,4,1)
handles = meshc(XXX,YYY,squeeze(irf_median_rolling(4,shock_var,:,:)));
datetick('x','yy')
xlim([datenum(StartDate) datenum(EndDate)])
if oo== 0
zlim([-0.2 0.5])
title('\fontsize{14}EPU Europe on EPU Europe')
else
zlim([-0.3 0.1])
title('\fontsize{14}EPU on EONIA')    
end
hContour = handles(2);
hContour.ContourZLevel = -0.3;

   % IR
    for i=1:3
    subplot(1,4,i+1)
        a = median(squeeze(irf_all_win(4,shock_var,:,:,time(i))),2);
        b1 = quantile(squeeze(irf_all_win(4,shock_var,:,:,time(i))),0.05,2);
        b2 = quantile(squeeze(irf_all_win(4,shock_var,:,:,time(i))),0.16,2);
        b3 = quantile(squeeze(irf_all_win(4,shock_var,:,:,time(i))),0.84,2);
        b4 = quantile(squeeze(irf_all_win(4,shock_var,:,:,time(i))),0.95,2);
        %figure
        plot(1:hor,a,'Color', 'black')
        xlim([1,hor])
        hold on 
        plot(1:hor,b1,'Color','none')
        plot(1:hor,b2,'Color','none')
        plot(1:hor,b3,'Color','none')
        plot(1:hor,b4,'Color','none')
        plot(1:hor, zeros(1,size(1:hor,2)), 'Color', 'black')
        x_axis2 = [1:hor,  fliplr(1:hor)];
        inBetween = [a', fliplr(b2')];
        fill(x_axis2, inBetween,[0.75    0.75    0.75],'LineStyle','none');
        inBetween = [b2', fliplr(b1')];
        fill(x_axis2, inBetween,[0.9   0.9   0.9],'LineStyle','none','FaceAlpha',.8);
        inBetween = [a', fliplr(b3')];
        fill(x_axis2, inBetween,[0.75    0.75    0.75],'LineStyle','none');
        inBetween = [b3', fliplr(b4')];
        fill(x_axis2, inBetween,[0.9    0.9   0.9],'LineStyle','none','FaceAlpha',.8);
        plot(1:hor,a,'Color', 'black')
        if oo==0
        title(['\fontsize{14}' titles ' on EPU Europe ' datestr(time_of_response(i,:), 'mmm yyyy')])
        ylim([-0.2 0.5])
        else
        title(['\fontsize{14}' titles ' on EONIA ' datestr(time_of_response(i,:), 'mmm yyyy')])
        ylim([-0.3 0.1])            
        end
        hold off
    end

temp=['pics\new\', dataset, save_name, 'EONIA_3D_2D.png'];        
% saveas(gcf,temp);
export_fig(temp, '-transparent')   
