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


%% Step 1) Download data for ALL countries
%% WORLD BANK: Annual Nominal GDP (current US$)
% Country: set ISO3 code to all
country = 'all';

%% ----------- Real GDP  (Index, 2015=100) -----------
url_rgdp = ['https://api.worldbank.org/v2/country/' country ...
            '/indicator/NY.GDP.MKTP.KD?format=json&per_page=20000']; %Last number is data max number of data requests per page

raw_rgdp = webread(url_rgdp);
data_rgdp = raw_rgdp{2};

%  Preallocate memory for the data
countries_rgdp = cell(length(data_rgdp), 1);
values_rgdp = cell(length(data_rgdp), 1);
periods_rgdp = cell(length(data_rgdp), 1);
countries_code_rgdp = cell(length(data_rgdp), 1);

% Extract Real GDP values and years from the data
for i = 1:length(data_rgdp)
    if isempty(data_rgdp(i).value)              % GDP value
        values_rgdp{i} = NaN;                   % Missing data = NaN
    else        
    values_rgdp{i} = data_rgdp(i).value;        % Extract GDP values 
    end
    periods_rgdp{i} = data_rgdp(i).date;     % Extract corresponding years (strings)
    countries_rgdp{i} = data_rgdp(i).country.value;  % Country name
    countries_code_rgdp{i} = data_rgdp(i).countryiso3code;  % Country name
end

years_rgdp=str2double(periods_rgdp); %convert periods to numerical values
WB_rgdp=cell2mat(values_rgdp); %convert values to numerical values

%% Step 2) Create the table
% Create a table from the extracted data
WB_table = table(countries_rgdp, countries_code_rgdp, years_rgdp, WB_rgdp, ...
                 'VariableNames', {'Country', 'ISO3Code', 'Year', 'RealGDP'});

%% Step 3) Compute Logs and YoY growth rates per each country only for positive values
% Create two new columns of all NaN values
WB_table.LogGDP = NaN(size(WB_table, 1), 1);
WB_table.YoYGrowth = NaN(size(WB_table, 1), 1);

% Get list of all country codes
country_codes = unique(WB_table.ISO3Code);

for i = 1:length(country_codes)
    code = country_codes{i};
    % Find rows for this country
    rows = strcmp(WB_table.ISO3Code, code);
    % Sort by year
    years = WB_table.Year(rows);
    [years_sorted, idx_sorted] = sort(years);
    realgdp = WB_table.RealGDP(rows);
    realgdp = realgdp(idx_sorted);

    % Compute log GDP (only for positive values)
    loggdp = NaN(size(realgdp));
    for j = 1:length(realgdp)
        if realgdp(j) > 0
            loggdp(j) = log(realgdp(j));
        end
    end

    % Compute YoY growth: log difference
    yoy = NaN(size(loggdp));
    for j = 2:length(loggdp)
        if ~isnan(loggdp(j)) && ~isnan(loggdp(j-1))
            yoy(j) = (loggdp(j) - loggdp(j-1)).*100;
        end
    end

    % Put results back in the table
    idx_all = find(rows);
    idx_all = idx_all(idx_sorted);
    WB_table.LogGDP(idx_all) = loggdp;
    WB_table.YoYGrowth(idx_all) = yoy;
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
% Extract the US data to a new table
WB_US_table = WB_table(strcmp(WB_table.ISO3Code, 'USA'), :);
%sort table in ascending order of year
WB_US_table = sortrows(WB_US_table, 'Year');

% Plot Real GDP
figure;
plot(WB_US_table.Year, WB_US_table.RealGDP./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_US_table.Year, WB_US_table.YoYGrowth, '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'};

mkdir('Figures')
mkdir('Output')
% Plot the Real GDP for the selected countries
% Define 4 line styles
styles = {'-','--',':','-.'};   
figure(1);
for i = 1:length(countries_to_plot)
    % Create a new table with the selected countries
    WB_selected_table = WB_table(ismember(WB_table.ISO3Code, countries_to_plot{i}), :); % Extract the rows for the selected country and store in a new table
    % Sort the table by year
    WB_selected_table = sortrows(WB_selected_table, 'Year');
    style = styles{mod(i-1, numel(styles))+1}; %cycle through the line styles
    % Plot the Real GDP for the selected country
    plot(WB_selected_table.Year, WB_selected_table.RealGDP./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.png');

% Plot the growth rates for the selected countries in different subplots
figure(2);
for i = 1:length(countries_to_plot)
    % Create a new table with the selected countries
    WB_selected_table = WB_table(ismember(WB_table.ISO3Code, countries_to_plot{i}), :); % Extract the rows for the selected country and store in a new table
    % Sort the table by year
    WB_selected_table = sortrows(WB_selected_table, 'Year');
    
    % Plot the growth rates for the selected country
    subplot(ceil(length(countries_to_plot)/3), 3, i);
    plot(WB_selected_table.Year, WB_selected_table.YoYGrowth, '-', 'LineWidth', 2);
    hold on;
    % add a line at the mean growth rate
    y = mean(WB_selected_table.YoYGrowth(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.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(ismember(WB_table.ISO3Code, countries_to_plot), :);
writetable(WB_selected_panel, 'Output/clean_rgdp_panel.csv');






