function [ R_squared_OS,R_squared_OSH,R_squared_OSL,p_value_m,p_value_m_h,p_value_m_l] = OOS_m_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;
Weights_m=zeros(6,DataSize-TrainingSample+1);
WeightsH_m=zeros(6,DataSize-TrainingSample+1);
WeightsL_m=zeros(6,DataSize-TrainingSample+1);

for t=TrainingSample:DataSize

    Rtt=Rt(1:t-1); % that is, use information up to t
    Mtt=Mt2(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,4);
    RtL=zeros(t-1-high_m,1);
    MtL1=zeros(t-1-high_m,6);
    %YtL1=zeros(t-1-high_m,4);
    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)))<Constraint_level
                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)))<Constraint_level
                WeightsL_m(i,t-TrainingSample+1)=0;
            end
        end
    end
    aligned_MtL=zscore(MtL1*WeightsL_m(:,t-TrainingSample+1));
    
    
   
    
    %% regressions 
    A1=[ones(t-1,1) aligned_Mtt(1:end-1)];
    [X1,STDX1]=lscov(A1,Rtt);
    
    Rt_hat(t)=X1(1)+X1(2)*aligned_Mtt(end); 
    
    A1=[ones(NumberH,1) aligned_MtH];
    [X2,STDX2]=lscov(A1,RtH);
    
    A1=[ones(NumberL,1) aligned_MtL];
    [X3,STDX3]=lscov(A1,RtL);
    
    if pi1(t)>=0.5
        NoH=NoH+1;
        Rt_hatH(NoH)=X2(1)+X2(2)*aligned_MtH(end); 
        Rt_realH(NoH)=Rt(t);
        MeanRt_H(NoH)=mean(RtH);
    else
        NoL=NoL+1;
        Rt_hatL(NoL)=X3(1)+X3(2)*aligned_MtL(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_hat(TrainingSample:DataSize)-Rt(TrainingSample:DataSize)).^2)/denominator;

R_squared_OSH=1-sum((Rt_hatH-Rt_realH).^2)/sum((Rt_realH-MeanRt_H).^2);
R_squared_OSL=1-sum((Rt_hatL-Rt_realL).^2)/sum((Rt_realL-MeanRt_L).^2);    
p_value_m=Perform_CW_test(Rt(TrainingSample:DataSize),MeanRt',Rt_hat(TrainingSample:DataSize));    
p_value_m_h=Perform_CW_test(Rt_realH',MeanRt_H',Rt_hatH'); 
p_value_m_l=Perform_CW_test(Rt_realL',MeanRt_L',Rt_hatL');  
end