%% Table 7
%% Date: 30/10/2025

%% Relation between lagged rr, atmv, and bf to future intervention
cd(ini.dirs.code)
%% Initialize all data matrix
load ccydates; 
T=size(ccydates,1);
%% intervention
load intervention; load interventiondates;
load lagusdils60;

idx=ismember(ccydates,interventiondates);
int=zeros(T,1);
int(idx)=intervention;
int(isnan(int))=0;

load lagliborchange;load lagtelborchange;

%% dates
idx=find(ccydates==datenum('01/01/2013','dd/mm/yyyy')); %% beginning of sample
%idx=find(ccydates==datenum('01/01/2016','dd/mm/yyyy')); %% beginning of sample

idx1=find(ccydates==datenum('31/12/2019','dd/mm/yyyy'));

%%% Create 10 week RR and BF lag %%%
load optionsdates;load usdilsrr10;load usdilsrr25;load usdilsatmv;

load uslibor;
mat=[1/365,30/365,30*3/365,30*6/365,30*9/365,1];
d25=-norminv(.25*exp( (uslibor/100).*mat) ); 
d10=-norminv(.1*exp( (uslibor/100).*mat) );

lead=10;

datmv=[repmat(NaN,lead,6);usdilsatmv(1+lead:end,:)-usdilsatmv(1:end-lead,:)];
lagatmvsimple=lagmatrix(datmv,0:10);


drr1w10=[repmat(NaN,lead,6);usdilsrr10(1+lead:end,:)./usdilsatmv(1+lead:end,:)-usdilsrr10(1:end-lead,:)./usdilsatmv(1:end-lead,:)];
drr1w25=[repmat(NaN,lead,6);usdilsrr25(1+lead:end,:)./usdilsatmv(1+lead:end,:)-usdilsrr25(1:end-lead,:)./usdilsatmv(1:end-lead,:)];



rr25lag10=lagmatrix(drr1w25,0:10);
rr10lag10=lagmatrix(drr1w10,0:10);



lead=10;
dbf1w10=[repmat(NaN,lead,6);usdilsbf10(1+lead:end,:)./usdilsatmv(1+lead:end,:)-usdilsbf10(1:end-lead,:)./usdilsatmv(1:end-lead,:)];
dbf1w25=[repmat(NaN,lead,6);usdilsbf25(1+lead:end,:)./usdilsatmv(1+lead:end,:)-usdilsbf25(1:end-lead,:)./usdilsatmv(1:end-lead,:)];


bf25lag10=lagmatrix(dbf1w25,0:10);
bf10lag10=lagmatrix(dbf1w10,0:10);


%% CDS
load('lagcds')

%% ccys
load ilsnyccy;
load ilslondonccy;
load NEERny;
load lagint;
%% Change in NEER
load lagNEER1
load lagNEER2
load lagNEER5
load lagNEER60
load lagNEER10;
load CITIindex;
%load lagNEER3

%% Flows
load lagvix;
load laginstflows;
load lageurusd;
load lagforeignflows
load intweeklag;
load NSindex1;
load NSindex2;
load ILshockindex;
load CPIindex;
res=NaN(10,6);


for j=1:6 %% run on all BF and all RR

[coeff,tstat,r,n]=fastnw(lagint(idx:idx1,1)/1000,[CITIindex(idx:idx1),NSindex1(idx:idx1),ILshockindex(idx:idx1),CPIindex(idx:idx1),NSindex2(idx:idx1),lagint(idx:idx1,2)/1000,lagvix(idx:idx1,1)-lagvix(idx:idx1,5),intweeklag(idx:idx1,2)/1000,lagNEER2(idx:idx1,2),lagNEER10(idx:idx1,2),.5*(rr25lag10(idx:idx1,6+j)+rr10lag10(idx:idx1,6+j)),.5*(bf25lag10(idx:idx1,6+j)+bf10lag10(idx:idx1,6+j)),lagatmvsimple(idx:idx1,6+j)],1); %% divide int by 1000.


nanstd(.5*(rr25lag10(idx:idx1,6+j)+rr10lag10(idx:idx1,6+j)))

%% intercept
res(1,j)=coeff(1);
res(2,j)=tstat(1);

%% rr
res(3,j)=coeff(end-2);
res(4,j)=tstat(end-2);
%% bf
res(5,j)=coeff(end-1);
res(6,j)=tstat(end-1);

%% ATMV
res(7,j)=coeff(end);
res(8,j)=tstat(end);

res(end,j)=r;
end

%% Pooled-OLS
totalrr=[];
totalatmv=[];
totalbf=[];
alldates=[];
controls=[];
ids=[];
interventions=[];
for j=1:6 %% run on all BF and all RR
    controls=[controls;[CITIindex(idx:idx1),NSindex1(idx:idx1),ILshockindex(idx:idx1),CPIindex(idx:idx1),NSindex2(idx:idx1),lagint(idx:idx1,2)/1000,lageurusd(idx:idx1,2),lagvix(idx:idx1,2)-lagvix(idx:idx1,3),intweeklag(idx:idx1,2)/1000,lagNEER2(idx:idx1,2),lagNEER10(idx:idx1,3)]];
    alldates=[alldates;ccydates(idx:idx1)];
    totalrr=[totalrr;.5*(rr25lag10(idx:idx1,6+j)+rr10lag10(idx:idx1,6+j))];
    totalbf=[totalbf;.5*(bf25lag10(idx:idx1,6+j)+bf10lag10(idx:idx1,6+j))];
    totalatmv=[totalatmv;lagatmvsimple(idx:idx1,6+j)]; %% divide int by 1000.
    cross=length(ccydates(idx:idx1));
    ids=[ids;repmat(j,cross,1)];
   interventions=[interventions;lagint(idx:idx1,1)/1000];
end

alldates=datetime(datestr(alldates));

[beta,tstat,V_T,numobs,numgroup,rsquared]=panel_reg(alldates,ids,interventions,[controls,totalbf,totalatmv,totalrr],2,30);
res=[beta,tstat];
res=res'; [m,n]=size(res);
res=reshape(res,m*n,1);
res=[res;r];