testValue.m 2.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
function [is_significant,p_value] = testValue(base_distribution,test_value,alpha);

% Based on a distribution of slopes, test whether a given test slope
% is significant for a given significance level alpha.
%
% USAGE: is_significant = testSlope(slope_distribution,test_slope,alpha);
%
%     OUTPUT:
%        is_significant:      1 for significant, 0 for insignificant
%
%     INPUT:
%        base_distribution:   Underlying slope distribution
%        test_value:          Which slope value to test?
%        alpha:               Significance level

% ----------------------------------------- %
% ESTIMATE CUMULATIVE DISTRIBUTION FUNCTION %
% ----------------------------------------- %

    % sort data in ascending order
    sorted_data = sort(base_distribution);

    % "integrate" the sorted data, normalize the integration
    estimated_cdf = cumsum(sorted_data)./sum(sorted_data);

% ---------------------------------------------- %
% CALCULATE CRITICAL VALUES FOR A TWO-SIDED TEST %
% ---------------------------------------------- %

    % For a two-sided test, the significance level
    % is the TOTAL significance level, i.e. the
    % tail-specific significance level is alpha/2.
    % A two-sided test is appropriate if the
    % test statistic might be larger or smaller
    % than the reference value.
    % source: https://stats.idre.ucla.edu/other/mult-pkg/faq/general/
    % faq-what-are-the-differences-between-one-tailed-and-two-tailed-tests/

    % find the two ciritical values
    crit_levels = [alpha/2 1-alpha/2];
    crit_values = zeros(1,2);

    for c_level = 1:2

        use_level = crit_levels(c_level);

        % where is the closest value in the cdf?
        [minimum_distance cdf_index] = min(abs(estimated_cdf-use_level));

        % what is the corresponding slope value?
        % this is the critical value
        crit_values(c_level) = sorted_data(cdf_index);

    % for: compute both ciritcal levels
    end

% --------------- %
% THE ACTUAL TEST %
% --------------- %

    is_significant = ( ( test_value < crit_values(1) ) | ( test_value > crit_values(2) ) );