%% Week 11: Data Download Script
% This script downloads US macroeconomic data from FRED (St. Louis Fed)
% and saves it to a CSV file for use in the lecture and homework.
%
% Series:
% 1. GDPC1    : Real Gross Domestic Product (Billions of Chained 2012 Dollars, SAAR)
% 2. GDPDEF   : Gross Domestic Product: Implicit Price Deflator (Index 2012=100, SA)
% 3. FEDFUNDS : Effective Federal Funds Rate (Percent, NSA) -> Aggregated to Quarterly if needed

clear; clc; close all;

if ~exist('Data','dir'), mkdir Data; end


%% 1. Define FRED URLs
base_url = 'https://fred.stlouisfed.org/graph/fredgraph.csv?id=';

url_gdp   = [base_url 'GDPC1'];
url_def   = [base_url 'GDPDEF'];
url_ffr   = [base_url 'FEDFUNDS']; % Monthly data

%% 2. Import Data
fprintf('Downloading data from FRED...\n');

% Real GDP (Quarterly)
opts = detectImportOptions(url_gdp);
opts.VariableNames = {'Observation_date', 'GDPC1'};
T_gdp = readtable(url_gdp, opts);
T_gdp.Observation_date = datetime(T_gdp.Observation_date);

% GDP Deflator (Quarterly)
opts = detectImportOptions(url_def);
opts.VariableNames = {'Observation_date', 'GDPDEF'};
T_def = readtable(url_def, opts);
T_def.Observation_date = datetime(T_def.Observation_date);

% Federal Funds Rate (Monthly -> convert to Quarterly)
opts = detectImportOptions(url_ffr);
opts.VariableNames = {'Observation_date', 'FEDFUNDS'};
T_ffr_m = readtable(url_ffr, opts);
T_ffr_m.Observation_date = datetime(T_ffr_m.Observation_date);

% Convert FFR to Quarterly (taking the average of the quarter)
T_ffr_q = retime(table2timetable(T_ffr_m), 'quarterly', 'mean');
T_ffr_q = timetable2table(T_ffr_q);
T_ffr_q.Properties.VariableNames{'Observation_date'} = 'Observation_date';

%% 3. Merge Data
% Find common dates
fprintf('Merging and cleaning data...\n');

% Inner join to keep only overlapping dates
T_merged = innerjoin(T_gdp, T_def, 'Keys', 'Observation_date');
T_merged = innerjoin(T_merged, T_ffr_q, 'Keys', 'Observation_date');

% Sort by date
T_merged = sortrows(T_merged, 'Observation_date');

%% 4. Basic Transformations (Pre-calculation for verification)
% We will leave the raw levels in the CSV so students can transform them.
% But let's check data quality.
figure;
subplot(3,1,1); plot(T_merged.Observation_date, T_merged.GDPC1); title('Real GDP');
subplot(3,1,2); plot(T_merged.Observation_date, T_merged.GDPDEF); title('GDP Deflator');
subplot(3,1,3); plot(T_merged.Observation_date, T_merged.FEDFUNDS); title('Fed Funds Rate');

%% 5. Save to CSV
output_filename = 'Data/US_macro_data.csv';
writetable(T_merged, output_filename);
fprintf('Data saved to %s\n', output_filename);

%% 6. Display head
disp(head(T_merged));

