%% Week 4 Homework – Solution (Revised with Units)
% Extended IS–LM and Cholesky
% -------------------------------------------------------------
% All variables in "levels".
% Interest rate i is measured in BASIS POINTS (bp):
%   100 bp = 1%  =>  i(bp)/100 = i(%)
% -------------------------------------------------------------

clear; clc; close all;

%% Parameters
Abar  = 120;     % autonomous demand
bG    = 0.8;     % fiscal multiplier of G on Y
alpha = 0.5;     % sensitivity of Y to i (IS slope)
beta  = 0.25;    % sensitivity of money demand to Y
gamma = 1.2;     % sensitivity of money demand to i
theta = 0.1;     % effect of G on money demand
G     = 100;     % government spending
MP    = 80;      % real money supply (M/P)

%% Build A and b
A = [  1,      alpha;
      -beta,   gamma ];

b = [ Abar + bG*G;
      MP   - theta*G ];

%% Solve for equilibrium
x = A\b;              % preferred method
Y_eq = x(1);
i_eq_bp = x(2);       % interest rate in basis points
i_eq_percent = i_eq_bp / 100; % interest rate in % points

fprintf('--- Equilibrium ---\n');
fprintf('Y = %.3f\n', Y_eq);
fprintf('i = %.3f bp  = %.3f %%  \n\n', ...
         i_eq_bp, i_eq_percent);

%% Comparative statics: vary G
Ggrid = 80:2:120;
Y_series = zeros(size(Ggrid));
i_series_bp = zeros(size(Ggrid));

for j = 1:numel(Ggrid)
    Gj = Ggrid(j);
    bj = [Abar + bG*Gj;
           MP   - theta*Gj];
    xj = A\bj;
    Y_series(j) = xj(1);
    i_series_bp(j) = xj(2);
end

% Convert to percent
i_series_percent = i_series_bp / 100;

mkdir('Figures')
%% Plot comparative statics
figure('Color','w');
subplot(1,2,1);
plot(Ggrid, Y_series, 'o-','LineWidth',1.5);
xlabel('Government spending (G)');
ylabel('Output (Y)');
title('Equilibrium Output');

subplot(1,2,2);
plot(Ggrid, i_series_percent, 's-','LineWidth',1.5);
xlabel('Government spending (G)');
ylabel('Interest rate (%)');
title('Equilibrium Interest Rate');
grid on;
saveas(gcf, 'Figures/week4_IS_LM.png');

%% Cholesky Decomposition
Sigma = [4 1.2; 1.2 3];
[V,D] = eig(Sigma);
fprintf('\nEigenvalues of Sigma: '); disp(diag(D).');
L = chol(Sigma,'lower');
fprintf('Cholesky factor L:\n'); disp(L);

% Draw random shocks: eps = L*u
T = 10000;
rng(123);   % Fix random seed for reproducibility
u = randn(2,T);
eps = L*u;
fprintf('Empirical covariance (should match Sigma):\n');
disp(cov(eps.'));
fprintf('Empirical correlation matrix:\n');
disp(corr(eps.'));




% Compute implied equilibrium responses for each draw
Ainv = inv(A);
x_sim = Ainv * (b + eps);   % each column = [Y; i] for one draw

% Analyze results
Y_sim = x_sim(1,:);
i_sim = x_sim(2,:);

fprintf('\nSimulated equilibrium (means should ≈ baseline):\n');
disp(mean(x_sim,2));

C = cov(Y_sim, i_sim);
fprintf('\nEmpirical covariance matrix (Y,i):\n');
disp(C);
fprintf('\nEmpirical var(Y)=%.3f, var(i)=%.3f, cov(Y,i)=%.3f\n', C(1,1), C(2,2), C(1,2));

% Optional plot
figure('Color','w');
plot(Y_sim, i_sim, '.', 'MarkerSize', 3);
xlabel('Output (Y)'); ylabel('Interest Rate (i)');
title('Equilibrium Distribution with Correlated Shocks');
grid on;
saveas(gcf, 'Figures/week4_shocks.png');