format long;
clear;
clc;
input_file='investor_Sentiment_Data_v23_POST.xlsx';% load Baker-Wurgler data
input_sheet='POST monthly';
date=xlsread(input_file,input_sheet,'a102:a647');
%% pdnd
pdnd=xlsread(input_file,input_sheet,'f90:f635');
%% nipo
nipo=xlsread(input_file,input_sheet,'g102:g647');
%% ripo
ripo=xlsread(input_file,input_sheet,'aa90:aa635');
%% turn
%turn=xlsread(input_file,input_sheet,'i90:i635');
turnover=xlsread(input_file,input_sheet,'i30:i635');
turn=nan(606,1);
for t=61:606
turn(t)=log(turnover(t))-sum(log(turnover(t-60:t-1)))/60;
end
turn=turn(61:end);
%% cefd
cefd=xlsread(input_file,input_sheet,'j102:j647');
%% eqti
se=xlsread(input_file,input_sheet,'k102:k647');
sd=xlsread(input_file,input_sheet,'l102:l647');
eqti=se./(se+sd);
IP=xlsread(input_file,input_sheet,'n102:n647');
lagIP=xlsread(input_file,input_sheet,'n101:n646');
gIP=log(IP)-log(lagIP);
Cons=xlsread(input_file,input_sheet,'o102:o647');
lagCons=xlsread(input_file,input_sheet,'o101:o646');
gCons=log(Cons)-log(lagCons);
employ=xlsread(input_file,input_sheet,'t102:t647');
lagemploy=xlsread(input_file,input_sheet,'t101:t646');
gemploy=log(employ)-log(lagemploy);
recess=xlsread(input_file,input_sheet,'s102:s647');
ECON=[gIP gCons gemploy recess];
IP2=xlsread(input_file,input_sheet,'n90:n635');
lagIP2=xlsread(input_file,input_sheet,'n89:n634');
gIP2=log(IP2)-log(lagIP2);
Cons2=xlsread(input_file,input_sheet,'o90:o635');
lagCons2=xlsread(input_file,input_sheet,'o89:o634');
gCons2=log(Cons2)-log(lagCons2);
employ2=xlsread(input_file,input_sheet,'t90:t635');
lagemploy2=xlsread(input_file,input_sheet,'t89:t634');
gemploy2=log(employ2)-log(lagemploy2);
recess2=xlsread(input_file,input_sheet,'s90:s635');
ECON2=[gIP2 gCons2 gemploy2 recess2];
%% sentiment component
X_SENT6=[cefd turn nipo ripo pdnd eqti];
T=546;
X_SENT6=X_SENT6(1:T,:);
%%
load SP500_sentment_new.mat;

TrainingSample=273;% train sample period
R=TrainingSample; 
P=T-R; % forecast evaluation period

% Computing recursive forecasts

    for t=1:P+1;
        results_SENT_1=ols(X_SENT6(1:R+t-1,1),[ones(R+t-1,1) ECON(1:R+(t-1),:)]);
        X_SENT6hat(1:R+t-1,1)=results_SENT_1.yhat;
        X_SENT6res(1:R+t-1,1)=results_SENT_1.resid;
        results_SENT_2=ols(X_SENT6(1:R+t-1,2),[ones(R+t-1,1) ECON2(1:R+(t-1),:)]);
        X_SENT6hat(1:R+t-1,2)=results_SENT_2.yhat;
        X_SENT6res(1:R+t-1,2)=results_SENT_2.resid;
        results_SENT_3=ols(X_SENT6(1:R+t-1,3),[ones(R+t-1,1) ECON(1:R+(t-1),:)]);
        X_SENT6hat(1:R+t-1,3)=results_SENT_3.yhat;
        X_SENT6res(1:R+t-1,3)=results_SENT_3.resid;
        results_SENT_4=ols(X_SENT6(1:R+t-1,4),[ones(R+t-1,1) ECON2(1:R+(t-1),:)]);
        X_SENT6hat(1:R+t-1,4)=results_SENT_4.yhat;
        X_SENT6res(1:R+t-1,4)=results_SENT_4.resid;
        results_SENT_5=ols(X_SENT6(1:R+t-1,5),[ones(R+t-1,1) ECON2(1:R+(t-1),:)]);
        X_SENT6hat(1:R+t-1,5)=results_SENT_5.yhat;
        X_SENT6res(1:R+t-1,5)=results_SENT_5.resid;
        results_SENT_6=ols(X_SENT6(1:R+t-1,6),[ones(R+t-1,1) ECON(1:R+(t-1),:)]);
        X_SENT6hat(1:R+t-1,6)=results_SENT_6.yhat;
        X_SENT6res(1:R+t-1,6)=results_SENT_6.resid;

        X_SENT6res(1:R+t-1,[1 5])=(-1).*X_SENT6res(1:R+t-1,[1 5]);
        X_SENT6res(1:R+(t-1),:)=mvavg(X_SENT6res(1:R+(t-1),:),6);
        X_SENT6res(1:R+(t-1),:)=zscore(X_SENT6res(1:R+(t-1),:));
    
        [Lambda_hat,F_hat(1:R+(t-1),:)]=princomp(zscore(X_SENT6res(1:R+(t-1),:)));
        PCres(1:R+(t-1),t)=F_hat(1:R+t-1,1);
        PCres(1:R+(t-1),t)=zscore(PCres(1:R+(t-1),t));%real time BW index
    end;
%% OOS
R_2_OS_Unconstraint=zeros(11,1); %no constraint whole sample
R_2_OSH_Unconstraint=zeros(11,1);  %no constraint H regime
R_2_OSL_Unconstraint=zeros(11,1);  %no constraint L regime
p_Unconstraint=zeros(11,1); 
pH_Unconstraint=zeros(11,1);  
pL_Unconstraint=zeros(11,1);  
R_2_OS_FixCoeff=zeros(11,1);   %Fixed Coefficient whole sample
R_2_OSH_FixCoeff=zeros(11,1);  %Fixed Coefficient H regime
R_2_OSL_FixCoeff=zeros(11,1);  %Fixed Coefficient L regime
p_FixCoeff=zeros(11,1); 
pH_FixCoeff=zeros(11,1);  
pL_FixCoeff=zeros(11,1);
load CT_Table2_data.mat
for i=1:11
    [ R_squared_OS1,R_squared_OSH1,R_squared_OSL1,p,pH,pL] = regressions_OOS_Unconstraint(PCres,TrainingSample,X1(:,i),R_excess);
    [ R_squared_OS3,R_squared_OSH3,R_squared_OSL3,p2,pH2,pL2] = regressions_OOS_FixCoeff(PCres,TrainingSample,X1(:,i),R_excess);
    R_2_OS_Unconstraint(i)=R_squared_OS1;
    R_2_OSH_Unconstraint(i)=R_squared_OSH1;
    R_2_OSL_Unconstraint(i)=R_squared_OSL1;
    R_2_OS_FixCoeff(i)=R_squared_OS3;
    R_2_OSH_FixCoeff(i)=R_squared_OSH3;
    R_2_OSL_FixCoeff(i)=R_squared_OSL3;

    p_Unconstraint(i)=p; 
    pH_Unconstraint(i)=pH;  
    pL_Unconstraint(i)=pL;
    p_FixCoeff(i)=p2; 
    pH_FixCoeff(i)=pH2;  
    pL_FixCoeff(i)=pL2;
end

Table1=[R_2_OS_Unconstraint R_2_OSH_Unconstraint R_2_OSL_Unconstraint R_2_OS_FixCoeff R_2_OSH_FixCoeff R_2_OSL_FixCoeff]*100;

Table2=[p_Unconstraint pH_Unconstraint pL_Unconstraint p_FixCoeff pH_FixCoeff pL_FixCoeff];


