%%% Construct basis: 31/05/2021
%% Per appendix of Wu et al. (2021)

data=readtable([ini.dirs.raw '/Basis/NEWBASIS.xlsx']);

%% First we create the short term basis < 12m

forwardpnts1m=data.ILS1MCurncy;
forwardpnts3m=data.ILS3MCurncy;
forwardpnts6m=data.ILS6MCurncy;
usdils=data.USDILSCurncy;
forward3m=usdils+forwardpnts3m/10000;
%% Calculate annulized forward premium in %
rho1m=100*( (360/(30-2))*(usdils+forwardpnts1m/10000-usdils)./usdils);
rho3m=100*( (360/(30*3-2))*(usdils+forwardpnts3m/10000-usdils)./usdils);
rho6m=100*( (360/(30*6-2))*(usdils+forwardpnts6m/10000-usdils)./usdils);

%% Calculate premium for >= 1 year

%% SOVEREIGN YIELDS
ils1y=data.I32501YIndex;
ils3y=data.I32503YIndex;
ils5y=data.I32505YIndex;
ils7y=data.I32507YIndex;
ils10y=data.I32510YIndex;

us1y=data.C0821YIndex;
us3y=data.C0823YIndex;
us5y=data.C0825YIndex;
us7y=data.C0827YIndex;
us10y=data.C08210YIndex;

%% ILS fixed for floating
ilsswap1y=data.ISSW1Curncy;
ilsswap3y=data.ISSW3Curncy;
ilsswap5y=data.ISSW5Curncy;
ilsswap7y=data.ISSW7Curncy;
ilsswap10y=data.ISSW10Curncy;
%% ILS to US floating swap
xccy1y=data.ISBS1Curncy;
xccy3y=data.ISBS3Curncy;
xccy5y=data.ISBS5Curncy;
xccy7y=data.ISBS7Curncy;
xccy10y=data.ISBS10Curncy;
%% US swap
usswap1y=data.USSW1Curncy;
usswap3y=data.USSW3Curncy;
usswap5y=data.USSW5Curncy;
usswap7y=data.USSW7Curncy;
usswap10y=data.USSW10Curncy;

rho1y=ilsswap1y+xccy1y/100-usswap1y;
rho3y=ilsswap3y+xccy3y/100-usswap3y;
rho5y=ilsswap5y+xccy5y/100-usswap5y;
rho7y=ilsswap7y+xccy7y/100-usswap7y;
rho10y=ilsswap10y+xccy10y/100-usswap10y;

%% TELBOR
telbor1m=data.TELBOR01Index;
telbor1m=fillmissing(telbor1m,'linear');

telbor3m=data.TELBOR03Index;
telbor3m=fillmissing(telbor3m,'linear');

telbor6m=data.TELBOR06Index;
telbor6m=fillmissing(telbor6m,'linear');

%% LIBOR
libor1m=data.US0001MIndex;
libor1m=fillmissing(libor1m,'linear');
libor3m=data.US0003MIndex;
libor13m=fillmissing(libor3m,'linear');

libor6m=data.US0006MIndex;
libor6m=fillmissing(libor6m,'linear');

%% basis
rho3m=100*((log(forward3m)-log(usdils))*(360/90));
basis1m=libor1m-(telbor1m-rho1m);
basis3m=libor3m-(telbor3m-rho3m);
basis6m=libor6m-(telbor6m-rho6m);
basis1y=us1y-(ils1y-rho1y);
basis3y=us3y-(ils3y-rho3y);
basis5y=us5y-(ils5y-rho5y);
basis7y=us7y-(ils7y-rho7y);
basis10y=us10y-(ils10y-rho10y);

newbasis=NaN(T,8); %% intersect with the dates in the main file
newforward3m=NaN(T,1);
newbasis3m=NaN(T,1);
idx1=ismember(datenum(data.Var1),ccydates);
idx2=ismember(ccydates,datenum(data.Var1));
newbasis(idx2,:)=[basis1m(idx1),basis3m(idx1),basis6m(idx1),basis1y(idx1),basis3y(idx1),basis5y(idx1),basis7y(idx1),basis10y(idx1)];

newforward3m(idx2,:)=forward3m(idx1);
%newbasis3m(idx2,2)=libor3m-(telbor3m-rho3m);
save('newbasis','newbasis');