Skip to content

Commit

Permalink
Performance enhancements in boot.m, bootclust.m and bootknife.m
Browse files Browse the repository at this point in the history
- Removed unnecessary element-wise calculations from boot.m
- Enabled vectorized function evaluations in jackknife computations in bootclust.m and bootknife.m functions
  • Loading branch information
acp29 committed Apr 14, 2024
1 parent 786200d commit a6ffaaf
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 33 deletions.
14 changes: 7 additions & 7 deletions inst/boot.m
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
% Assign weights (counts) for uniform sampling
c = ones (n, 1) * nboot;
end
N = sum (c);

% Perform balanced sampling
r = 0;
Expand All @@ -156,27 +157,26 @@
end
end
for i = 1:n
d = c;
d = c;
if (u)
d(r) = 0;
end
if (~ sum (d))
d = c;
if (N > d(r))
d(r) = 0;
end
end
d = cumsum (d);
j = sum (R(i) >= d ./ d(n)) + 1;
j = sum (R(i) * d(n) >= d) + 1;
if (isvec)
bootsam (i, b) = x(j);
else
bootsam (i, b) = j;
end
if (nboot > 1)
c(j) = c(j) - 1;
N = N - 1;
end
end
end


%!demo
%!
%! % N as input; balanced bootstrap resampling with replacement
Expand Down
36 changes: 23 additions & 13 deletions inst/bootclust.m
Original file line number Diff line number Diff line change
Expand Up @@ -521,21 +521,31 @@
warning ('off', 'all');
end
try
jackfun = @(i) bootfun (vertcat (x{1 : nx ~= i, :}));
if (ncpus > 1)
% PARALLEL evaluation of bootfun on each jackknife resample
if (ISOCTAVE)
% OCTAVE
T = cell2mat (pararrayfun (ncpus, jackfun, 1 : nx, ...
'UniformOutput', false));
if (VECTORIZED)
% Leave-one-out DATA resampling followed by vectorized function
% evaluations
T = bootfun (reshape (cell2mat (arrayfun (...
@(i) vertcat (x{1 : nx ~= i, :}), (1 : nx)', ...
'UniformOutput', false)), n - n / nx, []));
else
% Leave-one-out DATA resampling followed by looped function
% evaluations (if bootfun is not vectorized)
jackfun = @(i) bootfun (vertcat (x{1 : nx ~= i, :}));
if (ncpus > 1)
% PARALLEL evaluation of bootfun on each jackknife resample
if (ISOCTAVE)
% OCTAVE
T = cell2mat (pararrayfun (ncpus, jackfun, 1 : nx, ...
'UniformOutput', false));
else
% MATLAB
T = zeros (m, nx);
parfor i = 1 : nx; T(:, i) = feval (jackfun, i); end
end
else
% MATLAB
T = zeros (m, nx);
parfor i = 1 : nx; T(:, i) = feval (jackfun, i); end
% SERIAL evaluation of bootfun on each jackknife resample
T = cell2mat (arrayfun (jackfun, 1 : nx, 'UniformOutput', false));
end
else
% SERIAL evaluation of bootfun on each jackknife resample
T = cell2mat (arrayfun (jackfun, 1 : nx, 'UniformOutput', false));
end
% Calculate empirical influence function
U = (nx - 1) * bsxfun (@minus, mean (T, 2), T);
Expand Down
36 changes: 23 additions & 13 deletions inst/bootknife.m
Original file line number Diff line number Diff line change
Expand Up @@ -718,21 +718,31 @@
% Attempt to form bias-corrected and accelerated (BCa) bootstrap CIs
% Use the Jackknife to calculate the acceleration constant (a)
try
jackfun = @(i) bootfun (x(1 : n ~= i, :));
if (ncpus > 1)
% PARALLEL evaluation of bootfun on each jackknife resample
if (ISOCTAVE)
% OCTAVE
T = cell2mat (pararrayfun (ncpus, jackfun, 1 : n, ...
'UniformOutput', false));
if (vectorized)
% Leave-one-out DATA resampling followed by vectorized function
% evaluations
T = bootfun (reshape (cell2mat (arrayfun (...
@(i) x(1 : n ~= i, :), (1 : n)', ...
'UniformOutput', false)), n - 1, []));
else
% Leave-one-out DATA resampling followed by looped function
% evaluations (if bootfun is not vectorized)
jackfun = @(i) bootfun (x(1 : n ~= i, :));
if (ncpus > 1)
% PARALLEL evaluation of bootfun on each jackknife resample
if (ISOCTAVE)
% OCTAVE
T = cell2mat (pararrayfun (ncpus, jackfun, 1 : n, ...
'UniformOutput', false));
else
% MATLAB
T = zeros (m, n);
parfor i = 1 : n; T(:, i) = feval (jackfun, i); end
end
else
% MATLAB
T = zeros (m, n);
parfor i = 1 : n; T(:, i) = feval (jackfun, i); end
% SERIAL evaluation of bootfun on each jackknife resample
T = cell2mat (arrayfun (jackfun, 1 : n, 'UniformOutput', false));
end
else
% SERIAL evaluation of bootfun on each jackknife resample
T = cell2mat (arrayfun (jackfun, 1 : n, 'UniformOutput', false));
end
% Calculate empirical influence function
if (~ isempty (strata))
Expand Down

0 comments on commit a6ffaaf

Please sign in to comment.