function y = loadData(start, End)
% monthly data set for analysis including
% forward rates; 197209 - 201912  3m,6m,1-15y
% core CPI non-food and non-energy
% three-month Interbank Rates
% ex-post real rate = nominal rate - realized year-over-year inflation


%%--------------------- monthly forward rates ------------------------- 
fname = './data/GM_sv.csv' ;
% 197209 - 202203
mats= [3 6 12:12:120]';
Forwardrates = genFR(fname,mats);
mats_name = cell(1,1,size(mats,1));
for ii = 1:size(mats,1)
    mats_name{:,:,ii} = ['m' num2str(mats(ii))];
end

%-------------read inflation data -------------------------
% core CPI non-food and non-energy
corecpi = readtable('./data/DEUCPICORMINMEI.csv') ;
cpi_date = datenum(corecpi{:,1});
cpi_year = year(cpi_date);
cpi_month = month(cpi_date);
CPIdata = [cpi_year cpi_month log(table2array(corecpi(:,2)))]; 
% year-over-year inflation
CPIdata = [CPIdata, 100*[nan(12,1); CPIdata(13:end,3) - CPIdata(1:end-12,3)]];
% 1972-01 : 2022-03
CPIdata = CPIdata((CPIdata(:,1)>1971 & CPIdata(:,1)<2023),:);

% %------------------ nominal interest rate -------------------------
% % three-month Interbank Rates
% tb = readtable('./data/GM/IR3.csv');
% tb_date = datenum(tb{:,1});
% tb_year = year(tb_date);
% tb_month = month(tb_date);
% tbdata = [tb_year tb_month table2array(tb(:,2))];
% tbdata = tbdata(tbdata(:,1)>1962 & tbdata(:,1)<2020,:);

%----------------- generate real rate ---------------------
% monthly ex-post real rate = nominal rate - realized year-over-year inflation
% nominal: 3m interbank rate
% inflation: Core PCE inflation
% rr = tbdata(:,3) - CPIdata(:,4);

% calculate exponentially-weighted moving average of real rate as in Cieslak-Povala 
% v = 0.98 is the default smoothing parameter, appropriate for monthly data
% Cieslak-Povala (2015) eq(19)
% v = 0.98;
% ewma = nan(length(rr),1) ;
% ewma(1) = rr(1) ;
% for i = 2:length(rr)
%     ewma(i) = ewma(i-1) + (1-v)*(rr(i) - ewma(i-1)) ;
% end
% rr_ewma = ewma;  

%--------------- 10-year-average of inlfation ------------------
% linf = nan(size(CPIdata,1),1);
% for i = 120:size(CPIdata,1)
%     linf(i,1) = mean(CPIdata(i-119:i,4));
% end
% Y = year(rr.DATE); M = month(rr.DATE);
Y = CPIdata(:,1); M = CPIdata(:,2);
id_start = find(100*Y+M == start,1);
id_end = find(100*Y+M == End,1);
yyyymm = 100*CPIdata(id_start:id_end,1) + CPIdata(id_start:id_end,2);
temp = num2str(yyyymm, '%d');
temp = datetime(temp,'InputFormat','yyyymm', 'Format','yyyy-mm');
% data.DATE = datestr(datenum(num2str(data.Date,'%d'), 'yyyyMMdd'), 'yyyy-MM-dd') ;
% data.Date = Y(id_start:id_end)*100 + M(id_start:id_end) ;
data = table(temp,yyyymm,Forwardrates(2:end,:),CPIdata(id_start:id_end,3:4));
data = splitvars(data);
data.Properties.VariableNames = {'Date', 'yyyymm', mats_name{:,:,:}, 'CPI', 'Inflation'};
y = data ;


