Skip to content

Commit

Permalink
bootstrp: Fail safe to guarantee that the sum of the weights equals…
Browse files Browse the repository at this point in the history
… n * nboot
  • Loading branch information
acp29 committed May 14, 2024
1 parent 5b14d7d commit 7719d15
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions inst/bootstrp.m
Original file line number Diff line number Diff line change
Expand Up @@ -364,19 +364,10 @@
w = cellfun (@(n) ones (n, 1) / n, n, 'UniformOutput', false);
s = num2cell (ones (1, nvar), 1);
else
if (isnumeric (w))
w = repmat (mat2cell (w, n{1}, 1), 1, nvar);
end
if (any (arrayfun (@(v) any (bsxfun (@lt, w{v}, 0)), 1 : nvar)))
error ('bootstrp: Weights cannot contain negative values')
end
if (any (arrayfun (@(v) any (isinf(w{v})), 1 : nvar)))
error ('bootstrp: Weights cannot contain any infinite values')
end
if (any (arrayfun (@(v) any (isnan(w{v})), 1 : nvar)))
error ('bootstrp: Weights cannot contain NaN values')
end
if (match)
if (isnumeric (w))
w = repmat (mat2cell (w, n{1}, 1), 1, nvar);
end
if (numel (w) > 1)
if (~ isequal (w{:}))
error (cat (2, 'bootstrp: Weights must be the same for each row', ...
Expand All @@ -385,15 +376,31 @@
end
else
if (~ all (bsxfun (@eq, cat (2, 1, nvar), size (w))))
error (cat (2, 'bootstrp: Weights must be an array of cells equal ', ...
' equal in number to their non-matching data arguments'))
error (cat (2, 'bootstrp: Weights must be an array of cells equal', ...
' in number to their non-matching data arguments'))
end
end
if (any (arrayfun (@(v) any (bsxfun (@lt, w{v}, 0)), 1 : nvar)))
error ('bootstrp: Weights cannot contain negative values')
end
if (any (arrayfun (@(v) any (isinf(w{v})), 1 : nvar)))
error ('bootstrp: Weights cannot contain any infinite values')
end
if (any (arrayfun (@(v) any (isnan(w{v})), 1 : nvar)))
error ('bootstrp: Weights cannot contain NaN values')
end
s = arrayfun (@(v) fzero (@(s) sum (round (s * w{v} / mean (w{v}) * nboot) ...
- nboot), 1), 1 : nvar, 'UniformOutput', false);
end
w = arrayfun (@(v) round (s{v} * w{v} / mean (w{v}) * nboot), ...
1 : nvar, 'UniformOutput', false);
if (~ isempty (w))
% Fail safe to guarantee that the sum of the weights equals n * nboot
for v = 1:nvar
[wmax, imax] = max (w{v});
w{v}(imax) = wmax + nboot * n{v} - sum (w{v});
end
end

% Perform balanced bootstrap resampling
if (match)
Expand Down

0 comments on commit 7719d15

Please sign in to comment.