% US Investment from 1948 to 2025

%%      Housekeeping

clear all
close all
clc

%     FRED Set up:
disp('Connect to FRED database')
        
        url = 'https://fred.stlouisfed.org/';
        c = fred(url);
        format bank
        
        startdate   = '01/01/1960';
        enddate     = '01/01/2026';
        
        series(1) = {'GPDI'};           % investment, nominal
        series(2) = {'GDP'}             % gdp, nominal
        series(3) = {'GPDIC1'}          % investment, real
        series(4) = {'GDPC1'},          % gdp, real
        series(5) = {'A794RX0Q048SBEA'} % real consumption per capita
        series(6) = {'NCBEILQ027S'}     % tobins q numerator
        series(7) = {'TNWMVBSNNCB'}     % tobins q denominator

        Ns = max(size(series));
        
        for is = 1:Ns
            d = fetch(c,series(is),startdate,enddate);
            y(:, is) = d.Data(:, 2);
        end
        
        date = d.Data(:,1);
        T=length(y(:, 1));
        
        u = 100*y(:,1)./y(:,2);

        xbar = mean(u);

        inv_gr = 100*(y(5:T, 3)./y(1:T-4, 3) -1)

        gdp_gr = 100*(y(5:T, 4)./y(1:T-4, 4) -1)

        con_gr = 100*(y(5:T, 5)./y(1:T-4, 5) -1)

        tobinsq = y(1:T, 6)./y(1:T, 7)/1000


%%      Plot Series
disp('Plot charts');
close all

set(0, 'DefaultLineLineWidth', 1, ...
    'DefaultLineMarkerSize', 6,...
    'DefaultAxesFontSize', 18, ...
    'DefaultAxesTickLabelInterpreter', 'latex',...
    'DefaultFigurePaperOrientation', 'portrait',...
    'DefaultFigurePaperUnits', 'inches',...
    'DefaultFigurePaperSize', [6, 4.5],...
    'DefaultFigurePaperPosition', [0,0,6,4.5],...
    'DefaultLegendInterpreter', 'latex',...
    'DefaultTextInterpreter', 'latex',...
    'DefaultFigureRenderer', 'painters')

colororder = ...
    [    0    0.4470    0.7410;...
    0.8500    0.3250    0.0980;...
    0.9290    0.6940    0.1250;...
    0.4940    0.1840    0.5560;...
    0.4660    0.6740    0.1880;...
    0.3010    0.7450    0.9330;...
    0.6350    0.0780    0.1840];



%%      Investment to GDP ratio
figure
    plot(date, u, 'LineWidth', 1.5, 'Color', colororder(1,:)); hold on
    plot(date, xbar*ones(T,1), 'LineWidth', 1.5, 'Color', colororder(1,:),'LineStyle','--');
    datetick('x','yyyy','keeplimits');
    xlim([datenum(1960,01,01) datenum(2026,01,01)])
    grid
    recessionplot
    x0=100;
    y0=100;
    width=600;
    height=400
    xlabel('date, quarterly')
    title('$\textbf{US Investment/GDP rate (\%)}$', 'Interpreter', 'latex', 'FontName', 'CMU Serif');
set(gcf,'position',[x0,y0,width,height])
tightfig
saveas(gcf,'investment_rate.pdf')
saveas(gcf,'investment_rate.png')
%%
close all
figure
yyaxis left
    plot(date(5:T), u(5:T), 'LineWidth', 1.5, 'Color', colororder(1,:)); hold on
    %ylim([-6 6])

yyaxis right    
    plot(date(5:T), tobinsq(5:T), 'LineWidth', 1.5, 'Color', colororder(2,:));
    
    plot(date(5:T), 0*ones(T-4,1), 'LineWidth', 1.5, 'Color', 'k','LineStyle','--');
    datetick('x','yyyy','keeplimits');
    xlim([datenum(1960,01,01) datenum(2026,01,01)])
    grid
    recessionplot
    x0=100;
    y0=100;
    width=600;
    height=400
    xlabel('date, quarterly')
    title('$\textbf{US I/GDP(\%) and Q-proxy}$', 'Interpreter', 'latex', 'FontName', 'CMU Serif');
set(gcf,'position',[x0,y0,width,height])
tightfig
saveas(gcf,'investment_rate_q.pdf')
saveas(gcf,'investment_rate_q.png')

%%      Growth Rates
figure
    plot(date(5:end), [gdp_gr], 'LineWidth', 2.5); hold on
    datetick('x','yyyy','keeplimits');
    xlim([datenum(1960,01,01) datenum(2026,01,01)])
    grid
    recessionplot
    x0=100;
    y0=100;
    width=600
    height=400
    title('$\textbf{Macro variables growth rates (\%)}$', 'Interpreter', 'latex', 'FontName', 'CMU Serif');
    legend('GDP yoy growth \%')
set(gcf,'position',[x0,y0,width,height])
tightfig
saveas(gcf,'growth_rates_gdp_gfcf1.pdf')


figure
    plot(date(5:end), [gdp_gr], 'LineWidth', 2.5); hold on
    plot(date(5:end), [con_gr], 'LineWidth', 2.5); hold on
    datetick('x','yyyy','keeplimits');
    xlim([datenum(1960,01,01) datenum(2026,01,01)])
    grid
    recessionplot
    x0=100;
    y0=100;
    width=600
    height=400
    title('$\textbf{Macro variables growth rates (\%)}$', 'Interpreter', 'latex', 'FontName', 'CMU Serif');
    legend('GDP yoy growth \%', 'Consumption yoy growth \%')
set(gcf,'position',[x0,y0,width,height])
tightfig
saveas(gcf,'growth_rates_gdp_gfcf2.pdf')


figure
plot(date(5:end), [gdp_gr], 'LineWidth', 2.5); hold on
    plot(date(5:end), [con_gr], 'LineWidth', 2.5); hold on
    plot(date(5:end), [inv_gr], 'LineWidth', 2.5, 'Color', 'k'); hold on
    datetick('x','yyyy','keeplimits');
    xlim([datenum(1960,01,01) datenum(2026,01,01)])
    grid
    recessionplot
    x0=100;
    y0=100;
    width=600
    height=400
    title('$\textbf{Macro variables growth rates (\%)}$', 'Interpreter', 'latex', 'FontName', 'CMU Serif');
    legend('GDP yoy growth \%', 'Consumption yoy growth \%', 'GFCF yoy growth \%')
set(gcf,'position',[x0,y0,width,height])
tightfig
saveas(gcf,'growth_rates_gdp_gfcf.pdf')


%%      Scatter plot
close all
scatter(gdp_gr -mean(gdp_gr),   inv_gr -mean(inv_gr), "filled" ); hold on
scatter(gdp_gr -mean(inv_gr),   con_gr -mean(con_gr), "filled" ); hold on
plot(gdp_gr -mean(gdp_gr),     gdp_gr -mean(gdp_gr)); grid
ylim([-30 50]);
xlim([-10 10]);
    x0=10;
    y0=10;
    width=500;
    height=500;
    xlabel('GDP growth \%'); 
    ylabel('Investment growth \%')
    set(gcf,'position',[x0,y0,width,height])
    tightfig
saveas(gcf,'scatter_gdp_gfcf.pdf')


figure()
scatter(gdp_gr, con_gr, "filled" ); hold on
lsline;
plot(gdp_gr, gdp_gr); grid
ylim([-10 10]);
xlim([-10 10]);
    x0=10;
    y0=10;
    width=500;
    height=500;
    xlabel('GDP growth \%'); 
    ylabel('Investment growth \%')
    set(gcf,'position',[x0,y0,width,height])
    tightfig
saveas(gcf,'scatter_gdp_gfcf.pdf')

%%      Line Density plots
close all
figure;
ksdensity(gdp_gr); hold on;
ksdensity(inv_gr);
ksdensity(con_gr);
xlim([-30 30]);
legend({'Data 1','Data 2'})
xlabel('Value')
ylabel('Probability Density')
title('Overlapping Histograms', 'Interpreter', 'latex')

%%      Shaded Density plots

[yy1 xx1] = ksdensity(con_gr -mean(con_gr))
[yy2 xx2] = ksdensity(gdp_gr -mean(gdp_gr))
[yy3 xx3] = ksdensity(inv_gr -mean(inv_gr))

close all
area(xx1, yy1, LineWidth=1,   FaceAlpha=.33, EdgeAlpha=0.5); hold on
area(xx2, yy2, LineWidth=1,     FaceAlpha=.33, EdgeAlpha=0.5);
area(xx3, yy3, LineWidth=1,     FaceAlpha=.33, EdgeAlpha=0.5);
xlim([-30 30]);
