%% Week 11: In-Class Challenge
% Objective: Estimate a Bivariate VAR for US GDP Growth and Inflation
%
% Steps:
% 1. Load and transform data (Growth rates).
% 2. Estimate a simple OLS Phillips Curve.
% 3. Estimate a VAR(1) model equation-by-equation.
% 4. Identify structural shocks using Cholesky decomposition.
% 5. Compute and plot Impulse Response Functions (IRFs).

clear; clc; close all;
if ~exist('Figures','dir'), mkdir Figures; end

%% 1. Load Data
% Make sure 'US_macro_data.csv' is in your folder (run week11_get_data.m)
T = readtable('../Data/US_macro_data.csv'); % Note: Adjust path if needed or move file here
T.Observation_date = datetime(T.Observation_date);

% Transformations
% Calculate annualized growth rates for GDP and Inflation
% Hint: 400 * diff(log(...))
gdp_growth = [NaN; 400 * diff(log(T.GDPC1))];
inflation  = [NaN; 400 * diff(log(T.GDPDEF))];

% Clean data (remove NaNs)
data_mat = [gdp_growth, inflation];
dates    = T.Observation_date;

data_clean = data_mat(2:end, :);
dates_clean = dates(2:end);

%% 2. Step 1: OLS Estimation
% Estimate: Inflation_t = alpha + beta * GDP_Growth_t + u_t
% Use fitlm or manual (X'X)^-1 X'Y

% [YOUR CODE HERE]


%% 3. Step 2: VAR(1) Estimation
% Model: Y_t = A0 + A1 * Y_{t-1} + e_t
% where Y_t = [GDP_Growth, Inflation]'

% Construct Y_lhs (t=2 to T) and X_rhs (Constant + Y_{t-1})
% [YOUR CODE HERE]


% Estimate coefficients (B matrices)
% [YOUR CODE HERE]


% Extract A1 matrix for dynamics
% [YOUR CODE HERE]


%% 4. Step 3: Identification and IRFs
% Calculate residuals and Covariance Matrix Sigma
% [YOUR CODE HERE]


% Cholesky Decomposition: Sigma = P*P' (Lower triangular P)
% [YOUR CODE HERE]


% Compute IRFs for 12-16 periods
% Shock 1: GDP Shock (First column of P)
% Shock 2: Inflation Shock (Second column of P)
% Iterate: x_{h} = A1 * x_{h-1}

% [YOUR CODE HERE]


%% 5. Plotting
% Plot the 4 IRF panels
% [YOUR CODE HERE]

