clear all
clc
rng(1)
filename = 'Data_NS18_DYRF.xlsx';
Data_table = readtable(filename);
Data=table2array(Data_table);
Policy=Data(:,2);
FOMC_dates=Data(:,3);
FOMC=find(FOMC_dates);
No_FOMC=find(FOMC_dates==0);
D_bar_C=mean(Policy(No_FOMC));
Sigma2_hat_D_C=var(Policy(No_FOMC));
Sigma2_hat_D_P=var(Policy(FOMC));
ratio_Sigma_D_fullsample=Sigma2_hat_D_P/Sigma2_hat_D_C;

Real_Forward_5Y=Data(:,1);
Policy_1day=Data(:,5);

T_P=length(Real_Forward_5Y(FOMC));
T_C=length(Real_Forward_5Y(No_FOMC));
Sigma2_hat_Y_C=var(Real_Forward_5Y(No_FOMC));
Sigma2_hat_Y_P=var(Real_Forward_5Y(FOMC));

%% Check Var(D) in sub-samples
bandwith=[4/5];
ratio_Sigma2_D=NaN(10,length(bandwith));
Real_Forward_5Y_P=Real_Forward_5Y(FOMC);
Policy_P=Policy(FOMC);
beta_hat = OLS(Real_Forward_5Y_P, Policy_P,1);
beta_ES_hat_NS=beta_hat(2);

for i_band=1:length(bandwith)
n_T_P=floor(T_P^bandwith(i_band)); %4/5
n_block_P=floor(T_P/n_T_P);
Sigma2_D_P_block(1,1)=var(Policy_P(1:n_T_P));
for i_block=2:n_block_P
    Sigma2_D_P_block(i_block,1)= var(Policy_P( (i_block-1)*n_T_P+1:i_block*n_T_P ));
end
Sigma2_D_P_block(n_block_P+1,1)= var(Policy_P( (n_block_P)*n_T_P+1:T_P));  

% Eliminate the last 10 FOMC days in order to balance the sample
Policy_P=Policy_P(1:end-10);
Real_Forward_5Y_P=Real_Forward_5Y_P(1:end-10);
T_P=T_P-10;
Policy_C=Policy(No_FOMC);
n_T_P=floor(T_P^bandwith(i_band));
n_block_P=floor(T_P/n_T_P);
n_block_C=n_block_P;
n_T_C=floor(T_C/ n_block_C);

Sigma2_D_P_block(1,1)=var(Policy_P(1:n_T_P));
Sigma2_D_C_block(1,1)=var(Policy_C(1:n_T_C));
for i_block=2:n_block_P
    Sigma2_D_P_block(i_block,1)= var(Policy_P( (i_block-1)*n_T_P+1:i_block*n_T_P ));
end
Sigma2_D_P_block(n_block_P+1,1)= var(Policy_P( (n_block_P)*n_T_P+1:T_P));  
Sigma2_D_P_block=Sigma2_D_P_block;
for i_block=2:n_block_C
    Sigma2_D_C_block(i_block,1)= var(Policy_C( (i_block-1)*n_T_C+1:i_block*n_T_C ));
end
Sigma2_D_C_block(n_block_C+1,1)= var(Policy_C( (n_block_C)*n_T_C+1:T_C));  
Sigma2_D_C_block=Sigma2_D_C_block;

beta_hat = OLS(Real_Forward_5Y_P, Policy_P,1);
beta_ES_hat_sub=beta_hat(2);

ratio_Sigma2_D(1:length(Sigma2_D_P_block),i_band)=Sigma2_D_P_block./Sigma2_D_C_block;
end

%% Leakage 
FOMC_leak=find(FOMC_dates(1:end-10));
FOMC_leak_lag1=FOMC_leak-1;
FOMC_leak_lag2=FOMC_leak-2;
FOMC_leak_lag3=FOMC_leak-3;
FOMC_leak_lag4=FOMC_leak-4;
FOMC_leak_lag5=FOMC_leak-5;
FOMC_leak_lag6=FOMC_leak-6;

Sigma2_hat_D_P_lag1=var([Policy(FOMC_leak_lag1); Policy(FOMC_leak_lag2)]);
Sigma2_hat_D_P_lag2=var([Policy(FOMC_leak_lag3); Policy(FOMC_leak_lag4)]);
Sigma2_hat_D_P_lag3=var([Policy(FOMC_leak_lag5); Policy(FOMC_leak_lag6)]);

Policy_t=Policy(2:end-10);
Policy_lag=Policy(1:end-11);
autocorr(Policy_t);
auto_corr_1=corr(Policy_t, Policy_lag);
corr_leak_lag1=corr(Policy(FOMC_leak), Policy(FOMC_leak_lag1));
corr_leak_lag2=corr(Policy(FOMC_leak_lag1), Policy(FOMC_leak_lag2));
corr_leak_lag3=corr(Policy(FOMC_leak_lag2), Policy(FOMC_leak_lag3));
corr_leak_lag4=corr(Policy(FOMC_leak_lag3), Policy(FOMC_leak_lag4));
corr_leak_lag5=corr(Policy(FOMC_leak_lag4), Policy(FOMC_leak_lag5));
corr_leak_lag6=corr(Policy(FOMC_leak_lag5), Policy(FOMC_leak_lag6));

OLS(Policy(FOMC_leak), Policy(FOMC_leak_lag1),1)
OLS(Policy(FOMC_leak_lag1), Policy(FOMC_leak_lag2),1)
OLS(Policy(FOMC_leak_lag2), Policy(FOMC_leak_lag3),1)
OLS(Policy(FOMC_leak_lag3), Policy(FOMC_leak_lag4),1)
OLS(Policy(FOMC_leak_lag4), Policy(FOMC_leak_lag5),1)
OLS(Policy(FOMC_leak_lag5), Policy(FOMC_leak_lag6),1)



% Policy news shock, 1-Day window
Sigma2_hat_D_C_1day=var(Policy_1day(No_FOMC));
Sigma2_hat_D_P_1day=var(Policy_1day(FOMC));

Sigma2_hat_D_P_lag1_1day=var([Policy_1day(FOMC_leak_lag1); Policy_1day(FOMC_leak_lag2)]);
Sigma2_hat_D_P_lag2_1day=var([Policy_1day(FOMC_leak_lag3); Policy_1day(FOMC_leak_lag4)]);
Sigma2_hat_D_P_lag3_1day=var([Policy_1day(FOMC_leak_lag5); Policy_1day(FOMC_leak_lag6)]);

Policy_t_1day=Policy_1day(2:end-10);
Policy_lag_1day=Policy_1day(1:end-11);
autocorr(Policy_t_1day);
auto_corr_1=corr(Policy_t_1day, Policy_lag_1day);
corr_leak_lag1_1day=corr(Policy_1day(FOMC_leak), Policy_1day(FOMC_leak_lag1));
corr_leak_lag2_1day=corr(Policy_1day(FOMC_leak_lag1), Policy_1day(FOMC_leak_lag2));
corr_leak_lag3_1day=corr(Policy_1day(FOMC_leak_lag2), Policy_1day(FOMC_leak_lag3));
corr_leak_lag4_1day=corr(Policy_1day(FOMC_leak_lag3), Policy_1day(FOMC_leak_lag4));
corr_leak_lag5_1day=corr(Policy_1day(FOMC_leak_lag4), Policy_1day(FOMC_leak_lag5));
corr_leak_lag6_1day=corr(Policy_1day(FOMC_leak_lag5), Policy_1day(FOMC_leak_lag6));

OLS(Policy_1day(FOMC_leak), Policy_1day(FOMC_leak_lag1),1)
OLS(Policy_1day(FOMC_leak_lag1), Policy_1day(FOMC_leak_lag2),1)
OLS(Policy_1day(FOMC_leak_lag2), Policy_1day(FOMC_leak_lag3),1)
OLS(Policy_1day(FOMC_leak_lag3), Policy_1day(FOMC_leak_lag4),1)
OLS(Policy_1day(FOMC_leak_lag4), Policy_1day(FOMC_leak_lag5),1)
OLS(Policy_1day(FOMC_leak_lag5), Policy_1day(FOMC_leak_lag6),1)












