%% Week 3 Challenge — Solution
% Missing data handling, YoY growth, and comparison to a full benchmark.

clear; clc; close all;

%% 1) Load datasets
% Missing version from repo:
Tmiss = readtable('Input/gdp_quarterly_missing.csv');    % <-- ensure file exists in docs/data
Tmiss = sortrows(Tmiss,'Time');
% If Time was read as text, uncomment:
% Tmiss.Time = datetime(Tmiss.Time);

% Full dataset used in class
Tfull = readtable('Input/gdp_quarterly.csv'); 
Tfull = sortrows(Tfull,'Time');

%% 2) Build timetables
TTmiss = table2timetable(Tmiss, 'RowTimes','Time');
%if any(strcmp('Time', TTmiss.Properties.VariableNames)); TTmiss.Time = []; end
TTmiss = sortrows(TTmiss);

TTfull = table2timetable(Tfull(:,{'Time','GDP'}), 'RowTimes','Time');
%if any(strcmp('Time', TTfull.Properties.VariableNames)); TTfull.Time = []; end
TTfull = sortrows(TTfull);

%% 3) Diagnose missing
summary_TTmiss = summary(TTmiss.GDP);
disp(summary_TTmiss);
summary_TTfull = summary(TTfull.GDP);
disp(summary_TTfull);

% Check if the Time columns are exactly equal
are_times_equal = isequal(TTmiss.Time, TTfull.Time);
fprintf('Are the Time columns exactly equal? %d\n', are_times_equal);


%% 4) Clean missing: choose approach
% fillmissing (keeps full time span)
TTclean_linear = fillmissing(TTmiss, 'linear');   
TTclean_previous = fillmissing(TTmiss, 'previous');   


%% 5) Compute YoY for both series
TTclean_linear.GDP_yoy_clean = NaN(height(TTclean_linear),1);
TTclean_linear.GDP_yoy_clean(5:end) = 100 * ( TTclean_linear.GDP(5:end) ./ TTclean_linear.GDP(1:end-4) - 1 );

TTclean_previous.GDP_yoy_clean = NaN(height(TTclean_previous),1);
TTclean_previous.GDP_yoy_clean(5:end) = 100 * ( TTclean_previous.GDP(5:end) ./ TTclean_previous.GDP(1:end-4) - 1 );

Tfull.GDP_yoy_full = NaN(height(Tfull),1);
Tfull.GDP_yoy_full(5:end) = 100 * ( Tfull.GDP(5:end) ./ Tfull.GDP(1:end-4) - 1 );

mkdir('Figures')
%% 6) Plot comparison and save figures
figure('Color','w');
plot(TTclean_linear.Time, TTclean_linear.GDP_yoy_clean, '-o', 'LineWidth', 1.2); hold on;
plot(TTclean_previous.Time, TTclean_previous.GDP_yoy_clean, '-s', 'LineWidth', 1.2);
plot(Tfull.Time, Tfull.GDP_yoy_full,  '-', 'LineWidth', 1.2);
grid on; box on;
xlabel('Time'); ylabel('YoY growth (%)');
title('YoY GDP Growth: Cleaned (missing handled) vs Full Series');
legend({'Linear','Previous','Full'}, 'Location','best');
saveas(gcf, 'Figures/week3_yoy_compare.png');
savefig(gcf, 'Figures/week3_yoy_compare.fig');

mkdir('Output')
%% 7) export cleaned datasets
writetable(timetable2table(TTclean_linear), 'Output/gdp_quarterly_clean_linear.csv');
writetable(timetable2table(TTclean_previous), 'Output/gdp_quarterly_clean_previous.csv');

