%% Week 3 Homework (Solution) — WB data panel → growth → grouping → plots
clear all; close all; clc;


%% Step 1) Import data for ALL countries from CVS
%% WORLD BANK: Annual Nominal GDP (current US$)
%% ----------- Real GDP  (Index, 2015=100) -----------
% Read the data from the CSV file
file = 'Input/WB-WDI_time_series.csv';
% 1) Detect options and treat junk tokens as missing
opts = detectImportOptions(file, 'TextType', 'string'); % strings are easier to coerce
opts = setvaropts(opts, opts.VariableNames, ...
    'TreatAsMissing', {'', ' ', '..', 'n/a', 'NA', 'NaN', 'i'});  % <-- key fix

% 2) Force variables to double (NaN fill is automatic for missing)
opts = setvartype(opts, 'double');

% 3) Read
data_rgdp = readtable(file, opts);

%Now inspect the table and find the period column
periods_rgdp=data_rgdp.period;

%Now inspect the second column name (Uncomment this block to do it)
second_column_name = data_rgdp.Properties.VariableNames{2};
%disp(second_column_name); %Annual_GDP_constant2015US___Aruba_WB_WDI_A_NY_GDP_MKTP_KD_ABW_
%Now we need to extract from the second column name the country name and the country code
%First split the words in the string separated by _
split_name = strsplit(second_column_name, '_');
%Inspect split_name and find that the country name is the fourth element and the country code is the second to last element
country_name = split_name{4};
country_code = split_name{end-1};
%Now we need to extract the values for the country code
values_rgdp = data_rgdp.(second_column_name);

% Now subsitute the column names in the table data_rgdp with the country
% code and create wide table (Step 2)
WB_table=data_rgdp;
for i=1:size(WB_table,2)-1
    column_name=data_rgdp.Properties.VariableNames{i+1};
    split_name = strsplit(column_name, '_');
    country_code = split_name{end-1};
    WB_table.Properties.VariableNames{i+1}=country_code;
end




%% Step 3) Compute Logs and YoY growth rates per each country only for positive values
% Initialize the log GDP column in the WB_table
WB_table_LogGDP =WB_table;

% Loop through each country (columns 2 to 267)
for col = 2:267
    country_values = WB_table{:, col};

    % Compute log GDP for positive values
    log_values = NaN(size(country_values));
    positive_indices = country_values > 0; % Find positive values
    log_values(positive_indices) = log(country_values(positive_indices)); % Take log of positive values
    
    % Store the log values back in the WB_table
    WB_table_LogGDP.(col) = log_values; % Store as numeric array, not cell array
end

WB_table_yoy=WB_table_LogGDP;
% Loop through each country (columns 2 to 267)
for col = 2:267
    country_values = WB_table_LogGDP{:, col};
    yoy_values = NaN(size(country_values));
    for i = 2:length(country_values)
        if ~isnan(country_values(i)) && ~isnan(country_values(i-1))
            yoy_values(i) = (country_values(i) - country_values(i-1)).*100;
        end
    end
    WB_table_yoy.(col) = yoy_values;
end


%% Step 4) Check that you get then same results as in homework from week 1
% Plot Real GDP and growth rates for the US

mkdir('Figures')
mkdir('Output')
% Plot Real GDP
figure;
plot(WB_table.period, WB_table.USA./1e9, 'b-', 'LineWidth', 2);
xlabel('Year');
ylabel('Real GDP (in billions of US dollars)');
title('Real GDP for the US');
grid on;

% Plot growth rates 
% Note: in week 1 homework, the growth rates were computed as (Y_t - Y_{t-1})/Y_{t-1}*100
% Here, we are computing the growth rates as (log(Y_t) - log(Y_{t-1}))*100
% The small difference is due to the approximation of the log function
figure;
plot(WB_table_yoy.period, WB_table_yoy.USA, 'r-', 'LineWidth', 2);
xlabel('Year');
ylabel('YoY Growth');


%% Step 5) Select some countries of your choice and plot their Real GDP on the same graph
% Select some countries of your choice
countries_to_plot = {'USA', 'CHN', 'JPN', 'DEU', 'FRA', 'GBR', 'IND', 'BRA', 'ZAF', 'MEX'};

% Plot the Real GDP for the selected countries
% Define 4 line styles
styles = {'-','--',':','-.'};   
figure(1);
for i = 1:length(countries_to_plot)
    % Plot the Real GDP for the selected country using the column for that country
    style = styles{mod(i-1, numel(styles))+1}; % cycle through the line styles
    plot(WB_table.period, WB_table.(countries_to_plot{i})./1e9, style, 'LineWidth', 2);
    hold on;
end
xlabel('Year');
ylabel('Real GDP (in billions of US dollars)');
title('Real GDP for the selected countries');
legend(countries_to_plot, 'Location', 'best');
grid on;
axis tight;
saveas(gcf, 'Figures/week3_rgdp_comparison_b.png');

% Plot the growth rates for the selected countries in different subplots
figure(2);
for i = 1:length(countries_to_plot)
    
    % Plot the growth rates for the selected country
    subplot(ceil(length(countries_to_plot)/3), 3, i);
    plot(WB_table_yoy.period, WB_table_yoy.(countries_to_plot{i}), '-', 'LineWidth', 2);
    hold on;
    % add a line at the mean growth rate
    y = mean(WB_table_yoy.(countries_to_plot{i})(2:end));
    line(xlim, [y y], 'Color', 'r', 'LineWidth', 2);
    title(countries_to_plot{i});
    grid on;
    axis tight;
end
saveas(gcf, 'Figures/week3_rgdp_growth_b.png');

%% Step 6) Export the table with the panel of selected countries with the growth rates to CSV
% Extract rows for the selected countries
WB_selected_panel = WB_table_yoy(:, [{'period'}, countries_to_plot]);
writetable(WB_selected_panel, 'Output/clean_rgdp_panel_b.csv');






