%% This code generates figure A.1 


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

%% Price flexibilities/stickiness (excluding wage stickiness)

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


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


% baseline calibration

mu                 = mu_sum(:, year - 1996, 1);
sigma_c            = 1;
sigma              = 0.5 * ones(N,1);
[T, SC, ~, SP_sec] = supplyside(mu, theta, sigma_c, sigma, Omega, alpha, beta);
bar_dataset        = [SC; SP_sec];
percentages        = (bar_dataset / T) * 100;
secname            = indname(1:66,:);
secname            = [{'Households'}; secname];
percentages        = flipud(percentages);
bar_dataset        = flipud(bar_dataset);
secname            = flipud(secname);

figure('Position', [100, 100, 700, 1500]);
set(gcf, 'DefaultAxesFontName', 'Times New Roman');
b1 = barh(bar_dataset, 'EdgeColor', 'k', 'LineWidth', 1.5, 'FaceColor', [0.8, 0.8, 0.8]);
set(gca, 'YTickLabel', secname, 'YTick', 1:numel(secname));

% Annotate bars with percentages
hold on;
for i = 1:length(bar_dataset)
    text(bar_dataset(i), i, sprintf('%.1f%%', percentages(i)), ...
        'HorizontalAlignment', 'left', 'VerticalAlignment', 'middle')
end
hold off;

figdir = [cd, '/figures'];

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



IC_sec      = sector_aggregate_flexwage(SP_sec);

bar_dataset = [SC; IC_sec];
percentages = (bar_dataset / T) * 100;
secname     = {
    'Households'
    'Agriculture, Forestry, Fishing, and Hunting'
    'Mining'
    'Utilities'
    'Construction'
    'Durable Goods'
    'Nondurable Goods'
    'Wholesale Trade'
    'Retail Trade'
    'Transportation and Warehousing, Excluding Postal Service'
    'Information'
    'Finance and Insurance'
    'Real Estate and Rental and Leasing'
    'Professional and Technical Services'
    'Management of Companies and Enterprises'
    'Administrative and Waste Services'
    'Educational Services'
    'Health Care and Social Assistance'
    'Arts, Entertainment, and Recreation'
    'Accommodation and Food Services'
    'Other Services, Except Government'};
percentages = flipud(percentages);
bar_dataset = flipud(bar_dataset);
secname     = flipud(secname);

figure('Position', [100, 100, 700, 400]);
set(gcf, 'DefaultAxesFontName', 'Times New Roman');
b2 = barh(bar_dataset, 'EdgeColor', 'k', 'LineWidth', 1.5, 'FaceColor', [0.8, 0.8, 0.8]);
set(gca, 'YTickLabel', secname, 'YTick', 1:numel(secname));

% Annotate bars with percentages
hold on;
for i = 1:length(bar_dataset)
    text(bar_dataset(i), i, sprintf('%.1f%%', percentages(i)), ...
        'HorizontalAlignment', 'right', 'VerticalAlignment', 'middle')
end
hold off;


figdir = [cd, '/figures'];
exportgraphics(gcf, fullfile(figdir, 'IndustryContribution_sec.png'));
