% This is an executable file which reads matlab data structures PITSgdp1 and PITSpgdp1
% which include the stored versions of the PITS
% it further invokes the battery of tests, namely Kolmogorov-Smirnov, Anderson-Darling,
% Ljung-Box, Andrews QLR, Berkowitz, as well as Doornik and Hansen.

clear all; close all
load PITSgdp1.mat
load PITSpgdp1.mat

vnamegdp = [vnamegdp(1:32), 'Simple Average', 'BMA-OLS', 'BMA', 'BMA-MC3', 'Factor', 'BVAR'];
vnamepgdp = [vnamepgdp(1:32), 'Simple Average', 'BMA-OLS', 'BMA', 'BMA-MC3', 'Factor', 'BVAR'];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% tests of independence
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for mom = 1:2
    lbgdpsimple = testsofindependence(densforecastgdp,mom,4);
    lbgdppool   = testsofindependence(pooldensforecastgdp,mom,4);
    lbgdpfactor = testsofindependence(factordensforecastgdp,mom,4);
    lbgdpvar    = testsofindependence(vardensforecastgdp,mom,4);
    
    tablelbgdp(:,mom) =  [round(lbgdpsimple.pValue*100)/100; round(lbgdppool.pValue*100)/100; round(lbgdpfactor.pValue*100)/100; round(lbgdpvar.pValue*100)/100;];


    lbpgdpsimple = testsofindependence(densforecastpgdp,mom,4);
    lbpgdppool   = testsofindependence(pooldensforecastpgdp,mom,4);
    lbpgdpfactor = testsofindependence(factordensforecastpgdp, mom,4);
    lbpgdpvar    = testsofindependence(vardensforecastpgdp,mom,4);
    
    tablelbpgdp(:,mom) =  [round(lbpgdpsimple.pValue*100)/100; round(lbpgdppool.pValue*100)/100; round(lbpgdpfactor.pValue*100)/100; round(lbpgdpvar.pValue*100)/100;];
end

delete testofindependence1.out; diary testsofindependence1.out;
disp(strcat('Ljung-Box test results for GDP growth, horizon1'))
[vnamegdp'  num2cell(tablelbgdp)]
disp(strcat('Ljung-Box test results for GDP Deflator growth, horizon1'))
[vnamepgdp'  num2cell(tablelbpgdp)]
diary off


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % tests of uniformity on the PITS
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ufgdpsimple = testsofuniformity(densforecastgdp);
ufgdppool   = testsofuniformity(pooldensforecastgdp);
ufgdpfactor = testsofuniformity(factordensforecastgdp);
ufgdpvar    = testsofuniformity(vardensforecastgdp);

tableufgdp =  [[round(ufgdpsimple.kspValue*100)/100; round(ufgdppool.kspValue*100)/100; round(ufgdpfactor.kspValue*100)/100; round(ufgdpvar.kspValue*100)/100;] ...
               [round(ufgdpsimple.adpValue*100)/100; round(ufgdppool.adpValue*100)/100; round(ufgdpfactor.adpValue*100)/100; round(ufgdpvar.adpValue*100)/100;]];

ufpgdpsimple = testsofuniformity(densforecastpgdp);
ufpgdppool   = testsofuniformity(pooldensforecastpgdp);
ufpgdpfactor = testsofuniformity(factordensforecastpgdp);
ufpgdpvar    = testsofuniformity(vardensforecastpgdp);

tableufpgdp =  [[round(ufpgdpsimple.kspValue*100)/100; round(ufpgdppool.kspValue*100)/100; round(ufpgdpfactor.kspValue*100)/100; round(ufpgdpvar.kspValue*100)/100;] ...
                [round(ufpgdpsimple.adpValue*100)/100; round(ufpgdppool.adpValue*100)/100; round(ufpgdpfactor.adpValue*100)/100; round(ufpgdpvar.adpValue*100)/100;]];

delete testsofpituniformity1.out; diary testsofpituniformiaty1.out;
disp(strcat('Kolmogorov-Smirnov and Anderson-Darling test results for GDP growth, horizon1'))
[vnamegdp'  num2cell(tableufgdp)]
disp(strcat('Kolmogorov-Smirnov and Anderson-Darling test results for GDP Deflator growth, horizon1'))
[vnamepgdp'  num2cell(tableufpgdp)]
diary off

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% tests of identical distribution
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for mom = 1:2
    qlrgdpsimple = testsofidentical(densforecastgdp,mom,dategdp);
    qlrgdppool   = testsofidentical(pooldensforecastgdp,mom,dategdp);
    qlrgdpfactor = testsofidentical(factordensforecastgdp,mom,dategdp);
    qlrgdpvar    = testsofidentical(vardensforecastgdp,mom,dategdp);
    
    tableqlrgdp(:,mom) =  [round(qlrgdpsimple.pValue*100)/100; round(qlrgdppool.pValue*100)/100; round(qlrgdpfactor.pValue*100)/100; round(qlrgdpvar.pValue*100)/100;];
    tableqlrbdgdp(:,mom) = [qlrgdpsimple.brdate; qlrgdppool.brdate; qlrgdpfactor.brdate; qlrgdpvar.brdate;];

    qlrpgdpsimple = testsofidentical(densforecastpgdp,mom,datepgdp);
    qlrpgdppool   = testsofidentical(pooldensforecastpgdp,mom,datepgdp);
    qlrpgdpfactor = testsofidentical(factordensforecastpgdp, mom,datepgdp);
    qlrpgdpvar    = testsofidentical(vardensforecastpgdp,mom,datepgdp);
    
    tableqlrpgdp(:,mom) =  [round(qlrpgdpsimple.pValue*100)/100; round(qlrpgdppool.pValue*100)/100; round(qlrpgdpfactor.pValue*100)/100; round(qlrpgdpvar.pValue*100)/100;];
    tableqlrbdpgdp(:,mom) = [qlrpgdpsimple.brdate; qlrpgdppool.brdate; qlrpgdpfactor.brdate; qlrpgdpvar.brdate;];
end

delete testofidentical1.out; diary testsofidentical1.out;
disp(strcat('Andrews QLR test results for GDP growth, horizon1'))
[vnamegdp'  num2cell([tableqlrgdp(:,1) tableqlrbdgdp(:,1) tableqlrgdp(:,2) tableqlrbdgdp(:,2)])]
disp(strcat('Andrews QLR test results for GDP Deflator growth, horizon1'))
[vnamepgdp'  num2cell([tableqlrpgdp(:,1) tableqlrbdpgdp(:,1) tableqlrpgdp(:,2) tableqlrbdpgdp(:,2)])]
diary off

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % tests of uniformity on the Inverse Normal Transform
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
bkgdpsimple = testsofuniformityInvNorm(densforecastgdp);
bkgdppool   = testsofuniformityInvNorm(pooldensforecastgdp);
bkgdpfactor = testsofuniformityInvNorm(factordensforecastgdp);
bkgdpvar    = testsofuniformityInvNorm(vardensforecastgdp);

tablebkgdp =  [[round(bkgdpsimple.lmeanvarPvalue*100)/100; round(bkgdppool.lmeanvarPvalue*100)/100; round(bkgdpfactor.lmeanvarPvalue*100)/100; round(bkgdpvar.lmeanvarPvalue*100)/100;] ...
               [round(bkgdpsimple.lindPvalue*100)/100; round(bkgdppool.lindPvalue*100)/100; round(bkgdpfactor.lindPvalue*100)/100; round(bkgdpvar.lindPvalue*100)/100;]...
               [round(bkgdpsimple.lbothPvalue*100)/100; round(bkgdppool.lbothPvalue*100)/100; round(bkgdpfactor.lbothPvalue*100)/100; round(bkgdpvar.lbothPvalue*100)/100;]];
           
tabledhgdp = [round(bkgdpsimple.dhPvalue*100)/100; round(bkgdppool.dhPvalue*100)/100; round(bkgdpfactor.dhPvalue*100)/100; round(bkgdpvar.dhPvalue*100)/100;];

bkpgdpsimple = testsofuniformityInvNorm(densforecastpgdp);
bkpgdppool   = testsofuniformityInvNorm(pooldensforecastpgdp);
bkpgdpfactor = testsofuniformityInvNorm(factordensforecastpgdp);
bkpgdpvar    = testsofuniformityInvNorm(vardensforecastpgdp);

tablebkpgdp =  [[round(bkpgdpsimple.lmeanvarPvalue*100)/100; round(bkpgdppool.lmeanvarPvalue*100)/100; round(bkpgdpfactor.lmeanvarPvalue*100)/100; round(bkpgdpvar.lmeanvarPvalue*100)/100;] ...
               [round(bkpgdpsimple.lindPvalue*100)/100; round(bkpgdppool.lindPvalue*100)/100; round(bkpgdpfactor.lindPvalue*100)/100; round(bkpgdpvar.lindPvalue*100)/100;]...
               [round(bkpgdpsimple.lbothPvalue*100)/100; round(bkpgdppool.lbothPvalue*100)/100; round(bkpgdpfactor.lbothPvalue*100)/100; round(bkpgdpvar.lbothPvalue*100)/100;]];
           
tabledhpgdp = [round(bkpgdpsimple.dhPvalue*100)/100; round(bkpgdppool.dhPvalue*100)/100; round(bkpgdpfactor.dhPvalue*100)/100; round(bkpgdpvar.dhPvalue*100)/100;];

delete testsofInvNormuniformity1.out; diary testsofInvNormuniformiaty1.out;
disp(strcat('Berkowitz tests of (i) mean - var (ii) independence (iii) both for GDP growth, horizon1'))
[vnamegdp'  num2cell(tablebkgdp)]
disp(strcat('Berkowitz tests of (i) mean - var (ii) independence (iii) both for GDP Deflator growth, horizon1'))
[vnamepgdp'  num2cell(tablebkpgdp)]
disp(strcat('Doornik-Hansen test of normality for GDP growth and GDP Deflator growth, horizon1'))
[vnamegdp'  num2cell(tabledhgdp)  vnamepgdp' num2cell(tabledhpgdp)]
diary off