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

Mt1=zscore([F1 F2 F3 F4 F5 F6 F7]); %standardized

size_tech=size(Mt1,2);

pi_tau=zeros(2,size_tech);
aligned_Mt=zeros(2,DataSize); % aligned_Mt(2,:)' is the aligned Mt
for i=1:size_tech
    A1=[ones(DataSize,1) Rt(1:end-1)];
    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_Mt_test=zscore(Mt1*Mt1'*Rt(1:end-1)*(Rt(1:end-1)'*Mt1*Mt1'*Mt1*Mt1'*Rt(1:end-1))^(-1)*Rt(1:end-1)'*Mt1*Mt1'*Rt(1:end-1));
std1=std(Mt1*Mt1'*Rt(1:end-1)*(Rt(1:end-1)'*Mt1*Mt1'*Mt1*Mt1'*Rt(1:end-1))^(-1)*Rt(1:end-1)'*Mt1*Mt1'*Rt(1:end-1));
Weights1=Mt1'*Rt(1:end-1)*(Rt(1:end-1)'*Mt1*Mt1'*Mt1*Mt1'*Rt(1:end-1))^(-1)*Rt(1:end-1)'*Mt1*Mt1'*Rt(1:end-1)/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,size_tech);
MtH_PCA=zeros(Number_High,111);
RtL=zeros(DataSize-Number_High,1);
MtL1=zeros(DataSize-Number_High,size_tech);
MtL_PCA=zeros(DataSize-Number_High,111);
NumberH=0;
NumberL=0;
for i=1:DataSize
    if STATES(i)==1
        NumberH=NumberH+1;
        RtH(NumberH)=Rt(i);
        MtH1(NumberH,:)=Mt1(i,:);
        MtH_PCA(NumberH,:)=F8(i,:);
    elseif STATES(i)==0
        NumberL=NumberL+1;
        RtL(NumberL)=Rt(i);
        MtL1(NumberL,:)=Mt1(i,:);
        MtL_PCA(NumberL,:)=F8(i,:);
    end
end
MtH1=zscore(MtH1); %standardized
MtH_PCA=zscore(MtH_PCA); %standardized
MtL1=zscore(MtL1); %standardized
MtL_PCA=zscore(MtL_PCA); %standardized    

aligned_Mt_H=zscore(MtH1*MtH1'*RtH*(RtH'*MtH1*MtH1'*MtH1*MtH1'*RtH)^(-1)*RtH'*MtH1*MtH1'*RtH);
std1=std(MtH1*MtH1'*RtH*(RtH'*MtH1*MtH1'*MtH1*MtH1'*RtH)^(-1)*RtH'*MtH1*MtH1'*RtH);
Weights1H=MtH1'*RtH*(RtH'*MtH1*MtH1'*MtH1*MtH1'*RtH)^(-1)*RtH'*MtH1*MtH1'*RtH/std1;


aligned_Mt_L=zscore(MtL1*MtL1'*RtL*(RtL'*MtL1*MtL1'*MtL1*MtL1'*RtL)^(-1)*RtL'*MtL1*MtL1'*RtL);
std1=std(MtL1*MtL1'*RtL*(RtL'*MtL1*MtL1'*MtL1*MtL1'*RtL)^(-1)*RtL'*MtL1*MtL1'*RtL);
Weights1L=MtL1'*RtL*(RtL'*MtL1*MtL1'*MtL1*MtL1'*RtL)^(-1)*RtL'*MtL1*MtL1'*RtL/std1;


aligned_Yt=aligned_Mt;
Yt1=Mt1;
aligned_Yt_H=aligned_Mt_H;
YtH1=MtH1;
aligned_Yt_L=aligned_Mt_L;
YtL1=MtL1;


save('ECON_variable.mat','aligned_Yt','Rt','Yt1','aligned_Yt_H','RtH','YtH1','aligned_Yt_L','RtL','YtL1','DataSize','Number_High','F8','MtH_PCA','MtL_PCA') 


