%% WEEK7_HOMEWORK_SOLUTION.M
% Computational Tools for Macroeconomics — Week 7
% Homework Solution: Solow model with technological change and population growth
%
% Model in per effective worker terms:
%   A_{t+1} = (1+g) A_t
%   y_t     = A_t * k_t^alpha
%   c_t     = (1 - s) * y_t
%   k_{t+1} = [s * k_t^alpha + (1 - delta) * k_t] / [(1 + n) * (1 + g)]
%
% Steady state per effective worker:
%   k* = ( s / ( delta + n + g + n*g ) )^( 1 / (1 - alpha) )
%   y*/A = (k*)^alpha
%
% This script:
%   1) Defines parameters and three (n,g) scenarios
%   2) Simulates k_t, y_t, c_t, A_t using simulate_growth_tech()
%   3) Plots transition paths for k_t and y_t / A_t with steady-state lines
%   4) Prints a steady-state summary table and brief interpretation

clear; close all; clc;

%% --------------------------
%  Parameters (can be adjusted)
%  --------------------------
alpha = 0.4;      % capital share
s     = 0.3;      % savings rate
delta = 0.1;      % depreciation rate

% Growth rates: (n, g) triplets
scenarios = [
    0.00  0.00;   % no growth (benchmark)
    0.01  0.02;   % moderate population and technology growth
    0.02  0.03    % faster growth in both
];

T  = 80;          % simulation horizon (periods)
A0 = 1.0;         % initial technology level

% Choose an initial k0 common to all scenarios: start below the (n=0,g=0) k*
kstar_bench = ( s / (delta + scenarios(1,1) + scenarios(1,2) + scenarios(1,1)*scenarios(1,2)) )^( 1/(1-alpha) );
k0 = 0.5 * kstar_bench;

%% --------------------------
%  Containers for results
%  --------------------------
numS = size(scenarios,1);
K  = zeros(T, numS);   % capital per effective worker
Y  = zeros(T, numS);   % output (levels)
C  = zeros(T, numS);   % consumption (levels)
A  = zeros(T, numS);   % technology (levels)
Ye = zeros(T, numS);   % output per effective worker = Y./A
kstar = zeros(1, numS);
ystar_eff = zeros(1, numS);

%% --------------------------
%  Simulate each scenario
%  --------------------------
for j = 1:numS
    n = scenarios(j,1);
    g = scenarios(j,2);

    [k_path, y_path, c_path, A_path, k_star, y_star_eff] = ...
        simulate_growth_tech(alpha, s, delta, n, g, k0, A0, T);

    K(:,j)   = k_path;
    Y(:,j)   = y_path;
    C(:,j)   = c_path;
    A(:,j)   = A_path;
    Ye(:,j)  = y_path ./ A_path;   % output per effective worker
    kstar(j) = k_star;
    ystar_eff(j) = y_star_eff;
end

%% --------------------------
%  Plot: capital per effective worker, k_t
%  --------------------------
figure('Name','Capital per effective worker','Color','w');
lw = 1.8; hold on;

for j = 1:numS
    plot(0:T-1, K(:,j), 'LineWidth', lw);
end

% Steady-state lines
for j = 1:numS
    yline(kstar(j), '--', sprintf('k^* (n=%.2f, g=%.2f)', scenarios(j,1), scenarios(j,2)), ...
        'LineWidth', 1.0, 'LabelHorizontalAlignment','left');
end

grid on; box on;
xlabel('Time');
ylabel('Capital per effective worker, k_t');
title('Transition of k_t with Population and Technology Growth');
legText = arrayfun(@(r) sprintf('n=%.2f, g=%.2f', scenarios(r,1), scenarios(r,2)), 1:numS, 'UniformOutput', false);
legend(legText, 'Location','southeast', 'Interpreter','none');
set(gca,'FontSize',12);
exportgraphics(gcf, 'Figures/week7_hw_k_per_eff.png', 'Resolution', 300);

%% --------------------------
%  Plot: output per effective worker, y_t / A_t
%  --------------------------
figure('Name','Output per effective worker','Color','w');
hold on;

for j = 1:numS
    plot(0:T-1, Ye(:,j), 'LineWidth', lw);
end

for j = 1:numS
    yline(ystar_eff(j), '--', sprintf('(y/A)^* (n=%.2f, g=%.2f)', scenarios(j,1), scenarios(j,2)), ...
        'LineWidth', 1.0, 'LabelHorizontalAlignment','left');
end

grid on; box on;
xlabel('Time');
ylabel('Output per effective worker, y_t / A_t');
title('Transition of Output per Effective Worker');
legend(legText, 'Location','southeast', 'Interpreter','none');
set(gca,'FontSize',12);
exportgraphics(gcf, 'Figures/week7_hw_y_per_eff.png', 'Resolution', 300);

%% --------------------------
%  (Optional) Plot levels: technology A_t and output Y_t
%  --------------------------
figure('Name','Technology and Output (levels)','Color','w');
subplot(2,1,1);
hold on; for j=1:numS, plot(0:T-1, A(:,j), 'LineWidth', lw); end
grid on; box on; ylabel('A_t'); title('Technology Levels (grow with g)');
legend(legText, 'Location','northwest', 'Interpreter','none'); set(gca,'FontSize',11);

subplot(2,1,2);
hold on; for j=1:numS, plot(0:T-1, Y(:,j), 'LineWidth', lw); end
grid on; box on; ylabel('Y_t'); xlabel('Time'); title('Output Levels (grow with n and g)');
set(gca,'FontSize',11);
exportgraphics(gcf, 'Figures/week7_hw_levels_A_Y.png', 'Resolution', 300);

%% --------------------------
%  Steady-state summary (Command Window)
%  --------------------------
fprintf('\n=== Steady States per Effective Worker ===\n');
fprintf('alpha=%.2f, s=%.2f, delta=%.2f | k0=%.4f, A0=%.2f, T=%d\n\n', alpha, s, delta, k0, A0, T);
fprintf('%6s  %6s  %14s  %14s\n', 'n', 'g', 'k_star', '(y/A)_star');
for j = 1:numS
    fprintf('%6.2f  %6.2f  %14.6f  %14.6f\n', scenarios(j,1), scenarios(j,2), kstar(j), ystar_eff(j));
end

%% --------------------------
%  Brief interpretation (as comments)
%  --------------------------
% Interpretation:
% - Introducing g>0 and/or n>0 makes aggregate variables grow over time,
%   but in PER EFFECTIVE WORKER terms, the system is stationary and k_t
%   converges to k* = [s / (delta + n + g + n*g)]^(1/(1-alpha)).
% - Higher n or g raises the "effective depreciation" (delta + n + g + n*g),
%   reducing k* and (y/A)*. Hence, with the same s, economies with faster
%   population or technology growth have lower k* per effective worker.
% - Convergence speed is governed by the slope of the mapping near k*.
%   Typically, higher (n+g) implies more dilution each period and can
%   influence the transition speed (often faster replacement, but a lower
%   target k*). In practice you will observe different paths and long-run
%   levels across scenarios.
%
% End of solution.

%% =======================================================================
%  Local reusable function
%  =======================================================================
function [k_path, y_path, c_path, A_path, k_star, y_star_eff] = ...
    simulate_growth_tech(alpha, s, delta, n, g, k0, A0, T)
%SIMULATE_GROWTH_TECH  Solow model with population and technology growth.
% Inputs:
%   alpha, s, delta : production share, saving rate, depreciation
%   n, g            : population growth and technology growth rates
%   k0, A0          : initial capital per effective worker, initial A
%   T               : simulation horizon
%
% Outputs:
%   k_path          : capital per effective worker over time (Tx1)
%   y_path          : output in levels (Tx1), y_t = A_t * k_t^alpha
%   c_path          : consumption in levels (Tx1), c_t = (1-s) y_t
%   A_path          : technology path (Tx1), A_{t+1}=(1+g)A_t
%   k_star          : steady-state capital per effective worker
%   y_star_eff      : steady-state output per effective worker = (k*)^alpha

    % Steady state (per effective worker)
    eff_dep = delta + n + g + n*g;            % effective 'depreciation' term
    k_star  = ( s / eff_dep )^( 1 / (1 - alpha) );
    y_star_eff = k_star^alpha;

    % Preallocate
    k_path = zeros(T,1);
    y_path = zeros(T,1);
    c_path = zeros(T,1);
    A_path = zeros(T,1);

    % Initial conditions
    k_path(1) = k0;
    A_path(1) = A0;

    % Simulate forward
    for t = 1:T-1
        % law of motion for per effective worker capital
        k_path(t+1) = ( s * k_path(t)^alpha + (1 - delta) * k_path(t) ) / ((1 + n)*(1 + g));
        % technology growth
        A_path(t+1) = (1 + g) * A_path(t);
    end

    % Levels (output/consumption)
    y_path = A_path .* (k_path .^ alpha);
    c_path = (1 - s) * y_path;
end