format long;
clear;
load SP500_sentment_new.mat
load Para_sentiment.mat
load PCA_ECON.mat
load NONFUND_variable_new.mat


%% aligned Mt

No_73_75_period=[1:90 127:546]; % data less the period 01/1973-12/1975
Mt1=zscore(Mt1(No_73_75_period,:)); %standardized

DeMeanPr=([F1 F2 F3 F4 F5 F6 F7]); %standardized
DeMeanPr1=zscore(DeMeanPr(No_73_75_period,:)); %standardized
Rt=Rt(No_73_75_period);
DataSize=DataSize-36;

pi_tau=zeros(2,6);
aligned_Mt=zeros(2,DataSize); % aligned_Mt(2,:)' is the aligned Mt
for i=1:6
    A1=[ones(DataSize,1) Rt];
    pi_tau(:,i)=lscov(A1,Mt1(:,i));
end

for j=1:DataSize
    A2=[pi_tau(2,:)'];
    aligned_Mt(:,j)=lscov(A2,Mt1(j,:)');
end

aligned_Mt=zscore(aligned_Mt(2,:)');

%% aligned Y_t 
aligned_Yt=zscore(DeMeanPr1*DeMeanPr1'*Rt*(Rt'*DeMeanPr1*DeMeanPr1'*DeMeanPr1*DeMeanPr1'*Rt)^(-1)*Rt'*DeMeanPr1*DeMeanPr1'*Rt);
std1=std(DeMeanPr1*DeMeanPr1'*Rt*(Rt'*DeMeanPr1*DeMeanPr1'*DeMeanPr1*DeMeanPr1'*Rt)^(-1)*Rt'*DeMeanPr1*DeMeanPr1'*Rt);
Weights2=DeMeanPr1'*Rt*(Rt'*DeMeanPr1*DeMeanPr1'*DeMeanPr1*DeMeanPr1'*Rt)^(-1)*Rt'*DeMeanPr1*DeMeanPr1'*Rt/std1;

%% 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
    Number_High=high_m;
elseif Switch==1
    STATES=(RegimeSYY+9)/12; %STATES=1 if at state 1 (high);  STATES=0 if at state 2 (low)
    Number_High=round(DataSize/2);
end
RtH=zeros(Number_High,1);
MtH1=zeros(Number_High,1);
YtH1=zeros(Number_High,1);
RtL=zeros(DataSize-Number_High,1);
MtL1=zeros(DataSize-Number_High,1);
YtL1=zeros(DataSize-Number_High,1);
NumberH=0;
NumberL=0;
STATES=STATES(No_73_75_period);
for i=1:DataSize
    if STATES(i)==1
        NumberH=NumberH+1;
        RtH(NumberH)=Rt(i);
        MtH1(NumberH,:)=aligned_Mt(i,:);
        YtH1(NumberH,:)=aligned_Yt(i,:);
    elseif STATES(i)==0
        NumberL=NumberL+1;
        RtL(NumberL)=Rt(i);
        MtL1(NumberL,:)=aligned_Mt(i,:);
        YtL1(NumberL,:)=aligned_Yt(i,:);
    end
end
aligned_Mt_H=zscore(MtH1); %standardized
aligned_Yt_H=zscore(YtH1); %standardized
aligned_Mt_L=zscore(MtL1); %standardized
aligned_Yt_L=zscore(YtL1); %standardized      

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(DataSize-Number_High,1) aligned_Mt_L];
X3=lscov(A7,aligned_Yt_L);
MtOrthoAligned3L=aligned_Yt_L-X3(1)-X3(2)*aligned_Mt_L;


save('PLS_NO_73_75_period.mat','aligned_Mt','aligned_Yt','Rt','aligned_Mt_H','aligned_Yt_H','RtH','aligned_Mt_L','aligned_Yt_L','RtL','DataSize','Number_High') 


        
