%*********************************************************************
%  Program        : 5.GX_tests.m
%  Description    : Three-pass regression of Giglio and Xiu (2021)
%                   for the FF6 factors and the uncertainty shocks
%                   (Table 5 and Appendix Table H4 of the paper).
%                   The risk premia are estimated by the
%                   replication code provided by Giglio and Xiu
%                   (2021), which is included in this folder without
%                   any modification (together with its helper
%                   functions and the original replication package
%                   in Codes_RP\).
%                   The printed matrix reports, for each number of
%                   latent factors p = 1, ..., 15 (columns), the
%                   estimated risk premia and their standard errors.
%                   Table 5 uses seven latent factors (column 7);
%                   Appendix Table H4 uses fifteen latent factors
%                   (column 15). The Newey-West standard errors use
%                   five (annual) lags.
%                   The time-series R-squared of each factor on the
%                   latent factors is in ans.R2G, and the p-value of
%                   the weak-factor test is in ans.chi2test.
%
%  Input : Table5\ ff6_annual_dispall_negativeprc.csv
%          (45 annual observations of the FF6 factors and the three
%          uncertainty shocks: mktrfy, smby, hmly, cmay, rmwy, umdy,
%          d_tfpstd_star (UNC), d_pcahat (macro UNC), d_pcares
%          (micro UNC), and rfy)
%          Table5\ testasset198.csv
%          (198 annual test asset returns)
%*********************************************************************
diary 5.GX_tests.txt
%% 1. Read the data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
projpath = 'D:\SAS\TFP\Dispersion_replication';
% Factor returns (T by K): T---# of obs, K---# of factors
data = csvread(fullfile(projpath,'Table5','ff6_annual_dispall_negativeprc.csv'),1,0);
FF6_disp = data(1:45,2:10);
FF6 = FF6_disp(1:45,1:6);
FF3 = FF6_disp(1:45,1:3);
FF4 = FF6_disp(1:45,[1,2,3,6]);
FF5 = FF6_disp(1:45,1:5);
UNC = FF6_disp(1:45,7:9);
FF = FF6;
rf = data(1:45,11);
Factors = [FF UNC];
%
% Portfolio returns (T by N): T---# of obs, N---# of assets
data2 = csvread(fullfile(projpath,'Table5','testasset198.csv'),1,0);
Rawreturn=data2(1:45,2:end);
R=Rawreturn-rf(1:45,ones(1,size(Rawreturn,2)))  ;
% GX full
PCAFM(R',Factors',15,5);
%
disp('Risk premia (2nd to 8th row) and standard deviations (9th to 17th) over the number of k=1 to 15');
disp('Row order 2nd to 8th: Risk premia of MKT, SMB, HML, CMA, RMW, UMD, total unc, macro unc, micro unc');
disp('Row order 9nd to 17th: Standard deviation of MKT, SMB, HML, CMA, RMW, UMD, total unc, macro unc, micro unc');
[ans.Gammahat_nozero ; sqrt(ans.avarhat_nozero)]
%%
diary off
