clc;
clear;
% consumer expectation is No. 132 in group 4, the survey asks consumer's view about personal financial condition one year later and view about whole economy during next 5 years, so though it's different from investor sentiment, it will contain some NONFUND component
%% transformations
load vartype132;
input_file='macro_raw_data';
input_sheet='macrodata';
Xmacro=xlsread(input_file,input_sheet,'c66:ed613');%% 1965.5-2010.12
N1=size(Xmacro,2);
macrorawdata=[];
for i=1:N1;
  dum1=Xmacro(:,i);
  m1=mean(dum1);
  if isnan(m1) ==0;
    macrorawdata=[macrorawdata dum1];
  else
    disp([i m1]);
  end;
end; 
y1=nan(548,N1);
for i=1:N1;
   y1(:,i)=transx(Xmacro(:,i),vartype132(1,i));
end;
y1=y1(3:end,:); %% 1965.7-2010.12

%% drop bond in group 6 and stock in group 8 
% drop_index=[82:102];
% y1(:,drop_index)=[];

%% PCA
No1=[1 6:20]; % output and income
COEFF1 = pca((y1(:,No1)));
F1=zscore((y1(:,No1))*COEFF1(:,1));

No2=[21:50 129:131]; % labor market
COEFF2 = pca((y1(:,No2)));
F2=zscore((y1(:,No2))*COEFF2(:,1));

No3=[51:60]; % housing
COEFF3 = pca((y1(:,No3)));
F3=zscore((y1(:,No3))*COEFF3(:,1));

No4=[3:5 61:70 132]; % consumption order and inventories
COEFF4 = pca((y1(:,No4)));
F4=zscore((y1(:,No4))*COEFF4(:,1));

No5=[71:81]; % money and credit
COEFF5 = pca((y1(:,No5)));
F5=zscore((y1(:,No5))*COEFF5(:,1));

No6=[103:107]; % exchange rates
COEFF6 = pca((y1(:,No6)));
F6=zscore((y1(:,No6))*COEFF6(:,1));

No7=[108:128]; % prices
COEFF7 = pca((y1(:,No7)));
F7=zscore((y1(:,No7))*COEFF7(:,1));

y2=zeros(546,111);
y2(:,1:81)=y1(:,1:81);
y2(:,82:111)=y1(:,103:132);
COEFF8 = pca((y2));
F8=zscore((y2)*COEFF8);

%save('PCA_ECON.mat','F1','F2','F3','F4','F5','F6','F7','F8','y1') 


