Skip to content

Commit 7719d15

Browse files
committed
bootstrp: Fail safe to guarantee that the sum of the weights equals n * nboot
1 parent 5b14d7d commit 7719d15

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

inst/bootstrp.m

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -364,19 +364,10 @@
364364
w = cellfun (@(n) ones (n, 1) / n, n, 'UniformOutput', false);
365365
s = num2cell (ones (1, nvar), 1);
366366
else
367-
if (isnumeric (w))
368-
w = repmat (mat2cell (w, n{1}, 1), 1, nvar);
369-
end
370-
if (any (arrayfun (@(v) any (bsxfun (@lt, w{v}, 0)), 1 : nvar)))
371-
error ('bootstrp: Weights cannot contain negative values')
372-
end
373-
if (any (arrayfun (@(v) any (isinf(w{v})), 1 : nvar)))
374-
error ('bootstrp: Weights cannot contain any infinite values')
375-
end
376-
if (any (arrayfun (@(v) any (isnan(w{v})), 1 : nvar)))
377-
error ('bootstrp: Weights cannot contain NaN values')
378-
end
379367
if (match)
368+
if (isnumeric (w))
369+
w = repmat (mat2cell (w, n{1}, 1), 1, nvar);
370+
end
380371
if (numel (w) > 1)
381372
if (~ isequal (w{:}))
382373
error (cat (2, 'bootstrp: Weights must be the same for each row', ...
@@ -385,15 +376,31 @@
385376
end
386377
else
387378
if (~ all (bsxfun (@eq, cat (2, 1, nvar), size (w))))
388-
error (cat (2, 'bootstrp: Weights must be an array of cells equal ', ...
389-
' equal in number to their non-matching data arguments'))
379+
error (cat (2, 'bootstrp: Weights must be an array of cells equal', ...
380+
' in number to their non-matching data arguments'))
390381
end
391382
end
383+
if (any (arrayfun (@(v) any (bsxfun (@lt, w{v}, 0)), 1 : nvar)))
384+
error ('bootstrp: Weights cannot contain negative values')
385+
end
386+
if (any (arrayfun (@(v) any (isinf(w{v})), 1 : nvar)))
387+
error ('bootstrp: Weights cannot contain any infinite values')
388+
end
389+
if (any (arrayfun (@(v) any (isnan(w{v})), 1 : nvar)))
390+
error ('bootstrp: Weights cannot contain NaN values')
391+
end
392392
s = arrayfun (@(v) fzero (@(s) sum (round (s * w{v} / mean (w{v}) * nboot) ...
393393
- nboot), 1), 1 : nvar, 'UniformOutput', false);
394394
end
395395
w = arrayfun (@(v) round (s{v} * w{v} / mean (w{v}) * nboot), ...
396396
1 : nvar, 'UniformOutput', false);
397+
if (~ isempty (w))
398+
% Fail safe to guarantee that the sum of the weights equals n * nboot
399+
for v = 1:nvar
400+
[wmax, imax] = max (w{v});
401+
w{v}(imax) = wmax + nboot * n{v} - sum (w{v});
402+
end
403+
end
397404

398405
% Perform balanced bootstrap resampling
399406
if (match)

0 commit comments

Comments
 (0)