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;

% %% aligned Y_t using the 4 Ng variables
% aligned_Yt=zscore(DeMeanPr1*DeMeanPr1'*Rt(1:end-1)*(Rt(1:end-1)'*DeMeanPr1*DeMeanPr1'*DeMeanPr1*DeMeanPr1'*Rt(1:end-1))^(-1)*Rt(1:end-1)'*DeMeanPr1*DeMeanPr1'*Rt(1:end-1));
% std1=std(DeMeanPr1*DeMeanPr1'*Rt(1:end-1)*(Rt(1:end-1)'*DeMeanPr1*DeMeanPr1'*DeMeanPr1*DeMeanPr1'*Rt(1:end-1))^(-1)*Rt(1:end-1)'*DeMeanPr1*DeMeanPr1'*Rt(1:end-1));
% Weights2=DeMeanPr1'*Rt(1:end-1)*(Rt(1:end-1)'*DeMeanPr1*DeMeanPr1'*DeMeanPr1*DeMeanPr1'*Rt(1:end-1))^(-1)*Rt(1:end-1)'*DeMeanPr1*DeMeanPr1'*Rt(1:end-1)/std1;
% 
% %% orthogonal aligned Mt to aligned Y_t
% A5=[ones(DataSize,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
    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);
%MtH_PCA=zeros(Number_High,111);
%YtH1=zeros(Number_High,4);
RtL=zeros(DataSize-Number_High,1);
MtL1=zeros(DataSize-Number_High,1);
%MtL_PCA=zeros(DataSize-Number_High,111);
%YtL1=zeros(DataSize-Number_High,4);
NumberH=0;
NumberL=0;
for i=1:DataSize
    if STATES(i)==1
        NumberH=NumberH+1;
        RtH(NumberH)=Rt(i);
        MtH1(NumberH)=aligned_Mt(i);
        %MtH_PCA(NumberH,:)=F8(i,:);
        %YtH1(NumberH,:)=DeMeanPr(i,:);
    elseif STATES(i)==0
        NumberL=NumberL+1;
        RtL(NumberL)=Rt(i);
        MtL1(NumberL)=aligned_Mt(i);
        %MtL_PCA(NumberL,:)=F8(i,:);
        %YtL1(NumberL,:)=DeMeanPr(i,:);
    end
end


aligned_Yt=aligned_Mt;
aligned_Yt_H=zscore(MtH1); %standardized
aligned_Yt_L=zscore(MtL1); %standardized


save('ECON_variable_a.mat','aligned_Yt','Rt','aligned_Yt_H','RtH','aligned_Yt_L','RtL','DataSize','Number_High') 


