Skip to content

Commit 331e862

Browse files
author
acp29
committed
Made BOOTSAM a documented input argument
- Made BOOTSAM a documented input argument - Minor tweaks to coding style and documentation
1 parent 2dca3ca commit 331e862

File tree

6 files changed

+72
-60
lines changed

6 files changed

+72
-60
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: statistics-bootstrap
2-
version: 5.1.5
3-
date: 2023-01-21
2+
version: 5.1.6
3+
date: 2023-01-22
44
author: Andrew Penn <andy.c.penn@gmail.com>
55
maintainer: Andrew Penn <andy.c.penn@gmail.com>
66
title: A statistics package with a variety of bootstrap resampling tools

inst/bootci.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,9 @@
324324
% estimate of the standard error using bootknife resampling
325325
szx = cellfun (@(x) size (x, 2), data);
326326
data = [data{:}];
327-
cellfunc = @(bootsam) bootknife (mat2cell (data (bootsam,:), n, szx), nbootstd, bootfun, NaN, [], 0, [], ISOCTAVE);
327+
cellfunc = @(bootsam) bootknife (mat2cell (data (bootsam,:), n, szx), nbootstd, bootfun, NaN, [], 0, [], [], ISOCTAVE);
328328
else
329-
cellfunc = @(bootsam) bootknife (data (bootsam,:), nbootstd, bootfun, NaN, [], 0, [], ISOCTAVE);
329+
cellfunc = @(bootsam) bootknife (data (bootsam,:), nbootstd, bootfun, NaN, [], 0, [], [], ISOCTAVE);
330330
end
331331
if (ncpus > 1)
332332
if (ISOCTAVE)

inst/bootknife.m

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
% reduce Monte Carlo error [2,3].
1212
%
1313
% If single bootstrap is requested, the confidence intervals are obtained from
14-
% the quantiles of a kernel density estimate of bootstrap statistics (with
14+
% the quantiles of a kernel density estimate of the bootstrap statistics (with
1515
% shrinkage corrrection). By default, the confidence intervals are bias-
1616
% corrected and accelerated (BCa) [4-5]. BCa intervals are fast to compute and
1717
% have good coverage and correctness when combined with bootknife resampling
@@ -20,7 +20,7 @@
2020
% If double bootstrap is requested, the algorithm uses calibration to improve
2121
% the accuracy (of the bias and standard error) and coverage of the confidence
2222
% intervals [6-12], which are obtained from the empirical distribution of the
23-
% bootstrap statistics by linear interpolation.
23+
% bootstrap statistics by linear interpolation [9].
2424
%
2525
% STATS = bootknife (DATA)
2626
% STATS = bootknife ({DATA})
@@ -30,6 +30,7 @@
3030
% STATS = bootknife (DATA, NBOOT, ..., ALPHA)
3131
% STATS = bootknife (DATA, NBOOT, ..., ALPHA, STRATA)
3232
% STATS = bootknife (DATA, NBOOT, ..., ALPHA, STRATA, NPROC)
33+
% STATS = bootknife (DATA, NBOOT, ..., ALPHA, STRATA, NPROC, BOOTSAM)
3334
% [STATS, BOOTSTAT] = bootknife (...)
3435
% [STATS, BOOTSTAT] = bootknife (...)
3536
% [STATS, BOOTSTAT, BOOTSAM] = bootknife (...)
@@ -155,6 +156,11 @@
155156
% feature requires the Parallel package (in Octave), or the Parallel
156157
% Computing Toolbox (in Matlab).
157158
%
159+
% STATS = bootknife (DATA, NBOOT, ..., ALPHA, STRATA, NPROC, BOOTSAM) uses
160+
% bootstrap resampling indices provided in BOOTSAM. The BOOTSAM should be a
161+
% matrix with the same number of rows as the data. When BOOTSAM is provided,
162+
% the first element of NBOOT is ignored.
163+
%
158164
% [STATS, BOOTSTAT] = bootknife (...) also returns BOOTSTAT, a vector of
159165
% bootstrap statistics calculated over the (first, or outer layer of)
160166
% bootstrap resamples.
@@ -187,7 +193,7 @@
187193
% Bootstrap. New York, NY: Chapman & Hall
188194
% [6] Beran (1987). Prepivoting to Reduce Level Error of Confidence Sets.
189195
% Biometrika, 74(3), 457–468.
190-
% [7] Lee and Young (1999) The effectt of Monte Carlo approximation on coverage
196+
% [7] Lee and Young (1999) The effect of Monte Carlo approximation on coverage
191197
% error of double-bootstrap con®dence intervals. J R Statist Soc B.
192198
% 61:353-366.
193199
% [8] Booth J. and Presnell B. (1998) Allocation of Monte Carlo Resources for
@@ -225,11 +231,11 @@
225231
% along with this program. If not, see <http://www.gnu.org/licenses/>.
226232

227233

228-
function [stats, bootstat, BOOTSAM] = bootknife (x, nboot, bootfun, alpha, ...
229-
strata, ncpus, REF, ISOCTAVE, BOOTSAM, ERRCHK)
234+
function [stats, bootstat, bootsam] = bootknife (x, nboot, bootfun, alpha, ...
235+
strata, ncpus, bootsam, REF, ISOCTAVE, ERRCHK)
230236

231237
% Input argument names in all-caps are for internal use only
232-
% REF, ISOCTAVE, BOOTSAM, ERRCHK are undocumented input arguments required
238+
% REF, ISOCTAVE and ERRCHK are undocumented input arguments required
233239
% for some of the functionalities of bootknife
234240

235241
% Store local functions in a stucture for parallel processes
@@ -343,7 +349,7 @@
343349
error ('bootknife: NPROC must be a scalar value');
344350
end
345351
end
346-
if ((nargin < 8) || isempty (ISOCTAVE))
352+
if ((nargin < 9) || isempty (ISOCTAVE))
347353
% Check if running in Octave (else assume Matlab)
348354
info = ver;
349355
ISOCTAVE = any (ismember ({info.Name}, 'Octave'));
@@ -511,21 +517,21 @@
511517

512518
% Perform balanced bootknife resampling
513519
unbiased = true; % Set to true for bootknife resampling
514-
if ((nargin < 9) || isempty (BOOTSAM))
520+
if ((nargin < 7) || isempty (bootsam))
515521
if (~ isempty (strata))
516522
if (nvar > 1) || (nargout > 2)
517-
% We can save some memory by making BOOTSAM an int32 datatype
518-
BOOTSAM = zeros (n, B, 'int32');
523+
% We can save some memory by making bootsam an int32 datatype
524+
bootsam = zeros (n, B, 'int32');
519525
for k = 1:K
520526
if ((sum (g(:, k))) > 1)
521-
BOOTSAM(g(:, k), :) = boot (find (g(:, k)), B, unbiased);
527+
bootsam(g(:, k), :) = boot (find (g(:, k)), B, unbiased);
522528
else
523-
BOOTSAM(g(:, k), :) = find (g(:, k)) * ones (1, B);
529+
bootsam(g(:, k), :) = find (g(:, k)) * ones (1, B);
524530
end
525531
end
526532
else
527-
% For more efficiency, if we don't need BOOTSAM, we can directly resample values of x
528-
BOOTSAM = [];
533+
% For more efficiency, if we don't need bootsam, we can directly resample values of x
534+
bootsam = [];
529535
X = zeros (n, B);
530536
for k = 1:K
531537
if ((sum (g(:, k))) > 1)
@@ -537,19 +543,25 @@
537543
end
538544
else
539545
if (nvar > 1) || (nargout > 2)
540-
% We can save some memory by making BOOTSAM an int32 datatype
541-
BOOTSAM = zeros (n, B, 'int32');
542-
BOOTSAM(:, :) = boot (n, B, unbiased);
546+
% We can save some memory by making bootsam an int32 datatype
547+
bootsam = zeros (n, B, 'int32');
548+
bootsam(:, :) = boot (n, B, unbiased);
543549
else
544-
% For more efficiency, if we don't need BOOTSAM, we can directly resample values of x
545-
BOOTSAM = [];
550+
% For more efficiency, if we don't need bootsam, we can directly resample values of x
551+
bootsam = [];
546552
X = boot (x, B, unbiased);
547553
end
548554
end
555+
else
556+
if (size (bootsam, 1) ~= n)
557+
error ('bootknife: BOOTSAM must have the same number of rows as X')
558+
end
559+
nboot(1) = size (bootsam, 2);
560+
B = nboot(1);
549561
end
550562

551563
% Evaluate bootfun each bootstrap resample
552-
if (isempty (BOOTSAM))
564+
if (isempty (bootsam))
553565
if (vectorized)
554566
% Vectorized evaluation of bootfun on the DATA resamples
555567
bootstat = bootfun (X);
@@ -570,35 +582,35 @@
570582
end
571583
else
572584
if (vectorized)
573-
% DATA resampling (using BOOTSAM) and vectorized evaluation of bootfun on
585+
% DATA resampling (using bootsam) and vectorized evaluation of bootfun on
574586
% the DATA resamples
575587
if (nvar > 1)
576588
% Multivariate
577589
% Perform DATA sampling
578-
X = cell2mat (cellfun (@(i) reshape (x(BOOTSAM, i), n, B), ...
590+
X = cell2mat (cellfun (@(i) reshape (x(bootsam, i), n, B), ...
579591
num2cell (1:nvar, 1), 'UniformOutput', false));
580592
else
581593
% Univariate
582594
% Perform DATA sampling
583-
X = x(BOOTSAM);
595+
X = x(bootsam);
584596
end
585597
% Function evaluation on bootknife samples
586598
bootstat = bootfun (X);
587599
else
588-
cellfunc = @(BOOTSAM) bootfun (x(BOOTSAM, :));
600+
cellfunc = @(bootsam) bootfun (x(bootsam, :));
589601
if (ncpus > 1)
590602
% Evaluate bootfun on each bootstrap resample in PARALLEL
591603
if (ISOCTAVE)
592604
% OCTAVE
593-
bootstat = parcellfun (ncpus, cellfunc, num2cell (BOOTSAM, 1), 'UniformOutput', false);
605+
bootstat = parcellfun (ncpus, cellfunc, num2cell (bootsam, 1), 'UniformOutput', false);
594606
else
595607
% MATLAB
596608
bootstat = cell (1, B);
597-
parfor b = 1:B; bootstat{b} = cellfunc (BOOTSAM(:, b)); end
609+
parfor b = 1:B; bootstat{b} = cellfunc (bootsam(:, b)); end
598610
end
599611
else
600612
% Evaluate bootfun on each bootstrap resample in SERIAL
601-
bootstat = cellfun (cellfunc, num2cell (BOOTSAM, 1), 'UniformOutput', false);
613+
bootstat = cellfun (cellfunc, num2cell (bootsam, 1), 'UniformOutput', false);
602614
end
603615
end
604616
end
@@ -607,14 +619,14 @@
607619
end
608620

609621
% Remove bootstrap statistics that contain NaN, along with their associated
610-
% DATA resamples in X or BOOTSAM
622+
% DATA resamples in X or bootsam
611623
ridx = any (isnan (bootstat), 1);
612624
bootstat_all = bootstat;
613625
bootstat(:, ridx) = [];
614-
if (isempty (BOOTSAM))
626+
if (isempty (bootsam))
615627
X(:, ridx) = [];
616628
else
617-
BOOTSAM(:, ridx) = [];
629+
bootsam(:, ridx) = [];
618630
end
619631
if (isempty (bootstat))
620632
error ('bootknife: BOOTFUN returned NaN for every bootstrap resamples')
@@ -630,12 +642,12 @@
630642
% OCTAVE
631643
% Set unique random seed for each parallel thread
632644
pararrayfun (ncpus, @boot, 1, 1, false, 1:ncpus);
633-
if (vectorized && isempty (BOOTSAM))
634-
cellfunc = @(x) bootknife (x, C, bootfun, NaN, strata, 0, T0, ISOCTAVE, [], false);
645+
if (vectorized && isempty (bootsam))
646+
cellfunc = @(x) bootknife (x, C, bootfun, NaN, strata, 0, [], T0, ISOCTAVE, false);
635647
bootout = parcellfun (ncpus, cellfunc, num2cell (X, 1), 'UniformOutput', false);
636648
else
637-
cellfunc = @(BOOTSAM) bootknife (x(BOOTSAM, :), C, bootfun, NaN, strata, 0, T0, ISOCTAVE, [], false);
638-
bootout = parcellfun (ncpus, cellfunc, num2cell (BOOTSAM, 1), 'UniformOutput', false);
649+
cellfunc = @(bootsam) bootknife (x(bootsam, :), C, bootfun, NaN, strata, 0, [], T0, ISOCTAVE, false);
650+
bootout = parcellfun (ncpus, cellfunc, num2cell (bootsam, 1), 'UniformOutput', false);
639651
end
640652
else
641653
% MATLAB
@@ -644,22 +656,22 @@
644656
% Perform inner layer of resampling
645657
% Preallocate structure array
646658
bootout = cell (1, B);
647-
if (vectorized && isempty (BOOTSAM))
648-
cellfunc = @(x) bootknife (x, C, bootfun, NaN, strata, 0, T0, ISOCTAVE, [], false);
659+
if (vectorized && isempty (bootsam))
660+
cellfunc = @(x) bootknife (x, C, bootfun, NaN, strata, 0, [], T0, ISOCTAVE, false);
649661
parfor b = 1:B; bootout{b} = cellfunc (X(:, b)); end
650662
else
651-
cellfunc = @(BOOTSAM) bootknife (x(BOOTSAM, :), C, bootfun, NaN, strata, 0, T0, ISOCTAVE, [], false);
652-
parfor b = 1:B; bootout{b} = cellfunc (BOOTSAM(:, b)); end
663+
cellfunc = @(bootsam) bootknife (x(bootsam, :), C, bootfun, NaN, strata, 0, [], T0, ISOCTAVE, false);
664+
parfor b = 1:B; bootout{b} = cellfunc (bootsam(:, b)); end
653665
end
654666
end
655667
else
656668
% SERIAL execution of inner layer resampling for double bootstrap
657-
if (vectorized && isempty (BOOTSAM))
658-
cellfunc = @(x) bootknife (x, C, bootfun, NaN, strata, 0, T0, ISOCTAVE, [], false);
669+
if (vectorized && isempty (bootsam))
670+
cellfunc = @(x) bootknife (x, C, bootfun, NaN, strata, 0, [], T0, ISOCTAVE, false);
659671
bootout = cellfun (cellfunc, num2cell (X, 1), 'UniformOutput', false);
660672
else
661-
cellfunc = @(BOOTSAM) bootknife (x(BOOTSAM, :), C, bootfun, NaN, strata, 0, T0, ISOCTAVE, [], false);
662-
bootout = cellfun (cellfunc, num2cell (BOOTSAM, 1), 'UniformOutput', false);
673+
cellfunc = @(bootsam) bootknife (x(bootsam, :), C, bootfun, NaN, strata, 0, [], T0, ISOCTAVE, false);
674+
bootout = cellfun (cellfunc, num2cell (bootsam, 1), 'UniformOutput', false);
663675
end
664676
end
665677
% Double bootstrap bias estimation
@@ -807,7 +819,7 @@
807819
stats.CI_lower = ci(:, 1);
808820
stats.CI_upper = ci(:, 2);
809821
% Use quick interpolation to find the proportion (Pr) of bootstat <= REF
810-
if ((nargin > 6) && ~ isempty (REF))
822+
if ((nargin > 7) && ~ isempty (REF))
811823
I = bsxfun (@le, bootstat, REF);
812824
pr = sum (I, 2);
813825
t = cell2mat (arrayfun (@(j) ...
@@ -828,7 +840,7 @@
828840
if (nargout == 0)
829841
print_output (stats, nboot, alpha, l, m, bootfun_str, strata);
830842
else
831-
if (isempty (BOOTSAM))
843+
if (isempty (bootsam))
832844
[warnmsg, warnID] = lastwarn;
833845
if (ismember (warnID, {'bootknife:biasfail','bootknife:jackfail'}))
834846
warning ('bootknife:lastwarn', warnmsg);

inst/bootmode.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@
237237
h = criticalBandwidth;
238238

239239
% Random resampling with replacement from a smooth estimate of the distribution
240-
idx = boot (n,B,false);
240+
idx = boot (n, B, false);
241241
Y = x(idx);
242242
xvar = var (x, 1); % calculate sample variance
243243
Ymean = ones (n, 1) * mean(Y);

inst/bootnhst.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -717,9 +717,9 @@
717717
boot (1, 1, false, 1); % set random seed to make bootstrap resampling deterministic
718718
% Use newer, faster and balanced (less biased) resampling functions (boot and bootknife)
719719
if (paropt.UseParallel)
720-
[null, Q] = bootknife (data, nboot(1), func, NaN, [], paropt.nproc, [], ISOCTAVE);
720+
[null, Q] = bootknife (data, nboot(1), func, NaN, [], paropt.nproc, [], [], ISOCTAVE);
721721
else
722-
[null, Q] = bootknife (data, nboot(1), func, NaN, [], 0, [], ISOCTAVE);
722+
[null, Q] = bootknife (data, nboot(1), func, NaN, [], 0, [], [], ISOCTAVE);
723723
end
724724

725725
% Compute the estimate (theta) and it's pooled (weighted mean) sampling variance
@@ -747,7 +747,7 @@
747747
% Bootknife resampling involves less computation than Jackknife when sample sizes get larger
748748
theta(j) = bootfun (data(g == gk(j), :));
749749
nk(j) = sum (g == gk(j));
750-
bootout = bootknife (data(g == gk(j), :), [nboot(2), 0], bootfun, NaN, [], 0, [], ISOCTAVE, [], false);
750+
bootout = bootknife (data(g == gk(j), :), [nboot(2), 0], bootfun, NaN, [], 0, [], [], ISOCTAVE, false);
751751
SE(j) = bootout.std_error;
752752
if (j==1); se_method = 'Balanced, bootknife resampling'; end;
753753
end
@@ -1017,7 +1017,7 @@
10171017
% Bootknife resampling involves less computation than Jackknife when sample sizes get larger
10181018
theta(j) = bootfun (Y(g == gk(j), :));
10191019
nk(j) = sum (g == gk(j));
1020-
bootout = bootknife(Y(g == gk(j), :), [nboot, 0], bootfun, NaN, [], 0, [], ISOCTAVE, [], false);
1020+
bootout = bootknife (Y(g == gk(j), :), [nboot, 0], bootfun, NaN, [], 0, [], [], ISOCTAVE, false);
10211021
SE(j) = bootout.std_error;
10221022
end
10231023
Var(j) = ((nk(j) - 1) / (N - k)) * SE(j)^2;

test/test_script.m

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
% warning ('off', 'Octave:nearly-singular-matrix')
1111
% warning ('off', 'Octave:broadcast')
1212
%else
13-
warning ('off', 'MATLAB:rankDeficientMatrix')
14-
warning ('off', 'MATLAB:divideByZero')
13+
% warning ('off', 'MATLAB:rankDeficientMatrix')
14+
% warning ('off', 'MATLAB:divideByZero')
1515
%end
1616

17-
%try
17+
try
1818
% boot
1919
boot (3, 20);
2020
boot (3, 20, false, 1);
@@ -173,21 +173,21 @@
173173
func = @(M) subsref (M(:,2:end) \ M(:,1), struct ('type', '()', 'subs', {{2}}));
174174
p = bootnhst ([y, X], g, 'bootfun', func, 'DisplayOpt', false);
175175

176-
%catch exception
176+
catch exception
177177

178178
% Turn warnings back on
179179
warning ('on', 'bootknife:parallel')
180180
%if ISOCTAVE
181181
% warning ('on', 'Octave:divide-by-zero')
182182
% warning ('on', 'Octave:nearly-singular-matrix')
183183
%else
184-
warning ('on', 'MATLAB:rankDeficientMatrix')
185-
warning ('on', 'MATLAB:divideByZero')
184+
% warning ('on', 'MATLAB:rankDeficientMatrix')
185+
% warning ('on', 'MATLAB:divideByZero')
186186
%end
187187
%rethrow (exception)
188188

189189

190-
%end
190+
end
191191

192192
% Turn warnings back on
193193
warning ('on', 'bootknife:parallel');

0 commit comments

Comments
 (0)