clear;


    load ECON_variable_a.mat 
    load NONFUND_variable_new.mat 
    

    Mt=aligned_Mt;
    mu_t=aligned_Yt; % aligned_Yt(2,:)' is the aligned Yt, see PLS.m
    MtH=aligned_Mt_H;
    muH=aligned_Yt_H;
    MtL=aligned_Mt_L;
    muL=aligned_Yt_L;
    
A1=[ones(DataSize,1) mu_t(1:DataSize)];
[X1,STDX1]=lscov(A1,Rt(1:DataSize));
R_squared1=1-sum((Rt(1:DataSize)-X1(1)-X1(2)*mu_t(1:DataSize)).^2)/sum((Rt(1:DataSize)-mean(Rt(1:DataSize))).^2);
nlag=12; 
RegressionCoef1=nwest(Rt(1:DataSize),A1,nlag);
Newey_West_t=RegressionCoef1.tstat;


    NumberH=Number_High;
    A2=[ones(NumberH,1) muH];
    [X2,STDX2]=lscov(A2,RtH);
    R_squared2=1-sum((RtH-X2(1)-X2(2)*muH).^2)/sum((RtH-mean(RtH)).^2);
    RegressionCoef2=nwest(RtH,A2,nlag);
    Newey_West_tH=RegressionCoef2.tstat;
    
    NumberL=DataSize-Number_High;
    A3=[ones(NumberL,1) muL];
    [X3,STDX3]=lscov(A3,RtL);
    R_squared3=1-sum((RtL-X3(1)-X3(2)*muL).^2)/sum((RtL-mean(RtL)).^2);
    RegressionCoef3=nwest(RtL,A3,nlag);
    Newey_West_tL=RegressionCoef3.tstat;  

X1=X1';
X2=X2';
X3=X3';
TX1=Newey_West_t';%TX1';
TX2=Newey_West_tH';%TX2';
TX3=Newey_West_tL';%TX3';

subplot(4,2,1)
corrmu_t=corr(Rt(1:end-1,1),mu_t);
corrmuH=corr(RtH,muH);
corrmuL=corr(RtL,muL);
h=bar([corrmu_t corrmuH corrmuL corrmuH-corrmuL]);
set(h(1,1),'FaceColor',[1 1 1] * 0.8);
set(gca,'XTickLabel',{'whole','high','low','diff'})
title('Panel A. ECON Ret corr')
ylim([-0.2 0.3])
hold on;
subplot(4,2,3)
betamu_t=100*X1(1,2);
betamuH=100*X2(1,2);
betamuL=100*X3(1,2);
h=bar([betamu_t betamuH betamuL betamuH-betamuL]);
set(h(1,1),'FaceColor',[1 1 1] * 0.8);
set(gca,'XTickLabel',{'whole','high','low','diff'})
title('Panel C. ECON beta')
ylim([-0.5 1])
hold on;
subplot(4,2,5)
tstatmu_t=TX1(1,2);
tstatmuH=TX2(1,2);
tstatmuL=TX3(1,2);
h=bar([tstatmu_t tstatmuH tstatmuL tstatmuH-tstatmuL]);
set(h(1,1),'FaceColor',[1 1 1] * 0.8);
set(gca,'XTickLabel',{'whole','high','low','diff'})
title('Panel E. ECON t-stat')
ylim([-3 5])
hold on;
subplot(4,2,7)
R2mu_t=100*R_squared1;
R2muH=100*R_squared2;
R2muL=100*R_squared3;
h=bar([R2mu_t R2muH R2muL R2muH-R2muL]);
set(h(1,1),'FaceColor',[1 1 1] * 0.8);
set(gca,'XTickLabel',{'whole','high','low','diff'})
title('Panel G. ECON R-square')
ylim([-3 5])
hold on;



A1=[ones(DataSize,1) Mt(1:DataSize)];
[X1,STDX1]=lscov(A1,Rt(1:DataSize));
R_squared1=1-sum((Rt(1:DataSize)-X1(1)-X1(2)*Mt(1:DataSize)).^2)/sum((Rt(1:DataSize)-mean(Rt(1:DataSize))).^2);
R_squared_adjusted1=1-(DataSize-1)/(DataSize-2)*sum((Rt(1:DataSize)-X1(1)-X1(2)*Mt(1:DataSize)).^2)/sum((Rt(1:DataSize)-mean(Rt(1:DataSize))).^2);
nlag=12; 
RegressionCoef1=nwest(Rt(1:DataSize),A1,nlag);
Newey_West_t=RegressionCoef1.tstat;


    NumberH=Number_High;
    A2=[ones(NumberH,1) MtH];
    [X2,STDX2]=lscov(A2,RtH); 
    R_squared2=1-sum((RtH-X2(1)-X2(2)*MtH).^2)/sum((RtH-mean(RtH)).^2);
    R_squared_adjusted2=1-(NumberH-1)/(NumberH-2)*sum((RtH-X2(1)-X2(2)*MtH).^2)/sum((RtH-mean(RtH)).^2);
    RegressionCoef2=nwest(RtH,A2,nlag);
    Newey_West_tH=RegressionCoef2.tstat;
    
    NumberL=DataSize-Number_High;
    A3=[ones(NumberL,1) MtL];
    [X3,STDX3]=lscov(A3,RtL);
    R_squared3=1-sum((RtL-X3(1)-X3(2)*MtL).^2)/sum((RtL-mean(RtL)).^2);
    R_squared_adjusted3=1-(NumberL-1)/(NumberL-2)*sum((RtL-X3(1)-X3(2)*MtL).^2)/sum((RtL-mean(RtL)).^2);
    RegressionCoef3=nwest(RtL,A3,nlag);
    Newey_West_tL=RegressionCoef3.tstat;  

X1=X1';
X2=X2';
X3=X3';
TX1=Newey_West_t';%TX1';
TX2=Newey_West_tH';%TX2';
TX3=Newey_West_tL';%TX3';

subplot(4,2,2)
corrMt=corr(Rt(1:end-1,1),Mt);
corrMtH=corr(RtH,MtH);
corrMtL=corr(RtL,MtL);
h=bar([corrMt corrMtH corrMtL corrMtH-corrMtL]);
set(h(1,1),'FaceColor',[1 1 1] * 0.8);
set(gca,'XTickLabel',{'whole','high','low','diff'})
title('Panel B. NONFUND Ret corr')
ylim([-0.2 0.3])
hold on;
subplot(4,2,4)
betaMt=100*X1(1,2);
betaMtH=100*X2(1,2);
betaMtL=100*X3(1,2);
h=bar([betaMt betaMtH betaMtL betaMtH-betaMtL]);
set(h(1,1),'FaceColor',[1 1 1] * 0.8);
set(gca,'XTickLabel',{'whole','high','low','diff'})
title('Panel D. NONFUND beta')
ylim([-0.5 1])
hold on;
subplot(4,2,6)
tstatMt=TX1(1,2);
tstatMtH=TX2(1,2);
tstatMtL=TX3(1,2);
h=bar([tstatMt tstatMtH tstatMtL tstatMtH-tstatMtL]);
set(h(1,1),'FaceColor',[1 1 1] * 0.8);
set(gca,'XTickLabel',{'whole','high','low','diff'})
title('Panel F. NONFUND t-stat')
ylim([-3 5])
hold on;
subplot(4,2,8)
R2Mt=100*R_squared1;
R2MtH=100*R_squared2;
R2MtL=100*R_squared3;
h=bar([R2Mt R2MtH R2MtL R2MtH-R2MtL]);
set(h(1,1),'FaceColor',[1 1 1] * 0.8);
set(gca,'XTickLabel',{'whole','high','low','diff'})
title('Panel H. NONFUND R-square')
ylim([-3 5])




