function [out] = rs_test_adj(z, rvec, mult, testcase)
% INPUT
% z - T by 1 vector of the probability integral transforms
% rvec - 1 by x vector with values between 0 and 1 guiding the
% discretization of the uniform
% *************************************************************************
% Implementing the test
% *************************************************************************
% Creating the test statistics
% cumcumz corresponds to the xi and phi on page 8, respectively

T = size(z,1);

% If implementing the test as in Theorem 2, which is applicable for
% one-step-ahead density forecasts, the following two lines of code apply.
% The critical values are as in Table 1 of the paper and correspond to 1% 5% and 10%,
% respectively
if testcase == 1
    crit_value.ks = [1.61 1.34 1.21];
    crit_value.cvm = [0.74 0.46 0.35];
elseif testcase == 2 % r = [0,.25]
    crit_value.ks = [1.24 1.00 0.88];
    crit_value.cvm = [0.56 0.34 0.24];
    rvec = rvec(rvec<=0.25);
elseif testcase == 3 % r = [0,.5]
    crit_value.ks = [1.54 1.26 1.12];
    crit_value.cvm = [0.86 0.52 0.38];
    rvec = rvec(rvec<=0.5);
elseif testcase == 4 % r = [.5,1]
    crit_value.ks = [1.53 1.25 1.12];
    crit_value.cvm = [0.85 0.52 0.38];
    rvec = rvec(rvec>=0.5);
elseif testcase == 5 % r = [.75,1]
    crit_value.ks = [1.24 1.00 0.88 ];
    crit_value.cvm = [0.56 0.34 0.24];
    rvec = rvec(rvec>=0.75);
elseif testcase == 6 % r = [.25,.75]
    crit_value.ks = [1.61 1.33 1.19 ];
    crit_value.cvm = [1.18 0.71 0.52];
    rvec = rvec((rvec(rvec<=0.75))>=0.25);
elseif testcase == 7 % r = [.25,.75]
    crit_value.ks = [1.33 1.10 0.99];
    crit_value.cvm = [0.41 0.27 0.21];
    rvec = [rvec(rvec<=0.25) rvec(rvec>=0.75)];
end

if mult == 0
    cumcumz = (repmat(z,1,size(rvec,2)) < repmat(rvec,size(z,1),1)) - repmat(rvec,size(z,1),1);
    v = sum(cumcumz,1)/sqrt(T);
    
    KS = max(abs(v)); % Kolmogorov-Smirnov test statistics
    CVM = mean(v.^2); % Cramer-von-Mises test statistics

    QVrej_one_step_ahead = (KS > crit_value.ks);   % Kolmogorov-Smirnov test 
    CVMrej_one_step_ahead = (CVM > crit_value.cvm); % Cramer-von-Mises test 
    delete Results.out; diary Results.out;
    disp('Test statistics [KS CVM]');
    disp([KS CVM]);
    disp('Critical Values, 1% 5% 10%')
    disp('One-step-ahead density calibration test')
    disp('Kolmogorov-Smirnov Test');
    disp(crit_value.ks);
    disp('Cramer-von-Mises Test');
    disp(crit_value.cvm);
    out.teststat.ks  = KS;
    out.teststat.cvm = CVM;
    out.crit_value.ks  = crit_value.ks;
    out.crit_value.cvm = crit_value.cvm;
else
    % Simulate the critical values applicable for multi-step-ahead density
    % forecast calibration
    % Implements the test based on the Theorem 3
    % setup some preliminaries for the bootstrap
    el = floor(T.^(1/3)); % block length
    bootMC = 200;
    
    tableboot = CVfinalbootstrapInoue(el, bootMC, z, rvec);
    if size(tableboot,1)>size(tableboot,2)
        tableboot = tableboot';
    end
    boot_crit_value.ks = tableboot(1,:);
    boot_crit_value.cvm = tableboot(2,:);

    cumcumz = (repmat(z,1,size(rvec,2)) < repmat(rvec,size(z,1),1)) - repmat(rvec,size(z,1),1);
    v = sum(cumcumz,1)/sqrt(T);
    
    KS = max(abs(v)); % Kolmogorov-Smirnov test statistics
    CVM = mean(v.^2); % Cramer-von-Mises test statistics

    QVrej_multi_step_ahead = (KS > boot_crit_value.ks); 
    CVMrej_multi_step_ahead = (CVM > boot_crit_value.cvm);
    disp('Multi-step-ahead density calibration test');
    disp('Kolmogorov-Smirnov Test');
    disp(boot_crit_value.ks);
    disp('Cramer-von-Mises Test');
    disp(boot_crit_value.cvm);
    diary off;
    out.teststat.ks  = KS;
    out.teststat.cvm = CVM;
    out.crit_value.ks  = boot_crit_value.ks;
    out.crit_value.cvm = boot_crit_value.cvm;
end



