%%%% Appendix Table D2, D3, D4: 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'), 1, 'first');
idx2 = find(optionsdates <= datenum(enddate,'dd/mm/yyyy'), 1, 'last');
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'), 1, 'first');
ccyenddate = find(ccydates <= datenum('13/03/2020','dd/mm/yyyy'), 1, 'last');
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);
% Transpose dforwards to make it a column vector
dforwards = dforwards';

%% 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);
% Use ismember with two outputs to get the correct mapping
[lia, locb] = ismember(ccydates(ccystartdate:ccyenddate), flowsdates(flowsstartdate:flowsenddate));
% Only assign where there are matches
newflows(lia,:) = flows(locb(lia),:);

%% CDS - align to ccydates length
cdsstartdate = find(cdsdates >= datenum('01/01/2013','dd/mm/yyyy'), 1, 'first');
cdsenddate = find(cdsdates <= datenum('13/03/2020','dd/mm/yyyy'), 1, 'last');

% Create dCDS with same length as dCCYS
dCDS_full = [NaN; cds(cdsstartdate+1:cdsenddate) - cds(cdsstartdate:cdsenddate-1)];
dCDS_aligned = NaN(T, 1);

% Align CDS dates with ccy dates
[lia, locb] = ismember(ccydates(ccystartdate:ccyenddate), cdsdates(cdsstartdate:cdsenddate));
dCDS_aligned(lia) = dCDS_full(locb(lia));

% 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'), 1, 'first');
interestenddate = find(interestdates <= datenum('13/03/2020','dd/mm/yyyy'), 1, 'last');
dlibor=dlibor(intereststartdate:interestenddate,:);
dtelbor=dtelbor(intereststartdate:interestenddate,:);

% Now concatenate - all should be T x 1 or T x n
res4=corr([dCCYS, dforwards, newflows, dCDS_aligned, dlibor, dtelbor], 'rows', 'complete');