|
364 | 364 | w = cellfun (@(n) ones (n, 1) / n, n, 'UniformOutput', false);
|
365 | 365 | s = num2cell (ones (1, nvar), 1);
|
366 | 366 | 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 |
379 | 367 | if (match)
|
| 368 | + if (isnumeric (w)) |
| 369 | + w = repmat (mat2cell (w, n{1}, 1), 1, nvar); |
| 370 | + end |
380 | 371 | if (numel (w) > 1)
|
381 | 372 | if (~ isequal (w{:}))
|
382 | 373 | error (cat (2, 'bootstrp: Weights must be the same for each row', ...
|
|
385 | 376 | end
|
386 | 377 | else
|
387 | 378 | 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')) |
390 | 381 | end
|
391 | 382 | 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 |
392 | 392 | s = arrayfun (@(v) fzero (@(s) sum (round (s * w{v} / mean (w{v}) * nboot) ...
|
393 | 393 | - nboot), 1), 1 : nvar, 'UniformOutput', false);
|
394 | 394 | end
|
395 | 395 | w = arrayfun (@(v) round (s{v} * w{v} / mean (w{v}) * nboot), ...
|
396 | 396 | 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 |
397 | 404 |
|
398 | 405 | % Perform balanced bootstrap resampling
|
399 | 406 | if (match)
|
|
0 commit comments