% This is an executable file which reads matlab data structures PITSgdp4 and PITSpgdp4
% 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 PITSgdp4.mat
load PITSpgdp4.mat

hzn = 4;
totallbgdp = [];    totallbpgdp = [];
totalufgdp = [];    totalufpgdp = [];
totalqlrgdp = [];   totalqlrpgdp = [];
totalbkgdp = [];    totalbkpgdp = [];
totaldhgdp = [];    totaldhpgdp = [];

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'];

for i = 1:hzn
    seldensforecastgdp = densforecastgdp(:,i:hzn:end);
    selpooldensforecastgdp = pooldensforecastgdp(:,i:hzn:end);
    selfactordensforecastgdp = factordensforecastgdp(:,i:hzn:end);
    selvardensforecastgdp = vardensforecastgdp(:,i:hzn:end);
    
    seldensforecastpgdp = densforecastpgdp(:,i:hzn:end);
    selpooldensforecastpgdp = pooldensforecastpgdp(:,i:hzn:end);
    selfactordensforecastpgdp = factordensforecastpgdp(:,i:hzn:end);
    selvardensforecastpgdp = vardensforecastpgdp(:,i:hzn:end);

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % tests of independence
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    for mom = 1:2
        lbgdpsimple = testsofindependence(seldensforecastgdp,mom,4);
        lbgdppool   = testsofindependence(selpooldensforecastgdp,mom,4);
        lbgdpfactor = testsofindependence(selfactordensforecastgdp,mom,4);
        lbgdpvar    = testsofindependence(selvardensforecastgdp,mom,4);

        lbgdp(:,mom) =  [round(lbgdpsimple.pValue*100)/100; round(lbgdppool.pValue*100)/100; round(lbgdpfactor.pValue*100)/100; round(lbgdpvar.pValue*100)/100;];


        lbpgdpsimple = testsofindependence(seldensforecastpgdp,mom,4);
        lbpgdppool   = testsofindependence(selpooldensforecastpgdp,mom,4);
        lbpgdpfactor = testsofindependence(selfactordensforecastpgdp,mom,4);
        lbpgdpvar    = testsofindependence(selvardensforecastpgdp,mom,4);

        lbpgdp(:,mom) =  [round(lbpgdpsimple.pValue*100)/100; round(lbpgdppool.pValue*100)/100; round(lbpgdpfactor.pValue*100)/100; round(lbpgdpvar.pValue*100)/100;];
    end
    
    totallbgdp = [totallbgdp  lbgdp];
    totallbpgdp = [totallbpgdp lbpgdp];
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % tests of uniformity
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    ufgdpsimple = testsofuniformity(seldensforecastgdp);
    ufgdppool   = testsofuniformity(selpooldensforecastgdp);
    ufgdpfactor = testsofuniformity(selfactordensforecastgdp);
    ufgdpvar    = testsofuniformity(selvardensforecastgdp);

    ufgdp =  [[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(seldensforecastpgdp);
    ufpgdppool   = testsofuniformity(selpooldensforecastpgdp);
    ufpgdpfactor = testsofuniformity(selfactordensforecastpgdp);
    ufpgdpvar    = testsofuniformity(selvardensforecastpgdp);

    ufpgdp =  [[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;]];
  
    totalufgdp = [totalufgdp    ufgdp];
    totalufpgdp = [totalufpgdp  ufpgdp];
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % tests of identical distribution
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    for mom = 1:2
        qlrgdpsimple = testsofidentical(seldensforecastgdp,mom,dategdp);
        qlrgdppool   = testsofidentical(selpooldensforecastgdp,mom,dategdp);
        qlrgdpfactor = testsofidentical(selfactordensforecastgdp,mom,dategdp);
        qlrgdpvar    = testsofidentical(selvardensforecastgdp,mom,dategdp);

        qlrgdp(:,mom) =  [round(qlrgdpsimple.pValue*100)/100; round(qlrgdppool.pValue*100)/100; round(qlrgdpfactor.pValue*100)/100; round(qlrgdpvar.pValue*100)/100;];

        qlrpgdpsimple = testsofidentical(seldensforecastpgdp,mom,datepgdp);
        qlrpgdppool   = testsofidentical(selpooldensforecastpgdp,mom,datepgdp);
        qlrpgdpfactor = testsofidentical(selfactordensforecastpgdp,mom,datepgdp);
        qlrpgdpvar    = testsofidentical(selvardensforecastpgdp,mom,datepgdp);

        qlrpgdp(:,mom) = [round(qlrpgdpsimple.pValue*100)/100; round(qlrpgdppool.pValue*100)/100; round(qlrpgdpfactor.pValue*100)/100; round(qlrpgdpvar.pValue*100)/100;];
    end
    
    totalqlrgdp = [totalqlrgdp  qlrgdp];
    totalqlrpgdp = [totalqlrpgdp qlrpgdp];
    
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % tests of uniformity on the Inverse Normal Transform
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    bkgdpsimple = testsofuniformityInvNorm(seldensforecastgdp);
    bkgdppool   = testsofuniformityInvNorm(selpooldensforecastgdp);
    bkgdpfactor = testsofuniformityInvNorm(selfactordensforecastgdp);
    bkgdpvar    = testsofuniformityInvNorm(selvardensforecastgdp);

    bkgdp =  [[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;]];

    dhgdp = [round(bkgdpsimple.dhPvalue*100)/100; round(bkgdppool.dhPvalue*100)/100; round(bkgdpfactor.dhPvalue*100)/100; round(bkgdpvar.dhPvalue*100)/100;];

    bkpgdpsimple = testsofuniformityInvNorm(seldensforecastpgdp);
    bkpgdppool   = testsofuniformityInvNorm(selpooldensforecastpgdp);
    bkpgdpfactor = testsofuniformityInvNorm(selfactordensforecastpgdp);
    bkpgdpvar    = testsofuniformityInvNorm(selvardensforecastpgdp);

    bkpgdp =  [[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;]];
  
    dhpgdp = [round(bkpgdpsimple.dhPvalue*100)/100; round(bkpgdppool.dhPvalue*100)/100; round(bkpgdpfactor.dhPvalue*100)/100; round(bkpgdpvar.dhPvalue*100)/100;];

    totalbkgdp = [totalbkgdp    bkgdp];
    totalbkpgdp = [totalbkpgdp  bkpgdp];
    
    totaldhgdp = [totaldhgdp    dhgdp];
    totaldhpgdp = [totaldhpgdp  dhpgdp];
end

tablelbgdp(:,1) = min(totallbgdp(:,[1 3 5 7]),[],2);
tablelbgdp(:,2) = min(totallbgdp(:,[2 4 6 8]),[],2);

tablelbpgdp(:,1) = min(totallbpgdp(:,[1 3 5 7]),[],2);
tablelbpgdp(:,2) = min(totallbpgdp(:,[2 4 6 8]),[],2);

tableufgdp = [min(totalufgdp(:,[1 3 5 7]),[],2)    min(totalufgdp(:,[2 4 6 8]),[],2)];
tableufpgdp = [min(totalufpgdp(:,[1 3 5 7]),[],2)    min(totalufpgdp(:,[2 4 6 8]),[],2)];

tableqlrgdp(:,1) = min(totalqlrgdp(:,[1 3 5 7]),[],2);
tableqlrgdp(:,2) = min(totalqlrgdp(:,[2 4 6 8]),[],2);

tableqlrpgdp(:,1) = min(totalqlrpgdp(:,[1 3 5 7]),[],2);
tableqlrpgdp(:,2) = min(totalqlrpgdp(:,[2 4 6 8]),[],2);

tablebkgdp = [min(totalbkgdp(:,[1 4 7 10]),[],2)    min(totalbkgdp(:,[2 5 8 11]),[],2)  min(totalbkgdp(:,[3 6 9 12]),[],2)];
tablebkpgdp = [min(totalbkpgdp(:,[1 4 7 10]),[],2)    min(totalbkpgdp(:,[2 5 8 11]),[],2)  min(totalbkpgdp(:,[3 6 9 12]),[],2)];

tabledhgdp = min(totaldhgdp,[],2);
tabledhpgdp = min(totaldhpgdp,[],2);


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

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

delete testofidentical4.out; diary testsofidentical4.out;
disp(strcat('Andrews QLR test results for GDP growth, horizon4'))
[vnamegdp'  num2cell(tableqlrgdp)]
disp(strcat('Andrews QLR test results for GDP Deflator growth, horizon4'))
[vnamepgdp'  num2cell(tableqlrpgdp)]
diary off

delete testsofInvNormuniformity4.out; diary testsofInvNormuniformiaty4.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