Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-implement stat_surrogate_pvals #299

Merged
merged 3 commits into from
May 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 19 additions & 26 deletions functions/statistics/stat_surrogate_pvals.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,36 +52,29 @@
% THE POSSIBILITY OF SUCH DAMAGE.

function pvals = stat_surrogate_pvals(distribution,observed,tail)
numDims = ndims(distribution);
if iscolumn(distribution)
numDims = 1;
end

numDims = myndims(distribution);

% append observed to last dimension of surrogate distribution
distribution = cat(numDims,distribution,observed);

numDims = myndims(distribution);

% sort along last dimension (replications)
[tmp idx] = sort( distribution, numDims,'ascend');
[tmp mx] = max( idx,[], numDims);
n = size(distribution, numDims);
% once support for matlab <= R2016b is dropped:
% pvals = sum(distribution >= observed, numDims) / n;
pvals = sum(bsxfun(@ge, distribution, observed), numDims) / n;

len = size(distribution, numDims );
pvals = 1-(mx-0.5)/len;
if strcmpi(tail, 'both')
pvals = min(pvals, 1-pvals);
pvals = 2*pvals;
if any(strcmpi(tail, {'right', 'one'}))
% nothing to be done
return;
end

p_left = 1 - pvals + sum(bsxfun(@eq, distribution, observed), numDims) / n;

% get the number of dimensions in a matrix
function val = myndims(a)
if ndims(a) > 2
val = ndims(a);
if strcmpi(tail, 'both')
pvals = 2 * min(pvals, p_left);
elseif strcmpi(tail, 'left')
pvals = p_left;
else
if size(a,1) == 1,
val = 2;
elseif size(a,2) == 1,
val = 1;
else
val = 2;
end
error('invalid value for tail: "%s", should be left, right, one or both', tail);
end

end