format long;
clear;

load Para_sentiment.mat
load ECON_variable_a.mat 
load NONFUND_variable_new.mat 

%% collect data in expansion
load Recession_Expansion.mat

RtEx=zeros(NoExpansion,1);
MtEx=zeros(NoExpansion,1);
muEx1=zeros(NoExpansion,1);
NoEx=0;
for i=1:DataSize
    if REt(i)==0
        NoEx=NoEx+1;
        RtEx(NoEx)=Rt(i);
        MtEx(NoEx,:)=aligned_Mt(i,:);
        muEx1(NoEx,:)=aligned_Yt(i,:);
    end
end


aligned_Mt=zscore(MtEx);
aligned_Yt=zscore(muEx1);


%% orthogonal aligned Mt to aligned Y_t
A5=[ones(NoExpansion,1) aligned_Mt];
X2=lscov(A5,aligned_Yt);
MtOrthoAligned3=aligned_Yt-X2(1)-X2(2)*aligned_Mt;

%% regimes H and L

Switch=0; %0: Markov regime switch model; 1: Stambaugh_Yu_Yuan_JFE_12
if Switch==0
    STATES=(sign(pi1(2:end)-0.5)+1)/2; %STATES=1 if at state 1;  STATES=0 if at state 2
elseif Switch==1
    STATES=(RegimeSYY+9)/12; %STATES=1 if at state 1 (high);  STATES=0 if at state 2 (low)
end

Number_High=0;
Number_Low=0;
for i=1:DataSize
    if STATES(i)==1 && REt(i)==0
        Number_High=Number_High+1;
    elseif STATES(i)==0 && REt(i)==0
        Number_Low=Number_Low+1;
    end
end

RtH=zeros(Number_High,1);
RtL=zeros(Number_Low,1);

aligned_Mt_H=zeros(Number_High,1);
aligned_Yt_H=zeros(Number_High,1);
aligned_Mt_L=zeros(Number_Low,1);
aligned_Yt_L=zeros(Number_Low,1);

NumberH=0;
NumberL=0;

No_EXP=0;
for i=1:DataSize
    if REt(i)==0
        No_EXP=No_EXP+1;
        if STATES(i)==1 %&& REt(i)==0
            NumberH=NumberH+1;
            RtH(NumberH)=Rt(i);
            aligned_Mt_H(NumberH)=aligned_Mt(No_EXP);
            aligned_Yt_H(NumberH)=aligned_Yt(No_EXP);
        elseif STATES(i)==0 %&& REt(i)==0
            NumberL=NumberL+1;
            RtL(NumberL)=Rt(i);
            aligned_Mt_L(NumberL)=aligned_Mt(No_EXP);
            aligned_Yt_L(NumberL)=aligned_Yt(No_EXP);
        end
    end
end

aligned_Mt_H=zscore(aligned_Mt_H);
aligned_Yt_H=zscore(aligned_Yt_H);
aligned_Mt_L=zscore(aligned_Mt_L);
aligned_Yt_L=zscore(aligned_Yt_L);

A6=[ones(Number_High,1) aligned_Mt_H];
X3=lscov(A6,aligned_Yt_H);
MtOrthoAligned3H=aligned_Yt_H-X3(1)-X3(2)*aligned_Mt_H;

A7=[ones(Number_Low,1) aligned_Mt_L];
X3=lscov(A7,aligned_Yt_L);
MtOrthoAligned3L=aligned_Yt_L-X3(1)-X3(2)*aligned_Mt_L;


save('PLS_ECON_NONFUND_EXP.mat','aligned_Mt','MtOrthoAligned3','aligned_Yt','RtEx','aligned_Mt_H','MtOrthoAligned3H','aligned_Yt_H','RtH','aligned_Mt_L','MtOrthoAligned3L','aligned_Yt_L','RtL','NoExpansion','Number_High','Number_Low') 


        
