%% Week 2 Challenge — Solution
% Simulate a GDP path with shocks and plot it.

clear; clc; close all;

% ---- parameters ----
G0    = 100;    % initial GDP
g     = 0.02;   % deterministic growth (2% per period)
sigma = 0.03;   % volatility of shocks
T     = 20;     % periods
seed  = 123;    % RNG seed (reproducibility)

% ---- simulate ----
G = gdp_simulate(G0, g, sigma, T, seed);

% ---- basic checks & summaries ----
g_levels = G;
g_growth = 100 * (G(2:end) ./ G(1:end-1) - 1);   % percent growth

fprintf('Final GDP (t=%d): %.3f\n', T, G(end));
fprintf('Mean growth (%%): %.3f | Std growth (%%): %.3f\n', ...
        mean(g_growth, 'omitnan'), std(g_growth, 'omitnan'));

% ---- plot path ----
figure('Color','w');
plot(0:T, g_levels, '-o', 'LineWidth', 1.3);
grid on; box on;
xlabel('t'); ylabel('GDP level');
title(sprintf('Simulated GDP Path (g=%.2f%%, \\sigma=%.2f%%)', 100*g, 100*sigma));

% create a directory for figures
mkdir('Figures'); 
% save figure outputs
saveas(gcf, 'Figures/week2_gdp_path.png');
savefig(gcf, 'Figures/week2_gdp_path.fig');

% ---- optional: plot growth rates ----
figure('Color','w');
plot(1:T, g_growth, '-s', 'LineWidth', 1.3);
grid on; box on;
xlabel('t'); ylabel('Growth rate (%)');
title('Simulated GDP Growth Rates');

saveas(gcf, 'Figures/week2_gdp_growth.png');
savefig(gcf, 'Figures/week2_gdp_growth.fig');

% create a directory for output
mkdir('Output');
% ---- save workspace (optional) ----
save('Output/week2_challenge_workspace.mat', 'G0','g','sigma','T','seed','G','g_growth');