%% Appendix G
%% Predictability of intervention on future FX
%% Dates: 30/10/2025

cd(ini.dirs.code);
load ccydates;
idx=find(ccydates==datenum('01/01/2013','dd/mm/yyyy')); %% beginning of sample
idx1=find(ccydates==datenum('31/12/2019','dd/mm/yyyy'));
T=size(ccydates,1);

%% intervention
load intervention; load interventiondates;
intervention(isnan(intervention))=0;
load lagcds;load lagint; 
%% ccys
load NEERny.mat; load dilsny; load ilsnyccy;
load lageurusd; load lagvix; load lagliborchange; load lagusdils60; load lagNEER2; load lagNEER10;
load intweeklag; load newforwards;
load lagrr10;
load nsindex1;
load nsindex2;
load ILshockindex;
load CPIindex;
load CITIindex;
load intweeklag;
ilsnyccy=ilsnyccy';
%% 1  day returns
lead=1;
dilsny=[repmat(NaN,lead,1);log(ilsnyccy(lead+1:end,:)./ilsnyccy(1:end-lead,:) )]*100; %% change in pct.
dforward=[repmat(NaN,lead,5);log(newforwards(lead+1:end,:)./newforwards(1:end-lead,:) )]*100; %% change in pct.

dNEER1=NaN(T,1);
lead=0;
for i=lead+1:T
dNEER1(i,1)=sum(NEERny(i-lead:i),'omitnan');
end
%% 5  day returns
lead=5;
dilsny5=[repmat(NaN,lead,1);log(ilsnyccy(lead+1:end,:)./ilsnyccy(1:end-lead,:) )]*100; %% change in pct.
dforward5=[repmat(NaN,lead,5);(newforwards(lead+1:end,:)./newforwards(1:end-lead,:) -1)]*100; %% change in pct.

dNEER5=NaN(T,1);
lead=4;
for i=lead+1:T
dNEER5(i,1)=sum(NEERny(i-lead:i),'omitnan');
end
%% 10 day
lead=10;
dilsny10=[repmat(NaN,lead,1);log(ilsnyccy(lead+1:end,:)./ilsnyccy(1:end-lead,:) )]*100; %% change in pct.
dforward10=[repmat(NaN,lead,5);log(newforwards(lead+1:end,:)./newforwards(1:end-lead,:) )]*100; %% change in pct.

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

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

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

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

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

%% 66 month returns. 60 trading days.
lead=66;
dilsny66=[repmat(NaN,lead,1);log(ilsnyccy(lead+1:end,:)./ilsnyccy(1:end-lead,:) )]*100; %% change in pct.
dforward66=[repmat(NaN,lead,5);log(newforwards(lead+1:end,:)./newforwards(1:end-lead,:) )]*100; %% change in pct.

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

%% 132 month returns. 60 trading days.
lead=132;
dilsny132=[repmat(NaN,lead,1);log(ilsnyccy(lead+1:end,:)./ilsnyccy(1:end-lead,:) )]*100; %% change in pct.
dforward132=[repmat(NaN,lead,5);log(newforwards(lead+1:end,:)./newforwards(1:end-lead,:) )]*100; %% change in pct.

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

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

%% 264 day returns. 60 trading days.
lead=264;
dilsny264=[repmat(NaN,lead,1);log(ilsnyccy(lead+1:end,:)./ilsnyccy(1:end-lead,:) )]*100; %% change in pct.
dNEER264=NaN(T,1);
lead=263;
for i=lead+1:T
dNEER264(i,1)=sum(NEERny(i-lead:i),'omitnan');
end


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

laggedint=lagmatrix(intervention,0:300);
resusdils=NaN(200,2); %% coeff and t-stat

lead=1;
%% Constant and exogenous
T=length(intervention(idx:idx1));
z=[ones(T,1),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),laggedint(idx:idx1,2)/1000,intweeklag(idx:idx1,2),lagNEER2(idx:idx1,2),lagNEER10(idx:idx1,3)];
P_z=z/(z'*z)*z'; %% projection matrix

x=[ones(T,1),lagint(idx:idx1,1)/1000,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)];
xtag=P_z*x;

for i=1:26
   ret=100*(ilsnyccy(lead+1:end,:)./ilsnyccy(1:end-lead,:)-1);
 %   ret=100*log(ilsnyccy(lead+1:end,:)./ilsnyccy(1:end-lead,:));
    ret=[NaN(lead,1);ret];
    xtaglag=lagmatrix(xtag,lead-1);
    xlag=lagmatrix(x,lead-1);
%    xtaglag=lagmatrix(x,lead-1);
if i>1
    vixcontrol=[repmat(NaN,lead,1);vixfull(lead+1:end)-vixfull(1:end-lead)];
 %   eurusdcontrol=[]
  %  [coeff,tstat,r,n]=fastnw(ret(idx:idx1),[xtaglag,vixcontrol(idx:idx1),lageurusd(idx:idx1,1),lagNEER1(idx:idx1,1)],0,lead);
     [coeff,tstat,r,n]=fastnw(ret(idx:idx1),[xtaglag],0,lead);
else
    [coeff,tstat,r,n]=fastnw(ret(idx:idx1),[xtaglag],0,lead);
end
    resusdils(i,1)=coeff(2);
    resusdils(i,2)=tstat(2);
    lead=lead+1;
end

stderror=resusdils(:,1)./resusdils(:,2);
lowbound=resusdils(:,1)+1.65*stderror;
upperbound=resusdils(:,1)-1.65*stderror;
inbetween=[lowbound,upperbound];

% Define common x vector for fill
x = 0:25;

% Define color for the confidence band
band_color = [0.6 0.6 1];

% Fill the area between the bounds with the specified band_color
fill([x, fliplr(x)], [lowbound(1:26)', fliplr(upperbound(1:26)')], band_color, 'LineStyle', 'none');
hold on;

% Plot the main curve
plot(x, resusdils(1:26,1), 'b', 'LineWidth', 2); 

% Add the y-line at 0
yline(0, 'k', 'LineWidth', 2);

% Setting the labels, title, and ticks
ylabel('%');
xlabel('Trading days');
yticks([-20 -10 -5 0 5 10 20]);
ylim([-20 20]);
xticks(0:5:25);  % Adjust this as needed, based on your desired x-axis tick spacing

% Add the legend
lgd=legend('90\% CI', 'Cumulative response of USD/ILS', 'Box', 'off', 'Location', 'southoutside', 'interpreter', 'latex');

%lgd.Layout.Tile = 6;

% Set font size
set(gca, 'FontSize', 23);

% Save the current figure
fig = gcf;  % Get current figure

output_dir = '..\_results\Appendix G';
if ~exist(output_dir, 'dir')
    mkdir(output_dir);
end

% Save as PDF
exportgraphics(fig, fullfile(output_dir, 'USDLP.pdf'), 'ContentType', 'vector', 'Resolution', 300);
fprintf('Figure saved to: %s\n', fullfile(output_dir, 'USDLP.pdf'));

T=40;

laggedint=lagmatrix(intervention,0:300);
resNEER=NaN(200,2); %% coeff and t-stat
%% Transform NEER into an index
NEERindex=100;
for i=2:length(NEERny)
NEERindex(i)=NEERindex(i-1).*exp(NEERny(i)/100);
end
NEERindex=NEERindex';

lead=1;
%% Constant and exogenous
T=length(intervention(idx:idx1));
z=[ones(T,1),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),laggedint(idx:idx1,2)/1000,intweeklag(idx:idx1,2),lagNEER2(idx:idx1,2),lagNEER10(idx:idx1,3),lagusdils60(idx:idx1,2),lagcds(idx:idx1,2),lagvix(idx:idx1,2)-lagvix(idx:idx1,12)];
P_z=z/(z'*z)*z'; %% projection matrix

x=[ones(T,1),lagint(idx:idx1,1)/1000,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)];
xtag=P_z*x;
for i=1:26
    ret=100*(NEERindex(lead+1:end,:)./NEERindex(1:end-lead,:)-1);
    ret=[NaN(lead,1);ret];
    xtaglag=lagmatrix(xtag,lead-1);
   % xtaglag=lagmatrix(x,lead-1);
    [coeff,tstat,r,n]=fastnw(ret(idx:idx1),xtaglag,0,lead-1);
  %  results=nwest(ret(idx:idx1),xtaglag,0) 
    resNEER(i,1)=coeff(2);
    resNEER(i,2)=tstat(2);
    lead=lead+1;
    tsls_beta=(xtag'*xtag)\(xtag'*y);
end

stderror=resNEER(:,1)./resNEER(:,2);
lowbound=resNEER(:,1)+1.65*stderror;
upperbound=resNEER(:,1)-1.65*stderror;
inbetween=[lowbound,upperbound];


x = 0:25;

% Define color for the confidence band
band_color = [0.6 0.6 1];

% Fill the area between the bounds with the specified band_color
fill([x, fliplr(x)], [lowbound(1:26)', fliplr(upperbound(1:26)')], band_color, 'LineStyle', 'none');
hold on;

% Plot the main curve
plot(x, resusdils(1:26,1), 'b', 'LineWidth', 2); 

% Add the y-line at 0
yline(0, 'k', 'LineWidth', 2);

% Setting the labels, title, and ticks
ylabel('%');
xlabel('Trading days');
yticks([-20 -10 -5 0 5 10 20]);
ylim([-20 20]);
xticks(0:5:25);  % Adjust this as needed, based on your desired x-axis tick spacing

% Add the legend
lgd=legend('90\% CI', 'Cumulative response of NEER', 'Box', 'off', 'Location', 'southoutside', 'interpreter', 'latex');

%lgd.Layout.Tile = 6;

% Set font size
set(gca, 'FontSize', 23);

% Save the current figure
fig = gcf;  % Get current figure

output_dir = '..\_results\Appendix G';
if ~exist(output_dir, 'dir')
    mkdir(output_dir);
end

% Save as PDF
exportgraphics(fig, fullfile(output_dir, 'NEERDP.pdf'), 'ContentType', 'vector', 'Resolution', 300);
fprintf('Figure saved to: %s\n', fullfile(output_dir, 'NEERLP.pdf'));


%%% BASIS
lead=1;

%% Constant and exogenous
T=length(intervention(idx:idx1));
z=[ones(T,1),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),laggedint(idx:idx1,2)/1000,intweeklag(idx:idx1,2),lagNEER2(idx:idx1,2),lagNEER10(idx:idx1,3)];
P_z=z/(z'*z)*z'; %% projection matrix

x=[ones(T,1),lagint(idx:idx1,1)/1000,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)];
xtag=P_z*x;
load newbasis;
dnewbasis3m=[NaN;newbasis(2:end,2)-newbasis(1:end-1,2)];
dnewbasis3m=fillmissing(dnewbasis3m,'previous')*100;

for i=1:101
    ret=lagmatrix(dnewbasis3m,lead-1);
    xtaglag=lagmatrix(xtag,lead-1);

    [coeff,tstat,r,n]=fastnw(ret(idx:idx1),xtaglag,0,lead);
    resbasis(i,1)=coeff(2);
    resbasis(i,2)=tstat(2);
    lead=lead+1;
end

stderror=resbasis(:,1)./resbasis(:,2);
lowbound=resbasis(:,1)+1.96*stderror;
upperbound=resbasis(:,1)-1.96*stderror;


x = 0:100;

% Define color for the confidence band
band_color = [0.6 0.6 1];

% Fill the area between the bounds with the specified band_color
fill([x, fliplr(x)], [lowbound(1:101)', fliplr(upperbound(1:101)')], band_color, 'LineStyle', 'none');
hold on;

% Plot the main curve
plot(x, resbasis(1:101,1), 'b', 'LineWidth', 2); 

% Add the y-line at 0
yline(0, 'k', 'LineWidth', 2);

% Setting the labels, title, and ticks
ylabel('%');
xlabel('Trading days');

% Add the legend
lgd=legend('Response of three-month basis','95% CI','Location','southoutside');
ylabel('Basis points');

%lgd.Layout.Tile = 6;

% Set font size
set(gca, 'FontSize', 23);

% Save the current figure
fig = gcf;  % Get current figure

output_dir = '..\_results\Appendix G';
if ~exist(output_dir, 'dir')
    mkdir(output_dir);
end

% Save as PDF
exportgraphics(fig, fullfile(output_dir, 'BASISLP.pdf'), 'ContentType', 'vector', 'Resolution', 300);
fprintf('Figure saved to: %s\n', fullfile(output_dir, 'BASISLP.pdf'));

