function y = loadData(start, End)
% monthly data set for analysis including
% forward rates; 198201 - 202203  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/GM.xlsx' ;
% mats= [6 7 12:12:180]';
mats= (12:12:120)';
% Forwardrates = genFR(fname,mats, start, End);
% mats_name = cell(1,1,size(mats,1));
for ii = 1:size(mats,1)
    mats_name{:,:,ii} = ['m' num2str(mats(ii))];
end

% Import UK forward rates
opts = delimitedTextImportOptions("NumVariables", 18, "Encoding", "UTF-8");
% Specify range and delimiter
opts.DataLines = [2,Inf];
opts.Delimiter = ",";
% Specify column names and types
opts.VariableNames = ["Date", "m6", "m7", "y1", "y2", "y3", "y4", "y5", "y6", "y7", "y8", "y9", "y10", "y11", "y12", "y13", "y14", "y15"];
opts.VariableTypes = ["datetime", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, "Date", "InputFormat", "yyyy-MM-dd");
% Import the data
UKfwdr = readtable("./data/UK_fwdr.csv", opts);
UKfwdr.yyyymm = year(UKfwdr.Date)*100 + month(UKfwdr.Date);
clear opts

% for i = 1:size(UKfwdr,1)
%     if isnan(UKfwdr.m6(i))
%         UKfwdr.m6(i) = UKfwdr.m7(i);
%     end
% end
UKfwdr = movevars(UKfwdr,'yyyymm','After','Date');
%-------------read inflation data -------------------------
% core CPI non-food and non-energy
% Import the data
corecpi = readtable("./data/GBRCPICORMINMEI.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)]];
CPIdata = CPIdata((CPIdata(:,1)>1969 & CPIdata(:,1)<2023),:);

%------------------ nominal interest rate -------------------------
% three-month Interbank Rates
% tb = readtable('./data/UK/TB3.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)>1971 & 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;  


Y = CPIdata(:,1); M = CPIdata(:,2);
id_start = find(100*Y+M == start,1);
id_end = find(100*Y+M == End,1);
if isempty(id_end)
    id_end = size(CPIdata,1);
end
length1 = id_end - id_start;
id1 = find(UKfwdr.yyyymm==start);
id2 =  find(UKfwdr.yyyymm==End);
length2 = id2 - id1;
mylength = min(length1, length2);

yyyymm = 100*CPIdata(id_start:id_start+mylength,1) + CPIdata(id_start:id_start+mylength,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,UKfwdr{id1:id1+mylength,5:14},CPIdata(id_start:id_start+mylength,3:4));
data = splitvars(data);
data.Properties.VariableNames = {'Date', 'yyyymm', mats_name{:,:,:}, 'CPI', 'Inflation'};
y = data ;


