%% This code generates Figure A.2

clc;
clear;

Path_FPA          = [cd, '/data_clean', '/FPA', '/FPA_sum.mat'];
Path_IO           = [cd, '/data_clean', '/IO_table', '/IO_sum.mat'];
Path_markup       = [cd, '/data_clean', '/markup', '/markup_sum.mat'];

load(Path_FPA);
load(Path_IO);
load(Path_markup);

addpath('functions');


%% Baseline calibration

% Number of industries
N              = 66;

% Preference Parameters
gamma          = 1;
varphi         = 2;

% Substitution elasticities
sigma_c        = 1;
sigma          = 0.5 * ones(N,1);


% transforming the data from monthly to quarterly and calculating the
% degree of price flexibility of each industry
theta          = 1 - exp(-3*FPA);

% Calibrate the IO Table
IO             = IO_matrix_sum;
Final          = IO(:,93,:);
IO(isnan(IO))  = 0;
IO(IO < 0)     = 0;

% Labor as single input
grosscost      = squeeze(sum(IO(1:66,1:66,:)))+squeeze(IO(75,1:66,:));
labor          = squeeze(IO(75,1:66,:))./grosscost;
IO(67:end,:,:) = [];
IO(:,67:end,:) = [];



yr             = 1997:2015;
T              = zeros(19,2);
supplyside     = zeros(19,2);
demandside     = zeros(19,2);

for k = 1

    for t = 1:length(yr)

        year         = yr(t);
        mu           = mu_sum(:, t, k);

        % Baseline calibration

        % Time-varing IO
        temp         = IO(:,:,t);
        temp         = temp';
        Omega        = bsxfun(@rdivide, temp, grosscost(:,t));
        alpha        = labor(:,t);
        beta         = Final(1:66,t);
        beta(beta<0) = 0;
        beta         = beta/sum(beta);
        [supplyside(t,1), demandside(t,1)] = outputresponse(mu, theta, sigma_c, sigma, Omega, alpha, beta, gamma, varphi);

        % No IO

        Omega        = zeros(N,N);
        alpha        = ones(N,1);
        [supplyside(t,2), demandside(t,2)] = outputresponse(mu, theta, sigma_c, sigma, Omega, alpha, beta, gamma, varphi);

    end

    T = supplyside + demandside;



    figure('Position', [100, 100, 560, 180]) % [left, bottom, width, height] in pixels

    % Set the default font for the figure to Arial
    set(gcf, 'DefaultAxesFontName', 'Times New Roman');

    % Define a deep yellow color using RGB values
    purple = [0.5, 0, 0.5];  % This is deep yellow

    subplot(1,2,1);
    hold on;
    plot(yr, supplyside(:,1), '-b', 'LineWidth', 1); % Blue solid line
    plot(yr, demandside(:,1), '--', 'Color', purple, 'LineWidth', 1); % Deep yellow dash-dot line
    plot(yr, T(:,1), ':o', 'Color', 'r', 'LineWidth', 1, 'MarkerFaceColor', 'r', 'MarkerEdgeColor', 'r', 'MarkerSize', 3);
    box on;
    xlim([1997, 2015]);
    title("(a) Benchmark", 'FontSize', 10, 'FontName', 'Times New Roman','FontWeight', 'normal');
    xlabel('Year', 'FontSize', 10);
    ylabel('Output Response', 'FontSize', 10, 'Interpreter','latex');


    subplot(1,2,2);
    hold on;
    plot(yr, supplyside(:,2), '-b', 'LineWidth', 1); % Blue solid line
    plot(yr, demandside(:,2), '--', 'Color', purple, 'LineWidth', 1); % Deep yellow dash-dot line
    plot(yr, T(:,2), ':o', 'Color', 'r', 'LineWidth', 1, 'MarkerFaceColor', 'r', 'MarkerEdgeColor', 'r', 'MarkerSize', 3);
    box on;
    xlim([1997, 2015]);
    title("(b) No IO", 'FontSize', 10, 'FontName', 'Times New Roman','FontWeight', 'normal');
    xlabel('Year', 'FontSize', 10);
    ylabel('Output Response', 'FontSize', 10, 'Interpreter','latex');
    l = legend('Supply-Side Effect', 'Demand-Side Effect', 'Total', 'FontSize', 8);
    set(l, 'Location', 'best'); % Place the legend in the best position
    legend('boxon');

    % Initialize min and max y-limits
    minY = Inf;
    maxY = -Inf;

    for t = 1:2
        subplot(1, 2, t);
        currentYLim = ylim;
        minY = min(minY, currentYLim(1));
        maxY = max(maxY, currentYLim(2));
    end

    for t = 1:2
        subplot(1, 2, t);
        ylim([minY, maxY]);
    end

    figdir = [cd, '/figures'];

    if k == 1
        print(fullfile(figdir, 'outputresponsedecomp_ucmarkup'), '-depsc');
    elseif k == 2
        print(fullfile(figdir, 'outputresponsedecomp_apmarkup'), '-depsc');
    else
        print(fullfile(figdir, 'outputresponsedecomp_pfmarkup'), '-depsc');
    end
end


