%%%% Table D2: Cross correlation
%% Last update: 24/10/2025

%% daily cross correlatoin
load ilsnyccy;
load NEERny;
load eurusd;
dilsny=[NaN;100*log(ilsnyccy(2:end)./ilsnyccy(1:end-1))'];

%% RR with themselves

startdate='01/01/2013';
enddate='13/03/2020';

idx1=find(optionsdates==datenum(startdate,'dd/mm/yyyy'));
idx2=find(optionsdates==datenum(enddate,'dd/mm/yyyy'));
rr=[usdilsrr10(idx1:idx2,:),usdilsrr25(idx1:idx2,:),dilsny(idx1:idx2)];
[t,n]=size(rr);
%drr=[repmat(NaN,1,n);rr(2:end,:)-rr(1:end-1,:)];
res1=corr(rr,'rows','complete');

%% BF with themselves
bf=[usdilsbf10(idx1:idx2,:),usdilsbf25(idx1:idx2,:),dilsny(idx1:idx2)];
[t,n]=size(bf);
%dbf=[repmat(NaN,1,n);bf(2:end,:)-bf(1:end-1,:)];
res2=corr(bf,'rows','complete');

%% ATMV with themselves

atmv=[usdilsatmv(idx1:idx2,:),dilsny(idx1:idx2)];
[t,n]=size(atmv);
%datmv=[repmat(NaN,1,n);atmv(2:end,:)-atmv(1:end-1,:)];
res3=corr(atmv,'rows','complete');

%% Misc.
ccystartdate=find(ccydates==datenum('01/01/2013','dd/mm/yyyy' ) ) ;
ccyenddate=find(ccydates==datenum('13/03/2020','dd/mm/yyyy' ) );
dilsny=[NaN;100*log(ilsnyccy(2:end)./ilsnyccy(1:end-1))'];
deurusd=[NaN;100*log(eurusd(2:end)./eurusd(1:end-1))];
dCCYS=[dilsny,deurusd,NEERny]; %% change in pct.
dCCYS=dCCYS(ccystartdate:ccyenddate,:);
load newforwards;
dforwards=[repmat(NaN,1,5);100*log(newforwards(2:end,:)./newforwards(1:end-1,:))]; %% in %
dforwards=dforwards(ccystartdate:ccyenddate);

%% Flows
%% With flows, NaN = zero
flowsstartdate = find(flowsdates >= datenum('01/01/2013','dd/mm/yyyy'), 1, 'first');
flowsenddate = find(flowsdates <= datenum('13/03/2020','dd/mm/yyyy'), 1, 'last');foreign_total( isnan(foreign_total) )= 0; 
flows=[foreign_total,real_sector,finance_sector,inst];
flows=flows(flowsstartdate:flowsenddate,:);
T=size(dCCYS,1);
newflows=NaN(T,4);
idx=ismember(ccydates(ccystartdate:ccyenddate),flowsdates(flowsstartdate:flowsenddate));
newflows(idx,:)=flows;
%% CDS
cdsstartdate=find(cdsdates==datenum('01/01/2013','dd/mm/yyyy' ) ) ;
cdsenddate=find(cdsdates==datenum('13/03/2020','dd/mm/yyyy' ) );
dCDS=[NaN;cds(cdsstartdate+1:cdsenddate)-cds(cdsstartdate:cdsenddate-1)];
% Telbor minus libor 1 m and 1 y
dtelbor=[NaN;telbor(2:end,2)-telbor(1:end-1,2)];
dlibor=[NaN;uslibor(2:end,2)-uslibor(1:end-1,2)];

intereststartdate=find(interestdates==datenum('01/01/2013','dd/mm/yyyy' ) ) ;
interestenddate=find(interestdates==datenum('13/03/2020','dd/mm/yyyy' ) );
dlibor=dlibor(intereststartdate:interestenddate,:);
dtelbor=dtelbor(intereststartdate:interestenddate,:);

res4=corr([dCCYS,dforwards',newflows,dCDS,dlibor,dtelbor],'rows','complete');
