%%% Appendix H
%%% Date: 30/10/2025

cd(ini.dirs.code)
%% Initialize all data matrix
load ccydates; 
T=size(ccydates,1);
%% intervention
load intervention; load interventiondates;
idx=ismember(ccydates,interventiondates);
int=NaN(T,1);
int(idx)=intervention;
int(isnan(int))=0;

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

%% RR & BF
load('lagrr'); load('lagbf');
%% CDS
load('lagcds')

%% ccys
load ilsnyccy; load dilsnyccy;
%load ilslondonccy;
load NEERny;

%% Change in NEER
load lagNEER1
load lagNEER2
load lagNEER5
load lagNEER60
load lagNEER10
load newforwards
load newbasis
%load lagNEER3

%% Flows
load laginstflows
load lagforeignflows


%%%% Exogenous
%% lagged interest rate change
%% 5-day change in interest rate
load uslibor; load telbor;
p=10;
lead=5;
dtelbor=[repmat(NaN,lead,6);telbor(lead+1:end,:)-telbor(1:end-lead,:)];
dlibor=[repmat(NaN,lead,6);uslibor(lead+1:end,:)-uslibor(1:end-lead,:)];
%% Take the three-month change
lagtelborchange=lagmatrix(dtelbor(:,2),0:p);
lagliborchange=lagmatrix(dlibor(:,2),0:p);
save('lagtelborchange','lagtelborchange');
save('lagliborchange','lagliborchange');

%1-day libor change 
lead=1;
dlibor=[repmat(NaN,lead,6);uslibor(lead+1:end,:)-uslibor(1:end-lead,:)];
dtelbor=[repmat(NaN,lead,6);telbor(lead+1:end,:)-telbor(1:end-lead,:)];
lagtelborchange1=lagmatrix(dtelbor(:,2),0:p);
%% Take the three-month change
lagliborchange1=lagmatrix(dlibor(:,2),0:p);
save('lagliborchange1','lagliborchange1');
save('lagtelborchange1','lagtelborchange1');


load lageurusd;
load intweeklag;

%%% dforward
dforwards=[repmat(NaN,1,5);100*log(newforwards(2:end,:)./newforwards(1:end-1,:))]; %% in %
save('dforwards','dforwards');
load newbasis;

%% verbal intervention
load verbalindex;
%% pub index
load pubindex;

%% verbal intervention
load verbalindex;
%%% Verbal intervention. Set to zero days of intervention.
idx=find(ccydates==datenum('01/01/2013','dd/mm/yyyy')); %% beginning of sample
idx1=find(ccydates==datenum('31/12/2019','dd/mm/yyyy'));

load verbalindex;
idx=find(ccydates==datenum('01/01/2013','dd/mm/yyyy')); %% beginning of sample
idx1=find(ccydates==datenum('31/12/2019','dd/mm/yyyy'));

newidx=find(verbindex>0);
verbintervention=intervention;
verbintervention(newidx)=0;
verbintervention(isnan(intervention))=0;

%% pub index
load pubindex;
%% vix
load vix; load vixdates;
vixfull=NaN(T,1);

% Find matching dates
[~, idx_vixdates, idx_ccydates] = intersect(vixdates, ccydates);

% Assign VIX values to matching dates
vixfull(idx_ccydates) = vix(idx_vixdates);
vixfull = fillmissing(vixfull,'previous');lagvix=lagmatrix(vixfull,0:p);
save('lagvix','lagvix');


%% Panel A:
%%  Regular OLS %%%
idx=find(ccydates==datenum('01/01/2013','dd/mm/yyyy')); %% beginning of sample
idx1=find(ccydates==datenum('31/12/2019','dd/mm/yyyy'));

load NSindex1; load NSindex2;
NSindex1lag=lagmatrix(NSindex1,0:20);
NSindex2lag=lagmatrix(NSindex2,0:20);

%% if interest rate
dCITI=CITIindex(2:end)-CITIindex(1:end-1);
dCITI=[NaN;dCITI];


%% Write that adding CITI didn't do anything
data=[dilsnyccy,ones(T,1),verbintervention/1000,lageurusd(:,1),lagvix(:,1)-lagvix(:,6),ILshockindex,NSindex1,NSindex2,CPIindex,CITIindex/1000]; %% dataset used for the estimation
[coeff,tstat,r,n]=fastnw(data(idx:idx1,1),data(idx:idx1,2:end),0);
x1=coeff(1),x2=coeff(2),x3=coeff(3);x4=coeff(4);x5=coeff(5);
res=[coeff,tstat];
res=res'; [m,n]=size(res);
res=reshape(res,m*n,1);

intlag=lagmatrix(int,0:10);
load lagatmvshort; load lagatmvsimple; load lagrr; load lagbf1; load lagbf2; load lagusdils5;load lagNEER1;load lagNEER5; load lagusdils60;

data=[dilsnyccy,ones(T,1),verbintervention/1000,lageurusd(:,1),lagvix(:,1)-lagvix(:,6),ILshockindex,CPIindex,NSindex1,NSindex2,CITIindex]; %% dataset used for the estimation

inst=[lageurusd(idx:idx1,1),lagvix(idx:idx1,1)-lagvix(idx:idx1,6),ILshockindex(idx:idx1),NSindex1(idx:idx1),NSindex2(idx:idx1),CPIindex(idx:idx1),CITIindex(idx:idx1),intlag(idx:idx1,2),lagNEER2(idx:idx1,2),lagNEER10(idx:idx1,3),intweeklag(idx:idx1,2)];
temp=[data(idx:idx1,:),inst];
temp=packr(temp);
data=temp(:,[1 2 3 4 5 6 7 8 9 10]);
inst=temp(:,[2 11:end]); %% col. 2 is the constant
y=temp(:,1);
X=data(:,3:end);
Z=temp(:,end-3:end);

t=size(data,1);

%% Two-stage least square 
y=data(:,1);
z=inst; %% constant and exogenous
x=data(:,2:end); %% intervention
ols_beta=(x'*x)\x'*y;

%% 2SLS beta
P_z=z/(z'*z)*z'; %% projection matrix
xtag=P_z*x;
tsls_beta=(xtag'*xtag)\(xtag'*y);
e=y-x*tsls_beta;
n=size(data,1);
e=y-x*tsls_beta;
vc=diag( diag(e*e') );
varcovar=(inv(xtag'*xtag)*xtag')*vc*(inv(xtag'*xtag)*xtag')';
std=sqrt(diag(varcovar));
tstat_tsls=tsls_beta./std;
res=[tsls_beta,tstat_tsls];
res=res'; [m,n]=size(res);
res=reshape(res,m*n,1);


%% GMM was done in EVIEWS