%% Date: 30/10/2025
%% Appendix J
cd(ini.dirs.code)

%% Intervention predicatability: Table of First-stage without all specifications is in the bottom

%% Initialize table
p=500;
Table=NaN(39,6);
%% 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;
%% ccys

load ilsnyccy; 
load eurusd;
ilsnyccy=ilsnyccy';
%load NEERJP;
load NEERny;
%% lagged intervention
intlag=lagmatrix(int,0:200);

%% lagged interest rate change
load uslibor; load telbor;
laguslibor=lagmatrix(uslibor,0:50);
lagtelbor=lagmatrix(telbor,0:50);
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,:)];
lagtelborchange=lagmatrix(dtelbor(:,2),0:p);
lagliborchange=lagmatrix(dlibor(:,2),0:p);
save('lagtelborchange','lagtelborchange');
save('lagliborchange','lagliborchange');
save('laguslibor','laguslibor');
save('lagtelbor','lagtelbor');
%% 1  day returns
lead=1;
dilsny=[repmat(NaN,lead,1);log(ilsnyccy(lead+1:end,:)./ilsnyccy(1:end-lead,:) )]*100; %% change in pct.
save('dilsny','dilsny'); 

lead=1;
deurusd=[repmat(NaN,lead,1);log(eurusd(lead+1:end,:)./eurusd(1:end-lead,:) )]*100; %% change in pct.
save('deurusd','deurusd'); 
lageurusd=lagmatrix(deurusd,0:200);
save('lageurusd','lageurusd');
%% 1  day returns
dNEER1=NEERny;
%% 2  day returns
lead=2;
dilsny2=[repmat(NaN,lead,1);log(ilsnyccy(lead+1:end,:)./ilsnyccy(1:end-lead,:) )]*100; %% change in pct.

dNEER2=NaN(T,1);
lead=1;
for i=lead+1:T
dNEER2(i,1)=sum(NEERny(i-lead:i),'omitnan');
end
dNEER2((dNEER2)==0)=NaN;

%% 3-day return
lead=3;
dilsny3=[repmat(NaN,lead,1);log(ilsnyccy(lead+1:end,:)./ilsnyccy(1:end-lead,:) )]*100; %% change in pct.

lead=2;
dNEER3=NaN(T,1);
for i=lead+1:T
dNEER3(i,1)=sum(NEERny(i-lead:i),'omitnan');
end
dNEER3((dNEER3)==0)=NaN;

lead=2;
dilsny2=[repmat(NaN,lead,1);log(ilsnyccy(lead+1:end,:)./ilsnyccy(1:end-lead,:) )]*100; %% change in pct.


%% 5-day returns

lead=5;
dilsny5=[repmat(NaN,lead,1);log(ilsnyccy(lead+1:end,:)./ilsnyccy(1:end-lead,:) )]*100; %% change in pct.

lead=4;
dNEER5=NaN(T,1);
for i=lead+1:T
dNEER5(i,1)=sum(NEERny(i-lead:i),'omitnan');
end
dNEER5((dNEER5)==0)=NaN;


lead=21;
dilsny21=[repmat(NaN,lead,1);log(ilsnyccy(lead+1:end,:)./ilsnyccy(1:end-lead,:) )]*100; %% change in pct.

lead=6;
dilsny7=[repmat(NaN,lead,1);log(ilsnyccy(lead+1:end,:)./ilsnyccy(1:end-lead,:) )]*100; %% change in pct.

lagusdils7=lagmatrix(dilsny7,0:p);

lagusdils1=lagmatrix(dilsny,0:p);
save('lagusdils1','lagusdils1');

lead=10;
dilsny10=[repmat(NaN,lead,1);log(ilsnyccy(lead+1:end,:)./ilsnyccy(1:end-lead,:) )]*100; %% change in pct.
lagusdils10=lagmatrix(dilsny10,0:p);

lead=20;
dNEER21=NaN(T,1);
for i=lead+1:T
dNEER21(i,1)=sum(NEERny(i-lead:i),'omitnan');
end
dNEER21((dNEER21)==0)=NaN;


lead=9;
dNEER10=NaN(T,1);
for i=lead+1:T
dNEER10(i,1)=sum(NEERny(i-lead:i),'omitnan');
end
dNEER10((dNEER10)==0)=NaN;
%% 3 month returns. 60 trading days.
lead=60;
dilsny60=[repmat(NaN,lead,1);log(ilsnyccy(lead+1:end,:)./ilsnyccy(1:end-lead,:) )]*100; %% change in pct.

lead=59;
dNEER60=NaN(T,1);
for i=lead+1:T
dNEER60(i,1)=sum(NEERny(i-lead:i),'omitnan');
end
dNEER60((dNEER60)==0)=NaN;

lagusdils=lagmatrix(ilsnyccy,0:300);

% moving average US
maUSD=NaN(T,1);
lead=252;
for i=lead+1:T
    maUSD(i)=nanmean(lagusdils(i-lead:i,1));
end
maUSDlag=lagmatrix(maUSD,0:100);

load vix; load vixdates;
vixfull=NaN(T,1);

% Find common dates
[~, idx_ccy, idx_vix] = intersect(ccydates, vixdates);

% Assign vix values to corresponding positions in vixfull
vixfull(idx_ccy) = vix(idx_vix);

vixfull=fillmissing(vixfull,'previous');
save('vixfull','vixfull');
p=22*12+10;
lagvix=lagmatrix(vixfull,0:p);
save('lagvix','lagvix');

%% spot volume
load spotvol; load spotdates;
%eliminate sunday;
spotvolfull=NaN(T,1);
idxtilde=ismember(spotdates,datetime( datestr(ccydates) ));
idxtilde1=ismember(datetime( datestr(ccydates) ),spotdates);
spotvolfull(idxtilde1)=spotvol(idxtilde);
save('spotvolfull','spotvolfull');

p=22*12+10;
lagvix=lagmatrix(vixfull,0:p);
save('lagvix','lagvix');


lagusdils=lagmatrix(ilsnyccy,0:300);

%% VERBAL INTERVENTION DUMMY
tab=readtable('..\_data\verbal interventions\verbal interventions.xlsx');
verbdates=tab.Date;
verbindex=zeros(T,1);
idx=ismember(ccydates,datenum(tab.Date));
verbindex(idx)=1;
save('verbalindex');

%% deviations from target
devtarget=readtable('..\_data\target\dev target.xlsx','Sheet','data');
dev5d=devtarget.dev5dA;
dev21d=devtarget.dev21dA;
dev1y=devtarget.dev1yA;
dev3y=devtarget.dev3yA;
dev5y=devtarget.dev5yA;

deviations=NaN(T,5);
idx=ismember(ccydates,datenum(devtarget.Date));
deviations(idx,:)=[dev5d,dev21d,dev1y,dev3y,dev5y];
save('deviations','deviations');

%% DUMMY FOR DAYS WHERE INTERVENTION AMOUNTS IS PUBLISHED BY THE BOI
tab=readtable('..\_data\publication of interventions\publication.xlsx');
pubdates=tab.Date;
pubindex=zeros(T,1);
idx=ismember(ccydates,datenum(tab.Date));
pubindex(idx)=1;
save('pubindex','pubindex')
%% CITI US surprise index
tab=readtable('..\_data\citi surprise\CITISURPRISE.xlsx');
CITIdates=tab.ExchangeDate;
CITIindex=zeros(T,1);
idx=ismember(ccydates,datenum(CITIdates));
idx1=ismember(datenum(CITIdates),ccydates);
CITIindex(idx)=tab.Close(idx1);
save('CITIindex','CITIindex');

%% NS anf FF01 shocks
tab=readtable('..\_data\events\NS.xlsx','Sheet','shocks');
NSdates=tab.fomc;
NSindex1=zeros(T,1);
NSindex2=zeros(T,1);
idx=ismember(ccydates,datenum(NSdates));
idx1=ismember(datenum(NSdates),ccydates);
NSindex1(idx)=tab.ff_shock_0(idx1);
NSindex2(idx)=tab.ns_shock(idx1);

save('NSindex1','NSindex1');
save('NSindex2','NSindex2');

%% ILS monetary shocks %%
tab=readtable('..\_data\events\interest.xlsx');
ILshockdates=tab.dateonday;
ILshockindex=zeros(T,1);
idx=ismember(ccydates,datenum(ILshockdates));
idx1=ismember(datenum(ILshockdates),ccydates);
ILshockindex(idx)=tab.actual(idx1)-tab.forecast(idx1);
save('ILshockindex','ILshockindex');
  
%% ILS CPI shocks %%
tab=readtable('..\_data\cpi events\cpi_events.xlsx');
CPIdates=tab.tradingday;
CPIindex=zeros(T,1);
idx=ismember(ccydates,datenum(CPIdates));
idx1=ismember(datenum(CPIdates),ccydates);
CPIindex(idx)=tab.actual(idx1)-tab.pred(idx1);
save('CPIindex','CPIindex');


%% Uncertainty %%
tab=readtable('..\_data\uncertainty\uncertainty.xlsx');
uncertaintydates=tab.date;
uncertaintyindex=zeros(T,1);
uncertaintyindex2=zeros(T,1);
idx=ismember(ccydates,datenum(uncertaintydates));
idx1=ismember(datenum(uncertaintydates),ccydates);
uncertaintyindex(idx)=tab.uncertainty1(idx1);
uncertaintyindex2(idx)=tab.uncertainty2(idx1);
duncertainty=[NaN;uncertaintyindex(2:end)-uncertaintyindex(1:end-1)];
laguncertainty=lagmatrix(uncertaintyindex,0:20);
laguncertainty2=lagmatrix(uncertaintyindex2,0:20);

%%% HKM %%%
hkm=readtable('..\_data\intermediate\He_Kelly_Manela_Factors_daily_221001.csv');
ofr=readtable('..\_data\intermediate\fsi.csv');
hkmdates=hkm.yyyymmdd;
hkmdates=datenum(num2str(hkmdates),'yyyymmdd');
hkmindex=[hkm.intermediary_capital_ratio,hkm.intermediary_capital_risk_factor,hkm.intermediary_value_weighted_investment_return,hkm.intermediary_leverage_ratio_squared];
HKM=hkmindex;

id1=ismember(ccydates,datenum(ofrdates));
id2=ismember(datenum(ofrdates),ccydates);

ofr=zeros(T,1);
ofr(id1)=dofrindex(id2);

id1=ismember(ccydates,hkmdates);
id2=ismember(hkmdates,ccydates);

hkm=zeros(T,4);
hkm(id1,:)=HKM(id2,:);

save('hkm','hkm');

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


intlastweek=double(intlag(:,1)>0)+double(intlag(:,2)>0)+double(intlag(:,3)>0)+double(intlag(:,4)>0)+double(intlag(:,5)>0)>0;
intweeklag=lagmatrix(double(intlastweek),0:200);
save('intweeklag','intweeklag');

lagusdils=lagmatrix(dilsny,0:300);
lagusdils5=lagmatrix(dilsny5,0:300);
lagusdils60=lagmatrix(dilsny60,0:300);
save('lagusdils','lagusdils');save('lagusdils5','lagusdils5');save('lagusdils60','lagusdils60');
save('lagusdils10','lagusdils10');

%% With HKM


load hkm;
temphkm=hkm(:,4);
temphkm(temphkm==0)=NaN;
temphkm=fillmissing(temphkm,'previous');
temphkm=[NaN;diff(temphkm)];

y=dilsnyccy(idx:idx1);
Y=[int(idx:idx1)/1000,int(idx:idx1)/1000.*temphkm(idx:idx1)]; %% Endogenous
T=size(y,1);
B=ones(T,1);
X=[B,lageurusd(idx:idx1,1),lagvix(idx:idx1,1)-lagvix(idx:idx1,6),NSindex1(idx:idx1),NSindex2(idx:idx1),ILshockindex(idx:idx1),CPIindex(idx:idx1),CITIindex(idx:idx1)];
Z=[temphkm(idx:idx1).*intlag(idx:idx1,2),intlag(idx:idx1,2),temphkm(idx:idx1).*intweeklag(idx:idx1,2),intweeklag(idx:idx1,2),temphkm(idx:idx1).*lagNEER2(idx:idx1,2),lagNEER2(idx:idx1,2)];
%points = 1000;
alfa = 0.05;
tau  = 0.10;
output = gweakivtest(y,Y,X,Z,'NW',alfa,tau)

%% The regression itself is in EVIEWS