%data_1900_2022.m 
clear all

%Get the CPI index in 2022
%note that the BLS revised the earlier CPI numbers; but here we are using
%only the new numbers for 2021 and 2022 to obtain the new inflation number
%for 2022 to preserve comparability with the computations that ended in
%2021
opts = spreadsheetImportOptions("NumVariables", 1);
opts.DataRange = "N51:N52";
opts.VariableTypes = "double";
cpi = readmatrix("r-cpi-u-rs-allitems2022.xlsx", opts, "UseExcel", false);
pai2022 = diff(log(cpi))*100;


%add data from MeasuringWorth
opts = delimitedTextImportOptions("NumVariables", 2);
opts.DataLines = [12, Inf];
opts.VariableTypes = ["double", "double"];
newirate = readmatrix("INTERESTRATE_2015-2022.csv", opts);
irate2022 =  newirate(end,2);

%add data from BEA 
%new data that ends in 2022
opts = spreadsheetImportOptions("NumVariables", 21);
opts.DataRange = "C8:W8";
newrgdp = readmatrix("bea_rgdp_2022.xlsx", opts, "UseExcel", false)'; %real GDP
newrgdp = str2double(newrgdp);
%note this is in chained 2017 dollars, but the other data is in chained
%2012 dollars, that we need to make an adjustment (see below). 
aux = newrgdp(end)/newrgdp(end-1);%growth rate between 2021 and 2022

%data that ended in 2021
opts = spreadsheetImportOptions("NumVariables", 20);
opts.DataRange = "C8:V8";
newrgdp = readmatrix("bea_rgdp_2021.xls", opts, "UseExcel", false)'; %real GDP
newrgdp = str2double(newrgdp);
newrgdp= [newrgdp; aux*newrgdp(end)]; 


%Get population for 2022 
opts = spreadsheetImportOptions("NumVariables", 252);
opts.DataRange = "C50:IT50";
beapopulation = readmatrix("bea_population_2022.xlsx", opts, "UseExcel", false); %population
beapopulation = str2double(beapopulation);
beapopulation = reshape(beapopulation,12,21)';
newpop = mean(beapopulation(:,6:7),2); %mean of population in June and July

newgdppc = newrgdp ./ newpop;
newgdppc = newgdppc*100/ newgdppc(4);
gdppc2022 = newgdppc(end); 

CD = cd; 
cd data_2021
[~, y, pai, irate, ~, ~, ~, year]= data_1900_2021;
cd(CD)
y=[y; log(gdppc2022)];
pai = [pai; pai2022];
irate = [irate; irate2022];
year = [year; 2022];

%compute variables in differences
dy = diff(y)*100;
dy2022 = dy(end);
dpai = diff(pai);
dpai2022 = dpai(end);
dirate = diff(irate);
dirate2022 = dirate(end);

CD = cd; 
cd data_2021
[~, ~, ~, ~, dy, dpai, dirate]= data_1900_2021;
cd(CD)

dy = [dy; dy2022];
dpai = [dpai; dpai2022];
dirate = [dirate; dirate2022];

Y = [dy dpai dirate];

save data_1900_2022.mat  year Y y pai irate dy dpai dirate 
