%% This code generates Figure A.3


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');


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


%% Baseline calibration


% number of industries

N = 66;


% 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,:) = [];

start_date = 1997;


figure('Position', [100, 100, 900, 350]);
set(gcf, 'DefaultAxesFontName', 'Times New Roman');

for k = 1:3

    year = 2006;


    temp       = IO(:,:,year-1996);
    temp       = temp';


    Omega = bsxfun(@rdivide, temp, grosscost(:,year-1996));
    alpha = labor(:,year-1996);
    beta = Final(1:66,year-1996);
    beta(beta<0) = 0;
    beta = beta/sum(beta);

    mu           = mu_sum(:, year-1996, k);


    % Cost-based IO variables

    Omega_p_tilde  = Omega;
    Omega_f_tilde  = alpha;
    Psi_p_tilde    = (eye(N)-Omega_p_tilde)^(-1);
    Psi_f_tilde    = Psi_p_tilde * Omega_f_tilde;
    Domar_tilde    = (beta' * Psi_p_tilde)';
    Lambda_tilde   = beta' * Psi_f_tilde;

    % Revenue-based IO variables

    Omega_p        = diag(mu)^(-1) * Omega;
    Omega_f        = diag(mu)^(-1) * alpha;
    Psi_p          = (eye(N)-Omega_p)^(-1);
    Psi_f          = Psi_p * Omega_f;
    Domar          = (beta' * Psi_p)';
    Lambda         = beta' * Psi_f;

    % Sticky-price IO variables

    Omega_p_hat    = diag(theta) * Omega;
    Omega_f_hat    = diag(theta) * alpha;
    Psi_p_hat      = (eye(N)-Omega_p_hat)^(-1);
    Psi_f_hat      = Psi_p_hat * Omega_f_hat;
    Domar_hat      = (beta' * Psi_p_hat)';
    Lambda_hat     = beta' * Psi_f_hat;



    % Example Data
    x = Psi_f;
    y = Psi_f_hat; % example data



    cate = zeros(66,1); % Initialize cate as numerical for easier indexing
    cate(1:2) = 1; % Category 1 for Agriculture
    cate(3:26) = 2; % Category 2 for Manufacturing
    cate(27:66) = 3; % Category 3 for Services

    varcov_manu = cov(x, y);


    % Category colors

    colors = [102, 204, 0; 128, 128, 255; 255, 128, 128] / 255;

    % Marker styles
    markers = {'^', 's', 'o'}; % Circle, Square, Diamond

    % Create Scatter Plot
    subplot(1,3,k);
    hold on; % keep the scatter plot visible when adding each category and the line plot

    % Plot each category with a different color and marker
    for i = 1:3
        scatter(x(cate == i), y(cate == i), 20, colors(i,:), 'filled', 'Marker', markers{i});
    end

    % Continue with your plotting specifics, such as adding a fitted line, labels, etc.


    % Fit a Linear Model
    p = polyfit(x, y, 1); % p contains coefficients of the fitted line (1st degree polynomial)
    x_fit = linspace(min(x), max(x), 100); % generate points for a smooth line
    y_fit = polyval(p, x_fit); % evaluate the polynomial at the points in x_fit

    % Plot the Fitted Line
    plot(x_fit, y_fit, 'k-', 'LineWidth', 1); % plot the line in black with a line width of 1

    % Customize the Plot with LaTeX Labels
    xlabel('Upstream markup $\Psi_{iL}$', 'Interpreter', 'latex');
    ylabel('Wage pass-through $\hat{\Psi}_{iL}$', 'Interpreter', 'latex');
    if k == 1
        title('UC Markups', 'Interpreter', 'latex');
    elseif k == 2
        title('AP Markups', 'Interpreter', 'latex');
    else
        title('PF Markups', 'Interpreter', 'latex');
    end


    box on;
    grid on;
    axis tight; % tighten the axis around the data

    % Show the plot
    hold off;

end


legend('Agriculture', 'Manufacturing', 'Services', 'Location', 'best');

figdir = [cd, '/figures'];

exportgraphics(gcf, fullfile(figdir, 'scatterplot.png'));

