function y = loadData(start, End)
% monthly data set for analysis including
% yields; 197110 - 202204
% PTR estimates of pi*
% real-time macro estimate of r*
% proxy of trend i* = pi* + r*

% %----------------------- read yields ------------------------- 
% % treasury yields are from GSW on FED's website 1-15 years
% % 3-month and 6-month TB rates
% opts = delimitedTextImportOptions("NumVariables", 18);
% opts.DataLines = [2, Inf];
% opts.Delimiter = ",";
% opts.VariableNames = ["Date", "y025", "y05", "y1", "y2", "y3", "y4", "y5", "y6", "y7", "y8", "y9", "y10", "y11", "y12", "y13", "y14", "y15"];
% opts.VariableTypes = ["double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double"];
% data = readtable('./data/US_yields.csv', opts) ;
% data.yyyymm = floor(data{:,1}/100) ; 
% data.year = floor(data.yyyymm/100) ;
% data.month = data.yyyymm - 100*data.year ;
% data = data(data.yyyymm>start & data.yyyymm<End,:) ;

% monthly forward rates
fname = './data/US_sv.csv' ;
mats= [3 6 12:12:180]';
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


% %----------------- generate real rate ---------------------
% % monthlt ex-post real rate = nominal rate-realized year-over-year inflation
% % nominal: 3m T-bill rate
% % inflation: Core PCE inflation
% 
% PCE price index - seasonally adjusted
pce = readtable('./data/PCEPILFE.csv') ;
% Month = month(pce{:,1}) ;
% pce = pce(mod(Month,3)==0,:) ;
% year-over-year inflation
pce.pi = 100*[nan(12,1); log(pce.PCEPILFE(13:end))-log(pce.PCEPILFE(1:end-12))]  ;
pce(isnan(pce.pi),:) = [] ;
pce_year = year(pce{:,1});
pce_month = month(pce{:,1});
PCE = [pce_year pce_month pce{:,2:3}]; 
PCE = PCE(PCE(:,1)<2023 & PCE(:,1)>1970,:);
% % three-month T-bill rate - starts in 1934-01
% tb3 = readtable('./data/US/tb3MS.xlsx') ;
% tb3start = datetime(1934,01,01, 'Format','yyyy-MM-dd') ; 
% tb3end = datetime(2020,07,01, 'Format','yyyy-MM-dd');
% tb3.DATE = (tb3start:calmonths(1):tb3end)' ;
% % Month = month(tb3{:,3}) ; 
% % tb3 = tb3(mod(Month,3)==0,:) ;
% tb3 = tb3((year(tb3{:,3})>=1960 & year(tb3{:,3})<2020),2:3) ;   % PCE starts at 1960
% tb_year = year(tb3{:,2});
% tb_month = month(tb3{:,2});
% TB3 = [tb_year tb_month tb3{:,1}];
% % monthly ex-post real rate
% if ~isequal(100*TB3(:,1)+TB3(:,2), 100*PCE(:,1)+PCE(:,2))
%     return
% end
% % rr = join(pce, tb3) ;
% % rr.rr = rr.TB3MS - rr.pi ;
% rr = TB3(:,3) - PCE(:,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;  
% % Y = year(rr.DATE); M = month(rr.DATE);
% Y = TB3(:,1); M = TB3(:,2);
Y = PCE(:,1); M = PCE(:,2);
id_start = find(100*Y+M == start,1);
id_end = find(100*Y+M == End,1);
len = id_end - id_start ;
% Date = pce{id_start:id_end,1};
% yyyymm = 100*year(Date) + month(Date);
% % data.rr = rr.rr(id_start:id_end) ;
% rr_ewma = rr_ewma(id_start:id_end) ;
% % 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(Date, yyyymm,Forwardrates(:,1:end),PCE(id_start:id_end,4), TB3(id_start:id_end,3), rr_ewma, PTR(:,3));
% data = splitvars(data);
% data.Properties.VariableNames = {'Date', 'yyyymm', mats_name{:,:,:}, 'PCE', 'TB3', 'rr_ewma', 'PTR'};
% y = data ;

% % USE quarterly PTR as a proxy for long-run inflation
% frbus = readtable("./data/HISTDATA.TXT");
% PTR = frbus(:,{'OBS','PTR'});
% PTR.date = datenum(PTR.OBS,'YYQQ');
% PTR.year = year(PTR.date);
% PTR.month = month(PTR.date);
% PTR = table2array(PTR(:,{'year','month','PTR'}));
% PTR = kron(PTR, ones(3,1));
% [~,m,~] = unique(100*PTR(:,1)+PTR(:,2),'first');
% m2 = m+1; m3 = m+2; 
% PTR(m2,2) = PTR(m2,2)+1; PTR(m3,2) = PTR(m3,2)+2; 
% PTR(m,3) = nan; PTR(m2,3) = nan; 
% PTR = [PTR(:,1)*100 + PTR(:,2) PTR(:,3)];
% PTR = PTR(PTR(:,1)>197109,:);


Date = datetime(1971,10,1) + calmonths(0:len)';
yyyymm = 100*year(Date) + month(Date);
data = table(Date, yyyymm,Forwardrates, PCE(id_start:id_end,4));
data = splitvars(data);
data.Properties.VariableNames = {'Date', 'yyyymm', mats_name{:,:,:}, 'Inflation' };

y = data;



