function [ R_squared_OS,p_value_diff] = dif_vari_dif_regime_A(PCres,TrainingSample,Mt2,y1,DataSize,Rt,Constraint_level)

Rt_hat=zeros(DataSize,1); % predicted return 
Rt_hatH=zeros(1,1); % predicted return 
Rt_hatL=zeros(1,1); % predicted return 
Rt_realH=zeros(1,1);
Rt_realL=zeros(1,1);
MeanRt_H=zeros(1,1);
MeanRt_L=zeros(1,1);
NoH=0;
NoL=0;
No1=[1 6:20]; % output and income
No2=[21:50 129:131]; % labor market
No3=[51:60]; % housing
No4=[3:5 61:70 132]; % consumption order and inventories
No5=[71:81]; % money and credit
No6=[103:107]; % exchange rates
No7=[108:128]; % prices
Weights_m=zeros(6,DataSize-TrainingSample+1);
WeightsH_m=zeros(6,DataSize-TrainingSample+1);
WeightsL_m=zeros(6,DataSize-TrainingSample+1);
Weights_mu=zeros(7,DataSize-TrainingSample+1);
WeightsH_mu=zeros(7,DataSize-TrainingSample+1);
WeightsL_mu=zeros(7,DataSize-TrainingSample+1);

for t=TrainingSample:DataSize
    
    Rtt=Rt(1:t-1); % that is, use information up to t
    Mtt=Mt2(1:t,:);
    DeMeanPrt=y1(1:t,:);
    %% regime
    %Xtt=Xt(1:t); % use information up to t using the BW index rather than real time index
    
    Xtt=PCres(1:t,t-TrainingSample+1);
    pi1=regime_Sentiment(Xtt,t);
    high_m=0;
    for i=2:t
        if pi1(i)>=0.5
            high_m=high_m+1;
        end
    end
    
    RtH=zeros(high_m,1);
    MtH1=zeros(high_m,6);
    YtH1=zeros(high_m,132);
    RtL=zeros(t-1-high_m,1);
    MtL1=zeros(t-1-high_m,6);
    YtL1=zeros(t-1-high_m,132);
    NumberH=0;
    NumberL=0;
    for i=1:t-1
        if pi1(i+1)>=0.5
            NumberH=NumberH+1;
            RtH(NumberH)=Rtt(i);
            MtH1(NumberH,:)=Mtt(i,:);
            YtH1(NumberH,:)=DeMeanPrt(i,:);
        elseif pi1(i+1)<0.5
            NumberL=NumberL+1;
            RtL(NumberL)=Rtt(i);
            MtL1(NumberL,:)=Mtt(i,:);
            YtL1(NumberL,:)=DeMeanPrt(i,:);
        end
    end
    %% PLS
%     Mt1=zscore(Mtt); % reduce variables to reduce error and improve OOS.
%     
%     std1=std(Mt1*Mt1(1:end-1,:)'*Rtt*(Rtt'*Mt1(1:end-1,:)*Mt1(1:end-1,:)'*Mt1(1:end-1,:)*Mt1(1:end-1,:)'*Rtt)^(-1)*Rtt'*Mt1(1:end-1,:)*Mt1(1:end-1,:)'*Rtt);
%     Weights_m(:,t-TrainingSample+1)=Mt1(1:end-1,:)'*Rtt*(Rtt'*Mt1(1:end-1,:)*Mt1(1:end-1,:)'*Mt1(1:end-1,:)*Mt1(1:end-1,:)'*Rtt)^(-1)*Rtt'*Mt1(1:end-1,:)*Mt1(1:end-1,:)'*Rtt/std1;
%     if t>TrainingSample
%         for i=1:6
%             if (Weights_m(i,t-TrainingSample+1)*mean(Weights_m(i,1:t-TrainingSample)))<0.05
%                 Weights_m(i,t-TrainingSample+1)=0;
%             end
%         end
%     end
%     aligned_Mtt=zscore(Mt1*Weights_m(:,t-TrainingSample+1));
    
    
    MtH1=zscore(MtH1); %standardized
   
    std1=std(MtH1*MtH1'*RtH*(RtH'*MtH1*MtH1'*MtH1*MtH1'*RtH)^(-1)*RtH'*MtH1*MtH1'*RtH);
    WeightsH_m(:,t-TrainingSample+1)=MtH1'*RtH*(RtH'*MtH1*MtH1'*MtH1*MtH1'*RtH)^(-1)*RtH'*MtH1*MtH1'*RtH/std1;
    if t>TrainingSample
        for i=1:6
            if (WeightsH_m(i,t-TrainingSample+1)*mean(WeightsH_m(i,1:t-TrainingSample)))<Constraint_level
                WeightsH_m(i,t-TrainingSample+1)=0;
            end
        end
    end
    aligned_MtH=zscore(MtH1*WeightsH_m(:,t-TrainingSample+1));
    

%     MtL1=zscore(MtL1); %standardized
% 
%     std1=std(MtL1*MtL1'*RtL*(RtL'*MtL1*MtL1'*MtL1*MtL1'*RtL)^(-1)*RtL'*MtL1*MtL1'*RtL);
%     WeightsL_m(:,t-TrainingSample+1)=MtL1'*RtL*(RtL'*MtL1*MtL1'*MtL1*MtL1'*RtL)^(-1)*RtL'*MtL1*MtL1'*RtL/std1;
%     if t>TrainingSample
%         for i=1:6
%             if (WeightsL_m(i,t-TrainingSample+1)*mean(WeightsL_m(i,1:t-TrainingSample)))<0.05
%                 WeightsL_m(i,t-TrainingSample+1)=0;
%             end
%         end
%     end
%     aligned_MtL=zscore(MtL1*WeightsL_m(:,t-TrainingSample+1));
    
    % Yt
%     DeMeanPr1=zscore(DeMeanPrt); %standardized
%     
%     %PCA
%     COEFF1 = pca((DeMeanPr1(:,No1)));
%     F1=zscore((DeMeanPr1(:,No1))*COEFF1(:,1));
%     COEFF2 = pca((DeMeanPr1(:,No2)));
%     F2=zscore((DeMeanPr1(:,No2))*COEFF2(:,1));
%     COEFF3 = pca((DeMeanPr1(:,No3)));
%     F3=zscore((DeMeanPr1(:,No3))*COEFF3(:,1));
%     COEFF4 = pca((DeMeanPr1(:,No4)));
%     F4=zscore((DeMeanPr1(:,No4))*COEFF4(:,1));
%     COEFF5 = pca((DeMeanPr1(:,No5)));
%     F5=zscore((DeMeanPr1(:,No5))*COEFF5(:,1));
%     COEFF6 = pca((DeMeanPr1(:,No6)));
%     F6=zscore((DeMeanPr1(:,No6))*COEFF6(:,1));
%     COEFF7 = pca((DeMeanPr1(:,No7)));
%     F7=zscore((DeMeanPr1(:,No7))*COEFF7(:,1));
%     DeMeanPr2=[F1 F2 F3 F4 F5 F6 F7];
%     
%     %PLS
%     std1=std(DeMeanPr2*DeMeanPr2(1:end-1,:)'*Rtt*(Rtt'*DeMeanPr2(1:end-1,:)*DeMeanPr2(1:end-1,:)'*DeMeanPr2(1:end-1,:)*DeMeanPr2(1:end-1,:)'*Rtt)^(-1)*Rtt'*DeMeanPr2(1:end-1,:)*DeMeanPr2(1:end-1,:)'*Rtt);
%     Weights_mu(:,t-TrainingSample+1)=DeMeanPr2(1:end-1,:)'*Rtt*(Rtt'*DeMeanPr2(1:end-1,:)*DeMeanPr2(1:end-1,:)'*DeMeanPr2(1:end-1,:)*DeMeanPr2(1:end-1,:)'*Rtt)^(-1)*Rtt'*DeMeanPr2(1:end-1,:)*DeMeanPr2(1:end-1,:)'*Rtt/std1;
%     if t>TrainingSample
%         for i=1:7
%             if (Weights_mu(i,t-TrainingSample+1)*mean(Weights_mu(i,1:t-TrainingSample)))<0.05
%                 Weights_mu(i,t-TrainingSample+1)=0;
%             end
%         end
%     end
%     aligned_Ytt=zscore(DeMeanPr2*Weights_mu(:,t-TrainingSample+1));
%% H
%     DeMeanPr1=zscore(YtH1); %standardized 
%     %PCA
%     COEFF1 = pca((DeMeanPr1(:,No1)));
%     F1=zscore((DeMeanPr1(:,No1))*COEFF1(:,1));
%     COEFF2 = pca((DeMeanPr1(:,No2)));
%     F2=zscore((DeMeanPr1(:,No2))*COEFF2(:,1));
%     COEFF3 = pca((DeMeanPr1(:,No3)));
%     F3=zscore((DeMeanPr1(:,No3))*COEFF3(:,1));
%     COEFF4 = pca((DeMeanPr1(:,No4)));
%     F4=zscore((DeMeanPr1(:,No4))*COEFF4(:,1));
%     COEFF5 = pca((DeMeanPr1(:,No5)));
%     F5=zscore((DeMeanPr1(:,No5))*COEFF5(:,1));
%     COEFF6 = pca((DeMeanPr1(:,No6)));
%     F6=zscore((DeMeanPr1(:,No6))*COEFF6(:,1));
%     COEFF7 = pca((DeMeanPr1(:,No7)));
%     F7=zscore((DeMeanPr1(:,No7))*COEFF7(:,1));
%     DeMeanPr2=[F1 F2 F3 F4 F5 F6 F7];
%     %PLS
%     std1=std(DeMeanPr2*DeMeanPr2'*RtH*(RtH'*DeMeanPr2*DeMeanPr2'*DeMeanPr2*DeMeanPr2'*RtH)^(-1)*RtH'*DeMeanPr2*DeMeanPr2'*RtH);
%     WeightsH_mu(:,t-TrainingSample+1)=DeMeanPr2'*RtH*(RtH'*DeMeanPr2*DeMeanPr2'*DeMeanPr2*DeMeanPr2'*RtH)^(-1)*RtH'*DeMeanPr2*DeMeanPr2'*RtH/std1;
%     if t>TrainingSample
%         for i=1:7
%             if (WeightsH_mu(i,t-TrainingSample+1)*mean(WeightsH_mu(i,1:t-TrainingSample)))<0.05
%                 WeightsH_mu(i,t-TrainingSample+1)=0;
%             end
%         end
%     end
%     aligned_YtH=zscore(DeMeanPr2*WeightsH_mu(:,t-TrainingSample+1));
%% L
    DeMeanPr1=zscore(YtL1); %standardized
    %PCA
    COEFF1 = pca((DeMeanPr1(:,No1)));
    F1=zscore((DeMeanPr1(:,No1))*COEFF1(:,1));
    COEFF2 = pca((DeMeanPr1(:,No2)));
    F2=zscore((DeMeanPr1(:,No2))*COEFF2(:,1));
    COEFF3 = pca((DeMeanPr1(:,No3)));
    F3=zscore((DeMeanPr1(:,No3))*COEFF3(:,1));
    COEFF4 = pca((DeMeanPr1(:,No4)));
    F4=zscore((DeMeanPr1(:,No4))*COEFF4(:,1));
    COEFF5 = pca((DeMeanPr1(:,No5)));
    F5=zscore((DeMeanPr1(:,No5))*COEFF5(:,1));
    COEFF6 = pca((DeMeanPr1(:,No6)));
    F6=zscore((DeMeanPr1(:,No6))*COEFF6(:,1));
    COEFF7 = pca((DeMeanPr1(:,No7)));
    F7=zscore((DeMeanPr1(:,No7))*COEFF7(:,1));
    DeMeanPr2=[F1 F2 F3 F4 F5 F6 F7];
    %PLS
    std1=std(DeMeanPr2*DeMeanPr2'*RtL*(RtL'*DeMeanPr2*DeMeanPr2'*DeMeanPr2*DeMeanPr2'*RtL)^(-1)*RtL'*DeMeanPr2*DeMeanPr2'*RtL);
    WeightsL_mu(:,t-TrainingSample+1)=DeMeanPr2'*RtL*(RtL'*DeMeanPr2*DeMeanPr2'*DeMeanPr2*DeMeanPr2'*RtL)^(-1)*RtL'*DeMeanPr2*DeMeanPr2'*RtL/std1;
    if t>TrainingSample
        for i=1:7
            if (WeightsL_mu(i,t-TrainingSample+1)*mean(WeightsL_mu(i,1:t-TrainingSample)))<Constraint_level
                WeightsL_mu(i,t-TrainingSample+1)=0;
            end
        end
    end
    aligned_YtL=zscore(DeMeanPr2*WeightsL_mu(:,t-TrainingSample+1));
    
    %% regressions 
    
    
    if pi1(t)>=0.5
%         A1=[ones(t-1,1) aligned_Mtt(1:end-1)];
%         [X2,STDX1]=lscov(A1,Rtt);
%         Rt_hat(t)=X2(1)+X2(2)*aligned_Mtt(end); 
        NoH=NoH+1;
        A1=[ones(NumberH,1) aligned_MtH];
        [X2,STDX2]=lscov(A1,RtH);
        Rt_hatH(NoH)=X2(1)+X2(2)*aligned_MtH(end); 
        Rt_realH(NoH)=Rt(t);
        MeanRt_H(NoH)=mean(RtH);
    else
%         A1=[ones(t-1,1) aligned_Ytt(1:end-1)];
%         [X3,STDX1]=lscov(A1,Rtt);
%         Rt_hat(t)=X3(1)+X3(2)*aligned_Ytt(end); 
        NoL=NoL+1;
        A1=[ones(NumberL,1) aligned_YtL];
        [X3,STDX3]=lscov(A1,RtL);
        Rt_hatL(NoL)=X3(1)+X3(2)*aligned_YtL(end); 
        Rt_realL(NoL)=Rt(t);
        MeanRt_L(NoL)=mean(RtL);
    end
    
end
    
%% R squared
denominator=0;
for i=1:DataSize-TrainingSample+1
    denominator=denominator+(Rt(TrainingSample+i-1)-mean(Rt(1:TrainingSample+i-1-1)))^2;
    MeanRt(i)=mean(Rt(1:TrainingSample+i-1-1));
end

R_squared_OS=1-(sum((Rt_hatH-Rt_realH).^2)+sum((Rt_hatL-Rt_realL).^2))/denominator;
Rt_hat_diff=[Rt_hatL(1,1:76) Rt_hatH(1,1:2) Rt_hatL(1,77:98) Rt_hatH(1,3:19) Rt_hatL(1,99:123) Rt_hatH(1,20:48) Rt_hatL(1,124:226)]; 
p_value_diff=Perform_CW_test(Rt(TrainingSample:DataSize),MeanRt',Rt_hat_diff');    
% high 77-78 101-117 143-171
% low 1-76 79-100 118-142 172-274
%R_squared_OS=1-(sum((Rt_hatH-Rt_realH).^2)+sum((Rt_hatL-Rt_realL).^2))/(sum((Rt_realH-MeanRt_H).^2)+sum((Rt_realL-MeanRt_L).^2));

end
